-
Notifications
You must be signed in to change notification settings - Fork 7
/
flyingChairsTrain.py
312 lines (263 loc) · 18.4 KB
/
flyingChairsTrain.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
import os,sys
from flyingChairsLoader import flyingChairsLoader
import flyingChairsWrapFlow
import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np
import random
import subprocess
import cv2
import math
import utils as utils
tf.app.flags.DEFINE_string('train_log_dir', '/tmp/inception_v3_v2/',
'Directory where to write event logs.')
tf.app.flags.DEFINE_integer('batch_size', 4, 'The number of images in each batch.')
tf.app.flags.DEFINE_integer('overwrite', True, 'Overwrite existing directory.')
tf.app.flags.DEFINE_integer('save_interval_epoch', 18,
'The frequency with which the model is saved, in epoch.')
tf.app.flags.DEFINE_integer('max_number_of_steps', 10000000,
'The maximum number of gradient steps.')
tf.app.flags.DEFINE_float('learning_rate', 0.000016, 'The learning rate')
tf.app.flags.DEFINE_float('learning_rate_decay_factor', 0.5,
"""Learning rate decay factor.""")
tf.app.flags.DEFINE_float('num_epochs_per_decay', 18,
"""Number of epochs after which learning rate decays.""")
tf.app.flags.DEFINE_string('master', 'local',
'BNS name of the TensorFlow master to use.')
FLAGS = tf.app.flags.FLAGS
class train:
'''Pipeline for training
'''
def __init__(self, data_path, image_size):
self.image_size = image_size
self.origin_size = [384, 512]
self.numLosses = 6
self.lambda_smooth = 1
self.flyingChairs = flyingChairsLoader(data_path, self.image_size)
self.batch_size = FLAGS.batch_size
self.maxEpochs = 110
self.maxIterPerEpoch = int(math.floor(len(self.flyingChairs.trainList)/self.batch_size))
self.trainNet(self.batch_size)
def downloadModel(self, modelUrl):
subprocess.call(["wget %s" % modelUrl], shell=True)
def load_VGG16_weights(self, weight_file, sess):
weights = np.load(weight_file)
keys = sorted(weights.keys())
cutLayerNum = [2,3,6,7,12,13,18,19,24,25]
offNum = 0
for i, k in enumerate(keys):
if i <= 25:
if i in cutLayerNum:
offNum += 1
print i, k, np.shape(weights[k]), "not included in deep3D model"
else:
if i == 0:
sess.run(self.VGG_init_vars[i-offNum].assign(np.repeat(weights[k],2,axis=2)))
print i, k, np.shape(np.repeat(weights[k],2,axis=2))
else:
sess.run(self.VGG_init_vars[i-offNum].assign(weights[k]))
print i, k, np.shape(weights[k])
def load_deconv_weights(self, var, sess):
f_shape = sess.run(var).shape
width = f_shape[0]
heigh = f_shape[0]
f = math.ceil(width/2.0)
c = (2 * f - 1 - f % 2) / (2.0 * f)
bilinear = np.zeros([f_shape[0], f_shape[1]])
for x in range(width):
for y in range(heigh):
value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
bilinear[x, y] = value
weights = np.zeros(f_shape)
for i in range(f_shape[2]):
weights[:, :, i, i] = bilinear
sess.run(var.assign(weights))
def trainNet(self, batch_size):
if not os.path.isdir(FLAGS.train_log_dir):
os.makedirs(FLAGS.train_log_dir, mode=0777)
with tf.device('/gpu:0'):
source_img = tf.placeholder(tf.float32, [self.batch_size, self.image_size[0], self.image_size[1], 3])
target_img = tf.placeholder(tf.float32, [self.batch_size, self.image_size[0], self.image_size[1], 3])
loss_weight = tf.placeholder(tf.float32, [self.numLosses])
loss, midFlows, previous = flyingChairsWrapFlow.inception_v3(source_img, target_img, loss_weight)
print('Finished building Network.')
# Calculating the number of params inside a network
model_vars = tf.trainable_variables()
total_parameters = 0
for varCount in model_vars:
# shape is an array of tf.Dimension
shape = varCount.get_shape()
# print(shape)
# print(len(shape))
variable_parametes = 1
for dim in shape:
variable_parametes *= dim.value
total_parameters += variable_parametes
print("Our FlowNet has %4.2fM number of parameters. " % (total_parameters/1000000.0))
init = tf.initialize_all_variables()
total_loss = slim.losses.get_total_loss(add_regularization_losses=False)
lr = FLAGS.learning_rate
learning_rate = tf.placeholder(tf.float32, shape=[])
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(total_loss)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.allow_soft_placement = True
sess = tf.Session(config=config)
sess.run(tf.initialize_all_variables())
# What about pre-traind initialized model params and deconv parms?
self.VGG_init_vars = [var for var in model_vars if (var.name).startswith('conv')]
self.deconv_bilinearInit_vars = [var for var in model_vars if (var.name).startswith('up')]
VGG16Init = False
bilinearInit = True
# Use pre-trained VGG16 model to initialize conv filters
if VGG16Init:
VGG16modelPath = "vgg16_weights.npz"
if not os.path.exists(VGG16modelPath):
modelUrl = "http://www.cs.toronto.edu/~frossard/vgg16/vgg16_weights.npz"
self.downloadModel(modelUrl)
self.load_VGG16_weights(VGG16modelPath, sess)
print("-----Done initializing conv filters with VGG16 pre-trained model------")
# Use bilinear upsampling to initialize deconv filters
if bilinearInit:
for var in self.deconv_bilinearInit_vars:
if "weights" in var.name:
self.load_deconv_weights(var, sess)
print("-----Done initializing deconv filters with bilinear upsampling------")
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(FLAGS.train_log_dir)
if ckpt and ckpt.model_checkpoint_path:
print("Restore from " + ckpt.model_checkpoint_path)
saver.restore(sess, ckpt.model_checkpoint_path)
display = 500 # number of iterations to display training log
# Loss weights schedule
weight_L = [16,8,4,2,1,1]
for epoch in xrange(1, self.maxEpochs+1):
print("Epoch %d: \r\n" % epoch)
print("Learning Rate %f: \r\n" % lr)
# 5558 max iterations
# print self.maxIterPerEpoch
for iteration in xrange(1, self.maxIterPerEpoch+1):
source, target, flow = self.flyingChairs.sampleTrain(self.batch_size, iteration)
# source_geo, target_geo = self.geoAugmentation(source, target, sess)
##################################################################
# Training
train_op.run(feed_dict = {source_img: source, target_img: target, loss_weight: weight_L, learning_rate: lr}, session = sess)
if iteration % display == 0:
losses, flows_all, loss_sum = sess.run([loss, midFlows, total_loss], feed_dict={source_img: source, target_img: target, loss_weight: weight_L})
print("---Train Batch(%d): Epoch %03d Iter %04d: Loss_sum %4.4f \r\n" % (self.batch_size, epoch, iteration, loss_sum))
print(" PhotometricLoss1 = %4.4f (* %2.4f = %2.4f loss)" % (losses[0]["Charbonnier_reconstruct"], weight_L[0], losses[0]["Charbonnier_reconstruct"] * weight_L[0]))
print(" PhotometricLoss2 = %4.4f (* %2.4f = %2.4f loss)" % (losses[1]["Charbonnier_reconstruct"], weight_L[1], losses[1]["Charbonnier_reconstruct"] * weight_L[1]))
print(" PhotometricLoss3 = %4.4f (* %2.4f = %2.4f loss)" % (losses[2]["Charbonnier_reconstruct"], weight_L[2], losses[2]["Charbonnier_reconstruct"] * weight_L[2]))
print(" PhotometricLoss4 = %4.4f (* %2.4f = %2.4f loss)" % (losses[3]["Charbonnier_reconstruct"], weight_L[3], losses[3]["Charbonnier_reconstruct"] * weight_L[3]))
print(" PhotometricLoss5 = %4.4f (* %2.4f = %2.4f loss)" % (losses[4]["Charbonnier_reconstruct"], weight_L[4], losses[4]["Charbonnier_reconstruct"] * weight_L[4]))
print(" PhotometricLoss6 = %4.4f (* %2.4f = %2.4f loss)" % (losses[5]["Charbonnier_reconstruct"], weight_L[5], losses[5]["Charbonnier_reconstruct"] * weight_L[5]))
print(" SmoothnessLossU1 = %4.4f (* %2.4f = %2.4f loss)" % (losses[0]["U_loss"], weight_L[0]*self.lambda_smooth, losses[0]["U_loss"] * weight_L[0]*self.lambda_smooth))
print(" SmoothnessLossU2 = %4.4f (* %2.4f = %2.4f loss)" % (losses[1]["U_loss"], weight_L[1]*self.lambda_smooth, losses[1]["U_loss"] * weight_L[1]*self.lambda_smooth))
print(" SmoothnessLossU3 = %4.4f (* %2.4f = %2.4f loss)" % (losses[2]["U_loss"], weight_L[2]*self.lambda_smooth, losses[2]["U_loss"] * weight_L[2]*self.lambda_smooth))
print(" SmoothnessLossU4 = %4.4f (* %2.4f = %2.4f loss)" % (losses[3]["U_loss"], weight_L[3]*self.lambda_smooth, losses[3]["U_loss"] * weight_L[3]*self.lambda_smooth))
print(" SmoothnessLossU5 = %4.4f (* %2.4f = %2.4f loss)" % (losses[4]["U_loss"], weight_L[4]*self.lambda_smooth, losses[4]["U_loss"] * weight_L[4]*self.lambda_smooth))
print(" SmoothnessLossU6 = %4.4f (* %2.4f = %2.4f loss)" % (losses[5]["U_loss"], weight_L[5]*self.lambda_smooth, losses[5]["U_loss"] * weight_L[5]*self.lambda_smooth))
print(" SmoothnessLossV1 = %4.4f (* %2.4f = %2.4f loss)" % (losses[0]["V_loss"], weight_L[0]*self.lambda_smooth, losses[0]["V_loss"] * weight_L[0]*self.lambda_smooth))
print(" SmoothnessLossV2 = %4.4f (* %2.4f = %2.4f loss)" % (losses[1]["V_loss"], weight_L[1]*self.lambda_smooth, losses[1]["V_loss"] * weight_L[1]*self.lambda_smooth))
print(" SmoothnessLossV3 = %4.4f (* %2.4f = %2.4f loss)" % (losses[2]["V_loss"], weight_L[2]*self.lambda_smooth, losses[2]["V_loss"] * weight_L[2]*self.lambda_smooth))
print(" SmoothnessLossV4 = %4.4f (* %2.4f = %2.4f loss)" % (losses[3]["V_loss"], weight_L[3]*self.lambda_smooth, losses[3]["V_loss"] * weight_L[3]*self.lambda_smooth))
print(" SmoothnessLossV5 = %4.4f (* %2.4f = %2.4f loss)" % (losses[4]["V_loss"], weight_L[4]*self.lambda_smooth, losses[4]["V_loss"] * weight_L[4]*self.lambda_smooth))
print(" SmoothnessLossV6 = %4.4f (* %2.4f = %2.4f loss)" % (losses[5]["V_loss"], weight_L[5]*self.lambda_smooth, losses[5]["V_loss"] * weight_L[5]*self.lambda_smooth))
assert not np.isnan(loss_sum).any(), 'Model diverged with loss = NaN'
if iteration % (display * 10) == 0:
print("Start evaluating......")
self.evaluateNet(epoch, iteration, weight_L, sess)
if epoch % FLAGS.num_epochs_per_decay == 0:
lr *= FLAGS.learning_rate_decay_factor
if epoch % FLAGS.save_interval_epoch == 0:
print("Save to " + FLAGS.train_log_dir + str(epoch) + '_model.ckpt')
saver.save(sess, FLAGS.train_log_dir + str(epoch) + '_model.ckpt')
def evaluateNet(self, epoch, trainIter, weight_L, sess):
# For Sintel, the batch size should be 7, so that all validation images are covered.
testBatchSize = 8
source_img = tf.placeholder(tf.float32, [testBatchSize, self.image_size[0], self.image_size[1], 3])
target_img = tf.placeholder(tf.float32, [testBatchSize, self.image_size[0], self.image_size[1], 3])
loss_weight = tf.placeholder(tf.float32, [self.numLosses])
# Don't know if this is safe to set all variables reuse=True
# But because of different batch size, I don't know how to evaluate the model on validation data
tf.get_variable_scope().reuse_variables()
# loss, midFlows, prev = flyingChairsWrapFlow.VGG16(source_img, target_img, loss_weight)
loss, midFlows, prev = flyingChairsWrapFlow.inception_v3(source_img, target_img, loss_weight, is_training=False)
maxTestIter = int(math.floor(len(self.flyingChairs.valList)/testBatchSize))
Loss1, Loss2, Loss3, Loss4, Loss5, Loss6 = 0,0,0,0,0,0
U_Loss1, U_Loss2, U_Loss3, U_Loss4, U_Loss5, U_Loss6 = 0,0,0,0,0,0
V_Loss1, V_Loss2, V_Loss3, V_Loss4, V_Loss5, V_Loss6 = 0,0,0,0,0,0
flow_1 = []
flow_gt = []
previous_img = []
# print weight_L
for iteration in xrange(1, maxTestIter+1):
testBatch = self.flyingChairs.sampleVal(testBatchSize, iteration)
source, target, flow = testBatch[0]
imgPath = testBatch[1][0]
losses, flows_all, prev_all = sess.run([loss, midFlows, prev], feed_dict={source_img: source, target_img: target, loss_weight: weight_L})
Loss1 += losses[0]["total"]
Loss2 += losses[1]["total"]
Loss3 += losses[2]["total"]
Loss4 += losses[3]["total"]
Loss5 += losses[4]["total"]
Loss6 += losses[5]["total"]
U_Loss1 += losses[0]["U_loss"]
U_Loss2 += losses[1]["U_loss"]
U_Loss3 += losses[2]["U_loss"]
U_Loss4 += losses[3]["U_loss"]
U_Loss5 += losses[4]["U_loss"]
U_Loss6 += losses[5]["U_loss"]
V_Loss1 += losses[0]["V_loss"]
V_Loss2 += losses[1]["V_loss"]
V_Loss3 += losses[2]["V_loss"]
V_Loss4 += losses[3]["V_loss"]
V_Loss5 += losses[4]["V_loss"]
U_Loss6 += losses[5]["V_loss"]
flow1_list = []
previous_img_list = []
for batch_idx in xrange(testBatchSize):
flowImg = flows_all[0][batch_idx,:,:,:]*2 # pr1 is still half of the final predicted flow value
flowImg = np.clip(flowImg, -300.0, 250.0) # 300 and 250 is the min and max of the flow value in training dataset
flow1_list.append(np.expand_dims(cv2.resize(flowImg, (self.origin_size[1], self.origin_size[0])), 0))
previous_img_list.append(np.expand_dims(cv2.resize(prev_all[batch_idx,:,:,:], (self.origin_size[1], self.origin_size[0])), 0))
flow_1.append(np.concatenate(flow1_list, axis=0))
previous_img.append(np.concatenate(previous_img_list, axis=0))
flow_gt.append(flow)
# Visualize
# if False:
if iteration % 10 == 0:
if epoch == 1: # save ground truth images and flow
gt_1 = source[0, :, :, :].squeeze()
cv2.imwrite(FLAGS.train_log_dir + self.flyingChairs.valList[imgPath] + "_img1.jpeg", gt_1)
gt_2 = target[0, :, :, :].squeeze()
cv2.imwrite(FLAGS.train_log_dir + self.flyingChairs.valList[imgPath] + "_img2.jpeg", gt_2)
GTflowColor = utils.flowToColor(flow[0,:,:,:].squeeze())
cv2.imwrite(FLAGS.train_log_dir + self.flyingChairs.valList[imgPath] + "_gt_flow.jpeg", GTflowColor)
flowColor_1 = utils.flowToColor(flow_1[iteration-1][0,:,:,:].squeeze())
# print flowColor.max(), flowColor.min(), flowColor.mean()
cv2.imwrite(FLAGS.train_log_dir + str(epoch) + "_" + str(iteration) + "_" + str(trainIter) + "_flowColor_1" + ".jpeg", flowColor_1)
prev_frame = previous_img[iteration-1][0,:,:,:]
intensity_range = np.max(prev_frame, axis=None) - np.min(prev_frame, axis=None)
# save predicted next frames
prev_frame = (prev_frame - np.min(prev_frame, axis=None)) * 255 / intensity_range
cv2.imwrite(FLAGS.train_log_dir + str(epoch) + "_" + str(iteration) + "_" + str(trainIter) + "_prev_1" + ".jpeg", prev_frame.astype(int))
# Calculate endpoint error
f1 = np.concatenate(flow_1, axis=0)
f2 = np.concatenate(flow_gt, axis=0)
AEE = utils.flow_ee(f1, f2)
# calculate statistics
if epoch == 1:
print("***Test: max (flow_gt) %2.4f abs_mean (flow_gt) %2.4f \r\n"
% (np.amax(f2, axis=None), np.mean(np.absolute(f2), axis=None)))
print("***Test flow abs_mean: pr1 %2.4f pr2 %2.4f pr3 %2.4f pr4 %2.4f pr5 %2.4f pr6 %2.4f"
% (np.mean(np.absolute(flows_all[0]), axis=None), np.mean(np.absolute(flows_all[1]), axis=None), np.mean(np.absolute(flows_all[2]), axis=None),
np.mean(np.absolute(flows_all[3]), axis=None), np.mean(np.absolute(flows_all[4]), axis=None), np.mean(np.absolute(flows_all[5]), axis=None)))
print("***Test flow max: pr1 %2.4f pr2 %2.4f pr3 %2.4f pr4 %2.4f pr5 %2.4f pr6 %2.4f"
% (np.max(np.absolute(flows_all[0]), axis=None), np.max(np.absolute(flows_all[1]), axis=None), np.max(np.absolute(flows_all[2]), axis=None),
np.max(np.absolute(flows_all[3]), axis=None), np.max(np.absolute(flows_all[4]), axis=None), np.max(np.absolute(flows_all[5]), axis=None)))
Loss_sum = (Loss1*weight_L[0] + Loss2*weight_L[1] + Loss3*weight_L[2] + Loss4*weight_L[3] + Loss5*weight_L[4] + Loss6*weight_L[5])/maxTestIter
ULoss_sum = (U_Loss1*weight_L[0] + U_Loss2*weight_L[1] + U_Loss3*weight_L[2] + U_Loss4*weight_L[3] + U_Loss5*weight_L[4] + U_Loss6*weight_L[5])/maxTestIter*self.lambda_smooth
VLoss_sum = (V_Loss1*weight_L[0] + V_Loss2*weight_L[1] + V_Loss3*weight_L[2] + V_Loss4*weight_L[3] + V_Loss5*weight_L[4] + V_Loss6*weight_L[5])/maxTestIter*self.lambda_smooth
print("***Test: Epoch %03d Iter %04d: Loss_sum %4.4f ULoss_sum %4.4f VLoss_sum %4.4f AEE %4.4f \r\n"
% (epoch, trainIter, Loss_sum, ULoss_sum, VLoss_sum, AEE))