Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (2023)

In my last post, I guided you how to train YOLOv3. In this post, we will explore how to train YOLOv8 on custom dataset in Windows GPU. We will cover all required steps like preparing the dataset, configuring and training the model, evaluating performance of the model. Finally use custom trained model to inference on new images.

Machine Learning with Turi Create t...

Machine Learning with Turi Create to Develop Custom ML Models

Must Read: Recommend you to read below tutorials before this.

  • YOLO object detection using deep learning OpenCV | Real-time
  • Train YOLOv3 Custom object detection model in Windows GPU
  • Object Detection and More using YOLOv8

Configure YOLOv8 for Windows GPU

One thing I also liked about this YOLO v8 is that you don’t have to clone the repository or do any manual things. In my YOLO v3 setup post you can see that we used to clone the repository, set up requirements, and configure the model file manually. But with YOLO v8, you don’t have to do any of these hard manual things. You only need to install Ultra Analytics, That’s it.

There are two ways to use UltraAnalytics YOLO v8:

  1. Using CLI or command line
  2. Python script

So let’s set up UltraAnalytics YOLO v8 to train a custom model for our own dataset in Windows 10 (You can follow same steps for Google collab also). I will break entire configuration into some steps:

Step1: Create Virtual Environment

You must have Python 3.7 or above to use UltraAnalytics YOLO v8. It will be a good idea to create a fresh virtual environment with Python 3.7. If you are using Anaconda, you can create an isolated virtual environment using below command.

conda create -n ENV_NAME python=3.7activate ENV_NAME

Step2: Install Ultalytics

This is the fun part. You no need to do anything manually. You just need to run the below command and YOLO v8 will be installed in your windows system.

pip install ultralytics

Step3: Setup Jupyter notebook

I think we all love Jupyter Notebook to write Python scripts. To use Jupyter Notebook inside our virtual environment, run below three commands inside your Virtual env.

conda install jupyterconda install nb_condaconda install ipykernel

Step4: Validate Installation

Now we need to check whether we installed YOLO v8 correctly or not. To do that open Jupyter Notebook (inside virtual environment) and run below Python script.

from IPython import displaydisplay.clear_output()import ultralyticsultralytics.checks()

Output

Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (1)

As you can see we have successfully installed Ultralytics YOLOv8, but with CPU. Let’s now try to install YOLOv8 with GPU.

Step5: YOLOv8 GPU Configuration

In the above output, you can see that YOLOv8 is configured with CPU. This is because your torch is CPU version. You can check your torch backend by running below Python code.

import torchtorch.cuda.is_available()
Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (2)

The output is False because torch is not using CUDA or GPU as its backend. Its using CPU.

Also Read: Color detection using OpenCV and Python

To use your GPU for training custom YOLOv8 for your own data, you need to install torch with GPU corresponding to your CUDA version.

So let’s check our CUDA version by running below Python script in Jupyter Notebook.

!nvidia-smi
Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (3)

As you can see I am using CUDA version: 11.7. Now go to this link and select your system configuration to get the command to install torch with GPU.

Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (4)

Since I am using CUDA 11.7 and Windows system, I have selected them along with the Stable version. I find difficulty using pip command. So I will recommend you to use conda command.

conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

Along with that we also need to install conda version of cuda toolkit inside our virtual environment. Below command is to install that.

conda install cudatoolkit

Now let’s check our torch backend again.

import torchtorch.cuda.is_available()
Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (5)

Okay, now we are getting output as True. That means torch is now using GPU as its backend. Let’s now check YOLOv8 is also configured with GPU or not again.

from IPython import displaydisplay.clear_output()import ultralyticsultralytics.checks()
Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (6)

Yes! YOLOv8 is now configured with GPU as its backend. You can see it is also showing my NVIDIA graphics card model number which is GeForce GTX 1050 Ti (4 GB).

Train YOLOv8 on Custom dataset

So we successfully installed YOLOv8 from Ultralytics library with GPU on our Windows computer. Now let’s try to train yolov8 on our own custom dataset.

I will break the entire process of training a custom YOLOv8 model into some steps. Note that I am using Windows 10 with GPU but the steps will be same for Linux or Google Colab.

