Layout-Parser/layout-parser

Detectron2 Installation issue

bankyresearch opened this issue · 4 comments


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

Hey Did you get any solution for it

@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.

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