yolov5 icon indicating copy to clipboard operation
yolov5 copied to clipboard

Can't get attribute 'DetectionModel' on <module 'models.yolo'>

Open Ahuriste opened this issue 2 years ago • 14 comments

Search before asking

  • [X] I have searched the YOLOv5 issues and discussions and found no similar questions.

Question

I'm trying to adapt this notebook: https://github.com/openvinotoolkit/openvino_notebooks/blob/main/notebooks/220-yolov5-accuracy-check-and-quantization/220-yolov5-accuracy-check-and-quantization.ipynb to my custom model :

At the start, the notebook runs export.py, but I get an error when using my custom model:

Here's what I run:

python export.py --weights yolov5m/my_custom_weights.pt --imgsz 640 --batch-size 1 --include onnx

and here is the mistake:

Traceback (most recent call last): File "/home/remi/openvino_notebooks/notebooks/220-yolov5-accuracy-check-and-quantization/yolov5/export.py", line 559, in <module> main(opt) File "/home/remi/openvino_notebooks/notebooks/220-yolov5-accuracy-check-and-quantization/yolov5/export.py", line 554, in main run(**vars(opt)) File "/home/remi/openvino_notebooks/.venv/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 26, in decorate_context return func(*args, **kwargs) File "/home/remi/openvino_notebooks/notebooks/220-yolov5-accuracy-check-and-quantization/yolov5/export.py", line 446, in run model = attempt_load(weights, map_location=device, inplace=True, fuse=True) # load FP32 model File "/home/remi/openvino_notebooks/notebooks/220-yolov5-accuracy-check-and-quantization/yolov5/models/experimental.py", line 96, in attempt_load ckpt = torch.load(attempt_download(w), map_location=map_location) # load File "/home/remi/openvino_notebooks/.venv/lib/python3.9/site-packages/torch/serialization.py", line 594, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "/home/remi/openvino_notebooks/.venv/lib/python3.9/site-packages/torch/serialization.py", line 853, in _load result = unpickler.load() AttributeError: Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/home/remi/openvino_notebooks/notebooks/220-yolov5-accuracy-check-and-quantization/yolov5/models/yolo.py'>

Any help would be appreciated

Additional

No response

Ahuriste avatar Aug 25 '22 09:08 Ahuriste

👋 Hello @Ahuriste, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://ultralytics.com or email [email protected].

Requirements

