forked from WeiTang114/MVCNN-TensorFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
189 lines (142 loc) · 7.18 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
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
import numpy as np
import os,sys,inspect
import tensorflow as tf
import time
from datetime import datetime
import os
import hickle as hkl
import os.path as osp
from glob import glob
import sklearn.metrics as metrics
from input import Dataset
import globals as g_
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import model
FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_string('train_dir', osp.dirname(sys.argv[0]) + '/tmp/',
"""Directory where to write event logs """
"""and checkpoint.""")
tf.app.flags.DEFINE_boolean('log_device_placement', False,
"""Whether to log device placement.""")
tf.app.flags.DEFINE_string('weights', '',
"""finetune with a pretrained model""")
tf.app.flags.DEFINE_string('caffemodel', '',
"""finetune with a model converted by caffe-tensorflow""")
np.set_printoptions(precision=3)
def train(dataset_train, dataset_val, ckptfile='', caffemodel=''):
print 'train() called'
is_finetune = bool(ckptfile)
V = g_.NUM_VIEWS
batch_size = FLAGS.batch_size
dataset_train.shuffle()
dataset_val.shuffle()
data_size = dataset_train.size()
print 'training size:', data_size
with tf.Graph().as_default():
startstep = 0 if not is_finetune else int(ckptfile.split('-')[-1])
global_step = tf.Variable(startstep, trainable=False)
# placeholders for graph input
view_ = tf.placeholder('float32', shape=(None, V, 227, 227, 3), name='im0')
y_ = tf.placeholder('int64', shape=(None), name='y')
keep_prob_ = tf.placeholder('float32')
# graph outputs
fc8 = model.inference_multiview(view_, g_.NUM_CLASSES, keep_prob_)
loss = model.loss(fc8, y_)
train_op = model.train(loss, global_step, data_size)
prediction = model.classify(fc8)
# build the summary operation based on the F colection of Summaries
summary_op = tf.summary.merge_all()
# must be after merge_all_summaries
validation_loss = tf.placeholder('float32', shape=(), name='validation_loss')
validation_summary = tf.summary.scalar('validation_loss', validation_loss)
validation_acc = tf.placeholder('float32', shape=(), name='validation_accuracy')
validation_acc_summary = tf.summary.scalar('validation_accuracy', validation_acc)
saver = tf.train.Saver(tf.all_variables(), max_to_keep=1000)
init_op = tf.global_variables_initializer()
sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement))
if is_finetune:
# load checkpoint file
saver.restore(sess, ckptfile)
print 'restore variables done'
elif caffemodel:
# load caffemodel generated with caffe-tensorflow
sess.run(init_op)
model.load_alexnet_to_mvcnn(sess, caffemodel)
print 'loaded pretrained caffemodel:', caffemodel
else:
# from scratch
sess.run(init_op)
print 'init_op done'
summary_writer = tf.summary.FileWriter(FLAGS.train_dir,
graph=sess.graph)
step = startstep
for epoch in xrange(100):
print 'epoch:', epoch
for batch_x, batch_y in dataset_train.batches(batch_size):
step += 1
start_time = time.time()
feed_dict = {view_: batch_x,
y_ : batch_y,
keep_prob_: 0.5 }
_, pred, loss_value = sess.run(
[train_op, prediction, loss,],
feed_dict=feed_dict)
duration = time.time() - start_time
assert not np.isnan(loss_value), 'Model diverged with loss = NaN'
# print training information
if step % 10 == 0 or step - startstep <= 30:
sec_per_batch = float(duration)
print '%s: step %d, loss=%.2f (%.1f examples/sec; %.3f sec/batch)' \
% (datetime.now(), step, loss_value,
FLAGS.batch_size/duration, sec_per_batch)
# validation
if step % g_.VAL_PERIOD == 0:# and step > 0:
val_losses = []
predictions = np.array([])
val_y = []
for val_step, (val_batch_x, val_batch_y) in \
enumerate(dataset_val.sample_batches(batch_size, g_.VAL_SAMPLE_SIZE)):
val_feed_dict = {view_: val_batch_x,
y_ : val_batch_y,
keep_prob_: 1.0 }
val_loss, pred = sess.run([loss, prediction], feed_dict=val_feed_dict)
val_losses.append(val_loss)
predictions = np.hstack((predictions, pred))
val_y.extend(val_batch_y)
val_loss = np.mean(val_losses)
acc = metrics.accuracy_score(val_y[:predictions.size], np.array(predictions))
print '%s: step %d, validation loss=%.4f, acc=%f' %\
(datetime.now(), step, val_loss, acc*100.)
# validation summary
val_loss_summ = sess.run(validation_summary,
feed_dict={validation_loss: val_loss})
val_acc_summ = sess.run(validation_acc_summary,
feed_dict={validation_acc: acc})
summary_writer.add_summary(val_loss_summ, step)
summary_writer.add_summary(val_acc_summ, step)
summary_writer.flush()
if step % 100 == 0:
# print 'running summary'
summary_str = sess.run(summary_op, feed_dict=feed_dict)
summary_writer.add_summary(summary_str, step)
summary_writer.flush()
if step % g_.SAVE_PERIOD == 0 and step > startstep:
checkpoint_path = os.path.join(FLAGS.train_dir, 'model.ckpt')
saver.save(sess, checkpoint_path, global_step=step)
def main(argv):
st = time.time()
print 'start loading data'
listfiles_train, labels_train = read_lists(g_.TRAIN_LOL)
listfiles_val, labels_val = read_lists(g_.VAL_LOL)
dataset_train = Dataset(listfiles_train, labels_train, subtract_mean=False, V=g_.NUM_VIEWS)
dataset_val = Dataset(listfiles_val, labels_val, subtract_mean=False, V=g_.NUM_VIEWS)
print 'done loading data, time=', time.time() - st
train(dataset_train, dataset_val, FLAGS.weights, FLAGS.caffemodel)
def read_lists(list_of_lists_file):
listfile_labels = np.loadtxt(list_of_lists_file, dtype=str).tolist()
listfiles, labels = zip(*[(l[0], int(l[1])) for l in listfile_labels])
return listfiles, labels
if __name__ == '__main__':
main(sys.argv)