Skip to content

Incalos/YOLO-Datasets-And-Training-Methods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YOLO Datasets And Training Methods

中文文档: README_CN.md | English Documentation: README.md

This project mainly completed the following work:

  • It introduces how to make a custom dataset for YOLO and how to train a YOLO model by the custom dataset.
  • Some modifications have been made to Yolov5, YOLOV6, Yolov7 and Yolov8, which can be adapted to the custom dataset for training.
  • Pre-training weights for the object detection model of YOLO are provided.

1. Making Custom Datasets

(1)Capture images

(2)Install and launch Label Studio

  • Install Label Studio

    Label Studio is a powerful open-source data annotation platform that supports annotation for multiple data types. Compared to traditional annotation tools, it offers the following advantages:

    • Web-based interface with no complex installation and configuration
    • Support for collaborative annotation by multiple users
    • Built-in quality control and progress management
    • Support for multiple export formats (YOLO, COCO, VOC, etc.)
    • Active community support and continuous updates
    pip install label-studio
  • Launch Label Studio

    label-studio

    After startup, it will automatically open your browser and navigate to http://localhost:8080

  • Create Project and Configuration

    1. Create a new project
    2. Upload your image data
    3. Select "Computer Vision" -> "Object Detection with Bounding Boxes" template
    4. Label Studio will automatically configure the object detection annotation interface

(3)Configure the annotation tool

  • Label Studio's object detection annotation interface includes the following features:

    • Drag and drop to create bounding boxes
    • Assign category labels
    • Keyboard shortcuts
    • Automatic saving of annotation results
  • Export Settings:

    • Format selection: YOLO format
    • Export path: Configure in project settings

(4)Annotate the dataset

  • Label Studio Annotation Process:

    1. Upload Images: Batch upload images to be annotated in the Label Studio project

    2. Start Annotation:

      • Select an image to enter the annotation interface
      • Use mouse drag to create bounding boxes
      • Assign correct category labels for each bounding box
      • Use keyboard shortcuts to improve annotation efficiency
    3. Quality Control:

      • Preview and edit annotated data
      • Support collaborative annotation by multiple users
      • Real-time tracking of annotation progress
  • Export Annotation Data:

    After completing annotation, export data in Label Studio:

    • Select export format: YOLO or COCO format
    • If COCO format is selected, subsequent conversion to YOLO format is needed
  • Recommended Directory Structure:

    YoloDataSets/
     |——————images/
     |        └——————1.jpg
     |        └——————2.jpg  
     |        └——————3.jpg
     |        └——————...
     |——————Annotations/  (if using VOC/XML format)
     |        └——————1.xml
     |        └——————2.xml  
     |        └——————3.xml
     |        └——————...

(5)Dataset Format Conversion

  • If Label Studio exports YOLO format, it can be used directly without conversion.

  • If COCO or VOC format is exported, use the following method for conversion:

  • Save the annotated dataset in the following structure.

    YoloDataSets/
     |——————images/
     |        └——————1.jpg
     |        └——————2.jpg  
     |        └——————3.jpg
     |        └——————...
     |——————Annotations/
     |        └——————1.xml
     |        └——————2.xml  
     |        └——————3.xml
     |        └——————...
  • Run DataSet.py by typing the following command in the terminal.

    python DataSet.py --yoloversion yolov5 --trainval_percent 0.9 --train_percent 0.9 --mainpath YoloDataSets --classes ['dog','man']
                                    yolov6                    ···                ···             ····                   ['','',···]
                                    yolov7                    ···                ···             ····                   ['','',···]
                                    yolov8                    ···                ···             ····                   ['','',···]
                                    yolov9                    ···                ···             ····                   ['','',···]
                                    yolov10                   ···                ···             ····                   ['','',···]
                                    yolov11                   ···                ···             ····                   ['','',···]
  • The meaning of each parameter in the command is as follows.

    • yoloversion : the version of YOLO, which you can choose YOLOv5, YOLOv6, YOLOv7, YOLOv8, YOLOv9, YOLOv10 and YOLOv11
    • trainval_percent : the total percentage of the training and validation set
    • train_percent : the percentage of training set in training set and validation set
    • mainpath : the root directory of the custom dataset
    • classes : the name of the label, please fill in the list format according to the example

2. Training Model

