-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclassify_deep.py
More file actions
225 lines (172 loc) · 7.04 KB
/
classify_deep.py
File metadata and controls
225 lines (172 loc) · 7.04 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
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
"""
Extrack CNN codes from selected VGG16 layer,
Classify data with linear SVC.
"""
from __future__ import print_function
from preprocessor import utils
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from ConfigParser import SafeConfigParser
import argparse
import os, shutil
import h5py
import numpy as np
#Create command line arguments parser
ap = argparse.ArgumentParser()
ap.add_argument('-c', '--config', required=True, help='Path to the configuration file')
ap.add_argument('-e', '--extract', action='store_true',
help='Extract CNN codes from images')
ap.add_argument('-v', '--visualize', action='store_true',
help='Visualize CNN codes in 2 dimensions')
ap.add_argument('-t', '--train', action='store_true', help='Train and evaluate')
args = vars(ap.parse_args())
#Parse config file
parser = SafeConfigParser()
parser.read('config.ini')
#Execute if the script started with the -e option
if args['extract']:
#Loading keras is time consuming, so it may be reasonable
#to import it here, against the PEP
from deep_clf.features_extractor import VGG16_layer
#Local helper functions
def prepare_datasets(file_path, features_ds, features_dim):
data_file = h5py.File(file_path, mode='a')
data_file.create_dataset(features_ds,(0, features_dim),
maxshape=(None, features_dim),dtype='float')
return data_file
def get_file(source_path, destination_path, ds_name, dims):
if os.path.exists(destination_path):
file = h5py.File(destination_path, 'a')
else:
shutil.copyfile(source_path, destination_path)
file = prepare_datasets(destination_path, ds_name, dims)
return file
def extract_codes(data_array, batch_size=1000):
batches = utils.get_batches(data_array, batch_size)
for batch in batches:
print('[INFO] Processing next batch')
features = extractor.compute(batch)
utils.save_data(features, data_array)
#Get selected layer name
layer_name = parser.get('settings', 'vgg_layer')
#Create extractor instance
extractor = VGG16_layer(layer_name)
#Get CNN codes size
features_dim = extractor.model.get_layer(layer_name).output_shape[-1]
#Get setting and paths
vgg_training_path = parser.get('local_paths','training_vgg_features')
vgg_training_data = os.path.join(vgg_training_path, 'trn_vgg_{}.h5'.format(layer_name))
vgg_testing_path = parser.get('local_paths','testing_vgg_features')
vgg_testing_data = os.path.join(vgg_testing_path, 'test_vgg_{}.h5'.format(layer_name))
training_data = parser.get('local_paths', 'training_data')
testing_data = parser.get('local_paths', 'testing_data')
deep_ds = parser.get('datasets', 'deep')
data_ds = parser.get('datasets', 'data')
#--------------------#
#Process training data
#--------------------#
#Get training data file
trn_vgg_file = get_file(training_data, vgg_training_data, deep_ds, features_dim)
#Compute codes from selected layer
extract_codes(trn_vgg_file[data_ds])
trn_vgg_file.close()
#--------------------#
#Process testing data
#--------------------#
#Get training data file
test_vgg_file = get_file(testing_data, vgg_testing_data, deep_ds, features_dim)
#Compute codes from selected layer
extract_codes(test_vgg_file[data_ds])
test_vgg_file.close()
#Execute if the script started with the -v option
if args['visualize']:
#Get paths
layer_name = parser.get('settings', 'vgg_layer')
vgg_testing_path = parser.get('local_paths','testing_vgg_features')
vgg_testing_data = os.path.join(vgg_testing_path, 'test_vgg_{}.h5'.format(layer_name))
#Get data
data_file = h5py.File(vgg_testing_data, mode='r')
deep_ds = parser.get('datasets', 'deep')
labels_ds = parser.get('datasets', 'target')
data = data_file[deep_ds][:3000]
labels = data_file[labels_ds][:3000]
data_file.close()
#Reduce data dimensions using two techniques
reduced_tsne = TSNE(n_components=2, n_iter=1000).fit_transform(data)
pca = PCA(n_components=2)
reduced_pca = pca.fit_transform(data)
#Plot both representations of the data
plt.subplot(121)
plt.title('Dimensions reduced with TSNE')
plt.scatter(reduced_tsne[:,0], reduced_tsne[:,1], c=labels)
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.title('Dimensions reduced with PCA')
plt.scatter(reduced_pca[:,0], reduced_pca[:,1], c=labels)
plt.xticks([])
plt.yticks([])
plt.show()
#Execute if the script started with the -t option
if args['train']:
#Get paths
layer_name = parser.get('settings', 'vgg_layer')
vgg_training_path = parser.get('local_paths','training_vgg_features')
vgg_training_data = os.path.join(vgg_training_path, 'trn_vgg_{}.h5'.format(layer_name))
vgg_testing_path = parser.get('local_paths','testing_vgg_features')
vgg_testing_data = os.path.join(vgg_testing_path, 'test_vgg_{}.h5'.format(layer_name))
#Get data
deep_ds = parser.get('datasets', 'deep')
labels_ds = parser.get('datasets', 'target')
trn_vgg_file = h5py.File(vgg_training_data, mode='r')
test_vgg_file = h5py.File(vgg_testing_data, mode='r')
validation_size = 10000
(train_data, train_labels) = (trn_vgg_file[deep_ds][:-validation_size],
trn_vgg_file[labels_ds][:-validation_size])
(validation_data, validation_labels) = (trn_vgg_file[deep_ds][-validation_size:],
trn_vgg_file[labels_ds][-validation_size:])
(test_data, test_labels) = (test_vgg_file[deep_ds][:], test_vgg_file[labels_ds][:])
trn_vgg_file.close()
test_vgg_file.close()
#Create model with selected parameters
model = LinearSVC(random_state=42, C=0.001, dual=False)
#--------------#
#Initial trainig
#--------------#
#Train the model
model.fit(train_data, train_labels)
#Classify the validation data
predictions = model.predict(validation_data)
#Calculate the accuracy score
accuracy = accuracy_score(validation_labels, predictions)
print('Accuracy on the validation set: {}'.format(accuracy))
#-----------------------------------#
#Training on the whole dataset
#After selecting optimal parameters
#-----------------------------------#
#Train the model using the remaining data
model.fit(np.vstack([train_data,validation_data],
np.concatenate([train_labels, validation_labels]))
#Classify the testing data
predictions = model.predict(test_data)
#Calculate the accuracy score
accuracy = accuracy_score(test_labels, predictions)
print('Accuracy on the testing set: {}'.format(accuracy))
#-----------------------------------#
#Pseudo labelling
#-----------------------------------#
#Pseudo labelling
#Not tested due to RAM limitations
model.fit(np.vstack([train_data, validation_data, test_data]),
np.concatenate([train_labels, validation_labels, predictions])
)
final_predictions = model.predict(test_data)
#Calculate the accuracy score
accuracy = accuracy_score(test_labels, final_predictions)
print('Accuracy after pseudolabelling: {}'.format(accuracy))
clf_report = classification_report(test_labels, final_predictions)
report_path = parser.get('local_paths', 'reports')
with open(os.path.join(report_path, 'vgg_{}.txt'.format(layer_name)),'w') as file:
file.write(clf_report + '\n')
file.write('Accuracy: {}'.format(accuracy))