Welcome to the YOLOv8Lab repository, a dedicated platform for setting up and using YOLOv8 for object detection tasks in educational settings.
This repository and code is based on the YOLOv8 project by: Jocher, G., Chaurasia, A., & Qiu, J. (2023). Ultralytics YOLO (Version 8.0.0)
[Computer software]. https://github.com/ultralytics/ultralytics
This project assumes that Anaconda and Spyder are installed on your Windows system. Follow the steps below to set up your environment.
- Anaconda: Download and install Anaconda from the official website. See installation guides for different operating systems:
- Python >= 3.9 (comes with Anaconda)
- Create the yolov8 project folder
C:/
├─ Users
│ ├─ UserName
│ │ ├─ yolov8
│ │ │ ├─ labeling/
│ │ │ ├─ train/
│ │ │ ├─ test/
│ │ │ └─ valid/
│ │ └─ ...
│ └─ ...
└──...
-
Create a new conda environment (in the windows search bar type "Anaconda Prompt"):
conda create --name yolov8 python=3.9 spyder=5
-
Activate the environment:
conda activate yolov8
-
Install Required Packages:
pip install ultralytics
-
Launch Spyder within the new environment:
spyder
-
… and check your installation by opening and running the yolov8_test.py in Spyder:
-
... or by typing the following commads in your Command Line Interface (CLI):
yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
yolo predict model=yolov8n.pt source=0 show=True
- Install labelImg for image annotation using the same activated yolov8 environment:
conda install -c conda-forge labelimg
- Start the labeling software:
labelimg
- ... in the correct folder (labelImg [path to images] [path to predefined_classes.txt file]):
labelimg C:\Users\UserName\yolov8\labeling C:\Users\UserName\yolov8\labeling\predefined_classes.txt
yolo task=detect mode=train model=yolov8n.pt imgsz=800 data=path/to/boars.yaml epochs=200 batch=8 project=/path/to/your/project/training_runs/ name=yolov8n_imgsz800 device="cpu"
from ultralytics import YOLO
import os
# change the working directory so that the training results are saved to the correct folder.
os.chdir("path/to/yolov8/dataset/training_runs/")
# Load a pretrained model
model = YOLO("yolov8n.pt")
# train the model (transfer learning)
model.train(data="path/to/yolov8/boars.yaml",
epochs=20,
imgsz=800,
batch=8,
project = "path/to/yolov8/training_runs/",
name="yolov8n_imgsz800",
device="cpu") # 0 for GPU (check pytorch installation hints) or "cpu"
model=yolov8n.pt
: Indicates the model to be used. Here, the YOLOv8 Nano model (yolov8n.pt
) is used.imgsz=800
: Determines the size of the input images in pixels. In this case, the image size is 800x800 pixels (default: 640).data=path/to/boars.yaml
: Specifies the path to the data file that defines the training and validation data.epochs=200
: Sets the number of training epochs. Here, it is 200 epochs.batch=8
: Determines the batch size, i.e., the number of images processed simultaneously. Here, it is 8 (default 16).project=/path/to/your/project/training_runs/
: Indicates the project directory where the training runs will be saved.name=yolov8n_imgsz800
: Sets the name of the training run. This helps distinguish between different runs.device="cpu"
: Specifies the CPU to be used for training. Alternatively, GPUs 0 and 1 can be used (device=(0,1)).
Check here for additional train settings and hyperparameters
# Contents inside the .yaml file
train: path\to\yolov8\train
val: path\to\yolov8\valid
test: path\to\yolov8\test
# total number of classes
nc: 2
names: ['boar','human']
yolo predict model= .\training_runs\yolov8n_imgsz800\weights\best.pt source=.\test project=predictions name=yolov8n_imgsz800 conf=0.2 imgsz=800
from ultralytics import YOLO
import cv2
from matplotlib import pylab as plt
# Load a trained model
model = YOLO("path/to/yolov8/training_runs/yolov8m_imgsz800/weights/best.pt")
# Use the model (predict on an image)
results = model("path/to/yolov8/test",
project = "path/to/yolov8/predictions",
name = "yolov8m_imgsz800",
show=True,
show_labels=False,
conf=0.2,
imgsz=800,
classes=None,
save=True
)
#%%
# Plot the results (within matplotlib)
res_plotted = results[0].plot() # results[0]: show the first image
res_plotted_rgb=cv2.cvtColor(res_plotted, cv2.COLOR_BGR2RGB)
plt.imshow(res_plotted_rgb)
yolo detect val model=path\to\weights\best.pt data=path\to\yourFile.yaml
yolo track model=path\to\weights\best.pt project=path\to\trackings\ name=yolov8n_800_botsort source="path\to\test\" tracker=botsort.yaml
Here you can find the .yaml files for your tracker, as well as an additional script if you want to draw lines for your tracked paths.