Underwater Object Detection With YOLO v8
Underwater Object Detection With YOLO v8
Introducing Ultralytics YOLOv8, the latest version of the acclaimed real-time object detection and image
segmentation model.
Install YOLOv8 via the ultralytics pip package for the latest stable release
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 1/47
11/9/24, 2:29 PM __notebook__
In [1]:
# Install the ultralytics package from PyPI
! pip install ultralytics
! pip install wandb
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 2/47
11/9/24, 2:29 PM __notebook__
Collecting ultralytics
Downloading ultralytics-8.0.171-py3-none-any.whl (614 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 614.2/614.2 kB 13.3 MB/s et
a 0:00:00
Requirement already satisfied: matplotlib>=3.2.2 in /opt/conda/lib/
python3.10/site-packages (from ultralytics) (3.7.2)
Requirement already satisfied: numpy>=1.22.2 in /opt/conda/lib/pyth
on3.10/site-packages (from ultralytics) (1.23.5)
Requirement already satisfied: opencv-python>=4.6.0 in /opt/conda/l
ib/python3.10/site-packages (from ultralytics) (4.8.0.76)
Requirement already satisfied: pillow>=7.1.2 in /opt/conda/lib/pyth
on3.10/site-packages (from ultralytics) (9.5.0)
Requirement already satisfied: pyyaml>=5.3.1 in /opt/conda/lib/pyth
on3.10/site-packages (from ultralytics) (6.0)
Requirement already satisfied: requests>=2.23.0 in /opt/conda/lib/p
ython3.10/site-packages (from ultralytics) (2.31.0)
Requirement already satisfied: scipy>=1.4.1 in /opt/conda/lib/pytho
n3.10/site-packages (from ultralytics) (1.11.2)
Requirement already satisfied: torch>=1.8.0 in /opt/conda/lib/pytho
n3.10/site-packages (from ultralytics) (2.0.0)
Requirement already satisfied: torchvision>=0.9.0 in /opt/conda/li
b/python3.10/site-packages (from ultralytics) (0.15.1)
Requirement already satisfied: tqdm>=4.64.0 in /opt/conda/lib/pytho
n3.10/site-packages (from ultralytics) (4.66.1)
Requirement already satisfied: pandas>=1.1.4 in /opt/conda/lib/pyth
on3.10/site-packages (from ultralytics) (2.0.2)
Requirement already satisfied: seaborn>=0.11.0 in /opt/conda/lib/py
thon3.10/site-packages (from ultralytics) (0.12.2)
Requirement already satisfied: psutil in /opt/conda/lib/python3.10/
site-packages (from ultralytics) (5.9.3)
Requirement already satisfied: py-cpuinfo in /opt/conda/lib/python
3.10/site-packages (from ultralytics) (9.0.0)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/p
ython3.10/site-packages (from matplotlib>=3.2.2->ultralytics) (1.1.
0)
Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/pytho
n3.10/site-packages (from matplotlib>=3.2.2->ultralytics) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/
python3.10/site-packages (from matplotlib>=3.2.2->ultralytics) (4.4
0.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/
python3.10/site-packages (from matplotlib>=3.2.2->ultralytics) (1.
4.4)
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 3/47
11/9/24, 2:29 PM __notebook__
ite-packages (0.15.9)
Requirement already satisfied: Click!=8.0.0,>=7.1 in /opt/conda/li
b/python3.10/site-packages (from wandb) (8.1.7)
Requirement already satisfied: GitPython!=3.1.29,>=1.0.0 in /opt/co
nda/lib/python3.10/site-packages (from wandb) (3.1.31)
Requirement already satisfied: requests<3,>=2.0.0 in /opt/conda/li
b/python3.10/site-packages (from wandb) (2.31.0)
Requirement already satisfied: psutil>=5.0.0 in /opt/conda/lib/pyth
on3.10/site-packages (from wandb) (5.9.3)
Requirement already satisfied: sentry-sdk>=1.0.0 in /opt/conda/lib/
python3.10/site-packages (from wandb) (1.30.0)
Requirement already satisfied: docker-pycreds>=0.4.0 in /opt/conda/
lib/python3.10/site-packages (from wandb) (0.4.0)
Requirement already satisfied: PyYAML in /opt/conda/lib/python3.10/
site-packages (from wandb) (6.0)
Requirement already satisfied: pathtools in /opt/conda/lib/python3.
10/site-packages (from wandb) (0.1.2)
Requirement already satisfied: setproctitle in /opt/conda/lib/pytho
n3.10/site-packages (from wandb) (1.3.2)
Requirement already satisfied: setuptools in /opt/conda/lib/python
3.10/site-packages (from wandb) (68.0.0)
Requirement already satisfied: appdirs>=1.4.3 in /opt/conda/lib/pyt
hon3.10/site-packages (from wandb) (1.4.4)
Requirement already satisfied: protobuf!=4.21.0,<5,>=3.19.0 in /op
t/conda/lib/python3.10/site-packages (from wandb) (3.20.3)
Requirement already satisfied: six>=1.4.0 in /opt/conda/lib/python
3.10/site-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)
Requirement already satisfied: gitdb<5,>=4.0.1 in /opt/conda/lib/py
thon3.10/site-packages (from GitPython!=3.1.29,>=1.0.0->wandb) (4.
0.10)
Requirement already satisfied: charset-normalizer<4,>=2 in /opt/con
da/lib/python3.10/site-packages (from requests<3,>=2.0.0->wandb)
(3.1.0)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/pytho
n3.10/site-packages (from requests<3,>=2.0.0->wandb) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/li
b/python3.10/site-packages (from requests<3,>=2.0.0->wandb) (1.26.1
5)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/li
b/python3.10/site-packages (from requests<3,>=2.0.0->wandb) (2023.
7.22)
Requirement already satisfied: smmap<6,>=3.0.1 in /opt/conda/lib/py
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 5/47
11/9/24, 2:29 PM __notebook__
In [2]:
# Importing the required libraries
import os
import random
import pandas as pd
import wandb
from kaggle_secrets import UserSecretsClient
import cv2
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import torch
from ultralytics import YOLO
%matplotlib inline
/opt/conda/lib/python3.10/site-packages/scipy/__init__.py:146: User
Warning: A NumPy version >=1.16.5 and <1.23.0 is required for this
version of SciPy (detected version 1.23.5
warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxver
sion}"
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 6/47
11/9/24, 2:29 PM __notebook__
In [3]:
# Wandb login:
user_secrets = UserSecretsClient()
secret_value = user_secrets.get_secret("wandb_api_key")
wandb.login(key=secret_value)
Out[3]:
True
In [4]:
# Define the paths to the images and labels directories
train_images = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/trai
n/images'
train_labels = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/trai
n/labels'
val_images = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/valid/
images'
val_labels = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/valid/
labels'
test_images = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/test/
images'
test_labels = '/kaggle/input/aquarium-data-cots/aquarium_pretrain/test/
labels'
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 7/47
11/9/24, 2:29 PM __notebook__
In [5]:
# Define the labels
classes = ['fish', 'jellyfish', 'penguin', 'puffin', 'shark', 'starfis
h', 'stingray']
Idx2Label = {idx: label for idx, label in enumerate(classes)}
Label2Index = {label: idx for idx, label in Idx2Label.items()}
print('Index to Label Mapping:', Idx2Label)
print('Label to Index Mapping:', Label2Index)
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 8/47
11/9/24, 2:29 PM __notebook__
In [6]:
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 9/47
11/9/24, 2:29 PM __notebook__
# Loop over the random images and plot the bounding boxes
for i, image_file in enumerate(sample_image_files):
row = i // 3
col = i % 3
axs[row, col].imshow(image)
axs[row, col].axis('off')
plt.show()
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 10/47
11/9/24, 2:29 PM __notebook__
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 11/47
11/9/24, 2:29 PM __notebook__
In [7]:
# Read a image by path
image_path = os.path.join(train_images, os.listdir(train_images)[100])
image = cv2.imread(image_path)
In [8]:
%%writefile /kaggle/working/data.yaml
# Path
train: /kaggle/input/aquarium-data-cots/aquarium_pretrain/train/images
val: /kaggle/input/aquarium-data-cots/aquarium_pretrain/valid/images
test: /kaggle/input/aquarium-data-cots/aquarium_pretrain/test/images
# Classes
nc: 7
names: ['fish', 'jellyfish', 'penguin', 'puffin', 'shark', 'starfish',
'stingray']
Writing /kaggle/working/data.yaml
Model Training
Train mode is used for training a YOLOv8 model on a custom dataset. In this mode, the model is trained using
the specified dataset and hyperparameters. The training process involves optimizing the model's parameters so
that it can accurately predict the classes and locations of objects in an image.
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 12/47
11/9/24, 2:29 PM __notebook__
In [9]:
# Load a pretrained nano model
model = YOLO('yolov8n.pt')
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 13/47
11/9/24, 2:29 PM __notebook__
Downloading https://github.com/ultralytics/assets/releases/downloa
d/v0.0.0/yolov8n.pt to 'yolov8n.pt'...
100%|██████████| 6.23M/6.23M [00:00<00:00, 73.1MB/s]
Ultralytics YOLOv8.0.171 🚀 Python-3.10.12 torch-2.0.0 CUDA:0 (Tesl
a P100-PCIE-16GB, 16281MiB)
engine/trainer: task=detect, mode=train, model=yolov8n.pt, data=/ka
ggle/working/data.yaml, epochs=100, patience=50, batch=16, imgsz=(1
024, 768, 3), save=True, save_period=-1, cache=False, device=None,
workers=4, project=None, name=yolov8n_custom, exist_ok=False, pretr
ained=True, optimizer=auto, verbose=True, seed=42, deterministic=Tr
ue, single_cls=False, rect=False, cos_lr=False, close_mosaic=10, re
sume=False, amp=True, fraction=1.0, profile=False, freeze=None, ove
rlap_mask=True, mask_ratio=4, dropout=0.0, val=True, split=val, sav
e_json=False, save_hybrid=False, conf=None, iou=0.7, max_det=300, h
alf=False, dnn=False, plots=True, source=None, show=False, save_txt
=False, save_conf=False, save_crop=False, show_labels=True, show_co
nf=True, vid_stride=1, stream_buffer=False, line_width=None, visual
ize=False, augment=False, agnostic_nms=False, classes=None, retina_
masks=False, boxes=True, format=torchscript, keras=False, optimize=
False, int8=False, dynamic=False, simplify=False, opset=None, works
pace=4, nms=False, lr0=0.01, lrf=0.01, momentum=0.937, weight_decay
=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.
1, box=7.5, cls=0.5, dfl=1.5, pose=12.0, kobj=1.0, label_smoothing=
0.0, nbs=64, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, transl
ate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=
0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0, cfg=None, tracker=botso
rt.yaml, save_dir=runs/detect/yolov8n_custom
Downloading https://ultralytics.com/assets/Arial.ttf to '/root/.con
fig/Ultralytics/Arial.ttf'...
100%|██████████| 755k/755k [00:00<00:00, 21.7MB/s]
Overriding model.yaml nc=80 with nc=7
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 14/47
11/9/24, 2:29 PM __notebook__
0.367 0.184
Size
36/100 5.49G 1.171 0.9437 1.143 216
1024: 100%|██████████| 28/28 [00:19<00:00, 1.46it/s]
Class Images Instances Box(P R
mAP50 mAP50-95): 100%|██████████| 4/4 [00:02<00:00, 1.59it/s]
all 127 909 0.744 0.648
0.695 0.429
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 24/47
11/9/24, 2:29 PM __notebook__
0.718 0.429
Size
80/100 5.68G 0.9149 0.6278 1.008 166
1024: 100%|██████████| 28/28 [00:19<00:00, 1.41it/s]
Class Images Instances Box(P R
mAP50 mAP50-95): 100%|██████████| 4/4 [00:02<00:00, 1.52it/s]
all 127 909 0.77 0.701
0.751 0.461
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 33/47
11/9/24, 2:29 PM __notebook__
Validating runs/detect/yolov8n_custom/weights/best.pt...
Ultralytics YOLOv8.0.171 🚀 Python-3.10.12 torch-2.0.0 CUDA:0 (Tesl
a P100-PCIE-16GB, 16281MiB)
Model summary (fused): 168 layers, 3007013 parameters, 0 gradients
Class Images Instances Box(P R
mAP50 mAP50-95): 100%|██████████| 4/4 [00:01<00:00, 3.04it/s]
all 127 909 0.772 0.689
0.743 0.47
fish 127 459 0.817 0.763
0.822 0.475
jellyfish 127 155 0.873 0.819
0.923 0.548
penguin 127 104 0.637 0.644
0.672 0.344
puffin 127 74 0.75 0.528
0.564 0.311
shark 127 57 0.737 0.64
0.663 0.47
starfish 127 27 0.84 0.704
0.797 0.579
stingray 127 33 0.75 0.727
0.761 0.562
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 37/47
11/9/24, 2:29 PM __notebook__
Model Performance:
Train Box Loss: measures the difference between the predicted bounding boxes and the actual bounding
boxes of the objects in the training data.
Train Class Loss: measures the difference between the predicted class probabilities and the actual class
labels of the objects in the training data.
Train DFL (Dynamic Feature Learning) Loss: measures the difference between the predicted feature maps
and the actual feature maps of the objects in the training data.
Metrics Precision (B): measures the proportion of true positive detections among all the predicted
bounding boxes.
Metrics Recall (B): metric measures the proportion of true positive detections among all the actual
bounding boxes.
Metrics mAP50 (B): measures the mean average precision of the model across different object categories,
with a 50% intersection-over-union (IoU) threshold.
Metrics mAP50-95 (B): measures the mean average precision of the model across different object
categories, with IoU thresholds ranging from 50% to 95%.
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 38/47
11/9/24, 2:29 PM __notebook__
In [10]:
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 39/47
11/9/24, 2:29 PM __notebook__
%matplotlib inline
# read in the results.csv file as a pandas dataframe
df = pd.read_csv('/kaggle/working/runs/detect/yolov8n_custom/results.cs
v')
df.columns = df.columns.str.strip()
plt.show()
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 40/47
11/9/24, 2:29 PM __notebook__
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 41/47
11/9/24, 2:29 PM __notebook__
Evaluation
In [11]:
# Loading the best performing model
model = YOLO('/kaggle/working/runs/detect/yolov8n_custom/weights/best.p
t')
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 42/47
11/9/24, 2:29 PM __notebook__
In [12]:
print(f"Mean Average Precision @.5:.95 : {metrics.box.map}")
print(f"Mean Average Precision @ .50 : {metrics.box.map50}")
print(f"Mean Average Precision @ .70 : {metrics.box.map75}")
In [13]:
# Function to perform detections with trained model
def predict_detection(image_path):
# Read the image
image = cv2.imread(image_path)
# Pass the image through the detection model and get the result
detect_result = model(image)
return detect_image
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 43/47
11/9/24, 2:29 PM __notebook__
In [14]:
# Get list of all the image files in the test directory
image_files = sorted(os.listdir(test_images))
# Loop over the random images and plot the detections of the trained mod
el
for i, image_file in enumerate(sample_image_files):
row = i // 3
col = i % 3
axs[row, col].imshow(detect_image)
axs[row, col].axis('off')
plt.show()
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 44/47
11/9/24, 2:29 PM __notebook__
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 45/47
11/9/24, 2:29 PM __notebook__
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 46/47
11/9/24, 2:29 PM __notebook__
References:
https://docs.ultralytics.com/ (https://docs.ultralytics.com/)
https://www.kaggle.com/code/vencerlanz09/wind-turbines-object-detection-using-yolov8/notebook
(https://www.kaggle.com/code/vencerlanz09/wind-turbines-object-detection-using-yolov8/notebook)
https://www.kaggle.com/code/quydau/underwater-object-detection-with-yolo-v8 47/47