ultralytics / yolov5

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

'RandomSampler' object has no attribute 'set_epoch' #13233

Open lili084 opened 1 month ago

lili084 commented 1 month ago

Search before asking

Question

I'm using train.py to train my own data and I'm getting this error, I'm not sure how it's happening if RANK != -1: train_loader.sampler.set_epoch(epoch)

Additional

No response

github-actions[bot] commented 1 month ago

👋 Hello @lili084, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 🚀

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 🚀!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 1 month ago

@lili084 hi there,

Thank you for reaching out and providing details about the issue you're encountering.

The error you're seeing, 'RandomSampler' object has no attribute 'set_epoch', typically occurs when the train_loader is using a RandomSampler instead of a DistributedSampler. The set_epoch method is specific to DistributedSampler and is used in distributed training to shuffle the data differently at each epoch.

To resolve this, please ensure that your code is correctly setting up the DistributedSampler when running in a distributed environment. Here is a snippet that demonstrates how to set up the DistributedSampler:

from torch.utils.data import DistributedSampler

# Assuming dataset is already defined
if RANK != -1:
    sampler = DistributedSampler(dataset)
else:
    sampler = torch.utils.data.RandomSampler(dataset)

train_loader = torch.utils.data.DataLoader(dataset, sampler=sampler, ...)

If you are not running in a distributed environment, you can bypass the set_epoch call by adding a condition:

if RANK != -1:
    train_loader.sampler.set_epoch(epoch)

Please make sure you are using the latest version of YOLOv5 and all related dependencies. You can update YOLOv5 by pulling the latest changes from the repository:

git pull

If the issue persists after these changes, please let us know, and we can further investigate. Thank you for your patience and for being a part of the YOLO community! 😊