(1)Training method of YOLOv5

  • Enter the following command in the terminal to access the folder named yolov5.

    cd yolov5
  • Place the converted dataset in the root directory of yolov5.

  • Add the .yaml configuration file named data.yaml to the directory of YoloDataSets with the following content and format.

    path : YoloDataSets
    train: train.txt
    val: val.txt
    test: test.txt
    
    # number of classes
    nc: 2
    
    # class names
    names: ['dog','man']
    
  • Run the following command in the terminal, with the parameters adjusted as appropriate.

    python train.py --data YoloDataSets/data.yaml --epochs 300 --weights yolov5n.pt --cfg model/yolov5n.yaml  --batch-size 128
                                                                         yolov5s.pt       model/yolov5s.yaml               64
                                                                         yolov5m.pt       model/yolov5m.yaml               40
                                                                         yolov5l.pt       model/yolov5l.yaml               24
                                                                         yolov5x.pt       model/yolov5x.yaml               16
  • The official pre-training weights for object detection are provided as follows.

    Model size
    (pixels)
    mAPval
    50-95
    mAPval
    50
    Speed
    CPU b1
    (ms)
    Speed
    V100 b1
    (ms)
    Speed
    V100 b32
    (ms)
    params
    (M)
    FLOPs
    @640 (B)
    YOLOv5n 640 28.0 45.7 45 6.3 0.6 1.9 4.5
    YOLOv5s 640 37.4 56.8 98 6.4 0.9 7.2 16.5
    YOLOv5m 640 45.4 64.1 224 8.2 1.7 21.2 49.0
    YOLOv5l 640 49.0 67.3 430 10.1 2.7 46.5 109.1
    YOLOv5x 640 50.7 68.9 766 12.1 4.8 86.7 205.7
    YOLOv5n6 1280 36.0 54.4 153 8.1 2.1 3.2 4.6
    YOLOv5m6 1280 51.3 69.3 887 11.1 6.8 35.7 50.0
    YOLOv5l6 1280 53.7 71.3 1784 15.8 10.5 76.8 111.4
    YOLOv5x6
    + [TTA]
    1280
    1536
    55.0
    55.8
    72.7
    72.7
    3136
    -
    26.2
    -
    19.4
    -
    140.7
    -
    209.8
    -

(2)Training method of YOLOv6

  • Enter the following command in the terminal to access the folder named yolov6.

    cd yolov6
  • Place the converted dataset in the root directory of yolov6.

  • Add the .yaml configuration file named data.yaml to the directory of YoloDataSets with the following content and format.

    train: YoloDataSets/images/train       # train images
    val: YoloDataSets/images/val           # val images
    test: YoloDataSets/images/test
    is_coco: False
    
    # number of classes
    nc: 2
    
    # class names
    names: ['dog','man']
    
  • Run the following command in the terminal, with the parameters adjusted as appropriate.

    python tools/train.py --batch 64 --conf configs/yolov6s6_finetune.py --data YoloDataSets/data.yaml --epochs 300  --device 0         
    
  • The official pre-training weights for object detection are provided as follows.

    Model Size mAPval
    0.5:0.95
    SpeedT4
    trt fp16 b1
    (fps)
    SpeedT4
    trt fp16 b32
    (fps)
    Params
    (M)
    FLOPs
    (G)
    YOLOv6-N 640 37.5 779 1187 4.7 11.4
    YOLOv6-S 640 45.0 339 484 18.5 45.3
    YOLOv6-M 640 50.0 175 226 34.9 85.8
    YOLOv6-L 640 52.8 98 116 59.6 150.7
    YOLOv6-N6 1280 44.9 228 281 10.4 49.8
    YOLOv6-S6 1280 50.3 98 108 41.4 198.0
    YOLOv6-M6 1280 55.2 47 55 79.6 379.5
    YOLOv6-L6 1280 57.2 26 29 140.4 673.4

(3)Training method of YOLOv7

  • Enter the following command in the terminal to access the folder named yolov7.

    cd yolov7
  • Place the converted dataset in the root directory of yolov7.

  • Add the .yaml configuration file named data.yaml to the directory of YoloDataSets with the following content and format.

    train: YoloDataSets/train.txt
    val: YoloDataSets/val.txt
    test: YoloDataSets/test.txt
    
    # number of classes
    nc: 2
    
    # class names
    names: ['dog','man']
    
  • Run the following command in the terminal, with the parameters adjusted as appropriate.

    # finetune p5 models
    python train.py --workers 8 --device 0 --batch-size 32 --data YoloDataSets/data.yaml --img 640 640 --cfg cfg/training/yolov7-custom.yaml --weights 'yolov7_training.pt' --name yolov7-custom --hyp data/hyp.scratch.custom.yaml
    
    # finetune p6 models
    python train_aux.py --workers 8 --device 0 --batch-size 16 --data YoloDataSets/data.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6-custom.yaml --weights 'yolov7-w6_training.pt' --name yolov7-w6-custom --hyp data/hyp.scratch.custom.yaml        
    
  • The official pre-training weights for object detection are provided as follows.

    Model Test Size APtest AP50test AP75test batch 1 fps batch 32 average time
    YOLOv7 640 51.4% 69.7% 55.9% 161fps 2.8ms
    YOLOv7-X 640 53.1% 71.2% 57.8% 114fps 4.3ms
    YOLOv7-W6 1280 54.9% 72.6% 60.1% 84fps 7.6ms
    YOLOv7-E6 1280 56.0% 73.5% 61.2% 56fps 12.3ms
    YOLOv7-D6 1280 56.6% 74.0% 61.8% 44fps 15.0ms
    YOLOv7-E6E 1280 56.8% 74.4% 62.1% 36fps 18.7ms

