ultralytics / yolov5

YOLOv5 🚀 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
51.24k stars 16.44k forks source link

Visualization of feature maps #1067

Closed xinxin342 closed 4 years ago

xinxin342 commented 4 years ago

🚀 Feature

Maybe we can add one more function to Yolov5 that can conveniently show the feature maps of convolutional layers and detection layers?

Motivation

Pitch

So we can visually see where the model's attention is.

Alternatives

Additional context

github-actions[bot] commented 4 years ago

Hello @xinxin342, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

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 model or data training question, please note Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

For more information please visit https://www.ultralytics.com.

glenn-jocher commented 4 years ago

@xinxin342 we are open to PRs, so if you would like to work on this and submit a PR we'd be happy to review!

xinxin342 commented 4 years ago

@ xinxin342我们对PR保持开放,因此,如果您要从事此工作并提交PR,我们将很高兴进行审查!

Sorry, I'm completely out of my depth, but I see that it looks like YOLOv4 can do this.

glenn-jocher commented 4 years ago

@xinxin342 feature map visualization is something you can inject into the model forward function at the location of your choosing. It's up to you exactly where in the model you'd want to visualize this, as there are hundreds of layers and thousands of maps.

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

glenn-jocher commented 3 years ago

@xinxin342 good news 😃! Feature map visualization was added ✅ in PR #3804 by @Zigars today. This allows for visualizing feature maps from any part of the model from any function (i.e. detect.py, train.py, test.py). Feature maps are saved as *.png files in runs/features/exp directory. To turn on feature visualization set feature_vis=True in the model forward method and define the layer you want to visualize (default is SPP layer).

https://github.com/ultralytics/yolov5/blob/20d45aa4f18d7df7148fc6dd69a9c0607003f004/models/yolo.py#L158-L160

To receive this update:

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

layer_8_SPP_features

MLDavies commented 2 years ago

Can you tell me more about how to implement the feature maps functionality? I assumed from above that, if we're after the SPP layer, we just needed to go into yolo.py and change visualize=False to True in: def _forward_once(self, x, profile=False, visualize=True). Clearly this isn't enough. What am I missing?

glenn-jocher commented 2 years ago

@MLDavies 👋 Hello! Thanks for asking about feature visualization. YOLOv5 🚀 features can be visualized through all stages of the model from input to output. To visualize features from a given source run detect.py with the --visualize flag:

python detect.py --weights yolov5s.pt --source data/images/bus.jpg --visualize

An example Notebook visualizing bus.jpg features with YOLOv5s is shown below:

Open In Colab Open In Kaggle

Screenshot 2021-08-30 at 16 44 04

All stages are visualized by default, each with its own PNG showing the first 32 feature maps output from that stage. You can open any PNG for a closer look. For example the first 32 feature maps of the Focus() layer output are shown in stage0_Focus_features.png:

stage0_Focus_features

Feature maps may be customized by updating the feature_visualization() function in utils/plots.py: https://github.com/ultralytics/yolov5/blob/bb5ebc290e5d630a081d7cbc5a9725ed8cea0a24/utils/plots.py#L403-L427

Good luck 🍀 and let us know if you have any other questions!

MLDavies commented 2 years ago

Perfect. Love yolov5! Ya, I was (mistakenly) trying to call --visualize on train.py rather than detect.py. Thanks for what you do, and for your responses.

wanglegoc commented 2 years ago

@glenn-jocher I run the following script python detect.py --weights yolov5s.pt --source data/images/bus.jpg --visualize. Instead of obtaining results as you shown, here how it looks for stage22_Concat_features stage22_Concat_features

glenn-jocher commented 2 years ago

@wanglegoc --visualize works correctly. I just tested right now:

Screen Shot 2022-05-20 at 3 00 46 PM
abhigoku10 commented 2 years ago

@glenn-jocher can we use this feature for yolov4 architecture ? if not what are the changes to be made to the code

glenn-jocher commented 1 year ago

@abhigoku10 The feature map visualization is specific to the YOLOv5 architecture and is not directly compatible with YOLOv4. Porting this feature to YOLOv4 would require significant modifications to the codebase, including changes to the network architecture, model forward method, and visualization logic. If you are interested in adding feature map visualization to YOLOv4, I recommend referring to the YOLOv5 implementation as a reference and making the necessary adaptations to the YOLOv4 codebase. Good luck!