YOLOv8 – How to use it? Best Tutorial

Ultralytics has just released its latest version of YOLO: YOLOv8. In this article, we see in detail how to use it!

YOLOv8 is the first version of YOLO released in 2023, on January 10th.

YOLO (You Only Look Once) is one of the most popular object detection algorithms in the field of Deep Learning.

The first version was released in 2016!

The main idea that makes YOLO so attractive is its ability to detect objects in an image in a single pass.

At the time, this was a major breakthrough because most algorithms had to be run repeatedly on different parts of the image.

Why ?

Because these algorithms could only detect one object at a time.

A considerable waste of time!

It was only a matter of time before YOLO’s capabilities caught on with professionals.

And that’s why we get a new version of YOLO several times a year.

In 2022, we’ve already seen on Inside Machine Learning :

Now, I invite you to upgrade to version 8.

But first… Is YOLOv8 worth it?

I let you judge for yourself with the graph of its performance compared to the other versions of the model:

And now, let’s see how to use it!

How to use YOLOv8?

First of all, you will need the ultralytics library.

To install it from python use this command:

!pip install ultralytics

Remove the ! if you use a terminal.

Use on Python

To use YOLOv8 and display the result, you will need the following libraries:

from ultralytics import YOLO
import numpy as np
from PIL import Image
import requests
from io import BytesIO
import cv2

And if you are on Google Colab also import this one:

from google.colab.patches import cv2_imshow

Now we can load a pre-trained version of YOLOv8 (by default ultralytics gives us the most recent one):

model = YOLO("yolov8n.pt")

Then, we can load an image from the internet and transform it into a numpy array:

response = requests.get("https://images.unsplash.com/photo-1600880292203-757bb62b4baf?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80")
image = Image.open(BytesIO(response.content))
image = np.asarray(image)

Then, we run the prediction on our image (this should be fast, even without GPU):

results = model.predict(image)

We’ve run our prediction!

The results variable contains the list of bounding boxes enclosing the detected objects.

You can see them with print(results[0].boxes.boxes). They are in the format [x1, y1, x2, y2, score, label].

Now we’ll display our Bounding Boxes.

For that we use our function (short and simple) which allows us to display the bounding boxes with the label and the score. I have detailed it in this article.

Once you have copied the function, you can use it like this:

THE PANE METHOD FOR DEEP LEARNING!

Get your 7 DAYS FREE TRAINING to learn how to create your first ARTIFICIAL INTELLIGENCE!

For the next 7 days I will show you how to use Neural Networks.

You will learn what Deep Learning is with concrete examples that will stick in your head.

BEWARE, this email series is not for everyone. If you are the kind of person who likes theoretical and academic courses, you can skip it.

But if you want to learn the PANE method to do Deep Learning, click here :

plot_bboxes(image, results[0].boxes.boxes, score=False)
YOLOv8 predictions

We get a lot of objects.

Some are relevant.

Others are not.

Like the chair at the bottom left.

In fact, the model did not detect all these objects with the same confidence score.

We can filter these objects according to this score.

For example, we can display only the bounding boxes with a confidence score higher than 85%.

Thus, all the objects detected with a lower score will not be displayed.

To filter according to the confidence score, we indicate conf=0.85 :

plot_bboxes(image, results[0].boxes.boxes, score=False, conf=0.85)
YOLOv8 prédictions – seuil de confiance 0.85%

YOLOv8 detects both people with a score above 85%, not bad! ☄️

Use on Terminal

Ultralytics also allows you to use YOLOv8 without running Python, directly in a command terminal.

In this case, you have several options:

1. YOLOv8 on a single image

First of all you can use YOLOv8 on a single image, as seen previously in Python.

For this you only need to use the following command.

yolo task=detect mode=predict model=yolov8n.pt conf=0.25 source='/content/photo.jpeg'
  • conf – indicates the confidence threshold for accepting a Bounding Box (here the score must be at least 25%)
  • source – indicate the URL, or the path of your image if you have it locally

The result is in /runs/detect/predict/.

2. YOLOv8 on an image folder

Then, you can also use YOLOv8 directly on a folder containing images.

For that, you only have to indicate the path of your folder containing the images in source.

yolo task=detect mode=predict model=yolov8n.pt conf=0.25 source='/img_folder/'

If you’ve run the previous commande, the result will be in /runs/detect/predict2/.

Re-train YOLOv8

Finally you can also re-train YOLOv8.

To do this, load the model yolov8n.yaml. This is an untrained version of the model :

from ultralytics import YOLO

model = YOLO("yolov8n.yaml")

Then you can train your model on the COCO dataset like this:

results = model.train(data="coco128.yaml", epochs=3)

Evaluate it on your dataset:

results = model.val(data=your_data)

And run predictions on new images (as seen before):

results = model.predict("https://images.unsplash.com/photo-1600880292203-757bb62b4baf?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2070&q=80")

Lastly, you can also save your new model in ONNX format:

success = model.export(format="onnx")

You now have everything you need to use YOLOv8!

Good luck with your Computer Vision project.

And see you soon on Inside Machine Learning 😉

source :

THE PANE METHOD FOR DEEP LEARNING!

Get your 7 DAYS FREE TRAINING to learn how to create your first ARTIFICIAL INTELLIGENCE!

For the next 7 days I will show you how to use Neural Networks.

You will learn what Deep Learning is with concrete examples that will stick in your head.

BEWARE, this email series is not for everyone. If you are the kind of person who likes theoretical and academic courses, you can skip it.

But if you want to learn the PANE method to do Deep Learning, click here :

Tom Keldenich
Tom Keldenich

Data Engineer & passionate about Artificial Intelligence !

Founder of the website Inside Machine Learning

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter your email to receive for free

The PANE method for Deep Learning

* indicates required

 

You will receive one email per day for 7 days – then you will receive my newsletter.
Your information will never be given to third parties.

You can unsubscribe in 1 click from any of my emails.

Entre ton email pour recevoir gratuitement
la méthode PARÉ pour faire du Deep Learning


Tu recevras un email par jour pendant 7 jours - puis tu recevras ma newsletter.
Tes informations ne seront jamais cédées à des tiers.

Tu peux te désinscrire en 1 clic depuis n'importe lequel de mes emails.