Views
No views yet
python3 -m venv oyster_venvsource oyster_venv/bin/activate (Linux and macOS)
oyster_venv\Scripts\activate (Windows)python3 -m pip install --upgrade pip
python3 -m pip install ultralyticspython3 -m oysters """
Python3 script for YOLO prediction and sizing of oyster objects.
"""
from ultralytics import YOLO
# Locally written directories
results_dir = 'oyster_yolo_results'
predict_name = 'example_oyster_results'
# Files from this aquaculture_oysters HF repository
model = YOLO('aquaculture_oysters.pt')
oyster_img = 'oyster_sample.jpg'
# The size standard disk diameter. In this case, as millimeters,
# using a cutout from a 3 inch hockey puck.
std_mm = 76.2
# Run inference with these arguments
prediction_kwargs = dict(
save=True, # saves image to project/name
save_txt=True, # saves relative bounding box coordinates
save_conf=True, # includes conf scores
augment=False,
show_labels=True,
line_width=2,
project=results_dir,
name=predict_name,
exist_ok=False,
)
results = model.predict(
source=oyster_img,
half=False,
imgsz=960,
conf=0.7,
device='cpu',
iou=0.7,
max_det=400,
classes=[0,1], # 0 is 'disk', 1 is 'oyster'.
**prediction_kwargs
)
# Display detection bounding boxes with class names and confidence values.
results[0].show(line_width=2)
# Calculate and print size metrics, Are based on longest bounding box dimension.
# Sizes do not account for longer lengths of oysters oriented on the diagonal.
# An empirically determined correction factor is recommended for better accuracy.
# A good correction function is used in the companion GitHub repository.
boxes_tensor = results[0].boxes.xywh
boxes_numpy = boxes_tensor.cpu().numpy().astype(int)
w_h = boxes_numpy[:, 2:4]
px_lengths = w_h.max(axis=1)
classes = results[0].boxes.cls
class_distribution = classes.cpu().numpy().astype(int)
num_disks = len(class_distribution[class_distribution == 0])
num_oysters = len(class_distribution[class_distribution == 1])
oyster_px_sizes = px_lengths[class_distribution == 1]
disk_px_sizes = px_lengths[class_distribution == 0]
px2mm_factor = std_mm / disk_px_sizes.mean()
oyster_mean_mm = round(oyster_px_sizes.mean() * px2mm_factor, 2)
smallest = round(oyster_px_sizes.min() * px2mm_factor, 2)
largest = round(oyster_px_sizes.max() * px2mm_factor, 2)
print(f'Mean oyster size, mm: {oyster_mean_mm}')
print(f'Oyster size range, mm: [{smallest} -- {largest}]')hyperparam_args233 = {
'lr0': 0.00242,
'lrf': 0.01009,
'momentum': 0.92746,
'weight_decay': 0.00038,
'warmup_epochs': 1.94011,
'warmup_momentum': 0.47865,
'box': 5.89996,
'cls': 0.27165,
'dfl': 1.23119,
'hsv_h': 0.01303,
'hsv_s': 0.32101,
'hsv_v': 0.40969,
'degrees': 0.0,
'translate': 0.15574,
'scale': 0.75304,
'shear': 0.0,
'perspective': 0.0,
'flipud': 0.0,
'fliplr': 0.58715,
'bgr': 0.0,
'mosaic': 1.0,
'mixup': 0.0,
'copy_paste': 0.0,
}image 1/1 /home/craig/Desktop/oyster_sample.jpg: 960x768 3 disks, 105 oysters, 2903.9ms
Speed: 13.3ms preprocess, 2903.9ms inference, 28.1ms postprocess per image at shape (1, 3, 960, 768)
Results saved to oyster_yolo_results/example_oyster_results
1 label saved to oyster_yolo_results/example_oyster_results/labels
Mean oyster size, mm: 58.71
Oyster size range, mm: [47.22 -- 69.1]