(4)Training method of Ultralytics YOLO (YOLOv8/v9/v10/v11)

  • Step 1: Clone Ultralytics Repository

    Execute the following commands in the project root directory to pull the latest Ultralytics YOLO framework:

    git clone https://github.com/ultralytics/ultralytics.git Ultralytics
    cd Ultralytics

    Or download directly from the official website: Ultralytics YOLO

  • Step 2: Install Dependencies

    pip install ultralytics

    Or install full dependencies:

    pip install -r requirements.txt
  • Step 3: Prepare Dataset

    Place the converted dataset in the Ultralytics folder and add the .yaml configuration file named data.yaml to the YoloDataSets directory with the following content and format.

    path : YoloDataSets  # Note: Path relative to Ultralytics folder
    train: train.txt
    val: val.txt
    test: test.txt
    
    # number of classes
    nc: 2
    
    # class names
    names: ['dog','man']
    
  • Step 4: Start Training

    Method 1: Python API Training (Recommended)

    Create training script train_custom.py in the Ultralytics folder:

    from ultralytics import YOLO
    
    # Load model
    # YOLOv8: yolov8n.pt, yolov8s.pt, yolov8m.pt, yolov8l.pt, yolov8x.pt
    # YOLOv9: yolov9c.pt, yolov9e.pt
    # YOLOv10: yolov10n.pt, yolov10s.pt, yolov10m.pt, yolov10b.pt, yolov10l.pt, yolov10x.pt
    # YOLOv11: yolov11n.pt, yolov11s.pt, yolov11m.pt, yolov11l.pt, yolov11x.pt
    model = YOLO('yolov8n.pt')  # Can be replaced with other versions
    
    # Train the model
    results = model.train(
        data='YoloDataSets/data.yaml',
        epochs=100,
        imgsz=640,
        batch=16,
        device=0,
        project='runs/train',
        name='custom_dataset'
    )
    
    # Validate the model
    metrics = model.val()
    print(f"mAP50-95: {metrics.box.map:.4f}")

    Run training:

    python train_custom.py
    from ultralytics import YOLO
    
    # Load model
    # YOLOv8: yolov8n.pt, yolov8s.pt, yolov8m.pt, yolov8l.pt, yolov8x.pt
    # YOLOv9: yolov9c.pt, yolov9e.pt
    # YOLOv10: yolov10n.pt, yolov10s.pt, yolov10m.pt, yolov10b.pt, yolov10l.pt, yolov10x.pt
    # YOLOv11: yolov11n.pt, yolov11s.pt, yolov11m.pt, yolov11l.pt, yolov11x.pt
    model = YOLO('yolov8n.pt')  # Can be replaced with other versions
    
    # Train the model
    results = model.train(
        data='YoloDataSets/data.yaml',
        epochs=300,
        imgsz=640,
        batch=16,
        device=0,
        project='runs/train',
        name='ultralytics_custom'
    )

    Run training:

    python train_ultralytics.py
  • Method 2: Command Line Training

    # YOLOv8 series
    yolo task=detect mode=train model=yolov8n.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov8s.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov8m.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov8l.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov8x.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    
    # YOLOv9 series
    yolo task=detect mode=train model=yolov9c.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov9e.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    
    # YOLOv10 series
    yolo task=detect mode=train model=yolov10n.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov10s.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov10m.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov10b.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov10l.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov10x.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    
    # YOLOv11 series
    yolo task=detect mode=train model=yolov11n.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov11s.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov11m.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov11l.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
    yolo task=detect mode=train model=yolov11x.pt data=YoloDataSets/data.yaml batch=16 epochs=300 imgsz=640 device=0
  • Project Structure:

    After completing the above steps, your project directory structure should be as follows:

    YOLO-Datasets-And-Training-Methods/
    ├── assets/
    ├── yolov5/
    ├── yolov6/
    ├── yolov7/
    ├── Ultralytics/                    # Newly cloned Ultralytics repository
    │   ├── ultralytics/               # Core framework
    │   ├── YoloDataSets/              # Your dataset
    │   │   ├── data.yaml              # Dataset configuration
    │   │   ├── images/
    │   │   ├── labels/
    │   │   ├── train.txt
    │   │   ├── val.txt
    │   │   └── test.txt
    │   ├── train_custom.py            # Your training script
    │   └── runs/                      # Training results
    ├── DataSet.py
    └── README.md
    
  • Important Notes:

    • All YOLOv8/v9/v10/v11 versions use the same Ultralytics framework
    • Support direct fine-tuning with pre-trained weights
    • Different YOLO versions can be selected by modifying the model parameter
    • Training methods and parameter settings are completely consistent
    • It is recommended to perform all training operations in the Ultralytics folder
  • Additional Features:

    # Inference
    model = YOLO('runs/train/custom_dataset/weights/best.pt')
    results = model('path/to/image.jpg')
    
    # Validation
    metrics = model.val(data='YoloDataSets/data.yaml')
    
    # Export
    model.export(format='onnx')  # Support multiple formats
    | [**YOLOv8x**](https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x.pt) | 640                   | 53.9                 | 479.1                          | 3.53                                | 68.2               | 257.8             |

About

This project involves making custom datasets for the YOLO series and model training methods for YOLO.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published