-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathsqueezenet_demo.py
181 lines (152 loc) · 5.69 KB
/
squeezenet_demo.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
"""
Usage:
1. training
python squeezenet_demo.py --action='train'\
-p /home/db/train -v /home/db/validation
2. prediction
python squeezenet_demo.py --action='predice'\
-p /db/Roasted-Broccoli-Pasta-Recipe-5-683x1024.jpg
"""
import time
import json
import argparse
import model as km
from simdat.core import dp_tools
from keras.optimizers import Adam
from keras.optimizers import SGD
dp = dp_tools.DP()
def parse_json(fname):
"""Parse the input profile
@param fname: input profile path
@return data: a dictionary with user-defined data for training
"""
with open(fname) as data_file:
data = json.load(data_file)
return data
def write_json(data, fname='./output.json'):
"""Write data to json
@param data: object to be written
Keyword arguments:
fname -- output filename (default './output.json')
"""
with open(fname, 'w') as fp:
json.dump(data, fp, cls=NumpyAwareJSONEncoder)
def print_time(t0, s):
"""Print how much time has been spent
@param t0: previous timestamp
@param s: description of this step
"""
print("%.5f seconds to %s" % ((time.time() - t0), s))
return time.time()
def main():
parser = argparse.ArgumentParser(
description="SqueezeNet example."
)
parser.add_argument(
"--batch-size", type=int, default=32, dest='batchsize',
help="Size of the mini batch. Default: 32."
)
parser.add_argument(
"--action", type=str, default='train',
help="Action to be performed, train/predict"
)
parser.add_argument(
"--epochs", type=int, default=20,
help="Number of epochs, default 20."
)
parser.add_argument(
"--lr", type=float, default=0.001,
help="Learning rate of SGD, default 0.001."
)
parser.add_argument(
"--epsilon", type=float, default=1e-8,
help="Epsilon of Adam epsilon, default 1e-8."
)
parser.add_argument(
"-p", "--path", type=str, default='.', required=True,
help="Path where the images are. Default: $PWD."
)
parser.add_argument(
"-v", "--val-path", type=str, default='.',
dest='valpath', help="Path where the val images are. Default: $PWD."
)
parser.add_argument(
"--img-width", type=int, default=224, dest='width',
help="Rows of the images, default: 224."
)
parser.add_argument(
"--img-height", type=int, default=224, dest='height',
help="Columns of the images, default: 224."
)
parser.add_argument(
"--channels", type=int, default=3,
help="Channels of the images, default: 3."
)
args = parser.parse_args()
sgd = SGD(lr=args.lr, decay=0.0002, momentum=0.9)
t0 = time.time()
if args.action == 'train':
train_generator = dp.train_data_generator(
args.path, args.width, args.height)
validation_generator = dp.val_data_generator(
args.valpath, args.width, args.height)
classes = train_generator.class_indices
nb_train_samples = train_generator.samples
nb_val_samples = validation_generator.samples
print("[squeezenet_demo] N training samples: %i " % nb_train_samples)
print("[squeezenet_demo] N validation samples: %i " % nb_val_samples)
nb_class = train_generator.num_class
print('[squeezenet_demo] Total classes are %i' % nb_class)
t0 = print_time(t0, 'initialize data')
model = km.SqueezeNet(
nb_class, inputs=(args.channels, args.height, args.width))
# dp.visualize_model(model)
t0 = print_time(t0, 'build the model')
model.compile(
optimizer=sgd, loss='categorical_crossentropy',
metrics=['accuracy'])
t0 = print_time(t0, 'compile model')
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=args.epochs,
validation_data=validation_generator,
nb_val_samples=nb_val_samples)
t0 = print_time(t0, 'train model')
model.save_weights('./weights.h5', overwrite=True)
model_parms = {'nb_class': nb_class,
'nb_train_samples': nb_train_samples,
'nb_val_samples': nb_val_samples,
'classes': classes,
'channels': args.channels,
'height': args.height,
'width': args.width}
write_json(model_parms, fname='./model_parms.json')
t0 = print_time(t0, 'save model')
elif args.action == 'predict':
_parms = parse_json('./model_parms.json')
model = km.SqueezeNet(
_parms['nb_class'],
inputs=(_parms['channels'], _parms['height'], _parms['width']),
weights_path='./weights.h5')
dp.visualize_model(model)
model.compile(
optimizer=sgd, loss='categorical_crossentropy',
metrics=['accuracy'])
X_test, Y_test, classes, F = dp.prepare_data_test(
args.path, args.width, args.height)
t0 = print_time(t0, 'prepare data')
outputs = []
results = model.predict(
X_test, batch_size=args.batchsize, verbose=1)
classes = _parms['classes']
for i in range(0, len(F)):
_cls = results[i].argmax()
max_prob = results[i][_cls]
outputs.append({'input': F[i], 'max_probability': max_prob})
cls = [key for key in classes if classes[key] == _cls][0]
outputs[-1]['class'] = cls
print('[squeezenet_demo] %s: %s (%.2f)' % (F[i], cls, max_prob))
t0 = print_time(t0, 'predict')
if __name__ == '__main__':
main()