Detectron2 Installation issue
AttributeError Traceback (most recent call last)
/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/[email protected]#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/[email protected]#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
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
@mgutierrezc Were you able to find a solution to this? I'm having the same issue, where the model weights file (model_final.pth) is not correctly named in the directory. I'm only able to use EffDet, none of the Detectron2 models are working for me due to this filename issue.
I tried manually downloading the model weights file and making sure it is available in the directory mentioned in the checkpoint AssertionError, making sure it has the correct name there (model_final.pth). I also tried deleting the old dl=1 and .lock files manually, but they just show up again after running my code. (I also tried hardcoding the path to the model weights file on my local computer using the Detectron2LayoutModel(model_path="path-to-my-weights-file"), but nothing is working. This is my latest error message -
File "/Users/nkhalil/python_projects/research_paper_prez/.venv/lib/python3.9/site-packages/detectron2/checkpoint/detection_checkpoint.py", line 108, in _load_file raise ValueError( ValueError: Unsupported query remaining: f{'dl': ['1']}, orginal filename: /Users/nkhalil/.torch/iopath_cache/s/dgy9c10wykk4lq4/model_final.pth?dl=1 ).
Similar issue here. Couldn't install detectron unless
--no-build-isolationis specified:pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu117/torch2.0/index.html--no-build-isolationBut 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_map4 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
@mgutierrezc Were you able to find a solution to this? I'm having the same issue, where the model weights file (model_final.pth) is not correctly named in the directory. I'm only able to use EffDet, none of the Detectron2 models are working for me due to this filename issue.
I tried manually downloading the model weights file and making sure it is available in the directory mentioned in the checkpoint AssertionError, making sure it has the correct name there (model_final.pth). I also tried deleting the old dl=1 and .lock files manually, but they just show up again after running my code. (I also tried hardcoding the path to the model weights file on my local computer using the Detectron2LayoutModel(model_path="path-to-my-weights-file"), but nothing is working. This is my latest error message -
File "/Users/nkhalil/python_projects/research_paper_prez/.venv/lib/python3.9/site-packages/detectron2/checkpoint/detection_checkpoint.py", line 108, in _load_file raise ValueError( ValueError: Unsupported query remaining: f{'dl': ['1']}, orginal filename: /Users/nkhalil/.torch/iopath_cache/s/dgy9c10wykk4lq4/model_final.pth?dl=1 ).
Similar issue here. Couldn't install detectron unless
--no-build-isolationis specified:pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu117/torch2.0/index.html--no-build-isolationBut 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 modellabel_map4 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 aremodel_final.pth?dl=1 model_final.pth?dl=1.lock
I kinda did. There was another issue reporting the problem with the paths and pointing to a fork that didn’t have that problem. It’s been a while since I worked in this though, so I don’t remember exactly where was that solution.
I have finally solved the issue where the implementation of Detectron2 in Layout Parser seemed impossible. I have detailed the entire process here. In brief, I wanted to implement the tutorial and faced many issues such as these:
- Issue #415 : conda install pycocotools on windows
- Issue #15 : Pycotools using deprecated sklearn rather than successor scikit-learn
- Issue #5010 :
PIL.Image.LINEARno longer exists - Issue #168 : AssertError: Checkpoint /home/ec2-user/.torch/iopath_cache/s/dgy9c10wykk4lq4/model_final.pth not found!
- Issue #11040 : AttributeError: 'FreeTypeFont' object has no attribute 'getsize' (Pillow 10)
- Issue #161 : TypeError: 'inplace' is an invalid keyword argument for sort()
Depending on your OS, you can skip a few steps. I have implemented all the fixes for Windows WSL2 (Ubuntu 20.04.6) and it works!
Another option for installation on Windows 10, "examples/Deep Layout Parsing.ipynb" worked properly.
- Prepare python virtual environment
python -m venv .venv
.\.venv\Scripts\activate
- Install torch and torchvision
- Install ninja and wheel for building detectron2
- Install detectron2 from git
pip install torch torchvision ninja wheel
pip install --no-build-isolation 'git+https://github.com/facebookresearch/detectron2.git'
- Download iopath from git, change source code and install from local (ref. PyTorch throws OSError on Detectron2LayoutModel)
git clone https://github.com/facebookresearch/iopath --single-branch --branch v0.1.8
# Edit source code in IDE (e.x., vscode)
pip install -e iopath
- Downgrade pillow to 9.5.0 if necessary
pip install pillow==9.5.0
- Install layoutparser, layoutparser[layoutmodels], layoutparser[ocr]
pip install layoutparser layoutparser[layoutmodels] layoutparser[ocr]
using lp.models will do the job