-
Notifications
You must be signed in to change notification settings - Fork 15
/
UNetAttention.py
497 lines (408 loc) · 19 KB
/
UNetAttention.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
from __future__ import print_function
import BatchDatsetReader as DataSetReader
import read_10k_data as fashion_parsing
import read_CFPD_data as ClothingParsing
import read_LIP_data as HumanParsing
import TensorflowUtils as Utils
import function_definitions as fd
import tensorflow as tf
# Hide the warning messages about CPU/GPU
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
DATA_SET = "10k"
# DATA_SET = "CFPD"
# DATA_SET = "LIP"
FLAGS = tf.flags.FLAGS
if DATA_SET == "10k":
tf.flags.DEFINE_integer("batch_size", "2", "batch size for training")
tf.flags.DEFINE_integer(
"training_epochs",
"50",
"number of epochs for training")
tf.flags.DEFINE_string("logs_dir", "logs/UNetAttention_10k/",
"path to logs directory")
tf.flags.DEFINE_string(
"data_dir", "D:/Datasets/Dressup10k/", "path to dataset")
if DATA_SET == "CFPD":
tf.flags.DEFINE_integer("batch_size", "38", "batch size for training")
tf.flags.DEFINE_integer(
"training_epochs",
"70",
"number of epochs for training")
tf.flags.DEFINE_string("logs_dir", "logs/UNet_CFPD/",
"path to logs directory")
tf.flags.DEFINE_string(
"data_dir", "D:/Datasets/CFPD/", "path to dataset")
if DATA_SET == "LIP":
tf.flags.DEFINE_integer("batch_size", "40", "batch size for training")
tf.flags.DEFINE_integer(
"training_epochs",
"30",
"number of epochs for training")
tf.flags.DEFINE_string("logs_dir", "logs/UNet_LIP/",
"path to logs directory")
tf.flags.DEFINE_string(
"data_dir", "D:/Datasets/LIP/", "path to dataset")
tf.flags.DEFINE_float(
"learning_rate",
"1e-4",
"Learning rate for Adam Optimizer")
tf.flags.DEFINE_string("model_dir", "Model_zoo/", "Path to vgg model mat")
tf.flags.DEFINE_bool('debug', "False", "Debug mode: True/ False")
tf.flags.DEFINE_string('mode', "train", "Mode train/ test/ visualize")
# tf.flags.DEFINE_string('mode', "test", "Mode train/ test/ visualize")
# tf.flags.DEFINE_string('mode', "visualize", "Mode train/ test/ visualize")
# tf.flags.DEFINE_string('mode', "predonly", "Mode train/ test/ visualize")
# tf.flags.DEFINE_string('mode', "fulltest", "Mode train/ test/ visualize")
MAX_ITERATION = int(1e5 + 1001)
NUM_OF_CLASSES = 18 # Upper-lower cloth parsing # Dressup 10k
DISPLAY_STEP = 300
if DATA_SET == "CFPD":
NUM_OF_CLASSES = 23 # Fashion parsing 23 # CFPD
if DATA_SET == "LIP":
NUM_OF_CLASSES = 20 # human parsing # LIP
# IMAGE_SIZE = 224
IMAGE_SIZE = 384
TEST_DIR = FLAGS.logs_dir + "TestImage/"
VIS_DIR = FLAGS.logs_dir + "VisImage/"
"""
UNET
"""
def unetinference(image, keep_prob=0.5, is_training=False):
net = {}
l2_reg = FLAGS.learning_rate
# added for resume better
global_iter_counter = tf.Variable(0, name='global_step', trainable=False)
net['global_step'] = global_iter_counter
with tf.variable_scope("inference"):
inputs = image
teacher = tf.placeholder(
tf.float32, [
None, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CLASSES])
# 1, 1, 3
conv1_1 = Utils.conv(
inputs,
filters=64,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
conv1_2 = Utils.conv(
conv1_1,
filters=64,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
pool1 = Utils.pool(conv1_2)
# 1/2, 1/2, 64
conv2_1 = Utils.conv(
pool1,
filters=128,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
conv2_2 = Utils.conv(
conv2_1,
filters=128,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
pool2 = Utils.pool(conv2_2)
# 1/4, 1/4, 128
conv3_1 = Utils.conv(
pool2,
filters=256,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
conv3_2 = Utils.conv(
conv3_1,
filters=256,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
pool3 = Utils.pool(conv3_2)
# 1/8, 1/8, 256
conv4_1 = Utils.conv(
pool3,
filters=512,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
conv4_2 = Utils.conv(
conv4_1,
filters=512,
l2_reg_scale=l2_reg,
batchnorm_istraining=is_training)
pool4 = Utils.pool(conv4_2)
# 1/16, 1/16, 512
conv5_1 = Utils.conv(pool4, filters=1024, l2_reg_scale=l2_reg)
conv5_2 = Utils.conv(conv5_1, filters=1024, l2_reg_scale=l2_reg)
concated1 = tf.concat([Utils.conv_transpose(
conv5_2, filters=512, l2_reg_scale=l2_reg), conv4_2], axis=3)
conv_up1_1 = Utils.conv(concated1, filters=512, l2_reg_scale=l2_reg)
conv_up1_2 = Utils.conv(conv_up1_1, filters=512, l2_reg_scale=l2_reg)
concated2 = tf.concat([Utils.conv_transpose(
conv_up1_2, filters=256, l2_reg_scale=l2_reg), conv3_2], axis=3)
conv_up2_1 = Utils.conv(concated2, filters=256, l2_reg_scale=l2_reg)
conv_up2_2 = Utils.conv(conv_up2_1, filters=256, l2_reg_scale=l2_reg)
concated3 = tf.concat([Utils.conv_transpose(
conv_up2_2, filters=128, l2_reg_scale=l2_reg), conv2_2], axis=3)
conv_up3_1 = Utils.conv(concated3, filters=128, l2_reg_scale=l2_reg)
conv_up3_2 = Utils.conv(conv_up3_1, filters=128, l2_reg_scale=l2_reg)
concated4 = tf.concat([Utils.conv_transpose(
conv_up3_2, filters=64, l2_reg_scale=l2_reg), conv1_2], axis=3)
conv_up4_1 = Utils.conv(concated4, filters=64, l2_reg_scale=l2_reg)
conv_up4_2 = Utils.conv(conv_up4_1, filters=64, l2_reg_scale=l2_reg)
outputs = Utils.conv(
conv_up4_2, filters=NUM_OF_CLASSES, kernel_size=[
1, 1], activation=None)
annotation_pred = tf.argmax(outputs, dimension=3, name="prediction")
return tf.expand_dims(annotation_pred, dim=3), outputs, net, conv5_2
# return Model(inputs, outputs, teacher, is_training)
"""
Attention model
"""
def attention(scale_input, is_training=False):
l2_reg = FLAGS.learning_rate
dropout_ratio = 0
if is_training is True:
dropout_ratio = 0.5
conv1 = Utils.conv(scale_input, filters=512, l2_reg_scale=l2_reg)
conv1 = Utils.dropout(conv1, dropout_ratio)
conv2 = Utils.conv(conv1, filters=3, kernel_size=[1, 1], l2_reg_scale=l2_reg)
return conv2
"""inference
optimize with trainable parameters (Check which ones)
loss_val : loss operator (mean(
"""
def train(loss_val, var_list, global_step):
optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
grads = optimizer.compute_gradients(loss_val, var_list=var_list)
if FLAGS.debug:
# print(len(var_list))
for grad, var in grads:
Utils.add_gradient_summary(grad, var)
return optimizer.apply_gradients(grads, global_step=global_step)
def main(argv=None):
# 1. input placeholders
keep_probability = tf.placeholder(tf.float32, name="keep_probability")
image = tf.placeholder(
tf.float32,
shape=(
None,
IMAGE_SIZE,
IMAGE_SIZE,
3),
name="input_image")
annotation = tf.placeholder(
tf.int32,
shape=(
None,
IMAGE_SIZE,
IMAGE_SIZE,
1),
name="annotation")
# global_step = tf.Variable(0, trainable=False, name='global_step')
is_training = False
if FLAGS.mode == "train":
is_training = True
image075 = tf.image.resize_images(
image, [int(IMAGE_SIZE * 0.75), int(IMAGE_SIZE * 0.75)])
image050 = tf.image.resize_images(
image, [int(IMAGE_SIZE * 0.5), int(IMAGE_SIZE * 0.5)])
image125 = tf.image.resize_images(
image, [int(IMAGE_SIZE * 1.25), int(IMAGE_SIZE * 1.25)])
annotation075 = tf.cast(tf.image.resize_images(
annotation, [int(IMAGE_SIZE * 0.75), int(IMAGE_SIZE * 0.75)]), tf.int32)
annotation050 = tf.cast(tf.image.resize_images(
annotation, [int(IMAGE_SIZE * 0.5), int(IMAGE_SIZE * 0.5)]), tf.int32)
annotation125 = tf.cast(tf.image.resize_images(
annotation, [int(IMAGE_SIZE * 1.25), int(IMAGE_SIZE * 1.25)]), tf.int32)
# 2. construct inference network
reuse1 = False
reuse2 = True
with tf.variable_scope('', reuse=reuse1):
pred_annotation100, logits100, net100, att100 = unetinference(image, keep_probability, is_training=is_training)
with tf.variable_scope('', reuse=reuse2):
pred_annotation075, logits075, net075, att075 = unetinference(image075, keep_probability, is_training=is_training)
with tf.variable_scope('', reuse=reuse2):
pred_annotation050, logits050, net050, att050 = unetinference(image050, keep_probability, is_training=is_training)
with tf.variable_scope('', reuse=reuse2):
pred_annotation125, logits125, net125, att125 = unetinference(image125, keep_probability, is_training=is_training)
# apply attention model - train
score_final_train = None
score_final_test = None
final_annotation_pred_train = None
final_annotation_pred_test = None
train_op = None
reduced_loss = None
if FLAGS.mode == "train":
attn_input = []
attn_input.append(att100)
attn_input.append(tf.image.resize_images(att075, tf.shape(att100)[1:3, ]))
attn_input.append(tf.image.resize_images(att050, tf.shape(att100)[1:3, ]))
attn_input_train = tf.concat(attn_input, axis=3)
attn_output_train = attention(attn_input_train, is_training)
scale_att_mask = tf.nn.softmax(attn_output_train)
score_att_x = tf.multiply(logits100, tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 0], axis=3), tf.shape(logits100)[1:3, ]))
score_att_x_075 = tf.multiply(tf.image.resize_images(logits075, tf.shape(logits100)[1:3, ]), tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 1], axis=3), tf.shape(logits100)[1:3, ]))
score_att_x_050 = tf.multiply(tf.image.resize_images(logits050, tf.shape(logits100)[1:3, ]), tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 2], axis=3), tf.shape(logits100)[1:3, ]))
score_final_train = score_att_x + score_att_x_075 + score_att_x_050
final_annotation_pred = tf.expand_dims(tf.argmax(score_final_train, dimension=3, name="final_prediction"), dim=3)
final_annotation_pred_train = tf.reduce_mean(
tf.stack([tf.cast(final_annotation_pred, tf.float32), tf.cast(pred_annotation100, tf.float32),
tf.image.resize_images(pred_annotation075,
tf.shape(pred_annotation100)[1:3, ]),
tf.image.resize_images(pred_annotation050,
tf.shape(pred_annotation100)[1:3, ])]),
axis=0)
# 3. loss measure
loss = tf.reduce_mean(
(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=score_final_train,
labels=tf.squeeze(
annotation,
squeeze_dims=[3]),
name="entropy")))
tf.summary.scalar("entropy", loss)
loss100 = tf.reduce_mean(
(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits100,
labels=tf.squeeze(
annotation,
squeeze_dims=[3]),
name="entropy")))
tf.summary.scalar("entropy", loss100)
loss075 = tf.reduce_mean(
(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits075,
labels=tf.squeeze(
annotation075,
squeeze_dims=[3]),
name="entropy")))
tf.summary.scalar("entropy", loss075)
loss050 = tf.reduce_mean(
(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits050,
labels=tf.squeeze(
annotation050,
squeeze_dims=[3]),
name="entropy")))
tf.summary.scalar("entropy", loss050)
reduced_loss = loss + loss100 + loss075 + loss050
# 4. optimizing
trainable_var = tf.trainable_variables()
if FLAGS.debug:
for var in trainable_var:
Utils.add_to_regularization_and_summary(var)
train_op = train(reduced_loss, trainable_var, net100['global_step'])
else:
# apply attention model - test
attn_input = []
attn_input.append(att100)
attn_input.append(tf.image.resize_images(att075, tf.shape(att100)[1:3, ]))
attn_input.append(tf.image.resize_images(att125, tf.shape(att100)[1:3, ]))
attn_input_test = tf.concat(attn_input, axis=3)
attn_output_test = attention(attn_input_test, is_training)
scale_att_mask = tf.nn.softmax(attn_output_test)
score_att_x = tf.multiply(logits100, tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 0], axis=3),
tf.shape(logits100)[1:3, ]))
score_att_x_075 = tf.multiply(tf.image.resize_images(logits075, tf.shape(logits100)[1:3, ]),
tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 1], axis=3),
tf.shape(logits100)[1:3, ]))
score_att_x_125 = tf.multiply(tf.image.resize_images(logits125, tf.shape(logits100)[1:3, ]),
tf.image.resize_images(tf.expand_dims(scale_att_mask[:, :, :, 2], axis=3),
tf.shape(logits100)[1:3, ]))
score_final_test = score_att_x + score_att_x_075 + score_att_x_125
final_annotation_pred = tf.expand_dims(tf.argmax(score_final_test, dimension=3, name="final_prediction"), dim=3)
final_annotation_pred_test = tf.reduce_mean(tf.stack([tf.cast(final_annotation_pred, tf.float32), tf.cast(pred_annotation100, tf.float32),
tf.image.resize_images(pred_annotation075,
tf.shape(pred_annotation100)[1:3, ]),
tf.image.resize_images(pred_annotation125,
tf.shape(pred_annotation100)[1:3, ])]),
axis=0)
tf.summary.image("input_image", image, max_outputs=3)
tf.summary.image(
"ground_truth",
tf.cast(
annotation,
tf.uint8),
max_outputs=3)
tf.summary.image(
"pred_annotation",
tf.cast(
pred_annotation100,
tf.uint8),
max_outputs=3)
print("Setting up summary op...")
summary_op = tf.summary.merge_all()
print("Setting up image reader from ", FLAGS.data_dir, "...")
print("data dir:", FLAGS.data_dir)
train_records, valid_records = fashion_parsing.read_dataset(FLAGS.data_dir)
test_records = None
if DATA_SET == "CFPD":
train_records, valid_records, test_records = ClothingParsing.read_dataset(
FLAGS.data_dir)
print("test_records length :", len(test_records))
if DATA_SET == "LIP":
train_records, valid_records = HumanParsing.read_dataset(
FLAGS.data_dir)
print("train_records length :", len(train_records))
print("valid_records length :", len(valid_records))
print("Setting up dataset reader")
train_dataset_reader = None
validation_dataset_reader = None
test_dataset_reader = None
image_options = {'resize': True, 'resize_size': IMAGE_SIZE}
if FLAGS.mode == 'train':
train_dataset_reader = DataSetReader.BatchDatset(
train_records, image_options)
validation_dataset_reader = DataSetReader.BatchDatset(
valid_records, image_options)
if DATA_SET == "CFPD":
test_dataset_reader = DataSetReader.BatchDatset(
test_records, image_options)
if FLAGS.mode == 'visualize':
validation_dataset_reader = DataSetReader.BatchDatset(
valid_records, image_options)
if FLAGS.mode == 'test' or FLAGS.mode == 'crftest' or FLAGS.mode == 'predonly' or FLAGS.mode == "fulltest":
if DATA_SET == "CFPD":
test_dataset_reader = DataSetReader.BatchDatset(
test_records, image_options)
else:
test_dataset_reader = DataSetReader.BatchDatset(
valid_records, image_options)
test_records = valid_records
sess = tf.Session()
print("Setting up Saver...")
saver = tf.train.Saver()
summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, sess.graph)
# 5. parameter setup
# 5.1 init params
sess.run(tf.global_variables_initializer())
# 5.2 restore params if possible
ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
print("Model restored...")
# 6. train-mode
if FLAGS.mode == "train":
fd.mode_train(sess, FLAGS, net100, train_dataset_reader, validation_dataset_reader, train_records,
final_annotation_pred_train,
image, annotation, keep_probability, score_final_train, train_op, reduced_loss, summary_op, summary_writer,
saver, DISPLAY_STEP)
# test-random-validation-data mode
elif FLAGS.mode == "visualize":
fd.mode_visualize(sess, FLAGS, VIS_DIR, validation_dataset_reader,
final_annotation_pred_test, image, annotation, keep_probability, NUM_OF_CLASSES)
# test-full-validation-dataset mode
elif FLAGS.mode == "test":
fd.mode_new_test(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
final_annotation_pred_test, image, annotation, keep_probability, score_final_test, NUM_OF_CLASSES)
# fd.mode_test(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
# pred_annotation, image, annotation, keep_probability, logits, NUM_OF_CLASSES)
elif FLAGS.mode == "crftest":
fd.mode_predonly(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
final_annotation_pred_test, image, annotation, keep_probability, score_final_test, NUM_OF_CLASSES)
elif FLAGS.mode == "predonly":
fd.mode_predonly(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
final_annotation_pred_test, image, annotation, keep_probability, score_final_test, NUM_OF_CLASSES)
elif FLAGS.mode == "fulltest":
fd.mode_full_test(sess, FLAGS, TEST_DIR, test_dataset_reader, test_records,
final_annotation_pred_test, image, annotation, keep_probability, score_final_test, NUM_OF_CLASSES)
sess.close()
if __name__ == "__main__":
tf.app.run()