Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'draw_bounding_boxes' from 'torchvision.utils' #8898

Closed
1 of 2 tasks
abcsunshine opened this issue Aug 8, 2022 · 8 comments · Fixed by #8915 or #8993
Closed
1 of 2 tasks

ImportError: cannot import name 'draw_bounding_boxes' from 'torchvision.utils' #8898

abcsunshine opened this issue Aug 8, 2022 · 8 comments · Fixed by #8915 or #8993
Labels
bug Something isn't working

Comments

@abcsunshine
Copy link

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Training

Bug

image

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!
@abcsunshine abcsunshine added the bug Something isn't working label Aug 8, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Aug 8, 2022

👋 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 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.

@AyushExel
Copy link
Contributor

@thepycoder can you take a look please?
@abcsunshine is this error related to clearml logger? Does it work fine without clearml?

@thepycoder
Copy link
Contributor

Can you do a pip freeze and make sure you have torchvision installed? Is it the same version as in the requirements.txt?

@abcsunshine
Copy link
Author

I think this is the right version.
However, when I just ignore the import code,it can work well.

image

image

@thepycoder
Copy link
Contributor

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?

@glenn-jocher
Copy link
Member

glenn-jocher commented Aug 9, 2022

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

yolov5/utils/plots.py

Lines 68 to 126 in b551098

class Annotator:
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=False, example='abc'):
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to Annotator() input images.'
non_ascii = not is_ascii(example) # non-latin labels, i.e. asian, arabic, cyrillic
self.pil = pil or non_ascii
if self.pil: # use PIL
self.im = im if isinstance(im, Image.Image) else Image.fromarray(im)
self.draw = ImageDraw.Draw(self.im)
self.font = check_pil_font(font='Arial.Unicode.ttf' if non_ascii else font,
size=font_size or max(round(sum(self.im.size) / 2 * 0.035), 12))
else: # use cv2
self.im = im
self.lw = line_width or max(round(sum(im.shape) / 2 * 0.003), 2) # line width
def box_label(self, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255)):
# Add one xyxy box to image with label
if self.pil or not is_ascii(label):
self.draw.rectangle(box, width=self.lw, outline=color) # box
if label:
w, h = self.font.getsize(label) # text width, height
outside = box[1] - h >= 0 # label fits outside box
self.draw.rectangle(
(box[0], box[1] - h if outside else box[1], box[0] + w + 1,
box[1] + 1 if outside else box[1] + h + 1),
fill=color,
)
# self.draw.text((box[0], box[1]), label, fill=txt_color, font=self.font, anchor='ls') # for PIL>8.0
self.draw.text((box[0], box[1] - h if outside else box[1]), label, fill=txt_color, font=self.font)
else: # cv2
p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
cv2.rectangle(self.im, p1, p2, color, thickness=self.lw, lineType=cv2.LINE_AA)
if label:
tf = max(self.lw - 1, 1) # font thickness
w, h = cv2.getTextSize(label, 0, fontScale=self.lw / 3, thickness=tf)[0] # text width, height
outside = p1[1] - h >= 3
p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
cv2.rectangle(self.im, p1, p2, color, -1, cv2.LINE_AA) # filled
cv2.putText(self.im,
label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2),
0,
self.lw / 3,
txt_color,
thickness=tf,
lineType=cv2.LINE_AA)
def rectangle(self, xy, fill=None, outline=None, width=1):
# Add rectangle to image (PIL-only)
self.draw.rectangle(xy, fill, outline, width)
def text(self, xy, text, txt_color=(255, 255, 255)):
# Add text to image (PIL-only)
w, h = self.font.getsize(text) # text width, height
self.draw.text((xy[0], xy[1] - h + 1), text, fill=txt_color, font=self.font)
def result(self):
# Return annotated image as array
return np.asarray(self.im)

detect.py usage example for box annotation:

annotator.box_label(xyxy, label, color=colors(c, True))

glenn-jocher added a commit that referenced this issue Aug 9, 2022
Quick fix for #8898 (comment) until a more permanent fix is implemented
glenn-jocher added a commit that referenced this issue Aug 9, 2022
* Scope `torchvision.utils.draw_bounding_boxes` import

Quick fix for #8898 (comment) until a more permanent fix is implemented

* Update clearml_utils.py
@glenn-jocher glenn-jocher linked a pull request Aug 9, 2022 that will close this issue
@glenn-jocher
Copy link
Member

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

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

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

@glenn-jocher
Copy link
Member

@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 #8916

ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
* Scope `torchvision.utils.draw_bounding_boxes` import

Quick fix for ultralytics#8898 (comment) until a more permanent fix is implemented

* Update clearml_utils.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants