forked from beopst/dcn.tf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
136 lines (103 loc) · 5.1 KB
/
train.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
'''
Dynamic Capacity Networks (http://arxiv.org/abs/1511.07838) implementation using TensorFlow and slim library.
This code is using the cluttered MNITS dataset, which can be obtained in https://github.com/deepmind/mnist-cluttered.
Author: Sangheum Hwang
'''
from __future__ import absolute_import
from __future__ import division
import tensorflow as tf
import numpy as np
import time
from slim import slim
from models import dcn
import mnist_input
from random import shuffle
################### Parameters #######################
BATCH_SIZE = 64
NUM_EPOCHS = 100
NUM_EPOCHS_PER_DECAY = 5
INITIAL_LEARNING_RATE = 0.001
LEARNING_RATE_DECAY = 0.5
HINT_WEIGHT = 0.0001
######################################################
def error_rate(predictions, labels):
return 100.0 - (100.0 * np.sum(np.argmax(predictions,1)==labels) /
predictions.shape[0])
def train():
trn_data = mnist_input.load_data('data/mnist-cluttered/train')
n_trn = len(trn_data)
val_data = mnist_input.load_data('data/mnist-cluttered/valid', shuffle=False)
val_data_x, val_data_y = zip(*val_data)
n_val = len(val_data)
step_counter = tf.Variable(0)
num_batches_for_epoch = int(np.ceil(n_trn/BATCH_SIZE))
decay_steps = int(num_batches_for_epoch * NUM_EPOCHS_PER_DECAY)
with tf.device('/gpu:1'):
trn_x = tf.placeholder(tf.float32, shape=(BATCH_SIZE, 100, 100, 1))
trn_y = tf.placeholder(tf.int32, shape=(BATCH_SIZE,))
val_x = tf.placeholder(tf.float32, shape=(50, 100, 100, 1))
logits, hint_loss = dcn.inference(trn_x)
dcn.loss(logits,trn_y,batch_size=BATCH_SIZE)
#slim doesn't keep regularization losses in the LOSSES_COLLECTION
#see line 203 in slim/variables.py
total_loss = tf.add_n(tf.get_collection(slim.losses.LOSSES_COLLECTION) +
tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
batchnorm_updates = tf.get_collection(slim.ops.UPDATE_OPS_COLLECTION)
batchnorm_updates_op = tf.group(*batchnorm_updates)
lr = tf.train.exponential_decay(INITIAL_LEARNING_RATE,
step_counter, decay_steps,
LEARNING_RATE_DECAY,
staircase=True)
#optimizer = tf.train.MomentumOptimizer(lr,0.9).minimize(total_loss,
# global_step=step_counter)
optimizer = tf.train.AdamOptimizer(lr)
top_vars = slim.variables.get_variables('top_layers')
fine_vars = slim.variables.get_variables('fine_layers')
coarse_vars = slim.variables.get_variables('coarse_layers')
optimizer = tf.train.AdamOptimizer(lr)
top_fine_grads = optimizer.compute_gradients(total_loss,
var_list=top_vars + fine_vars)
coarse_grads = optimizer.compute_gradients(total_loss + hint_loss * HINT_WEIGHT,
var_list=coarse_vars)
apply_gradients = optimizer.apply_gradients(top_fine_grads + coarse_grads, global_step=step_counter)
train_op = tf.group(apply_gradients, batchnorm_updates_op)
# create Session
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
summary_writer = tf.train.SummaryWriter('./trials',sess.graph)
tf.initialize_all_variables().run(session=sess)
eval_logits, eval_hint_loss = dcn.inference(val_x,is_training=False)
eval_prediction = tf.nn.softmax(eval_logits)
def _evaluate(data, sess):
size = data.shape[0]
predictions = np.ndarray(shape=(size,10),dtype=np.float32)
for begin in xrange(0, size, 50):
end = begin + 50
feed_dict = {val_x: data[begin:end,...]}
predictions[begin:end,:] = sess.run(eval_prediction, feed_dict=feed_dict)
return predictions
start_time = time.time()
for step in xrange(NUM_EPOCHS*num_batches_for_epoch+1):
if step % num_batches_for_epoch == 0:
# shuffle training data
shuffle(trn_data)
trn_data_x, trn_data_y = zip(*trn_data)
# validation
predictions = _evaluate(np.asarray(val_data_x,dtype=np.float32),sess)
eval_error = error_rate(predictions, np.asarray(val_data_y))
print '\nValidation error: %.2f\n' % eval_error
offset = (step * BATCH_SIZE) % (n_trn - BATCH_SIZE)
batch_x = np.asarray(trn_data_x[offset:(offset+BATCH_SIZE)],dtype=np.float32)
batch_y = np.asarray(trn_data_y[offset:(offset+BATCH_SIZE)],dtype=np.int32)
_, res_lr, res_loss, res_hint= sess.run([train_op,lr,total_loss, hint_loss],
feed_dict={trn_x:batch_x,trn_y:batch_y})
if step % 100 == 0:
elapsed_time = time.time() - start_time
start_time = time.time()
print 'Step %d (epoch %.2f), Elapsed: %.1f ms, LR: %.4f, Loss: %.4f Hint loss: %.4f' % \
(step, float(step)/num_batches_for_epoch, 1000*elapsed_time, res_lr, res_loss, res_hint)
import ipdb
ipdb.set_trace()
def main(argv=None):
train()
if __name__ == '__main__':
tf.app.run()