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

[bug fix] potential problem if img fed to model is in rectangular shape #275

Merged
merged 2 commits into from
Jul 8, 2020

Conversation

wanghaoyang0106
Copy link
Contributor

@wanghaoyang0106 wanghaoyang0106 commented Jul 3, 2020

Hello! I made the PR of bug #266
I also noticed that, in utils/utils.scale_coords, the gain calculation is quite confusing.

gain = max(img1_shape) / max(img0_shape)  # gain  = old / new

It might be incorrect if the image to the model img1 is in rectangular shape but the original image img0 is in squre, or img1 is larger in width but img0 is larger in height.
Actually gain and pad here should be exactly idendity to the r and (dw, dh) in utils/datasets.letterbox, since it's just the reverse operation. So I also modified this part.

But still, there are several round operations in utils/datasets.letterbox, but the reverse operation of utils/utils.scale_coords uses exact float number, which may cause some slight error. So if possible, I hope to change to use ratio_pad in utils/utils.scale_coords to directly set the ratio and pad from utils/datasets.letterbox.

And actually in my own application, I will use some images with very large width/height ratio, and that's why I am focusing on this width/height process. But currently train.py only do preprocess to resize and pad the raw image to square. This is acceptable but not preferrable. If possible, I hope to have the feature of allowing rectangular shaped images for training.

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Enhancements to image resizing and coordinate scaling logic in YOLOv5's data preprocessing.

📊 Key Changes

  • 🛠 Fixed the new_unpad calculation in letterbox function to correctly reflect (height, width) ordering.
  • 🔍 Adjusted the gain calculation in scale_coords to use the minimum scale factor between width and height, rather than the maximum of the two dimensions.

🎯 Purpose & Impact

  • 📏 The letterbox change ensures the aspect ratio is maintained during image resizing without padding errors.
  • 📐 The scale_coords adjustment provides more accurate bounding box coordinates after scaling, important for object detection accuracy.
  • 🐛 These changes combine to fix aspect ratio distortion and improve the precision of object detection, leading to potentially better performance of the YOLOv5 models in various applications.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 4, 2020

@wanghaoyang0106 Hi, thanks for the PR! Can you please check that COCO mAP is not affected by this change, i.e. run test.py --weights yolov5s.pt before and after the change? Thanks!

@wanghaoyang0106
Copy link
Contributor Author

Hello!
Sorry for the late reply. I tested before and after the changes, and nothing is affected.

Before:
before
before

After:
after
after

@glenn-jocher
Copy link
Member

@wanghaoyang0106 ok great, thanks for running the checks! Everything looks good, I'll merge this now.

@wanghaoyang0106
Copy link
Contributor Author

Thank you!

@glenn-jocher glenn-jocher merged commit 31b07b3 into ultralytics:master Jul 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants