-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
372 lines (269 loc) · 10.1 KB
/
main.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
# coding: utf-8
# Comparative Evaluation of Feature Descriptors Through
# Bag of Visual Features with Support Vector Machine
# on Embedded GPU System
# Imports
from timer import Timer
import argparse
import cv2 as cv
import datasets
import features
import globals
import svm
import os
import outputs
import save_figures
import save_models
import sys
import time
# Initializing global variables
globals.initialize()
# Message from usage
message = '''main.py [-h]
--detector {SIFT, SURF, KAZE, ORB, BRISK, AKAZE}
--descriptor {SIFT, SURF, KAZE, BRIEF, ORB, BRISK, AKAZE, FREAK}
--dataset {MNIST, JAFFE, Extended-CK+, FEI, CIFAR-10, FER-2013}'''
# Create the parser
parser = argparse.ArgumentParser(description = 'Evaluating a Bag of Visual Features (BoVF) approach extracting ' +
'Features for Recognition and Classification Task through SVM Classifier.',
usage = message)
# Argument --detector
parser.add_argument('--detector',
action = 'store',
choices = ['SIFT', 'SURF', 'KAZE', 'ORB', 'BRISK', 'AKAZE'],
required = True,
metavar = '',
dest = 'detector',
help = 'select the detector to be used in this experiment')
# Argument --descriptor
parser.add_argument('--descriptor',
action = 'store',
choices = ['SIFT', 'SURF', 'KAZE', 'BRIEF', 'ORB', 'BRISK', 'AKAZE', 'FREAK'],
required = True,
metavar = '',
dest = 'descriptor',
help = 'select the descriptor to be used in this experiment')
# Argument --dataset
parser.add_argument('--dataset',
action = 'store',
choices = ['MNIST', 'JAFFE', 'Extended-CK+', 'FEI', 'CIFAR-10', 'FER-2013'],
required = True,
metavar = '',
dest = 'dataset',
help = 'select the visual dataset to be used in this experiment')
# Execute the parse_args() method
arguments = parser.parse_args()
# File name
filename = 'Outputs/Datasets/%s/%s-outputs.txt' % (arguments.dataset, arguments.descriptor)
# Open File to write
outputs.openFile(filename = filename)
# Initiate Detector and Descriptor
# Initiate detector selected
if arguments.detector == 'SIFT':
globals.detector = features.SIFT()
elif arguments.detector == 'SURF':
globals.detector = features.SURF()
elif arguments.detector == 'KAZE':
globals.detector = features.SIFT()
elif arguments.detector == 'ORB':
globals.detector = features.ORB()
elif arguments.detector == 'BRISK':
globals.detector = features.BRISK()
elif arguments.detector == 'AKAZE':
globals.detector = features.AKAZE()
# Print detector
features.printDetector()
# Initiate descriptor selected
if arguments.descriptor == 'SIFT':
globals.descriptor = features.SIFT()
elif arguments.descriptor == 'SURF':
globals.descriptor = features.SURF()
elif arguments.descriptor == 'KAZE':
globals.descriptor = features.SIFT()
elif arguments.descriptor == 'BRIEF':
globals.descriptor = features.BRIEF()
elif arguments.descriptor == 'ORB':
globals.descriptor = features.ORB()
elif arguments.descriptor == 'BRISK':
globals.descriptor = features.BRISK()
elif arguments.descriptor == 'AKAZE':
globals.descriptor = features.AKAZE()
elif arguments.descriptor == 'FREAK':
globals.descriptor = features.FREAK()
# Print descriptor
features.printDescriptor()
# Path of Dataset
if arguments.dataset == 'MNIST':
globals.data_train_images, globals.data_test_images = datasets.MNIST()
elif arguments.dataset == 'JAFFE':
globals.data_train_images, globals.data_test_images = datasets.JAFFE()
elif arguments.dataset == 'Extended-CK+':
globals.data_train_images, globals.data_test_images = datasets.extendedCK()
elif arguments.dataset == 'FEI':
globals.data_train_images, globals.data_test_images = datasets.FEI()
elif arguments.dataset == 'CIFAR-10':
globals.data_train_images, globals.data_test_images = datasets.CIFAR10()
elif arguments.dataset == 'FER-2013':
globals.data_train_images, globals.data_test_images = datasets.FER2013()
# Print training-set path
datasets.printTrainingPath()
# Print test-set path
datasets.printTestPath()
# Print number of classes
datasets.printNumberOfClasses()
# K-Means classes
globals.K = globals.num_classes * 5
# Print
print('\nExtract Features\n')
# Extract Features
all = {}
# Print
print('Extract Features\n', file = globals.file)
object = sorted(os.listdir(globals.data_train_images))
i, total = 0, len(object)
with Timer() as timer:
for subject in object:
i += 1
print('Processing the subdirectory named:', subject, '\t[', i , '/', total, ']', file = globals.file)
# Read in cropped data
crop_names = os.listdir(os.path.join(globals.data_train_images, subject))
crop_names = list(map(lambda x: os.path.join(globals.data_train_images, subject, x), crop_names))
crops = [cv.imread(x , cv.IMREAD_GRAYSCALE) for x in crop_names]
# Get Features
desc = features.extractFeatures(crops, features.features)
all[subject] = desc
print('Extracted', arguments.descriptor, '\n', file = globals.file)
print('Time:', timer, '\n', file = globals.file)
# Print
print('Done!\n')
# Print
print('Create Bag of Visual Features\n')
# Features
matrix = features.groupAllFeatures(all)
kmeans = None
# Print
print('Create Bag of Visual Features\n', file = globals.file)
# Train K-Means
print('Training', arguments.descriptor, 'K-Means\n', file = globals.file)
with Timer() as timer:
kmeans = features.trainKMeans(matrix)
print('Time:', timer, '\n', file = globals.file)
# Print
print('Done!\n')
# Print
print('Prepare Training Data\n')
# Training Data
globals.train_feature_vec = [[], []]
print(arguments.descriptor, 'Training Data\n', file = globals.file)
object = sorted(os.listdir(globals.data_train_images))
i, total = 0, len(object)
with Timer() as timer:
for subject in object:
i += 1
print('Processing the subdirectory named:', subject, '\t[', i , '/', total, ']\n', file = globals.file)
# Get Features
histograms = features.generateHistograms(all[subject], kmeans)
globals.train_feature_vec[0].extend(histograms)
globals.train_feature_vec[1].extend([subject] * len(histograms))
print('Time:', timer, '\n', file = globals.file)
# Print
print('Done!\n')
# Print
print('Prepare Testing Data\n')
# Testing Data
globals.test_feature_vec = [[], []]
print(arguments.descriptor, 'Testing Data\n', file = globals.file)
object = sorted(os.listdir(globals.data_test_images))
i, total = 0, len(object)
with Timer() as timer:
for subject in object:
i += 1
print('Processing the subdirectory named:', subject, '\t[', i , '/', total, ']\n', file = globals.file)
crop_names = os.listdir(os.path.join(globals.data_test_images, subject))
crop_names = list(map(lambda x: os.path.join(globals.data_test_images, subject, x), crop_names))
crops = [cv.imread(x , cv.IMREAD_GRAYSCALE) for x in crop_names ]
# Get Features
desc = features.extractFeatures(crops, features.features)
# Get Histograms
histograms = features.generateHistograms(desc, kmeans)
globals.test_feature_vec[0].extend(histograms)
globals.test_feature_vec[1].extend([subject] * len(histograms))
print('Time:', timer, '\n', file = globals.file)
# Print
print('Done!\n')
# Print
print('Training Support Vector Machine Model\n')
# Train SVM Model
print('Training %s SVM Models\n' % arguments.descriptor, file = globals.file)
# SVM Model
SVM = svm.train(gama = 0.001,
descriptor_name = arguments.descriptor,
model_name = 'SVM')
# Print
print('Done!\n')
# Print
print('Testing Support Vector Machine Model\n')
# Test SVM Model
print('Testing %s SVM Model\n' % arguments.descriptor, file = globals.file)
# SVM Model
SVM_predict = svm.test(model = SVM,
descriptor_name = arguments.descriptor,
model_name = 'SVM')
# Print
print('Done!\n')
# Print
print('Classification Report\n')
# Print
print('Classification Report\n', file = globals.file)
# SVM Model
svm.classificationReport(model = SVM,
predict = SVM_predict,
descriptor_name = arguments.descriptor,
model_name = 'SVM')
# Print
print('Done!\n')
# Print
print('Saving Confusion Matrix\n')
# Save Confusion Matrix figure SVM Model
save_figures.confusionMatrix(predict = SVM_predict,
descriptor_name = arguments.descriptor,
dataset_name = arguments.dataset,
model_name = 'SVM')
# Print
print('Done!\n')
# Print
print('Saving Bag of Visual Features\n')
# Save Bag of Visual Features
save_models.saveBoVF(kmeans = kmeans,
descriptor_name = arguments.descriptor,
dataset_name = arguments.dataset)
# Print
print('Done!\n')
# Print
print('Saving Descriptors\n')
# Save Training Descriptors
save_models.saveDescriptors(feature_vector = globals.train_feature_vec,
descriptor_name = arguments.descriptor,
dataset_name = arguments.dataset,
flag = 'train')
# Save Test Descriptors
save_models.saveDescriptors(feature_vector = globals.test_feature_vec,
descriptor_name = arguments.descriptor,
dataset_name = arguments.dataset,
flag = 'test')
# Print
print('Done!\n')
# Print
print('Saving SVM Models\n')
# SVM Model
save_models.saveSVM(model = SVM,
descriptor_name = arguments.descriptor,
dataset_name = arguments.dataset,
model_name = 'SVM')
# Print
print('Done!\n')
# Close File
outputs.closeFile()
# Print
print('BoVF with SVM Classifier executed with success!')