-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
191 lines (139 loc) · 6.09 KB
/
demo.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import torch
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
import numpy as np
import time
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import matplotlib.pyplot as plt
import cv2
import os
from PIL import Image
import PIL
import math
from tqdm import tqdm
from definitions import FruitRipenessDetector, evaluate
extensions = ['.jpg', '.jpeg', '.png', '.heic']
def process_imgs(folder):
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
imgs = []
total_images = 0
# Count the total number of images to process
for fruit in os.listdir(folder):
n_path = os.path.join(folder, fruit)
if ".ini" in n_path:
continue
for ripeness, file in enumerate(os.listdir(n_path)):
i_path = os.path.join(n_path, file)
if ".ini" in i_path:
continue
img_list = os.listdir(i_path)
for img in img_list:
if ".ini" in img:
continue
total_images += 1
# Use tqdm to create a progress bar
with tqdm(total=total_images, desc="Processing images") as pbar:
for fruit in os.listdir(folder):
n_path = os.path.join(folder, fruit)
if ".ini" in n_path:
continue
for ripeness, file in enumerate(os.listdir(n_path)):
i_path = os.path.join(n_path, file)
if ".ini" in i_path:
continue
label = ripeness
img_list = os.listdir(i_path)
for img in img_list:
if ".ini" in img:
continue
f_path = os.path.join(i_path, img)
image = PIL.Image.open(f_path).convert("RGB")
aspect_ratio = image.width / image.height
if aspect_ratio < 1:
new_width = 256
new_height = int(new_width / aspect_ratio)
else:
new_height = 256
new_width = int(new_height * aspect_ratio)
resized_image = image.resize((new_width, new_height))
padded_image = PIL.ImageOps.pad(resized_image, (256, 256), color="black")
hsv_image = padded_image.convert("HSV")
imgs.append((transform(hsv_image), torch.tensor(label)))
# Update the progress bar
pbar.update(1)
return imgs
def folder_to_pth(folder):
data = process_imgs(folder)
torch.save(data, f'dataset.pth')
def test_eval(net, loader, folder):
file_paths = []
images = []
total_images = 0
for root, dirs, files in os.walk(folder):
for file in files:
if any(file.endswith(ext) for ext in extensions):
file_path = os.path.join(root, file)
image = np.asarray(Image.open(file_path))
images.append(image)
file_paths.append(file_path)
total_images += 1
# Create a figure and axis object for displaying the images
num_images = total_images
num_cols = min(num_images, 4)
num_rows = math.ceil(num_images / num_cols)
# print(f"num_images: {num_images}")
# print(f"num_cols: {num_cols}")
# print(f"num_rows: {num_rows}")
# Create a figure and axis object for displaying the predicted labels
fig_pred, axs_pred = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(15, 3*math.ceil((total_images)/4)), squeeze=False)
# Create a figure and axis object for displaying the predicted and actual labels
fig_pred_actual, axs_pred_actual = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(15, 3*math.ceil((total_images)/4)), squeeze=False)
# Define a dictionary to map integer labels to string labels
label_map = {0: 'unripe', 1: 'semiripe', 2: 'ripe', 3: 'overripe'}
correct = 0
total = 0
with open('predictions.txt', 'w') as f:
for i, (inputs, labels) in enumerate(loader):
outputs = net(inputs)
predictions = torch.round(outputs)
for j in range(len(inputs)):
# count += 1
total += 1
prediction = int(predictions[j].item())
actual = int(labels[j].item())
# Write the prediction and actual label to the file
f.write(f"Prediction: {prediction} Actual: {actual}\n")
# f.write(f"Prediction: {label_map[prediction]}\nActual: {label_map[actual]}\n\n")
if prediction == actual:
correct += 1
image = inputs[j]
prediction = predictions[j]
actual = labels[j]
axs_pred[j//4, j%4].imshow(images[j])
axs_pred[j//4, j%4].set_title(f"Prediction: {label_map[int(prediction.item())]}")
axs_pred[j//4, j%4].axis('off')
axs_pred_actual[j//4, j%4].imshow(images[j])
axs_pred_actual[j//4, j%4].set_title(f"Prediction: {label_map[int(prediction.item())]}, Actual: {label_map[int(actual.item())]}")
axs_pred_actual[j//4, j%4].axis('off')
# Show the plot
# plt.show()
plt.savefig('predictions.png')
# Save the plots to files
fig_pred.savefig('predictions_pred.png')
fig_pred_actual.savefig('predictions_pred_actual.png')
accuracy = correct / total
print(f"Sample Accuracy Comparison with Human Visual: {accuracy * 100:.2f}%")
MODEL_PATH = "model_ripeness_detector_bs64_lr0.001_epoch150"
TEST_PATH = "dataset.pth"
FOLDER = "photos"
folder_to_pth(FOLDER)
test_model = FruitRipenessDetector()
test_dataset = torch.load(TEST_PATH)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=64, shuffle=False)
paramys = torch.load(MODEL_PATH)
test_model.load_state_dict(paramys)
test_eval(test_model, test_loader, FOLDER)