-
Notifications
You must be signed in to change notification settings - Fork 80
/
nobg_nodm.py
189 lines (154 loc) · 7.41 KB
/
nobg_nodm.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
import time
import os
import sys
import utils.realtime_renderer as rtr
from utils.tf_utils import *
class mv3d():
def __init__(self, sess):
self.sess = sess
self.batch_size = 64
self.image_shape = [128, 128, 3]
self.max_iter = 1000000
self.start_iter = 0
self.log_folder = "logs/nobg_nodm"
self.train_samples_folder = "samples/nobg_nodm/train"
self.test_samples_folder = "samples/nobg_nodm/test"
self.snapshots_folder = "snapshots/nobg_nodm"
self.rend = rtr.RealTimeRenderer(self.batch_size)
self.rend.load_model_names("data/cars_training.txt")
self.test_images1, self.test_images2,\
self.test_dm2, self.test_labels = load_test_set(True, False)
def buildModel(self):
self.images1 = tf.placeholder(tf.float32,
[self.batch_size] + self.image_shape,
name='input_images')
self.images2 = tf.placeholder(tf.float32,
[self.batch_size] + self.image_shape,
name='gt_images')
self.labels = tf.placeholder(tf.float32, [self.batch_size, 5],
name='labels')
# convolutional encoder
e0 = lrelu(conv2d_msra(self.images1, 32, 5, 5, 2, 2, "e0"))
e0_0 = lrelu(conv2d_msra(e0, 32, 5, 5, 1, 1, "e0_0"))
e1 = lrelu(conv2d_msra(e0_0, 32, 5, 5, 2, 2, "e1"))
e1_0 = lrelu(conv2d_msra(e1, 32, 5, 5, 1, 1, "e1_0"))
e2 = lrelu(conv2d_msra(e1_0, 64, 5, 5, 2, 2, "e2"))
e2_0 = lrelu(conv2d_msra(e2, 64, 5, 5, 1, 1, "e2_0"))
e3 = lrelu(conv2d_msra(e2_0, 128, 3, 3, 2, 2, "e3"))
e3_0 = lrelu(conv2d_msra(e3, 128, 3, 3, 1, 1, "e3_0"))
e4 = lrelu(conv2d_msra(e3_0, 256, 3, 3, 2, 2, "e4"))
e4_0 = lrelu(conv2d_msra(e4, 256, 3, 3, 1, 1, "e4_0"))
e4r = tf.reshape(e4_0, [self.batch_size, 4096])
e5 = lrelu(linear_msra(e4r, 4096, "fc1"))
# angle processing
a0 = lrelu(linear_msra(self.labels, 64, "a0"))
a1 = lrelu(linear_msra(a0, 64, "a1"))
a2 = lrelu(linear_msra(a1, 64, "a2"))
concated = tf.concat(1, [e5, a2])
# joint processing
a3 = lrelu(linear_msra(concated, 4096, "a3"))
a4 = lrelu(linear_msra(a3, 4096, "a4"))
a5 = lrelu(linear_msra(a4, 4096, "a5"))
a5r = tf.reshape(a5, [self.batch_size, 4, 4, 256])
# convolutional decoder
d4 = lrelu(deconv2d_msra(a5r, [self.batch_size, 8, 8, 128],
3, 3, 2, 2, "d4"))
d4_0 = lrelu(conv2d_msra(d4, 128, 3, 3, 1, 1, "d4_0"))
d3 = lrelu(deconv2d_msra(d4_0, [self.batch_size, 16, 16, 64],
3, 3, 2, 2, "d3"))
d3_0 = lrelu(conv2d_msra(d3, 64, 5, 5, 1, 1, "d3_0"))
d2 = lrelu(deconv2d_msra(d3_0, [self.batch_size, 32, 32, 32],
5, 5, 2, 2, "d2"))
d2_0 = lrelu(conv2d_msra(d2, 64, 5, 5, 1, 1, "d2_0"))
d1 = lrelu(deconv2d_msra(d2_0, [self.batch_size, 64, 64, 32],
5, 5, 2, 2, "d1"))
d1_0 = lrelu(conv2d_msra(d1, 32, 5, 5, 1, 1, "d1_0"))
self.gen = tf.nn.tanh(deconv2d_msra(d1_0,
[self.batch_size, 128, 128, 3],
5, 5, 2, 2, "d0"))
self.loss = euclidean_loss(self.gen, self.images2)
self.training_summ = tf.scalar_summary("training_loss", self.loss)
self.t_vars = tf.trainable_variables()
self.saver = tf.train.Saver(max_to_keep=20)
def restore(self):
self.start_iter = load_snapshot(self.saver, self.sess,
self.snapshots_folder)
def generate_sample_set(self, path, im1, im2, gen, iter_num):
save_images(gen, [8, 8], path + "/output_%s.png" % (iter_num))
save_images(np.array(im2), [8, 8],
path + '/tr_gt_%s.png' % (iter_num))
save_images(np.array(im1), [8, 8],
path + '/tr_input_%s.png' % (iter_num))
def test(self, global_iter):
self.test_iter = 19
sm_path = os.path.join(self.test_samples_folder,
str(global_iter).zfill(8))
if not os.path.exists(sm_path):
os.mkdir(sm_path)
local_loss = 0.0
for i in range(0, self.test_iter):
batch_images1 = self.test_images1[i*self.batch_size:
(i+1)*self.batch_size]
batch_images2 = self.test_images2[i*self.batch_size:
(i+1)*self.batch_size]
batch_labels = self.test_labels[i*self.batch_size:
(i+1)*self.batch_size]
output = self.sess.run([self.gen, self.loss],
feed_dict={
self.images1: batch_images1,
self.images2: batch_images2,
self.labels: batch_labels})
self.generate_sample_set(sm_path, batch_images1,
batch_images2, output[0], i)
local_loss += float(output[1])
total_loss = local_loss / self.test_iter
print("[i: %s] [test loss: %.6f]" %
(global_iter, total_loss))
if self.writer is not None:
log_value(self.writer, total_loss, 'test_loss', global_iter)
def train(self):
optim = tf.train.AdamOptimizer(
0.0001, beta1=0.9).minimize(
self.loss, var_list=self.t_vars)
self.writer = tf.train.SummaryWriter(
self.log_folder, self.sess.graph_def)
tf.global_variables_initializer().run()
self.restore()
for i in range(self.start_iter, self.max_iter):
iteration_start_time = time.time()
cl1, dm1, cl2, dm2, lb = self.rend.render(100.0)
output = self.sess.run(
[optim, self.loss, self.training_summ],
feed_dict={self.images1: cl1,
self.images2: cl2,
self.labels: lb})
if np.mod(i, 400) == 0:
self.test(i)
sm_path = os.path.join(self.train_samples_folder,
str(i).zfill(8))
if not os.path.exists(sm_path):
os.mkdir(sm_path)
sm = self.sess.run(self.gen,
feed_dict={
self.images1: cl1,
self.images2: cl2,
self.labels: lb})
self.generate_sample_set(sm_path, cl1, cl2, sm, i)
if np.mod(i, 5000) == 1:
save_snapshot(self.saver, self.sess,
self.snapshots_folder, i)
if np.mod(i, 10) == 0:
summ_str = output[2]
self.writer.add_summary(summ_str, i)
print("[i: %s] [time: %s] [global time: %s] [train loss: %.6f]" %
(i, time.time() - iteration_start_time,
time.time() - global_start_time, output[1]))
global_start_time = time.time()
with tf.Session() as sess:
net = mv3d(sess)
net.buildModel()
# ---TEST---
# net.restore()
# net.test(0)
# ---TRAIN---
net.train()