Python>=3.7.0 with all requirements.txt installed including PyTorch>=1.7. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), validation (val.py), inference (detect.py) and export (export.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit.

github-actions[bot] avatar Aug 25 '22 09:08 github-actions[bot]

I faced the same issue while executing detections (detect.py) for custom model i created. It seems there is a compatibility issue/script modifications in terms of Model and DetectionModel class. Though the latest yolo.py file has the compatibility issue covered.

Model = DetectionModel # retain YOLOv5 'Model' class for backwards compatibility

I suggest you copy the 'yolov5/models/yolo.py' file from official download link and replace it with the yolo.py file you already have in your library.

zerocool909 avatar Aug 26 '22 05:08 zerocool909

hey dude i have the same problem you can fix it ?

L0calBoy avatar Aug 29 '22 16:08 L0calBoy

@Ahuriste errors generated by 3rd party tools should be raised directly with those authors, they are outside the scope of our control or support.

glenn-jocher avatar Aug 29 '22 17:08 glenn-jocher

Same issue here using the official ultralytics/yolov5 repo, both export & detect failing. Checked that export fails with coreml, ONNX, torchscript, etc. !python export.py --weights myBadWeights --img 1280 --batch 1 --include coreml results in AttributeError: Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/content/yolov5/models/yolo.py'>

!python detect.py --weights myBadWeights --img 1280 --conf 0.75 --source dataset/rgb00100.jpg results in AttributeError: Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/content/yolov5/models/yolo.py'>

I have a model trained on similar training set that exports to coreml, as well as can detect directly just fine. Both failing and successful models used yolo5m6.pt, but the major difference for the two training runs is that the model that will export has an image size for training at 960 by 1280, so were trained --rect whereas this model is 1280 x 1280 and mosaic 1.0 other than that they one had a few more epochs, slightly different batch size, a few other slightly different hyper params (scale & hsv_s), and the exporting model was trained on an A100 (single) vs a V100 (single) for the failing model. Oh yeah, one more thing, both image datasets are large synthetic and I didn't bother to renumber the failing set so it's formatted rgb_4.jpg, rgb_7.jpg, rgb_10.jpg, etc. vs rgb000001.jpg, rgb000002.jpg, rgb000003.jpg, etc.
Any thoughts would greatly appreciated!

Mercury-ML avatar Sep 02 '22 18:09 Mercury-ML

@Mercury-ML your code or your models or both are out of date. Update them and retry.

glenn-jocher avatar Sep 03 '22 21:09 glenn-jocher

@glenn-jocher Could you please elaborate?
I think I am using current models and code. Just to be sure, I retrained my model on Colab using the current ultralytics/yolov5 repo finishing last night and the export failed today (coreml), again using the current yolov5 repo.

!python export.py --weights sept-6th-weights.pt --img 1280 --include coreml

first lines output: export: data=data/coco128.yaml, weights=['sept-6th-weights.pt'], imgsz=[1280], batch_size=1, device=cpu, half=False, inplace=False, train=False, optimize=False, int8=False, dynamic=False, simplify=False, opset=12, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['coreml'] YOLOv5 🚀 v6.1-135-g7dd0a79 torch 1.12.1+cu113 CPU

last error line: AttributeError: Can't get attribute 'DetectionModel' on <module 'models.yolo' from '/content/yolov5/models/yolo.py'>

Detect also fails with the same error.

As a triple check, I have a model I trained on a similar dataset finishing Aug 17 that currently does export with the current repo.
Again, any thoughts or advice is greatly appreciated!!

Mercury-ML avatar Sep 07 '22 16:09 Mercury-ML

@Mercury-ML CoreML export of custom trained models works correctly:

!python train.py --img 640 --batch 16 --epochs 3 --data coco128.yaml --weights yolov5s.pt --cache
!python export.py --weights runs/train/exp/weights/best.pt --include coreml
Screenshot 2022-09-07 at 19 39 38

We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.

How to create a Minimal, Reproducible Example

When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:

  • Minimal – Use as little code as possible to produce the problem
  • Complete – Provide all parts someone else needs to reproduce the problem
  • Reproducible – Test the code you're about to provide to make sure it reproduces the problem

For Ultralytics to provide assistance your code should also be:

  • Current – Verify that your code is up-to-date with GitHub master, and if necessary git pull or git clone a new copy to ensure your problem has not already been solved in master.
  • Unmodified – Your problem must be reproducible using official YOLOv5 code without changes. Ultralytics does not provide support for custom code ⚠️.

If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Report template with a minimum reproducible example to help us better understand and diagnose your problem.

Thank you! 😃

glenn-jocher avatar Sep 07 '22 16:09 glenn-jocher

@Mercury-ML --img 1280 CoreML export of custom trained models are works correctly:

Screenshot 2022-09-07 at 19 40 37

glenn-jocher avatar Sep 07 '22 16:09 glenn-jocher

Once again, @glenn-jocher is correct. I'm happy/sorry/embarrassed to say this was user error. Thanks for all help(!), everything is working correctly.

Mercury-ML avatar Sep 08 '22 14:09 Mercury-ML

@zerocool909 Your Solution works for the PyPI package yolo5. Can confirm!

OttomanZ avatar Sep 15 '22 20:09 OttomanZ

I faced the same issue while executing detections (detect.py) for custom model i created. It seems there is a compatibility issue/script modifications in terms of Model and DetectionModel class. Though the latest yolo.py file has the compatibility issue covered.

Model = DetectionModel # retain YOLOv5 'Model' class for backwards compatibility

I suggest you copy the 'yolov5/models/yolo.py' file from official download link and replace it with the yolo.py file you already have in your library.

Hey, i have the same problem, i tried to remplace files but it still doesnt' work. Somebody can fix it

DavidReveloLuna avatar Sep 16 '22 13:09 DavidReveloLuna

@DavidReveloLuna When did you train the mode, was it recent? My understanding is the model needs to be trained with the same repository version from which you are running detect.py

I apologize if this is obvious, but triple check the repository with which you trained the model. Should look something like:

!git clone https://github.com/ultralytics/yolov5.git
%cd /content/yolov5
!pip install -r requirements.txt

note - the repository ( https://github.com/ultralytics/yolov5.git ) is ultralytics so it's current. If you are using a forked directory, it may not be current which is leading to the compatibility issue. The only solution I know of, if you used an older repository, is to retrain with the current, ultralytics repo. (BTW, the code is for Colab / Jupyter Notebook - otherwise you may need to remove the "%" (magic) and "!" (shell command)

Mercury-ML avatar Sep 16 '22 14:09 Mercury-ML

@Mercury-ML thanks. You'r right, I updated the repo and now it's working correctly

DavidReveloLuna avatar Sep 16 '22 18:09 DavidReveloLuna

I had the same problem, I fixed it by deleting the c:\users\MyUser\ .cache\torch folder and running the project again, this reinstalls the dependencies. I hope it helps you

julianVelandia avatar Oct 03 '22 00:10 julianVelandia

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

  • Wiki – https://github.com/ultralytics/yolov5/wiki
  • Tutorials – https://docs.ultralytics.com/yolov5
  • Docs – https://docs.ultralytics.com

Access additional Ultralytics ⚡ resources:

  • Ultralytics HUB – https://ultralytics.com/hub
  • Vision API – https://ultralytics.com/yolov5
  • About Us – https://ultralytics.com/about
  • Join Our Team – https://ultralytics.com/work
  • Contact Us – https://ultralytics.com/contact

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐!

github-actions[bot] avatar Nov 03 '22 00:11 github-actions[bot]

@julianVelandia great to hear that you were able to resolve the issue! If you encounter any further challenges or have additional questions, feel free to ask. Happy coding!

glenn-jocher avatar Nov 15 '23 09:11 glenn-jocher