yolov5
yolov5 copied to clipboard
ImportError: cannot import name 'draw_bounding_boxes' from 'torchvision.utils'
Search before asking
- [X] I have searched the YOLOv5 issues and found no similar bug report.
YOLOv5 Component
Training
Bug

Environment
No response
Minimal Reproducible Example
No response
Additional
No response
Are you willing to submit a PR?
- [ ] Yes I'd like to help by submitting a PR!
π Hello @abcsunshine, 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):
- Google Colab and Kaggle notebooks with free GPU:
- Google Cloud Deep Learning VM. See GCP Quickstart Guide
- Amazon Deep Learning AMI. See AWS Quickstart Guide
- Docker Image. See Docker Quickstart Guide
Status
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.
@thepycoder can you take a look please? @abcsunshine is this error related to clearml logger? Does it work fine without clearml?
Can you do a pip freeze and make sure you have torchvision installed? Is it the same version as in the requirements.txt?
I think this is the right version. However, when I just ignore the import codeοΌit can work well.


It seems like the draw_bounding_boxes functionality was only added to torchvision starting from v0.9.0.
In contrast, the requirements of YOLOv5 specify torchvision>=0.8.1, which means if you have 0.8.1 or 0.8.2 installed, it will error because of this import statement.
So the solution for you @abcsunshine is to upgrade torchvision, or (if you don't use ClearML), commenting out the import statement is indeed also fine.
@AyushExel How do you want to solve this? I assume you won't like it to change the whole repo requirements file from torchvision>=0.8.1 to torchvision>=0.9.0. So in that case, we should re-implement the draw_bounding_boxes function without using torchvision. Any other ideas?
@thepycoder ah got it. Simplest quick fix is to scope the import to place it within the try: import clearml statement. This will reduce the affected userbase.
A more permanent fix might be to use the YOLOv5 Annotator class below. We can't bump the torchvision requirements right now because there are some Jetson Nano tutorials that require torch 1.7.0 torchvision 0.8.1
Annotator class: https://github.com/ultralytics/yolov5/blob/b551098d59cce7aa9755b711b9cbe49758d3beae/utils/plots.py#L68-L126
detect.py usage example for box annotation: https://github.com/ultralytics/yolov5/blob/b551098d59cce7aa9755b711b9cbe49758d3beae/detect.py#L170
@abcsunshine @thepycoder good news π! Your original issue may now be fixed β in PR #8915. This is not a permanent solution but rather a scoped import quickfix.
To receive this update:
- Git β
git pullfrom within youryolov5/directory orgit clone https://github.com/ultralytics/yolov5again - PyTorch Hub β Force-reload
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True) - Notebooks β View updated notebooks
- Docker β
sudo docker pull ultralytics/yolov5:latestto update your image
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 π!
@abcsunshine @thepycoder I think this shows the need for another CI check at the torch minimum requirement. I'll add this to the YOLOv5 CI to catch these problems earlier in the future. Trying to add in https://github.com/ultralytics/yolov5/pull/8916