Not able to load the model for some reasons.

#3
by PalmPalm7 - opened

Code:

# Use the DETR for inference (adopted from Freddie (https://github.com/freddiev4/comp-vision-scripts/blob/main/object-detection/detr.py))
# detr_model = 'spark-ds549/detr-label-detection'
detr_model = 'KabilanM/detr-label-detection'
# The DETR model returns the bounding boxes of the lables indentified from the images
# We will utilize the bounding boxes to rank lables in the downstream task
label_bboxes = detr.run(inputdir, workdir, detr_model)

Trace String:
```

AttributeError Traceback (most recent call last)
Cell In[4], line 6
3 detr_model = 'KabilanM/detr-label-detection'
4 # The DETR model returns the bounding boxes of the lables indentified from the images
5 # We will utilize the bounding boxes to rank lables in the downstream task
----> 6 label_bboxes = detr.run(inputdir, workdir, detr_model)

File /projectnb/sparkgrp/ml-herbarium-grp/spring2024/trocr/ml-herbarium/trocr/detr.py:92, in run(image_folder, output_folder, pretrained_model, cache_dir)
90 device = 0 # if GPU is avialable
91 else:
---> 92 model = DetrForObjectDetection.from_pretrained(pretrained_model, cache_dir=cache_dir)
93 device = -1 # if GPU is unavialable
94 processor = DetrImageProcessor.from_pretrained(pretrained_model, cache_dir=cache_dir)

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/transformers/modeling_utils.py:2276, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
2273 init_contexts.append(init_empty_weights())
2275 with ContextManagers(init_contexts):
-> 2276 model = cls(config, *model_args, **model_kwargs)
2278 if load_in_8bit:
2279 from .utils.bitsandbytes import get_keys_to_not_convert, replace_8bit_linear

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/transformers/models/detr/modeling_detr.py:1364, in DetrForObjectDetection.init(self, config)
1361 super().init(config)
1363 # DETR encoder-decoder model
-> 1364 self.model = DetrModel(config)
1366 # Object detection heads
1367 self.class_labels_classifier = nn.Linear(
1368 config.d_model, config.num_labels + 1
1369 ) # We add one for the "no object" class

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/transformers/models/detr/modeling_detr.py:1194, in DetrModel.init(self, config)
1191 super().init(config)
1193 # Create backbone + positional encoding
-> 1194 backbone = DetrTimmConvEncoder(
1195 config.backbone, config.dilation, config.use_pretrained_backbone, config.num_channels
1196 )
1197 position_embeddings = build_position_encoding(config)
1198 self.backbone = DetrConvModel(backbone, position_embeddings)

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/transformers/models/detr/modeling_detr.py:340, in DetrTimmConvEncoder.init(self, name, dilation, use_pretrained_backbone, num_channels)
336 kwargs["output_stride"] = 16
338 requires_backends(self, ["timm"])
--> 340 backbone = create_model(
341 name,
342 pretrained=use_pretrained_backbone,
343 features_only=True,
344 out_indices=(1, 2, 3, 4),
345 in_chans=num_channels,
346 **kwargs,
347 )
348 # replace batch norm by frozen batch norm
349 with torch.no_grad():

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/timm/models/_factory.py:97, in create_model(model_name, pretrained, pretrained_cfg, pretrained_cfg_overlay, checkpoint_path, scriptable, exportable, no_jit, **kwargs)
92 # Parameters that aren't supported by all models or are intended to only override model defaults if set
93 # should default to None in command line args/cfg. Remove them if they are present and not set so that
94 # non-supporting models don't break and default args remain in effect.
95 kwargs = {k: v for k, v in kwargs.items() if v is not None}
---> 97 model_source, model_name = parse_model_name(model_name)
98 if model_source == 'hf-hub':
99 assert not pretrained_cfg, 'pretrained_cfg should not be set when sourcing model from Hugging Face Hub.'

File /projectnb/ds549/students/hxie13/.conda/envs/trocr/lib/python3.9/site-packages/timm/models/_factory.py:16, in parse_model_name(model_name)
15 def parse_model_name(model_name: str):
---> 16 if model_name.startswith('hf_hub'):
17 # NOTE for backwards compat, deprecate hf_hub use
18 model_name = model_name.replace('hf_hub', 'hf-hub')
19 parsed = urlsplit(model_name)

AttributeError: 'NoneType' object has no attribute 'startswith'


Hey Kabilan, if you have time, could you take a look at the error string? I'm trying to run your pipeline from ml-hebaria but is facing unknown errors.

BU Spark! DS-549 org
edited Apr 25

Hi @PalmPalm7 , can you please check the version of the transformers library in use?

Can you run pip freeze | grep transformers? If I remember correctly, the version should be 4.27.0.

BU Spark! DS-549 org

Please follow these instructions to deploy the TrOCR pipeline.

Thank you @KabilanM Kabilan! It appears that I had 4.25.4 installed instead of the 4.27.0 version you stated in the README. The problem were caused by installing Taxonerd and the spacy models which has 4.25.4. Manually re-install 4.27.0 will fix it.

However, I found the problem originated from the conda env create -n my-conda-env --file=trocr_env.yml where the wheel broke somewhere in the middle. I will post how to replicate at a later time.

Then basically I have manually installed spacey models, Taxonerd, Torch and Torch Vision, Pandas, then installed transformers with no-dep

BU Spark! DS-549 org

It's good to know that the issue has been mitigated for now! Thanks for letting me know. Let's connect later to reproduce the error and I can work on a long-term fix.
All the best for the demo day!

Sign up or log in to comment