-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
33 lines (24 loc) · 1.03 KB
/
predict.py
File metadata and controls
33 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from ultralytics import YOLO
import numpy as np
# Load the trained model
model = YOLO('../runs/classify/train/weights/last.pt')
# Make prediction
results = model('yolov8_custom_training/train/normal/IM-0128-0001.jpeg')
# Get class names
names_dict = results[0].names
print("Class names:", names_dict)
probs_data = results[0].probs.data.tolist()
print("Probabilities (using .data):", probs_data)
# Option 2: Using .numpy() method
probs_numpy = results[0].probs.numpy()
print("Probabilities (using .numpy()):", probs_numpy)
# Get the predicted class
predicted_class_idx = results[0].probs.top1
predicted_class_name = names_dict[predicted_class_idx]
predicted_confidence = results[0].probs.top1conf
print(f"Predicted class: {predicted_class_name}")
print(f"Confidence: {predicted_confidence:.4f}")
# Alternative way using numpy argmax
predicted_class_idx_alt = np.argmax(probs_data)
predicted_class_name_alt = names_dict[predicted_class_idx_alt]
print(f"Predicted class (using argmax): {predicted_class_name_alt}")