Okay now let’s go into each step to train our custom yolov8 model for our own dataset.

Step1: Data Preparation

For data preparation, you need to follow below steps:

  • Collect images (either download or capture)
  • Select proper set of images
  • Annotate images

In this post, I will not guide you to prepare data to train a YOLO model. The training data format is same as YOLOv3. You can read this detailed post to learn more about data preparation to train custom YOLOv8 or v3 models.

I am going to use Goats detection dataset to train our custom YOLOv8 model. This dataset contains 1.5k training images (with annotation) of goats. You can find different formats of that data set. Since we need to train a YOLOv8 model, I am going to download that version.

Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (7)

Click on YOLOv8 button > select download zip to computer option in the pop-up window > Continue. It will download the dataset as zip file. Unzip that file.

This dataset has three folders: train, test, and valid. Each folder contains images (contains original images) and labels (contains coordinate information for the corresponding image) folder.

Also Read: Social distancing detector using OpenCV and Python

Along with that, you can find another important file named data.yaml, which contains class names and each folder location. So our working directory should look like below:

Woking directory ├── Downloaded dataset folder ├── train | ├── images | └── labels ├── test | ├── images | └── labels ├── valid | ├── images | └── labels └── data.yaml

If you find difficulty understanding this dataset, please read this tutorial where I explain data format to train YOLO model.

Step2: Rename Folder

So you unzipped the folder and placed it into your working directory. Now you need to rename the folder to “datasets“. With a little bit of research, I did not find the reason behind it, but we need to name the dataset folder like that. If you find any solution, please let me know in the comment section.

Now copy the data.yaml file from datasets (newly renamed) folder to your root directory.

So now our folder structure should look like below:

Woking directory ├── datasets | ├── train | | ├── images | | └── labels | ├── test | | ├── images | | └── labels | ├── valid | | ├── images | | └── labels | └── data.yaml └── data.yaml

That’s it we are all set to train the YOLOv8 model on our custom dataset.

Step3: Train YOLOv8 model for custom data

There are mainly two ways to train custom YOLOv8 model:

  1. Using CLI (or command line)
  2. Using Python Script
1. CLI Method

Using CLI method, you just need to run below command in the command line.

yolo task=detect mode=train model=yolov8n.pt data= data.yaml epochs=30 imgsz=832 plots=True device=0

In the above command:

  • task: Here you need to mention the task.
    • detect – for object detection
    • segment – for image segmentation
    • classify – for image classification
  • model: Here you can select any one of the 5 available pre-trained YOLOv8 models for custom object detection (you can also train from scratch with those models). List of models can be found here.
  • imgsz: This is the image dimension of your training images.
  • device = 0 is to utilize your GPU while training the model. You can skip this if you want to train using CPU
  • plot Ture is to save model statistics in your working directiory

In my system (4 GB NVIDIA 1050 Ti graphics) it took around 1 hour to complete training for 30 epochs.

After the successful completion of training yolov8 on your custom dataset, a folder called “runs” will be created inside your working directory.

You can find your final custom YOLOv8 model inside runs > detect > train> weights. So now our folder structure should looks like bellow:

Woking directory├── runs| └── detect| └── train| ├── Some statistical images| └── weights| ├── best.pt | └── last.pt| ├── datasets | ├── train | | ├── images | | └── labels | ├── test | | ├── images | | └── labels | ├── valid | | ├── images | | └── labels | └── data.yaml└── data.yaml
2. Python Method

Along with Command ine, you can train custom YOLO v8 model through Python. Below Python code is to train yolov8 on custom dataset:

from ultralytics import YOLO# Load a modelmodel = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)# Train the modelmodel.train(data='data.yaml', epochs=2, imgsz=832, device=0)

Note: Theoretically you can train using above Python script but when I was trying to do this using Jupyter. The Notebook was getting died after some time. But if you do it using CLI (command line) there is no issue.

Also Read: Download high resolution satellite imagery free online

Step4: Inference Trained YOLOv8 model

So now we successfully trained YOLOv8 model for our custom dataset. Now let’s see how our model is performing for images. Below Python code is to detect Goats from an input image.

