Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detectron2 Installation issue #218

Open
bankyresearch opened this issue Oct 14, 2024 · 4 comments
Open

Detectron2 Installation issue #218

bankyresearch opened this issue Oct 14, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@bankyresearch
Copy link


AttributeError Traceback (most recent call last)
in <cell line: 2>()
1 import layoutparser as lp
----> 2 model = lp.Detectron2LayoutModel('lp://HJDataset/faster_rcnn_R_50_FPN_3x/config')
3 model.detect(image)

/usr/local/lib/python3.10/dist-packages/layoutparser/file_utils.py in getattr(self, name)

AttributeError: module layoutparser has no attribute Detectron2LayoutModel

on Google colab

@bankyresearch bankyresearch added the bug Something isn't working label Oct 14, 2024
@AryamanGurjar
Copy link

Hey Did you get any solution for it

@Matteo-Frattaroli
Copy link

Matteo-Frattaroli commented Nov 4, 2024

@bankyresearch @AryamanGurjar apparently they moved around the files or modified some init files.

This should work:
lp.models.detectron2.Detectron2LayoutModel

Unluckily even though I resolved the AttributeError, i ran into another issue:

----> 1 model = lp.models.detectron2.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',
2 extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
3 label_map={0: "text", 1: "title", 2: "list", 3:"table", 4:"figure"})

File /usr/local/lib/python3.10/dist-packages/layoutparser/models/base_layoutmodel.py:87, in BaseLayoutModel.new(cls, *args, **kwargs)
85 def new(cls, *args, **kwargs):
---> 87 requires_backends(cls, cls.DEPENDENCIES)
88 return super().new(cls)

File /usr/local/lib/python3.10/dist-packages/layoutparser/file_utils.py:175, in requires_backends(obj, backends)
173 name = obj.name if hasattr(obj, "name") else obj.class.name
174 if not all(BACKENDS_MAPPING[backend]0 for backend in backends):
--> 175 raise ImportError(
176 "".join([BACKENDS_MAPPING[backend][1].format(name) for backend in backends])
177 )

ImportError:
Detectron2LayoutModel requires the detectron2 library but it was not found in your environment. Checkout the instructions on the
installation page: https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md and follow the ones
that match your environment. Typically the following would work for MacOS or Linux CPU machines:
pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2'

Of course i installed detectron both as suggested by the error message and as actually indicated in the repo's readme file but with no success. I keep getting the same error.

@finiteautomata
Copy link

Followed the instructions to install the library with detectron support

! pip install layoutparser torchvision && pip install "detectron2@git+https://github.com/facebookresearch/detectron2.git@v0.5#egg=detectron2"

The error posted by @Matteo-Frattaroli disappears but gets replaced by yet another one

DetectronModel = lp.models.detectron2.Detectron2LayoutModel


model = DetectronModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config',
    extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
    label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})

fails with

 > ---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 1
----> 1 DetectronModel = lp.models.detectron2.Detectron2LayoutModel
      4 model = DetectronModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', 
      5     extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
      6     label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/layoutparser/file_utils.py:221, in _LazyModule.__getattr__(self, name)
    219     return self._objects[name]
    220 if name in self._modules:
--> 221     value = self._get_module(name)
    222 elif name in self._class_to_module.keys():
    223     module = self._get_module(self._class_to_module[name])

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/layoutparser/file_utils.py:232, in _LazyModule._get_module(self, module_name)
    231 def _get_module(self, module_name: str):
--> 232     return importlib.import_module("." + module_name, self.__name__)

File ~/miniconda3/envs/ocr/lib/python3.11/importlib/__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File <frozen importlib._bootstrap>:1204, in _gcd_import(name, package, level)

File <frozen importlib._bootstrap>:1176, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1147, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:690, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:940, in exec_module(self, module)

