|
import logging |
|
from ultralytics import YOLO |
|
|
|
|
|
logging.getLogger('ultralytics').setLevel(logging.WARNING) |
|
|
|
|
|
models = { |
|
"yolov8n": "yolov8n.pt", |
|
"fine_tuned": "yolov8n_rubberducks.pt" |
|
} |
|
image_path = "test_image.jpg" |
|
|
|
|
|
performance = {} |
|
|
|
|
|
for model_name, model_path in models.items(): |
|
|
|
model = YOLO(model_path) |
|
|
|
|
|
results = model(image_path) |
|
first_result = results[0] |
|
|
|
|
|
num_detections = len(first_result.boxes) if hasattr(first_result, 'boxes') and first_result.boxes is not None else 0 |
|
|
|
|
|
if num_detections > 0: |
|
total_confidence = sum(float(box.conf) for box in first_result.boxes) |
|
else: |
|
total_confidence = 0.0 |
|
|
|
|
|
performance[model_name] = { |
|
"detections": num_detections, |
|
"confidence": total_confidence |
|
} |
|
|
|
|
|
yolo_detections = performance['yolov8n']['detections'] |
|
yolo_confidence = performance['yolov8n']['confidence'] |
|
fine_tuned_detections = performance['fine_tuned']['detections'] |
|
fine_tuned_confidence = performance['fine_tuned']['confidence'] |
|
|
|
|
|
diff_detections = fine_tuned_detections - yolo_detections |
|
diff_confidence = fine_tuned_confidence - yolo_confidence |
|
detection_diff_word = "more" if diff_detections > 0 else "less" |
|
confidence_diff_word = "more" if diff_confidence > 0 else "less" |
|
|
|
|
|
print() |
|
print(f" YOLOv8n detected {yolo_detections} ducks with a total confidence of {yolo_confidence:.2f}") |
|
print(f" The fine-tuned model detected {fine_tuned_detections} ducks with a total confidence of {fine_tuned_confidence:.2f}") |
|
print(f" The fine-tuned model detects ducks with {abs(diff_confidence * 100):.0f}% {confidence_diff_word} confidence.") |
|
print() |
|
|