-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dataset.py
289 lines (215 loc) · 9.77 KB
/
test_dataset.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
This file can be used to test the siamese network trained using the face_train.ipynb
"""
from tqdm import trange
import os
import keras as keras
import os, random
from PIL import Image
from matplotlib import pyplot
import numpy as np
from datetime import datetime
import pytz
from pytz import timezone
format = "%d-%m-%Y_%H-%M-%S"
from keras.models import load_model
class configuration():
""" A simple class to manage the configuration"""
dataset_type = 1 # 1 or 2 ---> more about dataset_type can be found in the README.txt file
testing_dataset_path = "datasets\\scface"
model_path = "test_model\\siamese_network_02-04-2020_15-11-49_thres_[0.52510107]_.h5"
model_threshold = 0.52510107 #provide the treshold of the trained model
split = 10 # total number of splits to make from total samples
batch_size = None #will be set automatically in the program -> (total samples/split)
image_size = None #will be set automatically
trials = 10 # number of trials to perform
results_file_name = "results_sc.txt" # name of the file where the results would be stored
def get_images(img0_paths, img1_paths):
""" A simple function to open images and append it in the array.
Returns the array of images"""
img0_arr = []
img1_arr = []
def return_image(img_path):
""" Open the image and return the image """
img = Image.open(img_path).convert('RGB')
img = img.resize((configuration.image_size,configuration.image_size))
img = (np.asarray(img, dtype=np.float32))/255.0
return img
for i in range(len(img0_paths)):
img0_arr.append( return_image(img0_paths[i]) )
img1_arr.append( return_image(img1_paths[i]) )
return img0_arr, img1_arr
def get_folder_list(path):
""" Returns the list of folder paths in the directory. """
folders = []
for folder in os.listdir(path):
folder_path = os.path.join(path, folder)
folders.append(folder_path)
#print(folders)
return folders
def get_image_pair_paths_type1(folders):
"""
returns 3 lists.
1st and 2nd lists contains paths to images in the pair. Thus image paths of ith pair can be accessed by list1[i], list2[i].
3rd list contains the tags for the respective pair. 0 means the pair is same and 1 means the pair is different.
"""
image1_paths = []
image2_paths = []
tags = []
print("Generating image pair paths: ")
for i in trange(len(folders)):
current_folder_path = folders[i]
for current_image_name in os.listdir(current_folder_path):
choice = random.randint(0,1)
if choice == 0:
image1_paths.append(os.path.join(folders[i], current_image_name))
image2_name = random.choice(os.listdir(folders[i]))
image2_paths.append(os.path.join(folders[i], image2_name))
tags.append(choice)
else:
folder1 = folders[i]
while True:
folder2 = random.choice(folders)
if folder1 != folder2 :
break
image1_paths.append(os.path.join(folder1, current_image_name))
image2_name = random.choice(os.listdir(folder2))
image2_paths.append(os.path.join(folder2, image2_name))
tags.append(choice)
combined_zip = list(zip(image1_paths, image2_paths, tags))
random.shuffle(combined_zip)
image1_paths[:], image2_paths[:], tags[:] = zip(*combined_zip)
return image1_paths, image2_paths, tags
def get_image_pair_paths_type2(folders):
"""
returns 3 lists.
1st and 2nd lists contains paths to images in the pair. Thus image paths of ith pair can be accessed by list1[i], list2[i].
3rd list contains the tags for the respective pair. 0 means the pair is same and 1 means the pair is different.
"""
image1_paths = []
image2_paths = []
tags = []
print("Generating image pair paths: ")
for i in range(len(folders)):
current_folder_path = folders[i]
current_subfolder = random.choice(os.listdir(current_folder_path))
current_subfolder_path = os.path.join(current_folder_path, current_subfolder)
img1_path = os.path.join( current_subfolder_path, random.choice(os.listdir(current_subfolder_path)))
choice = random.randint(0,1)
if choice == 0:
current_subfolder = random.choice(os.listdir(current_folder_path))
current_subfolder_path = os.path.join(current_folder_path, current_subfolder)
img2_path = os.path.join( current_subfolder_path, random.choice(os.listdir(current_subfolder_path)))
elif choice == 1:
current_folder_path = random.choice(folders)
current_subfolder = random.choice(os.listdir(current_folder_path))
current_subfolder_path = os.path.join(current_folder_path, current_subfolder)
img2_path = os.path.join( current_subfolder_path, random.choice(os.listdir(current_subfolder_path)))
image1_paths.append(img1_path)
image2_paths.append(img2_path)
tags.append(choice)
combined_zip = list(zip(image1_paths, image2_paths, tags))
random.shuffle(combined_zip)
image1_paths[:], image2_paths[:], tags[:] = zip(*combined_zip)
return image1_paths, image2_paths, tags
def check(path1, path2, label):
"""
prints the batch statistics about the percent of same pairs and different pairs
"""
count_same = count_diff = false_count = 0
for i in range(len(label)):
if path1[i].split("\\")[2] == path2[i].split("\\")[2]:
if label[i] == 0:
count_same += 1
else:
false_count += 1
else:
if label[i] == 1:
count_diff += 1
else:
false_count +=1
total = count_same + count_diff + false_count
print("same pairs {} ({:0.2f}%) | diff pairs {} ({:0.2f}%)".format(count_same, ((count_same*100)/total), count_diff, ((count_diff*100)/total)))
file.writelines("same pairs {} ({:0.2f}%) | diff pairs {} ({:0.2f}%)\n".format(count_same, ((count_same*100)/total), count_diff, ((count_diff*100)/total)))
return None
def calc_accuracy(tag, prediction, threshold):
"""
Returns the accuracy of the predictions and also number of correct and false classifications
"""
correct_classify = false_classify = 0
for i in range(0,len(tag)):
if prediction[i] >= threshold:
prediction[i] = 1
else:
prediction[i] = 0
if prediction[i] == tag[i]:
correct_classify = correct_classify + 1
else:
false_classify = false_classify + 1
accuracy = (correct_classify * 100)/(len(tag))
return accuracy, correct_classify, false_classify
#load the saved model and print the summary.
loaded_model = load_model(configuration.model_path, compile = False)
loaded_model.summary()
#set the image size from the input size of the model
configuration.image_size = loaded_model.input_shape[0][1]
#open file for writing the results
file = open(configuration.results_file_name,'w',encoding='utf-8')
testing_acc = []
for i in range(0,configuration.trials):
print("="*60)
file.writelines("="*30)
file.writelines("\n")
print("Run {}".format(i))
file.writelines(f"Run {i}\n")
# get the paths of all the folders in the directory
folders = get_folder_list(configuration.testing_dataset_path)
# generate image pair paths and tags based upon the dataset type
if configuration.dataset_type == 2:
img0_paths, img1_paths, tags = get_image_pair_paths_type2(folders)
else:
img0_paths, img1_paths, tags = get_image_pair_paths_type1(folders)
print("Total samples: ",len(img0_paths))
configuration.batch_size = int(len(tags)/configuration.split) # set the batch size
print("Batch size: ",configuration.batch_size)
# print the statistics for each batch
file.writelines("Batch Statistics:\n")
for i in range(0, len(tags), configuration.batch_size):
check(img0_paths[i:i+configuration.batch_size], img1_paths[i:i+configuration.batch_size], tags[i:i+configuration.batch_size])
file.writelines("\n")
batch_acc = []
correct_predict = false_predict = 0
for i in trange(0, len(tags), configuration.batch_size):
img0, img1 = get_images(img0_paths[i: i+configuration.batch_size], img1_paths[i: i+configuration.batch_size])
predictions = loaded_model.predict([img0,img1])
# as the model has two outputs
# 1st output is auxiliary output
# 2nd output is the final output of the model
# the predictions list contains two values for each pair. Each value corresponding to the auxiliary output and final output respectively.
# So considering only the final output
predictions = predictions[1]
# calculate accuracy
accuracy, correct, wrong = calc_accuracy(tags[i: i+configuration.batch_size], predictions, configuration.model_threshold)
batch_acc.append(accuracy)
correct_predict += correct
false_predict += wrong
# write the results to the file
for i in range(0,len(batch_acc)):
print("batch {} | size_of_batch {} | acc {}".format(i,configuration.batch_size,batch_acc[i]))
file.writelines("batch {} | size_of_batch {} | acc {}\n".format(i,configuration.batch_size,batch_acc[i]))
print(f"\ntotal images {correct_predict + false_predict} | correct predict {correct_predict} | false predict {false_predict}")
file.writelines(f"\ntotal images {correct_predict + false_predict} | correct predict {correct_predict} | false predict {false_predict}\n")
testing_acc.append(np.mean(batch_acc))
print("mean acc over {} batches: {}".format(len(batch_acc), np.mean(batch_acc)))
file.writelines("mean acc over {} batches: {}\n".format(len(batch_acc), np.mean(batch_acc)))
print("="*30)
file.writelines("="*30)
# write the mean accuracy over the multiple trials
print("="*30)
file.writelines("="*30)
print(f"Average accuracy over {configuration.trials} trials is {np.mean(testing_acc)}")
file.writelines(f"\n\nAverage accuracy over {configuration.trials} trials is {np.mean(testing_acc)}\n\n")
print("="*30)
file.writelines("="*60)
# close the file
file.close()