matlab-deep-learning / Pretrained-YOLOv8-Network-For-Object-Detection

YOLO v8 training and inference in MATLAB for Object Detection with yolov8n, yolov8s, yolov8m, yolov8l, yolov8x, networks
https://in.mathworks.com/help/vision/ug/getting-started-with-object-detection-using-deep-learning.html
GNU Affero General Public License v3.0
30 stars 3 forks source link
computer-vision deep-learning image-processing matlab matlab-deep-learning object-detection pretrained-models yolo yolov8

Pretrained YOLO v8 Network For Object Detection

This repository provides multiple pretrained YOLO v8[1] object detection networks for MATLAB®, trained on the COCO 2017[2] dataset. These object detectors can detect 80 different object categories including person, car, traffic light, etc. Open in MATLAB Online

Creator: MathWorks Development

Includes Codegen support: ✔

Includes transfer learning script: ✔

Includes Simulink support script: ✔

License

The software and model weights are released under the GNU Affero General Public License v3.0. For alternative licensing, contact Ultralytics Licensing.

Requirements

Getting Started

Download or clone this repository to your machine and open it in MATLAB®.

Download the pretrained network

Use the code below to download the pretrained network.

% Load YOLO v8 model
det = yolov8ObjectDetector('yolov8s');

% Analyze loaded model
analyzeNetwork(det.Network);

modelName of the pretrained YOLO v8 deep learning model, specified as one of these:

Following is the description of various YOLO v8 models available in this repo:

Model Description
yolov8n Nano pretrained YOLO v8 model optimized for speed and efficiency.
yolov8s Small pretrained YOLO v8 model balances speed and accuracy, suitable for applications requiring real-time performance with good detection quality.
yolov8m Medium pretrained YOLO v8 model offers higher accuracy with moderate computational demands.
yolov8l Large pretrained YOLO v8 model prioritizes maximum detection accuracy for high-end systems, at the cost of computational intensity.
yolov8x Extra Large YOLOv8 model is the most accurate but requires significant computational resources, ideal for high-end systems prioritizing detection performance.

Detect Objects Using Pretrained YOLO v8

To perform object detection on an example image using the pretrained model, utilize the provided code below.

% Read test image.
I = imread(fullfile('data','inputTeam.jpg'));

% Load YOLO v8 small network.
det = yolov8ObjectDetector('yolov8s');

% Perform detection using pretrained model.
[bboxes, scores, labels] = detect(det, I);

% Visualize detection results.
annotations = string(labels) + ': ' + string(scores);
Iout = insertObjectAnnotation(I, 'rectangle', bboxes, annotations);
figure, imshow(Iout);

Results

Metrics and Evaluation

Size and Accuracy Metrics

Model Input image resolution Size (MB) mAP
yolov8n 640 x 640 10.7 37.3
yolov8s 640 x 640 37.2 44.9
yolov8m 640 x 640 85.4 50.2
yolov8l 640 x 640 143.3 52.9
yolov8x 640 x 640 222.7 53.9

mAP for models trained on the COCO dataset is computed as average over IoU of .5:.95.

Transfer learning

Training of a YOLO v8 object detector requires GPU drivers. Additionally, when training on Windows, the Visual Studio C++ compiler is required to facilitate the process.

Setup

Run installUltralytics.m to install the required Python files and set up the Python environment for training YOLOv8. This MATLAB script automates downloading and setting up a standalone Python environment tailored for YOLOv8 training. It determines the system architecture, downloads the appropriate Python build, extracts it, and configures MATLAB settings to use this Python interpreter. Finally, it installs the Ultralytics package and its dependencies using pip.

Obtain data

Use the code below to download the multiclass object detection dataset, or the subsequent steps can be followed to create a custom dataset. The dataset downloaded using the following command will already be in the required format, allowing the Train YOLO v8 object detector section to be proceeded with directly.

helper.downloadMultiClassData()

