Sitemap
PixelMindX

Convert your pixel-level data into meaningful insights. It’s all about computer Vision, image processing, and building advanced AI-powered solutions that truly see and understand the world.

Train YOLOv8 on Custom Data?

--

Press enter or click to view image in full size
Fig-1.1: YOLOv8 Initial Test

YOLOv8🔥has been released by Ultralytics today (10th, Jan 2023).

Ultralytics yolov8 has provided advancements in computer vision that have brought about a massive novelty in the way we perceive, analyze, and understand the visual world. It will allow for unprecedented possibilities in various fields.

Considerable improvements have been made in terms of speed, accuracy, and architecture. Ultralytics yolov8 implementation is done from scratch, and no major modules (i.e. model architecture) have been used from YOLOv5. It is faster in speed and more accurate than its previous version (YOLOv7), and it has achieved a new high in terms of Mean Average Precision (MAP) with a score of 53.7.

Press enter or click to view image in full size
Fig-1.2: YOLOv8 Mean Average Precision

In this article, we will focus on steps that are required to do training on custom data with ultralytics yolov8. You can follow mentioned steps below to train ultralytics yolov8 on your data. All mentioned steps have been tested properly and working fine on Windows and Linux operating systems.

  • Installation of Modules
  • Pretrained Object Detection
  • Train YOLOv8 on Custom Data
  • Inference with Custom Weights

Installation of Modules

YOLOv8 released a package named “ultralytics”, that you can install with the mentioned command below.

pip install ultralytics==8.0.0

or

# latestversion
pip install ultralytics

The above command will install all the packages that are required to use YOLOv8 for detection and training on your data.

Note: Make sure, that you have Python 3.7.0 or newer installed on your system.

Pretrained Object Detection

What will be your feeling? if you will need to run a single command that can efficiently do object detection and provide you results with better accuracy and fast speed.

You can run the mentioned command below in terminal/(command prompt) to do detection with pre-trained weights on your selected video/image using YOLOv8.

#for image
yolo task=detect mode=predict model=yolov8n.pt source="test.png"


#for video
yolo task=detect mode=predict model=yolov8n.pt source="test.mp4"

You will get the results in the current directory inside the “runs/detect/exp” folder if everything works well.

Press enter or click to view image in full size
Fig-1.3: Pretrained Object Detection (Image from Author)

Train YOLOv8 on Custom Data

The steps for training a YOLOv8 object detection model on custom data can be summarized as follows,

  • Collect data
  • Label data
  • Split data (train, test, and val)
  • Creation of config files
  • Start training

Step-1: Collect Data

Create a dataset for YOLOv8 custom training. if you have no data, You can use the dataset from the openimages database or 👇

⭐ Create your Data by reading the article “Extraction of a frame from videos”

YOLOv8 takes label data in the text (.txt) file and has the following format:

<object-class-id> <x> <y> <width> <height>

Step 2: Label Data

You can use the labelImg tool or Roboflow platform for data labeling, whatever suit your needs. If you want to understand the workflow of the labelImg tool, then👇

👉Don’t forget to check out an article Labelling data for object detection (Yolo).

Step-3: Split Data (Train, Test, and Val)

When you want to train a computer vision model on custom data, it’s important to split your data into a training set and a test set. The training set is used to teach the model how to make predictions, while the test set is used to evaluate the accuracy of the model. The (80–20%) split ratio is a common one, but the exact ratio can depend on the size of your dataset and the specific task you’re working on. For example, if you have a small dataset, you may want to use a higher percentage of it for training, while if you have a large dataset, you can afford to use a smaller percentage of it for training.

👉 For data splitting, you can take a look at split-folders that will split your data randomly into the train, test, and validation.

Folder structure:

├── yolov8
## └── train
####└── images (folder including all training images)
####└── labels (folder including all training labels)
## └── test
####└── images (folder including all testing images)
####└── labels (folder including all testing labels)
## └── valid
####└── images (folder including all testing images)
####└── labels (folder including all testing labels)
Fig-1.4: So far, you have done good work learner. Just a few steps away from YOLOv8 Magic (Author Appreciation)

Step-4: Creation of Config Files

Creating a custom configuration file can be a helpful way to organize and store all of the important parameters for your computer vision model.

Create a file having the filename “custom. yaml”, inside the current directory where you have opened a terminal/(command prompt). Paste the below code in that file. set the correct path of the dataset folder, change the classes and their names, and then save it.

path:  (dataset directory path)
train: (Complete path to dataset train folder)
test: (Complete path to dataset test folder)
valid: (Complete path to dataset valid folder)

#Classes
nc: 5# replace according to your number of classes

#classes names
#replace all class names list with your class names
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane']

Note: Make sure to set the correct path of training and testing directories, because the training process will depend on that file.

Step-5: Start Training

Once you’ve completed the preprocessing steps, such as data collection, data labeling, data splitting, and creating a custom configuration file, you can start training ultralytics yolov8 on custom data by using the mentioned command below in the terminal/(command prompt).

yolo task=detect mode=train model=yolov8n.pt data=custom.yaml epochs=3 imgsz=640

task = detect (It can be segmented or classified)

mode = train (It can be predict or val)

model = yolov8n.pt (It can yolov8s/yolov8l/yolov8x)

epochs = 3 (It can be any number)

imgsz = 640 (It can be 320, 416, etc., but make sure it needs to be a multiple of 32)

Press enter or click to view image in full size
Fig-1.5: YOLOv8 Training on Custom Data

YOLOv8 will not start training on custom data if any image is corrupt, If some label file is corrupted so there will be no issue in training because YOLOv8 will ignore those (image and label) files.

👉Wait for training to complete, and then do inference with newly created weights. Custom-trained weights will be saved in the folder path mentioned below.

[runs/train/exp/weights/best.pt]

Inference with Custom Weights

Once your model is trained, you can use it to make predictions on new data. Use the mentioned command below for detection with custom weights.

yolo task=detect mode=predict model="runs/train/exp/weights/best.pt" source="test.png"

or

yolo task=detect mode=predict model="runs/train/exp/weights/best.pt" source="test.mp4"

I have used person class data for training, the results with custom weights are shown below.

Fig-1.6: YOLOv8 Trained on Custom Data (Live Demo)

That is all regarding “Train YOLOv8 on Custom Data”. you can try this on your data.

Trending Articles

About Author

  • Muhammad Rizwan Munawar has 2.5 years of experience working in Computer Vision and Software development. Currently, he is working as a Computer Vision Engineer improving products and services for customers by using retail analytics, standing up big-data analytical tools, creating and maintaining models, and onboarding compelling new data sets. His services include Personal Protective Equipment (PPE) Detection, Staff Exclusion from Retail Stores, People Counting, Vehicle Speed Estimation, Dashboard Development for Computer Vision Products, Vehicle Counting, Automatic Number Plate Recognition, Undersea Objects Detection, People Segmentation, Person Pose Estimation, etc.
  • LinkedIn Profile

Please feel free to comment if you have any questions 🙂

--

--

PixelMindX
PixelMindX

Published in PixelMindX

Convert your pixel-level data into meaningful insights. It’s all about computer Vision, image processing, and building advanced AI-powered solutions that truly see and understand the world.

Muhammad Rizwan Munawar
Muhammad Rizwan Munawar

Written by Muhammad Rizwan Munawar

Passionate Computer Vision Engineer | Solving Real-World Challenges🔎| Python | Published Research | Open Source Contributor | GitHub 🌟 | Top Rated Upwork 💪

Responses (7)