-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdeepstreet_predict.py
213 lines (165 loc) · 7.2 KB
/
deepstreet_predict.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
"""VGG16 model for Keras.
# Reference
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
"""
import warnings
from keras.applications import vgg16
from keras.models import Model
from keras import optimizers
from keras.layers import Input
from keras.layers import Conv2D
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D, GlobalMaxPooling2D
from keras.engine.topology import get_source_inputs
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.applications.imagenet_utils import _obtain_input_shape
from keras.layers.normalization import BatchNormalization
import os
import h5py
import cv2
import numpy as np
from os.path import join
from mcc_multiclass import multimcc, confusion_matrix
import argparse
import matplotlib.pyplot as plt
from timeit import default_timer as timer
start = timer()
class myArgumentParser(argparse.ArgumentParser):
def __init__(self, *args, **kwargs):
super(myArgumentParser, self).__init__(*args, **kwargs)
def convert_arg_line_to_args(self, line):
for arg in line.split():
if not arg.strip():
continue
if arg[0] == '#':
break
yield arg
def load_im2(paths, img_cols, img_rows):
'''Returns a list containing the loaded images from paths'''
l = []
if K.image_data_format() == 'channels_first': #theano
for name in paths:
#print(name)
im2 = cv2.resize(cv2.imread(name), (img_cols, img_rows)).astype(np.float32)
#print(im2.shape)
# 'RGB'->'BGR'
im2 = im2[::-1, :, :]
# Zero-center by mean pixel
im2 -= np.mean(im2)
im2 = im2.transpose((2,0,1))
l.append(im2)
elif K.image_data_format() == 'channels_last': #tensorflow
for name in paths:
#print(name)
im2 = cv2.resize(cv2.imread(name), (img_cols, img_rows)).astype(np.float32)
# 'RGB'->'BGR'
im2 = im2[:, :, ::-1]
# Zero-center by mean pixel
im2 -= np.mean(im2)
l.append(im2)
return l
def main():
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'
parser = myArgumentParser(description='Run a prediction experiment using pretrained VGG16, specified on the deepstreet DataSet.',
fromfile_prefix_chars='@')
parser.add_argument('--gpu', type=int, default=0, help='GPU Device (default: %(default)s)')
parser.add_argument('--output_dir', type=str, default="./experiment_output/",help='Output directory')
parser.add_argument('--input_dir', type=str, default="./",help='Input directory')
parser.add_argument('--debug', type=bool, default=False, help='Debug mode')
args = parser.parse_args()
GPU = args.gpu
OUTDIR = args.output_dir+"/"
INDIR = args.input_dir+"/"
DEBUG = args.debug
if not os.path.exists(OUTDIR):
os.makedirs(OUTDIR)
if DEBUG:
validation_data_dir = INDIR + "small_dataset/val/"
else:
#validation_data_dir = "dataset/val/"
validation_data_dir = INDIR + "val/"
if os.path.exists(INDIR + validation_data_dir + ".DS_Store"):
os.remove(INDIR + validation_data_dir + ".DS_Store")
#set dimensions of the images
img_rows, img_cols = 224, 224
if K.image_data_format() == 'channels_first':
shape_ord = (3, img_rows, img_cols)
else: # channel_last
shape_ord = (img_rows, img_cols, 3)
vgg16_model = vgg16.VGG16(weights=None, include_top=False, input_tensor=Input(shape_ord))
vgg16_model.summary()
#add last fully-connected layers
x = Flatten(input_shape=vgg16_model.output.shape)(vgg16_model.output)
x = Dense(4096, activation='relu', name='ft_fc1')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
predictions = Dense(43, activation='softmax')(x)
model = Model(inputs=vgg16_model.input, outputs=predictions)
#compile the model
model.compile(optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
loss='categorical_crossentropy', metrics=['accuracy'])
#load validation images and create labels list
validation_filenames = os.listdir(validation_data_dir)
validation_filenames.sort()
validation_images = []
validation_labels = []
for name in validation_filenames:
if name.endswith(".ppm"):
validation_images.append(validation_data_dir + name)
label = name.split("_")[0]
label_int = int(label)
labels_array = [0]*43
labels_array[label_int] = 1
validation_labels.append(labels_array)
else:
validation_filenames.remove(name)
print("Validation Filenames loaded.")
validation = np.array(load_im2(validation_images, img_cols, img_rows))
print("Validation images loaded.")
model.load_weights("experiment_output/vgg16_deepstreet_training1.h5")
predicted_labels = model.predict(validation)
print("Labels predicted.")
#write summary file
prediction_summary = open(OUTDIR + "vgg16_deepstreet_t_prediction_summary_deepstreet_v.txt", "w")
prediction_summary.write("\t".join(['FILENAME', 'REAL_LABEL', 'PREDICTED_LABELS']) + '\n')
predicted_labels_linear = []
validation_labels_linear = []
#make linear labels list
for lbl in validation_labels:
for i,val in enumerate(lbl):
if val == 1:
validation_labels_linear.append(i)
for i in range(len(predicted_labels)):
cls_prob = predicted_labels[i] #percentage of belonging for i image
predicted_label_index = np.argmax(cls_prob) #get the index of the class with higher probability
line = [validation_images[i], str(validation_labels_linear[i]), str(predicted_label_index), str(round(cls_prob[predicted_label_index],3))]
s = ""
for i in range(42):
s += "{}:{}; ".format(i,round(cls_prob[i],3))
#s += str(i) + ":" + str(round(cls_prob[i],3)) + "; "
s += "42:{}".format(round(cls_prob[42],3))
#s += "42:" + str(round(cls_prob[42],3))
line.append(s)
predicted_labels_linear.append(np.argmax(cls_prob))
prediction_summary.write(";".join(line) + "\n")
prediction_summary.flush()
validation_labels_linear = np.array(validation_labels_linear)
predicted_labels_linear = np.array(predicted_labels_linear)
#calculate MCC
MCC = multimcc(validation_labels_linear, predicted_labels_linear)
print(MCC)
prediction_summary.write("MCC = {}".format(MCC))
prediction_summary.flush()
prediction_summary.close()
#compute confusion matrix and save the image
conf_matrix = confusion_matrix(validation_labels_linear,predicted_labels_linear)[0]
plt.matshow(conf_matrix)
plt.colorbar()
plt.savefig("confusion_matrix.png")
end = timer()
print("Total time: ", end - start)
if __name__ == "__main__":
main()