For more information about multiclass object detection dataset, see Multiclass Object Detection Using YOLO v2 Deep Learning example.

Configure custom data to train YOLO v8 object detector

Refer to getYOLOFormat.m to configure custom data to the YOLO format for training the YOLO v8 object detector. The following sections provides more details on the dataset format.

Dataset directory structure for custom dataset

When using custom dataset for YOLO v8 training, organize training and validation images and labels as shown in the datasets example directory below. It is mandatory to have both training and validation data to train YOLO v8 network.

datasetDir

Dataset format for custom dataset

Labels for training YOLO v8 must be in YOLO format, with each image having its own .txt file. If an image contains no objects, a .txt file is not needed. Each *.txt file should have one row per object in the format: class xCenter yCenter width height, where class numbers start from 0, following a zero-indexed system. Box coordinates should be in normalized xywh format, ranging from 0 to 1. If the coordinates are in pixels, divide xCenter and width by the image width, and yCenter and height by the image height as shown below.

datasetFormat

The label file corresponding to the above image contains 3 chairs (class 2).

labelFormat

Refer to the code below to convert bounding box [xCord, yCord, wdt, ht] to [xCenter, yCenter, wdt, ht] format.

boxes(:,1) = boxes(:,1) + (boxes(:,3)./2);
boxes(:,2) = boxes(:,2) + (boxes(:,4)./2);

Refer to the code below to normalize ground truth bounding box [xCenter, yCenter, wdt, ht] with respect to input image.

% Read input image.
[Irow, Icol, ~] = size(inpImage);

% Normalize xCenter and width of ground truth bounding box.
xCenter = xCenter./Icol;
wdt = wdt./Icol;

% Normalize yCenter and height of ground truth bounding box.
yCenter = yCenter./Irow;
ht = ht./Irow;

Train YOLO v8 object detector

Run below code to train YOLO v8 object detector on multiclass object detection dataset. For more information about evaluation, see Multiclass Object Detection Using YOLO v2 Deep Learning example.

yolov8Det = trainYOLOv8ObjectDetector('data.yaml','yolov8n.pt', ImageSize=[720 720 3], MaxEpochs=10);

Infer trained model

% Detect Multiple Indoor Objects
I = imread(fullfile('data','indoorTest.jpg'));
[bbox,score,label] = detect(yolov8Det,I);

annotatedImage = insertObjectAnnotation(I,"rectangle",bbox,label,LineWidth=4,FontSize=24);
figure
imshow(annotatedImage)

Deployment

Code generation enables you to generate code and deploy YOLO v8 on multiple embedded platforms. The list of supported platforms is shown below:

Target Support Notes
GPU Coder run gpuCodegenYOLOv8.m
MATLAB Coder run codegenYOLOv8.m

To deploy YOLO v8 to GPU Coder, run gpuCodegenYOLOv8.m. This script calls the yolov8Predict.m entry point function and generate CUDA code for it. It will run the generated MEX and give an output. For more information about codegen, see Deep Learning with GPU Coder.

Simulink

Simulink is a block diagram environment used to design systems with multidomain models, simulate before moving to hardware, and deploy without writing code. For more information about simulink, see Get Started with Simulink

% Read test image.
I = imread(fullfile('data','inputTeam.jpg'));

% Open Simulink model.
open('yolov8SimulinkSupport.slx')

To run the simulation, click Run from the Simulation tab.

The output will be logged to the workspace variable out from the Simulink model.

Network Overview

YOLO v8 is one of the best performing object detectors and is considered as an improvement to the existing YOLO variants such as YOLO v5, and YOLOX.

Following are the key features of the YOLO v8 object detector compared to its predecessors:

References

[1] https://github.com/ultralytics/ultralytics

[2] Lin, T., et al. "Microsoft COCO: Common objects in context. arXiv 2014." arXiv preprint arXiv:1405.0312 (2014).

Copyright 2024 The MathWorks, Inc.