from ultralytics import YOLO# Load our custom goat modelmodel = YOLO("runs/detect/train/weights/best.pt")# Use the model to detect object - goatmodel.predict(source="goat_image.jpeg", save=True, show=True)
Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (8)

That looks good from a model trained on only 30 epochs. It is successfully detecting goat from the input image.

Step5: Real-time object detection using YOLOv8

Now let’s see how we can detect object real-time using our custom-trained YOLOv8 model. There are two ways: 1. Using Webcam, 2. Using external video. Let’s see both of them.

Webcam Code

Below Python code is for real time yolov8 object detection using Webcam.

# For webcamfrom ultralytics import YOLO# Load custom trained YOLOv8 modelmodel = YOLO("runs/detect/train/weights/best.pt")# Use the model to detect objectmodel.predict(source="0", show=True)

In this code source, 0 means webcam.

Video Code

To do the same thing for external video, you just need to mention the video path instead of 0 in the source.

# For videofrom ultralytics import YOLO# Load custom trained YOLOv8 modelmodel = YOLO("runs/detect/train/weights/best.pt")# Use the model to detect objectmodel.predict(source="goat_video.mp4", show=True)

End Note

In this tutorial, I showed how you can train your own YOLOv8 model on custom dataset. For this tutorial I used windows 10 system with GPU. But you can follow same steps for other systems like Linux or google collab etc using CPU also.

I have not discussed much about data preparation for YOLO model in this post. If you want to understand more about data preparation, read this detailed tutorial.

That’s it for this tutorial. If you have any questions or suggestions regarding this post, please let me know in the comment section below.

Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi (9)

Anindya

Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.

Share this

FAQs

Train YOLOv8 on Custom dataset in Windows GPU - ThinkInfi? ›

Table 1 shows the performance (mAP) and speed (FPS) benchmarks of five YOLOv5 variants on the MS COCO validation dataset at 640×640 image resolution on Volta 100 GPU. All five models were trained on the MS COCO training dataset.

How to train YOLOv8 classification on a custom dataset? ›

Let me show you how!
  1. Step 1: Creating project. Before you start, you need to create a Roboflow account. ...
  2. Step 2: Uploading images. Next, add the data to your newly created project. ...
  3. Step 3: Labeling. ...
  4. Step 4: Generate new dataset version. ...
  5. Step 5: Exporting dataset.

What dataset was YOLOv5 trained on? ›

Table 1 shows the performance (mAP) and speed (FPS) benchmarks of five YOLOv5 variants on the MS COCO validation dataset at 640×640 image resolution on Volta 100 GPU. All five models were trained on the MS COCO training dataset.

How do you train a dataset in Roboflow? ›

Usage. Choose image preprocessing and image augmentation settings, then generate a version of your dataset. For Object Detection projects, there is a "Fast" model and an "Accurate" model available. For all other project types, there is only one training checkpoint option available.

How many epochs does it take to train Yolo? ›

Start with 300 epochs. If this overfits early then you can reduce epochs. If overfitting does not occur after 300 epochs, train longer, i.e. 600, 1200 etc epochs.

Does YOLOv5 work with TensorFlow? ›

YoloV5 implemented by TensorFlow2 , with support for training, evaluation and inference.

Why YOLOv5 is better than other models? ›

YOLOv5 is fast, easy to use, and capable of achieving state-of-the-art results for object detection tasks. It is also more accurate and easier to train than its predecessors, making it a popular choice for many developers.

Is YOLOv5 a Pretrained model? ›

Load From PyTorch Hub

This example loads a pretrained YOLOv5s model and passes an image for inference. YOLOv5 accepts URL, Filename, PIL, OpenCV, Numpy and PyTorch inputs, and returns detections in torch, pandas, and JSON output formats. See the YOLOv5 PyTorch Hub Tutorial for details.

How can I train my own dataset? ›

Preparing Your Dataset for Machine Learning: 10 Basic Techniques That Make Your Data Better
  1. Articulate the problem early.
  2. Establish data collection mechanisms. ...
  3. Check your data quality.
  4. Format data to make it consistent.
  5. Reduce data.
  6. Complete data cleaning.
  7. Create new features out of existing ones.