File <frozen importlib._bootstrap>:241, in _call_with_frames_removed(f, *args, **kwds)

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/layoutparser/models/__init__.py:15
      1 # Copyright 2021 The Layout Parser team. All rights reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
---> 15 from .detectron2.layoutmodel import Detectron2LayoutModel
     16 from .paddledetection.layoutmodel import PaddleDetectionLayoutModel
     17 from .effdet.layoutmodel import EfficientDetLayoutModel

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/layoutparser/models/detectron2/__init__.py:18
     15 from . import catalog as _UNUSED
     16 # A trick learned from
     17 # https://github.com/facebookresearch/detectron2/blob/62cf3a2b6840734d2717abdf96e2dd57ed6612a6/detectron2/checkpoint/__init__.py#L6
---> 18 from .layoutmodel import Detectron2LayoutModel

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/layoutparser/models/detectron2/layoutmodel.py:26
     23 from ...file_utils import is_torch_cuda_available, is_detectron2_available
     25 if is_detectron2_available():
---> 26     import detectron2.engine
     27     import detectron2.config
     30 __all__ = ["Detectron2LayoutModel"]

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/engine/__init__.py:11
      6 __all__ = [k for k in globals().keys() if not k.startswith("_")]
      9 # prefer to let hooks and defaults live in separate namespaces (therefore not in __all__)
     10 # but still make them available here
---> 11 from .hooks import *
     12 from .defaults import *

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/engine/hooks.py:19
     16 from fvcore.nn.precise_bn import get_bn_modules, update_bn_stats
     18 import detectron2.utils.comm as comm
---> 19 from detectron2.evaluation.testing import flatten_results_dict
     20 from detectron2.solver import LRMultiplier
     21 from detectron2.utils.events import EventStorage, EventWriter

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/evaluation/__init__.py:2
      1 # Copyright (c) Facebook, Inc. and its affiliates.
----> 2 from .cityscapes_evaluation import CityscapesInstanceEvaluator, CityscapesSemSegEvaluator
      3 from .coco_evaluation import COCOEvaluator
      4 from .rotated_coco_evaluation import RotatedCOCOEvaluator

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/evaluation/cityscapes_evaluation.py:11
      8 import torch
      9 from PIL import Image
---> 11 from detectron2.data import MetadataCatalog
     12 from detectron2.utils import comm
     13 from detectron2.utils.file_io import PathManager

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/data/__init__.py:2
      1 # Copyright (c) Facebook, Inc. and its affiliates.
----> 2 from . import transforms  # isort:skip
      4 from .build import (
      5     build_batch_data_loader,
      6     build_detection_test_loader,
   (...)
     10     print_instances_class_histogram,
     11 )
     12 from .catalog import DatasetCatalog, MetadataCatalog, Metadata

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/data/transforms/__init__.py:4
      2 from fvcore.transforms.transform import Transform, TransformList  # order them first
      3 from fvcore.transforms.transform import *
----> 4 from .transform import *
      5 from .augmentation import *
      6 from .augmentation_impl import *

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/data/transforms/transform.py:36
     25     pass
     27 __all__ = [
     28     "ExtentTransform",
     29     "ResizeTransform",
   (...)
     32     "PILColorTransform",
     33 ]
---> 36 class ExtentTransform(Transform):
     37     """
     38     Extracts a subregion from the source image and scales it to the output size.
     39 
   (...)
     43     See: https://pillow.readthedocs.io/en/latest/PIL.html#PIL.ImageTransform.ExtentTransform
     44     """
     46     def __init__(self, src_rect, output_size, interp=Image.LINEAR, fill=0):

File ~/miniconda3/envs/ocr/lib/python3.11/site-packages/detectron2/data/transforms/transform.py:46, in ExtentTransform()
     36 class ExtentTransform(Transform):
     37     """
     38     Extracts a subregion from the source image and scales it to the output size.
     39 
   (...)
     43     See: https://pillow.readthedocs.io/en/latest/PIL.html#PIL.ImageTransform.ExtentTransform
     44     """
