-
-
Notifications
You must be signed in to change notification settings - Fork 16.6k
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
Fix reshape_classifier_output function to correctly reshape the final output layer #13052
Fix reshape_classifier_output function to correctly reshape the final output layer #13052
Conversation
All Contributors have signed the CLA. ✅ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👋 Hello @goksmisama, thank you for submitting a YOLOv5 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
- ✅ Verify your PR is up-to-date with
ultralytics/yolov5
master
branch. If your PR is behind you can update your code by clicking the 'Update branch' button or by runninggit pull
andgit merge master
locally. - ✅ Verify all YOLOv5 Continuous Integration (CI) checks are passing.
- ✅ Reduce changes to the absolute minimum required for your bug fix or feature addition. "It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is." — Bruce Lee
I have read the CLA Document and I sign the CLA |
@glenn-jocher Could you please review this PR when you have time? Thank you! |
Hi @goksmisama, just a gentle reminder to review this PR at your earliest convenience. Thanks for your time! 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @glenn-jocher, thanks for your comment! I have reviewed the PR. Please let me know if there is anything else I need to do. Thanks again!
Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@goksmisama PR merged! Thank you for your contributions :) |
Description:
This Pull Request addresses an issue in the
reshape_classifier_output
function, which incorrectly modifies the firstnn.Linear
ornn.Conv2d
layer encountered in annn.Sequential
module instead of the final output layer. The proposed change ensures that the final output layer is correctly identified and reshaped to match the specified number of classesn
.Issue:
In the original implementation, when the function encounters an
nn.Sequential
module, it modifies the firstnn.Linear
ornn.Conv2d
layer found. This is problematic because this layer is not necessarily the final output layer of the model, which is the one that should be reshaped.When using VGG13 to replace the YOLOv5 model for MNIST classification, my configuration was as follows:
Error message:
# exception info RuntimeError: mat1 and mat2 shapes cannot be multiplied (64x10 and 4096x4096)
With the above configuration, the
reshape_classifier_output
function incorrectly modifies the first linear layer instead of the final output layer, resulting in a shape mismatch error. For example, after reshaping the classification count, the structure of VGG13 is as follows:Solution:
The updated implementation identifies and modifies the last
nn.Linear
ornn.Conv2d
layer within annn.Sequential
module, ensuring that the correct layer is reshaped to match the class countn
.Changes Made:
reshape_classifier_output
function to modify the lastnn.Linear
ornn.Conv2d
layer within annn.Sequential
module.Code:
Testing:
nn.Linear
andnn.Conv2d
layers withinnn.Sequential
modules.n
.Impact:
This change ensures that models using the
reshape_classifier_output
function correctly modify the final output layer, supporting accurate classification outputs and preventing potential shape mismatch issues.Related Issues:
Please link any related issues or discussions here.
Acknowledgements:
Thank you to the maintainers for reviewing this Pull Request. I greatly appreciate your feedback.
🛠️ PR Summary
Made with ❤️ by Ultralytics Actions
🌟 Summary
Improvements in neural network classifier output reshaping.
📊 Key Changes
nn.Linear
andnn.Conv2d
layers in a network has been updated to find the last occurrence of these layers instead of the first.🎯 Purpose & Impact
nn.Linear
ornn.Conv2d
) is being reshaped to match a specified number of outputs (n=1000
), the last layer is modified. This is crucial for networks where these layers might appear multiple times.