-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_model.py
More file actions
111 lines (87 loc) · 3.67 KB
/
validate_model.py
File metadata and controls
111 lines (87 loc) · 3.67 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from PIL import Image
import matplotlib.pyplot as plt
from tqdm import tqdm
# Configurations
MODEL_PATH = "model/person_vs_horse_model.h5"
VALIDATION_DIR = r"validation-horse-or-human"
OUTPUT_DIR = "validation_results" # New directory for results
IMG_SIZE = (224, 224)
CLASSES = {0: "horses", 1: "humans"} # Classes mapping
# Load the model
try:
model = load_model(MODEL_PATH)
except Exception as e:
print(f"Error while loading the model: {e}")
exit()
def preprocess_image(image_path):
"""Preprocess the image for prediction."""
try:
img = Image.open(image_path).convert("RGB")
img = img.resize(IMG_SIZE)
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
except Exception as e:
print(f"Error while preprocessing {image_path}: {e}")
return None
def run_validation():
"""
Processes each image, makes predictions, and saves the results.
It also calculates the accuracy of the model on the validation set.
"""
correct_predictions = 0
total_images = 0
os.makedirs(OUTPUT_DIR, exist_ok=True)
for class_name in os.listdir(VALIDATION_DIR):
class_path = os.path.join(VALIDATION_DIR, class_name)
if not os.path.isdir(class_path):
continue
true_label_id = list(CLASSES.keys())[list(CLASSES.values()).index(class_name)]
image_files = [f for f in os.listdir(class_path) if f.endswith(('jpg', 'jpeg', 'png'))]
for image_file in tqdm(image_files, desc=f"Processing images from '{class_name}'"):
image_path = os.path.join(class_path, image_file)
img_array = preprocess_image(image_path)
if img_array is None:
continue
pred = model.predict(img_array, verbose=0)[0][0]
predicted_label_id = 1 if pred > 0.5 else 0
predicted_label = CLASSES[predicted_label_id]
if predicted_label == "horses":
confidence = 1 - pred # Probability of being a horse
else:
confidence = pred # Probability of being a person
is_correct = (predicted_label_id == true_label_id)
if is_correct:
correct_predictions += 1
total_images += 1
# Save the result as an image with prediction details
img_display = Image.open(image_path).convert("RGB")
plt.figure(figsize=(6, 6))
plt.imshow(img_display)
plt.axis("off")
title_color = "green" if is_correct else "red"
outcome_text = "Correct" if is_correct else "Incorrect"
title_text = (
f"{outcome_text}\n"
f"True: {class_name}\n"
f"Predicted: {predicted_label}\n"
f"(Confidence: {confidence:.5f})"
)
plt.title(title_text, color=title_color)
output_filename = f"{outcome_text.split()[0].lower()}_{image_file}"
save_path = os.path.join(OUTPUT_DIR, output_filename)
plt.savefig(save_path, bbox_inches='tight', pad_inches=0.1)
plt.close()
# Accuracy calculation
if total_images > 0:
accuracy = (correct_predictions / total_images) * 100
print(f"Images classified correctly: {correct_predictions}")
print(f"General accuracy: {accuracy:.2f}%")
else:
print("No image found.")
if __name__ == "__main__":
run_validation()