Mar 19, 2021

How do I train a custom dataset in TensorFlow? ›

  1. On this page.
  2. TensorFlow programming.
  3. Penguin classification problem.
  4. Setup.
  5. Import the dataset. Preview the data. Download the preprocessed dataset.
  6. Build a simple linear model. Why model? Select the model. ...
  7. Train the model. Define the loss and gradients function. ...
  8. Evaluate the model's effectiveness. Set up the test set.
Dec 15, 2022

How do you split a custom dataset into train and test? ›

Using Train Test Split In Python
  1. Load the Data Set.
  2. Arrange Data into Features and Target.
  3. Split Data Into Training and Testing Sets.
  4. Import the Model You Want to Use.
  5. Make An Instance of the Model.
  6. Train the Model on the Data.
  7. Predict Labels of Unseen Test Data.
  8. Parameters vs Hyperparameters.
Jul 28, 2022

What is the batch size for YOLOv8 training? ›

Defaults to 16. The optimal batch size.

Can YOLOv5 be used for classification? ›

YOLOv5 for Classification

YOLOv5 was released by Glenn Jocher on June 9, 2020 for object detection. Recently, YOLOv5 added support for classification (August 2022) and instance segmentation (September 2022).

What is the batch size of YOLOv8? ›

In YOLOv8, the default batch size is set to 64. Input size: The input size is the size of the input image that is fed into the neural network. In YOLOv8, the default input size is 608x608.

How to train an object detection classifier using TensorFlow? ›

There are six steps to training an object detection model:
  1. Choose an object detection model architecture.
  2. Load the dataset.
  3. Train the TensorFlow model with the training data.
  4. Evaluate the model with the test data.
  5. Export as a TensorFlow Lite model.
  6. Evaluate the TensorFlow Lite model.
  7. Install the EdgeTPU Compiler.

How do you train dataset for object detection? ›

Procedure
  1. From the cluster management console, select Workload > Spark > Deep Learning.
  2. Select the Datasets tab.
  3. Click New.
  4. Create a dataset from Images for Object Detection.
  5. Provide a dataset name.
  6. Specify a Spark instance group.
  7. Provide a training folder. ...
  8. Provide the percentage of training images for validation.

Is 100 epochs enough? ›

Observing the enormous discrepancy between epoch 99 and epoch 100 reveals that the model is already overfitting. As a general rule, the optimal number of epochs is between 1 and 10 and should be achieved when the accuracy in deep learning stops improving. 100 seems excessive already.

Is 3 epochs enough? ›

The right number of epochs depends on the inherent perplexity (or complexity) of your dataset. A good rule of thumb is to start with a value that is 3 times the number of columns in your data. If you find that the model is still improving after all epochs complete, try again with a higher value.

Which Yolo algorithm is fastest? ›

YOLOv7 is the fastest and most accurate real-time object detection model for computer vision tasks.

Will TensorFlow automatically use GPU? ›

By default, if a GPU is available, TensorFlow will use it for all operations. You can control which GPU TensorFlow will use for a given operation, or instruct TensorFlow to use a CPU, even if a GPU is available.

Can TensorFlow run on any GPU? ›

TensorFlow supports running computations on a variety of types of devices, including CPU and GPU.

What GPU is TensorFlow compatible with? ›

The following GPU-enabled devices are supported:
  • NVIDIA® GPU card with CUDA® architectures 3.5, 5.0, 6.0, 7.0, 7.5, 8.0 and higher. ...
  • For GPUs with unsupported CUDA® architectures, or to avoid JIT compilation from PTX, or to use different versions of the NVIDIA® libraries, see the Linux build from source guide.
Apr 29, 2023

What is the best GPU for Yolo? ›

The fastest YOLO models on each GPU – RTX, GTX, and TESLA. From the above graph, we can observe the following: On the RTX 4090 GPU and TESLA P100, YOLOv5 Nano emerges as the fastest.

Is YOLOv5 better than YOLOv8? ›

