Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to .gitignore are not germane to replacing pkg_resource.resource_filename, right?
Should it be removed from the PR?

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ model_ts*.txt
.idea
.vscode
_darcs
.venv
.envrc

# project dirs
/detectron2/model_zoo/configs
Expand Down
14 changes: 9 additions & 5 deletions detectron2/model_zoo/model_zoo.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Copyright (c) Facebook, Inc. and its affiliates.

import atexit
import os
from typing import Optional
import pkg_resources
import torch

from contextlib import ExitStack
from detectron2.checkpoint import DetectionCheckpointer
from detectron2.config import CfgNode, LazyConfig, get_cfg, instantiate
from detectron2.modeling import build_model
from importlib import resources as importlib_resources

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

detectron2 is still advertising compatibility w/ Python 3.7.
importlib.resources is introduced in Python 3.9.
So I think this breaks some compatibility. Easy to fix, but needs attention.

from typing import Optional


class _ModelZooUrls:
Expand Down Expand Up @@ -136,9 +139,10 @@ def get_config_file(config_path):
Returns:
str: the real path to the config file.
"""
cfg_file = pkg_resources.resource_filename(
"detectron2.model_zoo", os.path.join("configs", config_path)
)
file_manager = ExitStack()
atexit.register(file_manager.close)
ref = importlib_resources.files("detectron2.model_zoo") / os.path.join("configs", config_path)
cfg_file = file_manager.enter_context(importlib_resources.as_file(ref))
if not os.path.exists(cfg_file):
raise RuntimeError("{} not available in Model Zoo!".format(config_path))
return cfg_file
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ known_standard_library=numpy,setuptools,mock
skip=./datasets,docs
skip_glob=*/__init__.py,**/configs/**,**/tests/config/**
known_myself=detectron2
known_third_party=fvcore,matplotlib,cv2,torch,torchvision,PIL,pycocotools,yacs,termcolor,cityscapesscripts,tabulate,tqdm,scipy,lvis,psutil,pkg_resources,caffe2,onnx,panopticapi,black,isort,av,iopath,omegaconf,hydra,yaml,pydoc,submitit,cloudpickle,packaging,timm,pandas,fairscale,pytorch3d,pytorch_lightning
known_third_party=fvcore,matplotlib,cv2,torch,torchvision,PIL,pycocotools,yacs,termcolor,cityscapesscripts,tabulate,tqdm,scipy,lvis,psutil,importlib_resources,caffe2,onnx,panopticapi,black,isort,av,iopath,omegaconf,hydra,yaml,pydoc,submitit,cloudpickle,packaging,timm,pandas,fairscale,pytorch3d,pytorch_lightning

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, remove pkg_resources. But I'm thinking that adding importlib_resources here only makes sense if you might possibly import it (and importing importlib.resources as importlib_resources doesn't count, right?).

no_lines_before=STDLIB,THIRDPARTY
sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER
default_section=FIRSTPARTY
Expand Down