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

Fix bug when enable --quad training option #13355

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

imyhxy
Copy link
Contributor

@imyhxy imyhxy commented Oct 14, 2024

When enabling the --quad training option, the collate_fn4 function assumes the batch size will be greater than 4, but without enabling drop_last in the dataloader, the size of the last batch is unpredictable. So in some cases, an Exception may be raised, depending on the size of the dataset.

Original Traceback (most recent call last):                                                                                                                                                                                                                                                 
  File "/home/user/envs/edge/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop                                                                                                                                                                    
    data = fetcher.fetch(index)                                                                                                                                                                                                                                                             
  File "/home/user/envs/edge/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 61, in fetch                                                                                                                                                                             
    return self.collate_fn(data)                                                                                                                                                                                                                                                            
  File "/home/user/workspace/srcs/public/yolov5/utils/dataloaders.py", line 1070, in collate_fn4                                                                                                                                                                                          
    return torch.stack(im4, 0), torch.cat(label4, 0), path4, shapes4                                                                                                                                                                                                                        
RuntimeError: stack expects a non-empty TensorList      

n will be zero, if batch size per GPU is smaller than 4.

n = len(shapes) // 4

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Added drop_last parameter to data loaders for improved batch handling.

📊 Key Changes

  • Added drop_last argument to create_dataloader() function in both dataloaders.py and segment/dataloaders.py.

🎯 Purpose & Impact

  • Purpose: Ensures consistency in batch sizes during training when using the quad parameter.
  • Impact: Improves training stability and performance by avoiding incomplete batches, leading to more efficient resource utilization. 📈

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

This PR introduces an enhancement to the data loading process for YOLOv5 by incorporating the drop_last parameter to better handle batched data.

📊 Key Changes

  • Added the drop_last=quad parameter in the data loader configurations within utils/dataloaders.py and utils/segment/dataloaders.py.

🎯 Purpose & Impact

  • Purpose: The drop_last parameter helps manage batches more effectively, particularly when using "quad" mode, by dropping the last incomplete batch if it's smaller than the set batch size.
  • Impact: This change ensures more consistent batch sizes during training, which can enhance stability and performance, especially when using data augmentations that require specific batch configurations.

@UltralyticsAssistant UltralyticsAssistant added bug Something isn't working python labels Oct 14, 2024
@UltralyticsAssistant
Copy link
Member

👋 Hello @imyhxy, thank you for your contribution to the ultralytics/yolov5 repository! 🚀 This is an automated response, and one of our Ultralytics engineers will follow up with you soon. To facilitate a smooth integration, please review the checklist below:

  • Define a Purpose: Make sure your PR description clearly explains the purpose of your fix. If there are any related issues, please link to them. Clearly articulate your commit messages for better understanding.
  • Synchronize with Source: Ensure your branch is up to date with the main branch. You can update it by clicking 'Update branch' or running git pull and git merge main in your local environment.
  • Check CI Results: Verify that all Ultralytics Continuous Integration (CI) checks have passed. If any issues occur, please resolve them.
  • Refresh Documentation: If you've made any changes or added new features, ensure the related documentation is up to date.
  • Include Tests: Add or update tests to cover your changes. Confirm all tests pass successfully.
  • Sign the CLA: For your first contribution, sign our Contributor License Agreement by stating "I have read the CLA Document and I sign the CLA" in a comment.
  • Add MRE if Necessary: If this PR addresses a bug, providing a Minimum Reproducible Example (MRE) will help us better assess the solution.
  • Minimize Changes: Ensure that your changes are as concise as possible. As Bruce Lee wisely said, "Hack away at the unessential."

For further guidance, please refer to our Contributing Guide. Feel free to ask any questions in the comments below. Thank you for helping make Ultralytics better! 🎉

@imyhxy
Copy link
Contributor Author

imyhxy commented Oct 14, 2024

I have read the CLA Document and I sign the CLA

@glenn-jocher glenn-jocher merged commit 94a6245 into ultralytics:master Oct 15, 2024
8 checks passed
@glenn-jocher
Copy link
Member

@imyhxy thank you, PR merged!

@imyhxy imyhxy deleted the fix-quad branch October 15, 2024 16:24
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
Development

Successfully merging this pull request may close these issues.

3 participants