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

With mosaic augmentation, are only 3 of the 4 images shuffled? #1016

Closed
bsugerman opened this issue Sep 22, 2020 · 7 comments · Fixed by #5623
Closed

With mosaic augmentation, are only 3 of the 4 images shuffled? #1016

bsugerman opened this issue Sep 22, 2020 · 7 comments · Fixed by #5623
Labels
question Further information is requested Stale

Comments

@bsugerman
Copy link

Looking at the create_dataloader and load_mosaic functions, it looks like the order of the first image in each mosaic never changes, while the 2-4th images are random choices from the full dataset. In other words, the first mosaic uses image[0] in the image data list and then 3 random ones, the 2nd mosaic uses image[1] and 3 random ones, etc. However, the ordering of the image list is never shuffled. Is that correct?

@bsugerman bsugerman added the question Further information is requested label Sep 22, 2020
@glenn-jocher
Copy link
Member

@bsugerman I looked into the code and yes, your interpretation is correct.

If you wanted to shuffle image 0 in the mosaic you would pass an additional argument shuffle=True to the dataloader here:

yolov5/utils/datasets.py

Lines 66 to 71 in 702c4fa

dataloader = InfiniteDataLoader(dataset,
batch_size=batch_size,
num_workers=nw,
sampler=sampler,
pin_memory=True,
collate_fn=LoadImagesAndLabels.collate_fn) # torch.utils.data.DataLoader()

@glenn-jocher
Copy link
Member

@bsugerman BTW, if you notice any improved results with this change please let us know!

@NanoCode012
Copy link
Contributor

Btw, I don't think you should set shuffle for DDP mode since we use sampler with the current code.

sampler (Sampler or Iterable, optional) – defines the strategy to draw samples from the dataset. Can be any Iterable with len implemented. If specified, shuffle must not be specified.

From https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader

An option would be to implement RandomSampler or similar for Single GPU.

https://pytorch.org/docs/stable/data.html

@glenn-jocher
Copy link
Member

@NanoCode012 ah good point. In the end I doubt shuffling would have a significant impact, as @bsugerman mentioned the train set is already 75% shuffled even without any shuffling.

@bsugerman
Copy link
Author

Also however, this means that training uses each image an average of 4 times per epoch.

@NanoCode012 ah good point. In the end I doubt shuffling would have a significant impact, as @bsugerman mentioned the train set is already 75% shuffled even without any shuffling.

@github-actions
Copy link
Contributor

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
Copy link
Member

@bsugerman @NanoCode012 good news 😃! Your original issue may now be fixed ✅ in PR #5623 by @werner-duvaud. This PR turns on shuffling in the YOLOv5 training DataLoader by default, which was missing until now. This works for all training formats: CPU, Single-GPU, Multi-GPU DDP.

train_loader, dataset = create_dataloader(train_path, imgsz, batch_size // WORLD_SIZE, gs, single_cls,
                                          hyp=hyp, augment=True, cache=opt.cache, rect=opt.rect, rank=LOCAL_RANK,
                                          workers=workers, image_weights=opt.image_weights, quad=opt.quad,
                                          prefix=colorstr('train: '), shuffle=True)  # <--- NEW

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

To receive this update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants