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

Default DataLoader shuffle=True for training #5623

Merged
merged 7 commits into from
Nov 13, 2021
Merged

Default DataLoader shuffle=True for training #5623

merged 7 commits into from
Nov 13, 2021

Conversation

werner-duvaud
Copy link
Contributor

@werner-duvaud werner-duvaud commented Nov 12, 2021

If the issue #5622 is relevant, I have added the shuffle argument to create_dataloader. It defaults to False and is set to True only when creating the training dataloader.
This shuffle parameter is then passed to the DataLoader or DistributedSampler to maintain a coherent behaviour. (in pytorch doc, shuffle defaults to False for DataLoader and to True for DistributedSampler)

🛠️ PR Summary

Made with ❤️ by Ultralytics Actions

🌟 Summary

Enhanced data loading with shuffle option for YOLOv5 training.

📊 Key Changes

  • train.py now passes a shuffle=True argument to the data loader, enabling data shuffling during training.
  • create_dataloader function in utils/datasets.py now accepts a shuffle argument.
  • Rectangular training (rect=True) and shuffling are made mutually exclusive to avoid potential conflicts.
  • Refactored the use of DataLoader and InfiniteDataLoader to allow attribute updates, with a conditional use of DistributedSampler based on rank.
  • Adjusted imports in utils/datasets.py for clearer code structure.

🎯 Purpose & Impact

  • Data shuffling is a common technique to improve model generalization and prevent overfitting, which can lead to better performance.
  • By preventing the incompatible configuration of rectangular batches with shuffling, the PR ensures stable training behavior.
  • The changes enhance scalability and usability for distributed training scenarios, potentially improving user experiences during training on various hardware setups.
  • Overall, these modifications might contribute to more robust model performance and smoother user experiences with the YOLOv5 training pipeline.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 Hello @werner-duvaud, thank you for submitting a 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:

  • ✅ Verify your PR is up-to-date with upstream/master. If your PR is behind upstream/master an automatic GitHub actions rebase may be attempted by including the /rebase command in a comment body, or by running the following code, replacing 'feature' with the name of your local branch:
git remote add upstream https://github.com/ultralytics/yolov5.git
git fetch upstream
git checkout feature  # <----- replace 'feature' with local branch name
git merge upstream/master
git push -u origin -f
  • ✅ Verify all 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

@glenn-jocher
Copy link
Member

glenn-jocher commented Nov 12, 2021

@werner-duvaud one conflict here is --rect, which will force single-image (no mosaic) minimum rectangular batch sizes. This precomputes batch size dimensions based on the dataset sorted by aspect ratio. val.py uses this by default for example, but it's a setting we also have in train.py also.

So we need something like shuffle = shuffle and not rect in place.

@werner-duvaud
Copy link
Contributor Author

@glenn-jocher Thanks!

I have added and not rect.

The downside could be that by passing shuffle = True to create_dataloader one expects random indexes but if rect is True shuffle will be overwritten. And the shuffle effect will come from the sort by aspect ratio which is not 100% shuffled in some cases.
It seems ok to me given the purpose of the rect argument. If necessary I can add a log warning when shuffle and rect are both passed to True to create_dataloader to warn in case for future modifications.

(I have rebased to make the CI happy)

@glenn-jocher
Copy link
Member

glenn-jocher commented Nov 12, 2021

@werner-duvaud yes good idea. Can you all a logger warning for this case?

EDIT: added this update and a bit of cleanup myself in 89abf9e

@glenn-jocher glenn-jocher linked an issue Nov 13, 2021 that may be closed by this pull request
2 tasks
@glenn-jocher glenn-jocher changed the title Fix shuffle DataLoader argument Fix #5622 Default DataLoader shuffle=True for training Nov 13, 2021
@glenn-jocher glenn-jocher linked an issue Nov 13, 2021 that may be closed by this pull request
@glenn-jocher
Copy link
Member

@werner-duvaud cleaned this up a bit and added a rect-shuffle conflict warning and handling. Evaluating now on VOC. Also linked to #2582 to close that (long-running) TODO.

@glenn-jocher
Copy link
Member

@werner-duvaud PR is merged. Thank you for your contributions to YOLOv5 🚀 and Vision AI ⭐

@glenn-jocher
Copy link
Member

@werner-duvaud I evaluated this PR against master on VOC finetuning for 50 epochs, and the results show a slight improvement in most metrics and losses, particularly in objectness loss and mAP@0.5, perhaps indicating that the shuffle addition may help delay overtraining.

https://wandb.ai/glenn-jocher/VOC
Screenshot 2021-11-13 at 13 03 26

BjarneKuehl pushed a commit to fhkiel-mlaip/yolov5 that referenced this pull request Aug 26, 2022
* Fix shuffle DataLoader argument

* Add shuffle argument

* Disable shuffle when rect

* Cleanup, add rect warning

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

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

* Cleanup2

* Cleanup3

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment