ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
50.56k stars 16.31k forks source link

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

Closed Ahuriste closed 1 year ago

Ahuriste commented 2 years ago

Search before asking

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

github-actions[bot] commented 2 years ago

πŸ‘‹ 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 support@ultralytics.com.

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.

zerocool909 commented 2 years ago

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.

L0calBoy commented 2 years ago

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

glenn-jocher commented 2 years ago

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

Mercury-ML commented 2 years ago

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!

glenn-jocher commented 2 years ago

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

Mercury-ML commented 2 years ago

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

glenn-jocher commented 2 years ago

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

For Ultralytics to provide assistance your code should also be:

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 commented 2 years ago

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

Screenshot 2022-09-07 at 19 40 37
Mercury-ML commented 2 years ago

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.

OttomanZ commented 2 years ago

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

DavidReveloLuna commented 2 years ago

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

Mercury-ML commented 2 years ago

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

DavidReveloLuna commented 2 years ago

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

julianVelandia commented 2 years ago

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

github-actions[bot] commented 1 year ago

πŸ‘‹ 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:

Access additional Ultralytics ⚑ resources:

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 ⭐!

glenn-jocher commented 11 months ago

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