YOLOv8 has outperformed YOLOv5 in terms of accuracy. The YOLOv8s model has achieved an average precision of 51.4% on the COCO dataset, while the YOLOv8m model has achieved an average precision of 54.2% on the same dataset.

Is YOLOv5 better than SSD? ›

... In terms of recognition speed, YOLOv5 is quicker than SSD with 30 frames per second, whereas SSD only has 3.49 frames per second. YOLOv5 is, in our opinion, more suited for TSR in a real-time traffic situation [6] .

What is the best batch size for YOLOv5? ›

To achieve a robust YOLOv5 model, it is recommended to train with over 1500 images per class, and more then 10,000 instances per class.

Can YOLOv5 detect small objects? ›

Considering that the SF-YOLOv5 mainly detects small objects, while the original WIDER FACE dataset contains not only small targets, but also large targets, which will affect the experimental results; therefore, we first screened according to the number and size of face images in a single image in the original dataset.

Does YOLOv5 use neural networks? ›

It uses a single neural network to process an entire image.

How to train YOLOv5 on custom objects? ›

Training Custom YOLOv5 Detector
  1. img: define input image size.
  2. batch: determine batch size.
  3. epochs: define the number of training epochs. ...
  4. data: set the path to our yaml file.
  5. cfg: specify our model configuration.
  6. weights: specify a custom path to weights. ...
  7. name: result names.
  8. nosave: only save the final checkpoint.

Can you train a model with multiple datasets? ›

Multiple Dataset feature allows you to train your model on multiple datasets which helps fine-tune your model to offer accurate recommendations and improve the end-user experience over time.

Which algorithm is used to train the dataset? ›

The KNN algorithm is very simple and very effective. The model representation for KNN is the entire training dataset.

How do I run a custom dataset on YOLOv5? ›

Train On Custom Data
  1. Create Dataset. YOLOv5 models must be trained on labelled data in order to learn classes of objects in that data. ...
  2. Select a Model. Select a pretrained model to start training from. ...
  3. Train. ...
  4. Visualize.
Apr 13, 2023

How do I use my own image dataset for TensorFlow? ›

  1. On this page.
  2. Setup. Download the flowers dataset.
  3. Load data using a Keras utility. Create a dataset. Visualize the data. Standardize the data. ...
  4. Using tf.data for finer control. Configure dataset for performance. Visualize the data. Continue training the model.
  5. Using TensorFlow Datasets.
  6. Next steps.

How to install TensorFlow Object Detection API on Windows? ›

  1. Install your Editor of choice alongside with GIT. ...
  2. Get your Anaconda installation up and running. ...
  3. Install NVIDIA's drivers for your GPU. ...
  4. Install the C++ Build Tools and Microsoft's © Visual Studio © ...
  5. Install Protoc. ...
  6. Install TensorFlow and its pre-trained models on your machine. ...
  7. Run a Demo Detector Model.

What is X_train and Y_train? ›

x_train : The training part of the first sequence ( x ) x_test : The test part of the first sequence ( x ) y_train : The training part of the second sequence ( y ) y_test : The test part of the second sequence ( y )

How to use two different datasets as train and test sets in Python? ›

1 Answer
  1. reg = LinearRegression() Use the test set to predict the output after training.
  2. # Load the data. Follow the below steps to accomplish your task: ...
  3. train = pd.read_csv('train.csv')
  4. test = pd.read_csv('test.csv') # Fit (train) model.
  5. reg.fit(X_train, y_train) # Predict.
  6. accuracy = reg.socre(X_test, y_test)

Do we always need to split your dataset into train and test? ›

Data should be split so that data sets can have a high amount of training data. For example, data might be split at an 80-20 or a 70-30 ratio of training vs. testing data. The exact ratio depends on the data, but a 70-20-10 ratio for training, dev and test splits is optimal for small data sets.

How to train yolov3 on your own dataset? ›

Make the following changes in the file:
  1. Change line batch to batch=64 .
  2. Change line subdivisions to subdivisions=8 .
  3. Change line max_batches to the value (num_classes*2000). ...
  4. Change line steps to 80% and 90% of max_batches. ...
  5. Change line classes in each Yolo layer to the number of classes/objects.
