-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodeltester.py
More file actions
65 lines (51 loc) · 2.1 KB
/
Copy pathmodeltester.py
File metadata and controls
65 lines (51 loc) · 2.1 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
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import os
from PIL import Image
from torchnn import ImageClassifier, transform, device, target_to_class
#---------PARAM---------
directory = r'DATASET\Test_SET'
state_dict = torch.load('model.pth')
# Load the pretrained model
model = ImageClassifier()
model.load_state_dict(state_dict)
model.eval()
# CUDA OR CPU
model = model.to(device)
# Define the label ma
label_map = target_to_class
# Create a figure for plotting
fig = plt.figure(figsize=(10, 10), facecolor='lightgray')
# Loop over all files in the directory
for i, filename in enumerate(os.listdir(directory)):
if filename.endswith(".png"): # add more conditions if you have images of different types
# Load the image
image_path = os.path.join(directory, filename)
image = Image.open(image_path).convert("RGB")
# Preprocess the image
image_tensor = transform(image).unsqueeze(0)
image_tensor = image_tensor.to(device)
# Get the prediction
with torch.no_grad():
outputs = model(image_tensor)
# The output has unnormalized probabilities. To get probabilities, you can run a softmax on it.
probabilities = F.softmax(outputs, dim=1)
# You can get the class here by finding the index with the maximum probability
_, predicted_class = torch.max(probabilities, 1)
# Get the confidence level
confidence_level = torch.max(probabilities).item()
# Get the predicted label
predicted_label_index = predicted_class.item()
predicted_label_name = label_map[predicted_label_index]
if confidence_level < 0.8:
predicted_label_name = '?'
else:
predicted_label_name = label_map[predicted_label_index]
# Plot the image with the predicted label and confidence level
ax = fig.add_subplot(8, 8, i+1)
ax.imshow(image)
ax.set_title(f"{predicted_label_name} \n ({confidence_level*100:.2f}%)")
ax.axis('off')
plt.tight_layout()
plt.show()