Setup

Purpose of making these notes is for the YOLOv8 with Roboflow CV pipeline on Google Colab.

Step 1 - Install Dependencies

Installing dependencies is the first step for this. Two important dependencies to install:

  1. ultralytics
  2. roboflow

Code to install dependencies:

!pip install ultralytics
!pip install roboflow

NOTE: Before installing dependencies, make sure to have the runtime to be set using the GPU and not the CPU. Using the GPU will allow the model to utilize parallelism–this enables CUDA.

CUDA - Enabling CUDA will allow operations to be evenly distributed across the GPU’s cores, allowing for significant speedups.

Since machine learning models demand signficant computational resources, using CUDA will accelerate computations, which would result in reducing training times.

Example of when CUDA is used: Data Processing - CUDA is used to accelerate preprocessing tasks (image augmentation, feature extraction, etc.).

Step 2 - Set Up Roboflow

You must have Roboflow set up. To have a Roboflow CV model set up, you must have a dataset ready to be exported (you do this in the roboflow website). Code to set roboflow up:

from roboflow import Roboflow
rf = Roboflow( api_key="API KEY HERE" )
project = rf.workspace( "research-XXXXX" ).project( "PROJECT NAME HERE" )
version = project.version( 1 )
dataset = version.download( "yolov8" )

Step 3 - Load the Model

After Roboflow is set up, you must load the model.

from ultralytics import YOLO
model = YOLO( 'yolov8m.pt' )

NOTE: There are different variations of YOLOv8 Models (as you can see above, one of those models are called ‘yolov8m’.)

  1. YOLOv8n (Nano) - Is the fastest and has the fewest parameters. It is designed for scenarios where computational resources are limited.
  2. YOLOv8s (Small) - Is the second fastest and has few parameters. The model strikes the balance between speed and accuracy. It is designed for scenarios where moderate resources are available and both speed and accuracy are important.
  3. YOLOv8m (Moderate) - Has moderate parameters and moderate speed. It is designed for scenarios where there are balanced use cases.
  4. YOLOv8l (Large) - It is designed for scenarios such as detailed analysis and less resource-constrained settings.
  5. YOLOv8x - Model that has the most parameters and is the slowest of all. It is designed for scenarios where high accuracy is needed and is suitable for powerful systems.

Step 4 - Train Model

After loading the model, you must train the model.

model.train(data=f'{dataset.location}/data.yaml', epochs=100, imgsz=640)

Epochs - Is a single complete pass that goes through the entire training dataset.

NOTE: On Colab, dataset.location will use the directory where ‘data.yaml’ is currently in. ALSO, YOU MUST FIX PATHS FOR TEST, TRAIN, and VAL IMAGES IN ‘data.yaml’

Step 5 - Model Predictions

After training the model, you must have the model make a set of predictions.

!yolo task=detect mode=predict model=/content/runs/detect/train7/weights/best.pt conf=0.25 source={dataset.location}/test/images save=True

There are two types of confidence:

  1. Objectness Confidence - It is the probability that an object exists within the bounding box.
  2. Class Confidence - It is the probability distribution over all possible classes for the detected object.

conf=0.25 indicates the MINIMUM confidence score that a detected object mus thave to be considered a valid object.

NOTE: For the source, you must use the test images directory.

Step 6 - Show Results

After the model has made its predictions, show the results.

import glob
from IPython.display import Image, display

for imagePath in glob.glob( '/content/runs/detect/predict/*.jpg' ):
  display( Image(filename=imagePath, width=600) )
  print("\n")