-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagenet_alexnet.py
415 lines (303 loc) · 15.1 KB
/
imagenet_alexnet.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import argparse
import os
import sys
##############################################
parser = argparse.ArgumentParser()
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--lr', type=float, default=1e-2)
parser.add_argument('--eps', type=float, default=1.)
parser.add_argument('--dropout', type=float, default=0.5)
parser.add_argument('--act', type=str, default='relu')
parser.add_argument('--bias', type=float, default=0.)
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--dfa', type=int, default=0)
parser.add_argument('--sparse', type=int, default=0)
parser.add_argument('--rank', type=int, default=0)
parser.add_argument('--init', type=str, default="glorot_uniform")
parser.add_argument('--save', type=int, default=0)
parser.add_argument('--name', type=str, default="imagenet_alexnet")
parser.add_argument('--load', type=str, default=None)
args = parser.parse_args()
if args.gpu >= 0:
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=str(args.gpu)
exxact = 0
if exxact:
val_path = '/home/bcrafton3/Data_SSD/ILSVRC2012/val/'
train_path = '/home/bcrafton3/Data_SSD/ILSVRC2012/train/'
else:
val_path = '/usr/scratch/bcrafton/ILSVRC2012/val/'
train_path = '/usr/scratch/bcrafton/ILSVRC2012/train/'
val_labels = './imagenet_labels/validation_labels.txt'
train_labels = './imagenet_labels/train_labels.txt'
IMAGENET_MEAN = [123.68, 116.78, 103.94]
##############################################
import keras
import tensorflow as tf
import numpy as np
np.set_printoptions(threshold=1000)
from lib.Model import Model
from lib.Layer import Layer
from lib.ConvToFullyConnected import ConvToFullyConnected
from lib.FullyConnected import FullyConnected
from lib.Convolution import Convolution
from lib.MaxPool import MaxPool
from lib.Dropout import Dropout
from lib.FeedbackFC import FeedbackFC
from lib.FeedbackConv import FeedbackConv
from lib.Activation import Activation
from lib.Activation import Relu
##############################################
def in_top_k(x, y, k):
x = tf.cast(x, dtype=tf.float32)
y = tf.cast(y, dtype=tf.int32)
_, topk = tf.nn.top_k(input=x, k=k)
topk = tf.transpose(topk)
correct = tf.equal(y, topk)
correct = tf.cast(correct, dtype=tf.int32)
correct = tf.reduce_sum(correct, axis=0)
return correct
##############################################
# Preprocessing (for both training and validation):
# (1) Decode the image from jpg format
# (2) Resize the image so its smaller side is 256 pixels long
def parse_function(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string, channels=3) # (1)
image = tf.cast(image_decoded, tf.float32)
smallest_side = 256.0
height, width = tf.shape(image)[0], tf.shape(image)[1]
height = tf.to_float(height)
width = tf.to_float(width)
scale = tf.cond(tf.greater(height, width),
lambda: smallest_side / width,
lambda: smallest_side / height)
new_height = tf.to_int32(height * scale)
new_width = tf.to_int32(width * scale)
resized_image = tf.image.resize_images(image, [new_height, new_width]) # (2)
return resized_image, label
# Preprocessing (for training)
# (3) Take a random 227x227 crop to the scaled image
# (4) Horizontally flip the image with probability 1/2
# (5) Substract the per color mean `IMAGENET_MEAN`
# Note: we don't normalize the data here, as VGG was trained without normalization
def train_preprocess(image, label):
crop_image = tf.random_crop(image, [227, 227, 3]) # (3)
flip_image = tf.image.random_flip_left_right(crop_image) # (4)
means = tf.reshape(tf.constant(IMAGENET_MEAN), [1, 1, 3])
centered_image = flip_image - means # (5)
return centered_image, label
# Preprocessing (for validation)
# (3) Take a central 227x227 crop to the scaled image
# (4) Substract the per color mean `IMAGENET_MEAN`
# Note: we don't normalize the data here, as VGG was trained without normalization
def val_preprocess(image, label):
crop_image = tf.image.resize_image_with_crop_or_pad(image, 227, 227) # (3)
means = tf.reshape(tf.constant(IMAGENET_MEAN), [1, 1, 3])
centered_image = crop_image - means # (4)
return centered_image, label
##############################################
def get_validation_dataset():
label_counter = 0
validation_images = []
validation_labels = []
print ("building validation dataset")
for subdir, dirs, files in os.walk(val_path):
for file in files:
validation_images.append(os.path.join(val_path, file))
validation_images = sorted(validation_images)
validation_labels_file = open(val_labels)
lines = validation_labels_file.readlines()
for ii in range(len(lines)):
validation_labels.append(int(lines[ii]))
remainder = len(validation_labels) % args.batch_size
validation_images = validation_images[:(-remainder)]
validation_labels = validation_labels[:(-remainder)]
return validation_images, validation_labels
def get_train_dataset():
label_counter = 0
training_images = []
training_labels = []
f = open(train_labels, 'r')
lines = f.readlines()
labels = {}
for line in lines:
line = line.split(' ')
labels[line[0]] = label_counter
label_counter += 1
f.close()
print ("building train dataset")
for subdir, dirs, files in os.walk(train_path):
for folder in dirs:
for folder_subdir, folder_dirs, folder_files in os.walk(os.path.join(subdir, folder)):
for file in folder_files:
training_images.append(os.path.join(folder_subdir, file))
training_labels.append(labels[folder])
remainder = len(training_labels) % args.batch_size
training_images = training_images[:(-remainder)]
training_labels = training_labels[:(-remainder)]
return training_images, training_labels
###############################################################
filename = tf.placeholder(tf.string, shape=[None])
label = tf.placeholder(tf.int64, shape=[None])
###############################################################
val_imgs, val_labs = get_validation_dataset()
val_dataset = tf.data.Dataset.from_tensor_slices((filename, label))
val_dataset = val_dataset.shuffle(len(val_imgs))
val_dataset = val_dataset.map(parse_function, num_parallel_calls=4)
val_dataset = val_dataset.map(val_preprocess, num_parallel_calls=4)
val_dataset = val_dataset.batch(args.batch_size)
val_dataset = val_dataset.repeat()
val_dataset = val_dataset.prefetch(8)
###############################################################
train_imgs, train_labs = get_train_dataset()
train_dataset = tf.data.Dataset.from_tensor_slices((filename, label))
train_dataset = train_dataset.shuffle(len(train_imgs))
train_dataset = train_dataset.map(parse_function, num_parallel_calls=4)
train_dataset = train_dataset.map(train_preprocess, num_parallel_calls=4)
train_dataset = train_dataset.batch(args.batch_size)
train_dataset = train_dataset.repeat()
train_dataset = train_dataset.prefetch(8)
###############################################################
handle = tf.placeholder(tf.string, shape=[])
iterator = tf.data.Iterator.from_string_handle(handle, train_dataset.output_types, train_dataset.output_shapes)
features, labels = iterator.get_next()
features = tf.reshape(features, (-1, 227, 227, 3))
labels = tf.one_hot(labels, depth=1000)
train_iterator = train_dataset.make_initializable_iterator()
val_iterator = val_dataset.make_initializable_iterator()
###############################################################
if args.act == 'tanh':
act = Tanh()
elif args.act == 'relu':
act = Relu()
else:
assert(False)
###############################################################
weights_conv = './transfer/alexnet_weights.npy'
weights_fc = None
train_conv = weights_conv == None
train_fc = weights_fc == None
###############################################################
batch_size = tf.placeholder(tf.int32, shape=())
dropout_rate = tf.placeholder(tf.float32, shape=())
lr = tf.placeholder(tf.float32, shape=())
###############################################################
l0 = Convolution(input_shape=[batch_size, 227, 227, 3], filter_sizes=[11, 11, 3, 96], init=args.init, strides=[1,4,4,1], padding="VALID", activation=act, bias=args.bias, load=weights_conv, name='conv1', train=train_conv)
l1 = MaxPool(size=[batch_size, 55, 55, 96], ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="VALID")
l2 = FeedbackConv(size=[batch_size, 27, 27, 96], num_classes=1000, sparse=args.sparse, rank=args.rank, name='conv1_fb')
l3 = Convolution(input_shape=[batch_size, 27, 27, 96], filter_sizes=[5, 5, 96, 256], init=args.init, activation=act, bias=args.bias, load=weights_conv, name='conv2', train=train_conv)
l4 = MaxPool(size=[batch_size, 27, 27, 256], ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="VALID")
l5 = FeedbackConv(size=[batch_size, 13, 13, 256], num_classes=1000, sparse=args.sparse, rank=args.rank, name='conv2_fb')
l6 = Convolution(input_shape=[batch_size, 13, 13, 256], filter_sizes=[3, 3, 256, 384], init=args.init, activation=act, bias=args.bias, load=weights_conv, name='conv3', train=train_conv)
l7 = FeedbackConv(size=[batch_size, 13, 13, 384], num_classes=1000, sparse=args.sparse, rank=args.rank, name='conv3_fb')
l8 = Convolution(input_shape=[batch_size, 13, 13, 384], filter_sizes=[3, 3, 384, 384], init=args.init, activation=act, bias=args.bias, load=weights_conv, name='conv4', train=train_conv)
l9 = FeedbackConv(size=[batch_size, 13, 13, 384], num_classes=1000, sparse=args.sparse, rank=args.rank, name='conv4_fb')
l10 = Convolution(input_shape=[batch_size, 13, 13, 384], filter_sizes=[3, 3, 384, 256], init=args.init, activation=act, bias=args.bias, load=weights_conv, name='conv5', train=train_conv)
l11 = MaxPool(size=[batch_size, 13, 13, 256], ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding="VALID")
l12 = FeedbackConv(size=[batch_size, 6, 6, 256], num_classes=1000, sparse=args.sparse, rank=args.rank, name='conv5_fb')
l13 = ConvToFullyConnected(input_shape=[6, 6, 256])
l14 = FullyConnected(input_shape=6*6*256, size=4096, init=args.init, activation=act, bias=args.bias, load=weights_fc, name='fc1', train=train_fc)
l15 = Dropout(rate=dropout_rate)
l16 = FeedbackFC(size=[6*6*256, 4096], num_classes=1000, sparse=args.sparse, rank=args.rank, name='fc1_fb')
l17 = FullyConnected(input_shape=4096, size=4096, init=args.init, activation=act, bias=args.bias, load=weights_fc, name='fc2', train=train_fc)
l18 = Dropout(rate=dropout_rate)
l19 = FeedbackFC(size=[4096, 4096], num_classes=1000, sparse=args.sparse, rank=args.rank, name='fc2_fb')
l20 = FullyConnected(input_shape=4096, size=1000, init=args.init, bias=args.bias, load=weights_fc, name='fc3', train=train_fc)
###############################################################
model = Model(layers=[l0, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17, l18, l19, l20])
predict = tf.nn.softmax(model.predict(X=features))
weights = model.get_weights()
if args.dfa:
grads_and_vars = model.dfa_gvs(X=features, Y=labels)
else:
grads_and_vars = model.gvs(X=features, Y=labels)
train = tf.train.AdamOptimizer(learning_rate=lr, epsilon=args.eps).apply_gradients(grads_and_vars=grads_and_vars)
correct = tf.equal(tf.argmax(predict,1), tf.argmax(labels,1))
total_correct = tf.reduce_sum(tf.cast(correct, tf.float32))
top5 = in_top_k(predict, tf.argmax(labels,1), k=5)
total_top5 = tf.reduce_sum(tf.cast(top5, tf.float32))
###############################################################
config = tf.ConfigProto(allow_soft_placement=True)
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
sess.run(tf.global_variables_initializer())
train_handle = sess.run(train_iterator.string_handle())
val_handle = sess.run(val_iterator.string_handle())
###############################################################
results_filename = args.name + '.results'
f = open(results_filename, "w")
f.write(results_filename + "\n")
f.write("total params: " + str(model.num_params()) + "\n")
f.close()
###############################################################
train_accs = []
train_accs_top5 = []
val_accs = []
val_accs_top5 = []
phase = 0
lr_decay = args.lr
for ii in range(args.epochs):
sess.run(train_iterator.initializer, feed_dict={filename: train_imgs, label: train_labs})
train_total = 0.0
train_correct = 0.0
train_top5 = 0.0
for j in range(0, len(train_imgs), args.batch_size):
[_total_correct, _top5, _] = sess.run([total_correct, total_top5, train], feed_dict={handle: train_handle, batch_size: args.batch_size, dropout_rate: args.dropout, lr: lr_decay})
train_total += args.batch_size
train_correct += _total_correct
train_top5 += _top5
train_acc = train_correct / train_total
train_acc_top5 = train_top5 / train_total
if (j % (1000 * args.batch_size) == 0):
p = "train accuracy: %f %f" % (train_acc, train_acc_top5)
print (p)
f = open(results_filename, "a")
f.write(p + "\n")
f.close()
train_accs.append(train_acc)
train_accs_top5.append(train_acc_top5)
##################################################################
sess.run(val_iterator.initializer, feed_dict={filename: val_imgs, label: val_labs})
val_total = 0.0
val_correct = 0.0
val_top5 = 0.0
for j in range(0, len(val_imgs), args.batch_size):
[_total_correct, _top5] = sess.run([total_correct, total_top5], feed_dict={handle: val_handle, batch_size: args.batch_size, dropout_rate: 0.0, lr: 0.0})
val_total += args.batch_size
val_correct += _total_correct
val_top5 += _top5
val_acc = val_correct / val_total
val_acc_top5 = val_top5 / val_total
if (j % (1000 * args.batch_size) == 0):
p = "val accuracy: %f %f" % (val_acc, val_acc_top5)
print (p)
f = open(results_filename, "a")
f.write(p + "\n")
f.close()
val_accs.append(val_acc)
val_accs_top5.append(val_acc_top5)
if phase == 0:
phase = 1
print ('phase 1')
elif phase == 1:
dacc = val_accs[-1] - val_accs[-2]
if dacc <= 0.01:
lr_decay = 0.1 * args.lr
phase = 2
print ('phase 2')
elif phase == 2:
dacc = val_accs[-1] - val_accs[-2]
if dacc <= 0.005:
lr_decay = 0.05 * args.lr
phase = 3
print ('phase 3')
if args.save:
[w] = sess.run([weights], feed_dict={handle: val_handle, dropout_rate: 0.0, learning_rate: 0.0})
w['train_acc'] = train_accs
w['train_acc_top5'] = train_accs_top5
w['val_acc'] = val_accs
w['val_acc_top5'] = val_accs_top5
np.save(args.name, w)
print('epoch %d/%d' % (ii, args.epochs))