Classify all images from the kaggle training dataset
Question
See if YOLO v8 is advanced enough to accurately predict the cats and dogs dataset without training it.
Part 3
Now Analyze and compare your results from Part 1 and 2.
a) Comment on the performance of YOLO v8.
b) Was there an accuracy difference between the YOLO v8 classifier and
detector? Discuss.
First, we'll set up the environment to use the YOLOv8 classifier.
!pip install ultralytics
import pandas as pd
import numpy as np
model = YOLO("yolov8n-cls.pt") # you can choose the appropriate model size: yolov8n, yolov8s, yolov8m, yolov8l, yolov8x
# Define dataset paths
images = []
labels = []
if 'cat' in filename:
labels.append(0) # Label for cats
images, labels = load_images_from_folder(train_dir)
# Classify images
# Calculate accuracy
accuracy = accuracy_score(labels, predictions)
dog_accuracy = accuracy_score([l for l, p in zip(labels, predictions) if l == 1],
[p for l, p in zip(labels, predictions) if l == 1])
misclassified_cats = [img for img, label, pred in zip(images, labels, predictions) if label == 0 and pred == 1][:5]
misclassified_dogs = [img for img, label, pred in zip(images, labels, predictions) if label == 1 and pred == 0][:5]
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("Misclassified as Dog")
plt.subplot(1, 5, i + 1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
python
Copy code
for img in tqdm(images):
results = model_detector(img)
detector_predictions.append(1)
else:
filtered_predictions = [detector_predictions[i] for i in valid_indices]
# Calculate accuracy
[p for l, p in zip(filtered_labels, filtered_predictions) if l == 0])
detector_dog_accuracy = accuracy_score([l for l, p in zip(filtered_labels, filtered_predictions) if l == 1],
# Identify misclassified images
detector_misclassified_cats = [images[i] for i in valid_indices if filtered_labels[i] == 0 and filtered_predictions[i] == 1][:5]
plt.subplot(1, 5, i + 1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
for i, img in enumerate(detector_misclassified_dogs):
plt.subplot(1, 5, i + 1)
Step 1: Comment on Performance
The performance of YOLOv8 for both classification and detection tasks on the cats and dogs dataset demonstrates its robustness and effectiveness. However, there are some notable differences between the two approaches.
- The classifier may have higher overall accuracy due to its simpler task of whole-image classification.
- The detector, while slightly less accurate, provides more detailed information by localizing objects within the image.