---> 46     def __init__(self, src_rect, output_size, interp=Image.LINEAR, fill=0):
     47         """
     48         Args:
     49             src_rect (x0, y0, x1, y1): src coordinates
   (...)
     52             fill: Fill color used when src_rect extends outside image
     53         """
     54         super().__init__()

AttributeError: module 'PIL.Image' has no attribute 'LINEAR

@mgutierrezc
Copy link

Similar issue here. Couldn't install detectron unless --no-build-isolation is specified:
pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu117/torch2.0/index.html--no-build-isolation

But this leads to an assertion error

{
	"name": "AssertionError",
	"message": "Checkpoint /Users/mgutierrezc/.torch/iopath_cache/s/dgy9c10wykk4lq4/model_final.pth not found!",
	"stack": "---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[8], line 1
----> 1 model = lp.Detectron2LayoutModel(
      2             config_path ='lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', # In model catalog
      3             label_map   ={0: \"Text\", 1: \"Title\", 2: \"List\", 3:\"Table\", 4:\"Figure\"}, # In model`label_map`
      4             extra_config=[\"MODEL.ROI_HEADS.SCORE_THRESH_TEST\", 0.8] # Optional
      5         )

File ~/Documents/Environments/el_peruano_env/lib/python3.12/site-packages/layoutparser/models/detectron2/layoutmodel.py:119, in Detectron2LayoutModel.__init__(self, config_path, model_path, label_map, extra_config, enforce_cpu, device)
    116 self.cfg = cfg
    118 self.label_map = label_map
--> 119 self._create_model()

File ~/Documents/Environments/el_peruano_env/lib/python3.12/site-packages/layoutparser/models/detectron2/layoutmodel.py:122, in Detectron2LayoutModel._create_model(self)
    121 def _create_model(self):
--> 122     self.model = detectron2.engine.DefaultPredictor(self.cfg)

File ~/Documents/Environments/el_peruano_env/lib/python3.12/site-packages/detectron2/engine/defaults.py:320, in DefaultPredictor.__init__(self, cfg)
    317     self.metadata = MetadataCatalog.get(cfg.DATASETS.TEST[0])
    319 checkpointer = DetectionCheckpointer(self.model)
--> 320 checkpointer.load(cfg.MODEL.WEIGHTS)
    322 self.aug = T.ResizeShortestEdge(
    323     [cfg.INPUT.MIN_SIZE_TEST, cfg.INPUT.MIN_SIZE_TEST], cfg.INPUT.MAX_SIZE_TEST
    324 )
    326 self.input_format = cfg.INPUT.FORMAT

File ~/Documents/Environments/el_peruano_env/lib/python3.12/site-packages/detectron2/checkpoint/detection_checkpoint.py:62, in DetectionCheckpointer.load(self, path, *args, **kwargs)
     60     path = parsed_url._replace(query=\"\").geturl()  # remove query from filename
     61     path = self.path_manager.get_local_path(path)
---> 62 ret = super().load(path, *args, **kwargs)
     64 if need_sync:
     65     logger.info(\"Broadcasting model states from main worker ...\")

File ~/Documents/Environments/el_peruano_env/lib/python3.12/site-packages/fvcore/common/checkpoint.py:153, in Checkpointer.load(self, path, checkpointables)
    151 if not os.path.isfile(path):
    152     path = self.path_manager.get_local_path(path)
--> 153     assert os.path.isfile(path), \"Checkpoint {} not found!\".format(path)
    155 checkpoint = self._load_file(path)
    156 incompatible = self._load_model(checkpoint)

AssertionError: Checkpoint /Users/mgutierrezc/.torch/iopath_cache/s/dgy9c10wykk4lq4/model_final.pth not found!"
}

The only two files I've inside are

model_final.pth?dl=1            model_final.pth?dl=1.lock

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants