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

[Classify]: Allow inference on dirs and videos #9003

Merged
merged 9 commits into from
Aug 17, 2022

Conversation

AyushExel
Copy link
Contributor

@AyushExel AyushExel commented Aug 17, 2022

This PR allows running classification inference on dirs.
Usage:
python classify/predict.py --source data/images
Screenshot 2022-08-17 at 11 32 06 PM

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Enhanced the image classification inference to support various input types.

📊 Key Changes

  • Extended classify/predict.py to handle file, directory, URL, and glob patterns as input sources.
  • Removed dependency on OpenCV (cv2) from the main inference script.
  • Added a new LoadImages class to the classify/predict.py to handle different input types.
  • Implemented a post-processing step to enumerate top-5 predicted categories with their associated probabilities.
  • Streamlined the preprocessing of images by using a unified classify_transforms function.
  • Updated argument parser to accept directory, URL, or glob as the --source parameter.

🎯 Purpose & Impact

  • Allows users to perform classification on a wider variety of input sources seamlessly.
  • Simplifies the code by removing OpenCV dependencies from the classification script itself, which could reduce memory usage and improve portability.
  • Enhances user experience by reporting more detailed classification results.
  • These updates make it easier for experts and non-experts alike to use YOLOv5 for image classification tasks across a broader range of scenarios with potentially higher efficiency and ease of use. 🚀

@glenn-jocher
Copy link
Member

@AyushExel this is a good idea, but the current implementation loads every image twice, which we want to avoid. I think the challenge is the input transforms are different for cls models. This might take some updating to LoadImages to accept a transform object, and to apply default transforms if T=None.

@AyushExel
Copy link
Contributor Author

@glenn-jocher yes the double image loading should slow down the video inference but should be negligible for single images or a dir. You want to address the transforms support in LoadImages here or take it to a separate PR?

@glenn-jocher
Copy link
Member

@AyushExel I think I can add them in this PR. Let's see.

@glenn-jocher glenn-jocher changed the title [Classify]: Allow inference on dirs [Classify]: Allow inference on dirs and videos Aug 17, 2022
@glenn-jocher
Copy link
Member

@AyushExel ok awesome. This passes the cls transforms to the dataloader and adds inference support for dirs, globs, URLs and videos.

@AyushExel
Copy link
Contributor Author

@glenn-jocher ok perfect! While you're at it, what do you think about adding support for --show-vid wherein the video inference displays each frame in cv2 windows with top 5 labels being printed at the top or bottom ?

@glenn-jocher glenn-jocher merged commit 64e0757 into ultralytics:master Aug 17, 2022
@glenn-jocher
Copy link
Member

@AyushExel yup there's definitely more we could do on the logging/plotting results side of things with classification. I wasn't sure how to save/show classification results, but yes we could just write it directly on the image like you suggested.

@glenn-jocher
Copy link
Member

glenn-jocher commented Aug 17, 2022

Should probably also refactor the URL-file download code into LoadImages since now it's duplicated in detect.py and classify/predict.py.

ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this pull request Sep 8, 2022
* allow image dirs

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update predict.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update dataloaders.py

* Update predict.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update predict.py

* Update predict.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
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.

None yet

2 participants