ultralytics / yolov5

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

'RecursiveScriptModule' object has no attribute 'get' #12188

Closed KonstiDE closed 1 year ago

KonstiDE commented 1 year ago

Search before asking

YOLOv5 Component

Export

Bug

Hello, I convert a yolov5 model via mm-deploy like the following:

config_path = 'yolov5_base.py'
model_path = 'epoch_500.pth'

register_all_modules()

model = init_detector(
    config=config_path,
    checkpoint=model_path,
    device='cpu'
).cpu()
model = model.eval()

example = torch.rand(1, 3, 512, 512).cpu()

traced_script_module = torch.jit.trace(model, example, strict=False)
optimized_traced_model = optimize_for_mobile(traced_script_module)
optimized_traced_model._save_for_lite_interpreter("traced_yolo.pt")

Works like a charm. However, I want to use it in the Flutter package pytorch_lite and they say to export the model via te export.py file in the yolov5 Github Repo, so I use the Colab environment of it and run:

!python export.py --weights traced_yolo.pt --include torchscript --img 512--optimize`

My model for producing a minimal example can be found here.

The final error is:

Traceback (most recent call last): File "/content/yolov5/export.py", line 863, in <module> main(opt) File "/content/yolov5/export.py", line 858, in main run(**vars(opt)) File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context return func(*args, **kwargs) File "/content/yolov5/export.py", line 736, in run model = attempt_load(weights, device=device, inplace=True, fuse=True) # load FP32 model File "/content/yolov5/models/experimental.py", line 80, in attempt_load ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model File "/usr/local/lib/python3.10/dist-packages/torch/jit/_script.py", line 784, in __getattr__ return super().__getattr__(attr) File "/usr/local/lib/python3.10/dist-packages/torch/jit/_script.py", line 501, in __getattr__ return super().__getattr__(attr) File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1614, in __getattr__ raise AttributeError("'{}' object has no attribute '{}'".format( AttributeError: 'RecursiveScriptModule' object has no attribute 'get'

Is there anything wrong with the workflow itself? What does the error indicate? A close ticket is #7269 but it did not rly got solved with a solution.

Environment

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 1 year ago

πŸ‘‹ Hello @KonstiDE, 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 a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. 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

YOLOv5 CI

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

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 year ago

@KonstiDE this issue seems to be related to using YOLOv5 with mm-deploy and exporting the model via the export.py script.

The error message indicates that the RecursiveScriptModule object does not have the attribute get. This issue has been reported before (#7269), but it seems that a solution hasn't been found yet.

To troubleshoot this issue, I recommend checking the compatibility between YOLOv5 and mm-deploy, ensuring that they are using compatible versions of PyTorch and other dependencies. Additionally, you can try exporting the model without the --optimize flag to see if that resolves the issue.

If the issue persists, I encourage you to submit a bug report with the necessary details, including the steps to reproduce the error and any error logs or traceback messages. This will help the YOLOv5 community and the Ultralytics team investigate and address the issue.

Thank you for your understanding and your willingness to contribute!

KonstiDE commented 1 year ago

Unfortunately, removing the --optimize flag is not resolving the issue. I will play around a bit and update the ticket accordingly.

glenn-jocher commented 1 year ago

@KonstiDE i understand that removing the --optimize flag did not resolve the issue. Thank you for letting us know. Please feel free to experiment further and update the ticket with any new findings or details. We appreciate your contribution to troubleshooting the problem.

github-actions[bot] commented 1 year ago

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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 YOLO πŸš€ and Vision AI ⭐