-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_images.py
294 lines (178 loc) · 8.28 KB
/
check_images.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
290
291
292
293
294
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# */AIPND/intropylab-classifying-images/check_images.py
#
# PROGRAMMER: zakaria boukernafa
# DATE CREATED: 22-09-2019
# REVISED DATE: <=(Date Revised - if any)
# REVISED DATE: 05/14/2018 - added import statement that imports the print
# functions that can be used to check the lab
# PURPOSE: Check images & report results: read them in, predict their
# content (classifier), compare prediction to actual value labels
# and output results
#
##
# Imports python modules
import argparse
from time import time, sleep
from os import listdir
from classifier import classifier
from print_functions_for_lab_checks import *
def main():
start_time = time()
in_arg = get_input_args()
check_command_line_arguments(in_arg)
answers_dic = get_pet_labels(in_arg.dir)
check_creating_pet_image_labels(answers_dic)
result_dic = classify_images(in_arg.dir, answers_dic, in_arg.arch)
check_classifying_images(result_dic)
adjust_results4_isadog(result_dic, in_arg.dogfile)
check_classifying_labels_as_dogs(result_dic)
results_stats_dic = calculates_results_stats(result_dic)
check_calculating_results(result_dic, results_stats_dic)
print_results(result_dic, results_stats_dic, in_arg.arch, True, True)
end_time = time()
tot_time = end_time - start_time
print("\n** Total Elapsed Runtime:",
str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":"
+str(int((tot_time%3600)%60)) )
# Functions defined below
def get_input_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dir', type=str, default='pet_images/',
help='path to folder of images')
parser.add_argument('--arch', type=str, default='vgg',
help='chosen model')
parser.add_argument('--dogfile', type=str, default='dognames.txt',
help='text file that has dognames')
return parser.parse_args()
def get_pet_labels(image_dir):
in_files = listdir(image_dir)
petlabels_dic = dict()
for idx in range(0, len(in_files), 1):
if in_files[idx][0] != ".":
image_name = in_files[idx].split("_")
pet_label = ""
for word in image_name:
if word.isalpha():
pet_label += word.lower() + " "
pet_label = pet_label.strip()
if in_files[idx] not in petlabels_dic:
petlabels_dic[in_files[idx]] = pet_label
else:
print("Warning: Duplicate files exist in directory",
in_files[idx])
return(petlabels_dic)
def classify_images(images_dir, petlabel_dic, model):
results_dic = dict()
for index in petlabel_dic:
model_label = classifier(images_dir+index, model)
model_label = model_label.lower()
model_label = model_label.strip()
truth = petlabel_dic[index]
found = model_label.find(truth)
if found >= 0:
if ( (found == 0 and len(truth)==len(model_label)) or
( ( (found == 0) or (model_label[found - 1] == " ") ) and
( (found + len(truth) == len(model_label)) or
(model_label[found + len(truth): found+len(truth)+1] in
(","," ") )
)
)
):
if index not in results_dic:
results_dic[index] = [truth, model_label, 1]
else:
if index not in results_dic:
results_dic[index] = [truth, model_label, 0]
else:
if index not in results_dic:
results_dic[index] = [truth, model_label, 0]
return(results_dic)
def adjust_results4_isadog(results_dic, dogsfile):
dognames_dic = dict()
with open(dogsfile, "r") as file_content:
line = file_content.readline()
while line != "":
line = line.rstrip()
if line not in dognames_dic:
dognames_dic[line] = 1
else:
print("**Warning: Duplicate dognames", line)
line = file_content.readline()
for index in results_dic:
if results_dic[index][0] in dognames_dic:
if results_dic[index][1] in dognames_dic:
results_dic[index].extend((1, 1))
else:
results_dic[index].extend((1, 0))
else:
if results_dic[index][1] in dognames_dic:
results_dic[index].extend((0, 1))
else:
results_dic[index].extend((0, 0))
def calculates_results_stats(results_dic):
results_stats=dict()
results_stats['n_dogs_img'] = 0
results_stats['n_match'] = 0
results_stats['n_correct_dogs'] = 0
results_stats['n_correct_notdogs'] = 0
results_stats['n_correct_breed'] = 0
for index in results_dic:
if results_dic[index][2] == 1:
results_stats['n_match'] += 1
if sum(results_dic[index][2:]) == 3:
results_stats['n_correct_breed'] += 1
if results_dic[index][3] == 1:
results_stats['n_dogs_img'] += 1
if results_dic[index][4] == 1:
results_stats['n_correct_dogs'] += 1
else:
if results_dic[index][4] == 0:
results_stats['n_correct_notdogs'] += 1
results_stats['n_images'] = len(results_dic)
results_stats['n_notdogs_img'] = (results_stats['n_images'] -
results_stats['n_dogs_img'])
results_stats['pct_match'] = (results_stats['n_match'] /
results_stats['n_images'])*100.0
results_stats['pct_correct_dogs'] = (results_stats['n_correct_dogs'] /
results_stats['n_dogs_img'])*100.0
results_stats['pct_correct_breed'] = (results_stats['n_correct_breed'] /
results_stats['n_dogs_img'])*100.0
if results_stats['n_notdogs_img'] > 0:
results_stats['pct_correct_notdogs'] = (results_stats['n_correct_notdogs'] /
results_stats['n_notdogs_img'])*100.0
else:
results_stats['pct_correct_notdogs'] = 0.0
return results_stats
def print_results(results_dic, results_stats, model,
print_incorrect_dogs = False, print_incorrect_breed = False):
print("\n\n*** Results Summary for CNN Model Architecture",model.upper(),
"***")
print("%20s: %3d" % ('N Images', results_stats['n_images']))
print("%20s: %3d" % ('N Dog Images', results_stats['n_dogs_img']))
print("%20s: %3d" % ('N Not-Dog Images', results_stats['n_notdogs_img']))
print(" ")
for index in results_stats:
if index[0] == "p":
print("%20s: %5.1f" % (index, results_stats[index]))
if (print_incorrect_dogs and
( (results_stats['n_correct_dogs'] + results_stats['n_correct_notdogs'])
!= results_stats['n_images'] )
):
print("\nINCORRECT Dog/NOT Dog Assignments:")
for index in results_dic:
if sum(results_dic[index][3:]) == 1:
print("Real: %-26s Classifier: %-30s" % (results_dic[index][0],
results_dic[index][1]))
if (print_incorrect_breed and
(results_stats['n_correct_dogs'] != results_stats['n_correct_breed'])
):
print("\nINCORRECT Dog Breed Assignment:")
for index in results_dic:
if ( sum(results_dic[index][3:]) == 2 and
results_dic[index][2] == 0 ):
print("Real: %-26s Classifier: %-30s" % (results_dic[index][0],
results_dic[index][1]))
if __name__ == "__main__":
main()