Tutorial: Train Yolo v8 with custom dataset

This tutorial is about learning how to train YOLO v8 with a custom dataset of Mask-Dataset.

This Tutorial also works for YOLOv5

Step 0. Install YOLOv8 in local drive

Follow Tutorial: Installation of Yolov8

Step 1. Create Project Folder

  1. We will create the working space directory as

\DLIP\YOLOv8\

  1. Then, create the sub-folder /datasets in the same parent of /yolov8 folder

Step 2. Prepare Custom Dataset

Download Dataset and Label

We will use the Labeled Mask YOLO to detect people wearing mask.

This annotation file has 4 lines being each one referring to one specific face in the image. Let’s check the first line:

The first integer number (0) is the object class id. For this dataset, the class id 0 refers to the class “using mask” and the class id 1 refers to the “without mask” class. The following float numbers are the xywh bounding box coordinates. As one can see, these coordinates are normalized to [0, 1].

  1. Download the dataset : Labeled Mask YOLO.

  2. Under the directory /datasets , create a new folder for the MASK dataset. Then, copy the downloaded dataset under this folder. Example: /datasets/dataset_mask/archive/obj/

img

The dataset is indeed a bunch of images and respective annotation files:

Visualize Train Dataset image with Boundary Box and Label

  1. Under the working space ( YOLOv8/ ) , create the following python file ( visualizeLabel.py) to view images and labels.

  2. Download code here

You will see this result

Step 3. Split Dataset

The YOLO training process will use the training subset to actually learn how to detect objects. The validation dataset is used to check the model performance during the training.

We need to split this data into two groups for training model: training and validation.

  • About 90% of the images will be copied to the folder /training/.

  • The remaining images (10% of the full data) will be saved in the folder /validation/.

For the inference dataset, you can use any images with people wearing mask.

Under the working directory create the following python file split_data.py.

This code will save image files under the folder /images/ folder and label data under the folder /labels/

  • Under each folders, /training and /validation datasets will be splitted.

Run the following script and check your folders

Step 4. Training configuration file

The next step is creating a text file called maskdataset.yaml inside the yolov8 directory with the following content.

Step 5. Train Model

change batch number and epochs number for better training

Create the following python file ( Yolov8_train.py) to train model.

Finally, in the end, we have the following output:

Now, confirm that you have a yolov8/runs/detect/train/weights/best.pt file:

Depending on the number of runs, it can be under /train#/weights/best.pt, where #:number of train

For my PC, it was train3

Also, check the output of runs/detect/train#/results.png which demonstrates the model performance indicators during the training:

Step 6. Test the model (Inference)

Now we have our model trained with the Labeled Mask dataset, it is time to get some predictions. This can be easily done using an out-of-the-box YOLOv8 script specially designed for this:

Download a test image here and copy the file under the folder of yolov8/datasets/dataset_mask/images/testing

Create the following python file ( Yolov8_test.py) to test model.

Your result image will be saved under runs/detect/predict#/

NEXT

Test trained YOLO with webcam

Last updated

Was this helpful?