Jul 10, 2019

How to train yolov4 on a custom dataset in darknet? ›

  1. Create 'yolov4' and 'training' folders in your drive. Create a folder named yolov4 in your google drive. Next, create another folder named training inside the yolov4 folder. ...
  2. Mount your drive and navigate to the “yolov4” folder in your drive. Mount drive. %cd .. ...
  3. Clone Darknet git repository.
Feb 9, 2021

How to use YOLOv8 in Python? ›

Python Usage
  1. Train. From pretrained(recommended) From scratch Resume. from ultralytics import YOLO model = YOLO('yolov8n.pt') # pass any model type model. ...
  2. Val. Val after training Val independently. ...
  3. Export. Export to ONNX Export to TensorRT.
Mar 12, 2023

How do you train a custom model for object detection? ›

There are six steps to training an object detection model:
  1. Choose an object detection model architecture.
  2. Load the dataset.
  3. Train the TensorFlow model with the training data.
  4. Evaluate the model with the test data.
  5. Export as a TensorFlow Lite model.
  6. Evaluate the TensorFlow Lite model.
  7. Install the EdgeTPU Compiler.

How many images do you need to train Yolo? ›

To achieve a robust YOLOv5 model, it is recommended to train with over 1500 images per class, and more then 10,000 instances per class.

How to install Darknet Yolo in Windows? ›

(A) Installing YOLOv4 on Windows
  1. STEP 1) Download Darknet. ...
  2. STEP 2) Install MSVC (Microsoft Visual Studio) ...
  3. STEP 3) Install CMake. ...
  4. STEP 4) Install CUDA, cuDNN, and OpenCV on your system. ...
  5. STEP 5) Run CMake GUI. ...
  6. STEP 6) Run MSVC to build darknet. ...
  7. STEP 7) Setup darknet directory.

How do you train a Darknet on custom dataset? ›

Train Darknet on Custom Dataset
  1. Step 1: Get the Darknet Repo locally and set up the data folders.
  2. Step 2: Make Darknet.
  3. Step 3: Setup the darknet/data folder.
  4. Step 4: Setup the cfg folder. ...
  5. Step 5: Download the weights files.
  6. Step 6: Modify the config files for mAP improvement.
  7. Step 6: Run Darknet.
Apr 27, 2021

Which Darknet is used in Yolo? ›

Darknet-53 is a convolutional neural network that acts as a backbone for the YOLOv3 object detection approach. The improvements upon its predecessor Darknet-19 include the use of residual connections, as well as more layers.

Is YOLOv8 faster than YOLOv5? ›

Both YOLOv8 and YOLOv5 are fast object detection models, capable of processing images in real-time. However, YOLOv8 is faster than YOLOv5, making it a better choice for applications that require real-time object detection.

What is the best model of YOLOv8? ›

YOLOv8 Nano is the fastest and smallest, while YOLOv8 Extra Large (YOLOv8x) is the most accurate yet the slowest among them. YOLOv8 comes bundled with the following pre-trained models: Object Detection checkpoints trained on the COCO detection dataset with an image resolution of 640.

How to install YOLOv8 in python? ›

Install
  1. Install YOLOv8 via the ultralytics pip package for the latest stable release or by cloning the https://github.com/ultralytics/ultralytics repository for the most up-to-date version.
  2. See the ultralytics requirements.txt file for a list of dependencies.
Dec 5, 2022

References

Top Articles
Latest Posts
Article information

Author: Pres. Carey Rath

Last Updated: 20/10/2023

Views: 6396

Rating: 4 / 5 (41 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Pres. Carey Rath

Birthday: 1997-03-06

Address: 14955 Ledner Trail, East Rodrickfort, NE 85127-8369

Phone: +18682428114917

Job: National Technology Representative

Hobby: Sand art, Drama, Web surfing, Cycling, Brazilian jiu-jitsu, Leather crafting, Creative writing

Introduction: My name is Pres. Carey Rath, I am a faithful, funny, vast, joyous, lively, brave, glamorous person who loves writing and wants to share my knowledge and understanding with you.