-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtrain.py
More file actions
351 lines (276 loc) · 10.8 KB
/
Copy pathtrain.py
File metadata and controls
351 lines (276 loc) · 10.8 KB
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
"""
Simple U-Net implementation in TensorFlow
Objective: detect vehicles
y = f(X)
X: image (640, 960, 3)
y: mask (640, 960, 1)
- binary image
- background is masked 0
- vehicle is masked 255
Loss function: maximize IOU
(intersection of prediction & grount truth)
-------------------------------
(union of prediction & ground truth)
Notes:
In the paper, the pixel-wise softmax was used.
But, I used the IOU because the datasets I used are
not labeled for segmentations
Original Paper:
https://arxiv.org/abs/1505.04597
"""
import argparse
import sys
import os
import datetime
import tensorflow as tf
import numpy as np
from six.moves import xrange
import matplotlib.pyplot as plt
from nets.unet import Unet_32_512, Unet_64_1024
# from _dataset.dataset_loader import DataLoader
from input_data import Data
from input_data import DataLoader
FLAGS = None
from utils.checkmate import BestCheckpointSaver
def IOU(y_pred, y_true):
"""Returns a (approx) batch_norm_wrapper score
intesection = y_pred.flatten() * y_true.flatten()
Then, IOU = 2 * intersection / (y_pred.sum() + y_true.sum() + 1e-7) + 1e-7
Args:
y_pred (4-D array): (N, H, W, 1)
y_true (4-D array): (N, H, W, 1)
Returns:
float: IOU score
"""
H, W, _ = y_pred.get_shape().as_list()[1:]
pred_flat = tf.reshape(y_pred, [-1, H * W])
true_flat = tf.reshape(y_true, [-1, H * W])
intersection = 2 * tf.reduce_sum(pred_flat * true_flat, axis=1) + 1e-7
denominator = tf.reduce_sum(pred_flat, axis=1) + \
tf.reduce_sum(true_flat, axis=1) + 1e-7
return tf.reduce_mean(intersection / denominator)
# smooth = 1.
# intersection = 2. * tf.reduce_sum(pred_flat * true_flat, axis=1) + smooth
# denominator = tf.reduce_sum(pred_flat, axis=1) + tf.reduce_sum(true_flat, axis=1) + smooth
# return tf.reduce_mean(intersection / denominator)
def get_start_epoch_number(latest_check_point):
chck = latest_check_point.split('-')
chck.reverse()
return int(chck[0]) + 1
def main(_):
# specify GPU
if FLAGS.gpu_index:
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = FLAGS.gpu_index
# We want to see all the logging messages for this tutorial.
tf.logging.set_verbosity(tf.logging.INFO)
tf.reset_default_graph()
X = tf.placeholder(tf.float32, shape=[None, FLAGS.img_size, FLAGS.img_size, 3], name="X")
GT = tf.placeholder(tf.float32, shape=[None, FLAGS.label_size, FLAGS.label_size, 1], name="GT")
mode = tf.placeholder(tf.bool, name="mode") # training or not
if FLAGS.use_64_channel:
pred = Unet_64_1024(X, mode, FLAGS)
else:
pred = Unet_32_512(X, mode, FLAGS)
tf.add_to_collection("inputs", X)
tf.add_to_collection("inputs", mode)
tf.add_to_collection("outputs", pred)
tf.summary.histogram("Predicted Mask", pred)
tf.summary.image("Predicted Mask", pred)
# IOU is
#
# (the area of intersection)
# --------------------------
# (the area of two boxes)
iou_op = IOU(pred, GT)
loss = -iou_op
tf.summary.scalar("loss", loss)
# Updates moving mean and moving variance for BatchNorm (train/inference)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
# other optimizer will be used
train_op = tf.train.MomentumOptimizer(0.001, 0.99).minimize(loss)
global_step = tf.train.get_or_create_global_step()
increment_global_step = tf.assign(global_step, global_step + 1)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
summary_op = tf.summary.merge_all()
train_summary_writer = tf.summary.FileWriter(FLAGS.logdir + '/train', sess.graph)
val_summary_writer = tf.summary.FileWriter(FLAGS.logdir + '/validation')
saver = tf.train.Saver()
# For, checkpoint saver
if FLAGS.best_train_dir:
best_ckpt_saver = BestCheckpointSaver(title='unet.ckpt', save_dir=FLAGS.best_train_dir, num_to_keep=3, maximize=True)
start_epoch = 1
epoch_from_ckpt = 0
if FLAGS.ckpt_path:
saver.restore(sess, FLAGS.ckpt_path)
tmp = FLAGS.ckpt_path
tmp = tmp.split('-')
tmp.reverse()
epoch_from_ckpt = int(tmp[0])
start_epoch = epoch_from_ckpt + 1
if epoch_from_ckpt != FLAGS.epochs + 1:
tf.logging.info('Training from epoch: %d ', start_epoch)
# Saving as Protocol Buffer (pb)
tf.train.write_graph(sess.graph_def,
FLAGS.train_dir,
'unet.pbtxt',
as_text=True)
############################
# Get data
############################
raw = Data(FLAGS.data_dir, FLAGS.validation_percentage)
tr_data = DataLoader(raw.data_dir,
raw.get_data('training'),
FLAGS.img_size,
FLAGS.label_size,
FLAGS.batch_size)
val_data = DataLoader(raw.data_dir,
raw.get_data('validation'),
FLAGS.img_size,
FLAGS.label_size,
FLAGS.batch_size)
iterator = tf.data.Iterator.from_structure(tr_data.dataset.output_types,
tr_data.dataset.output_shapes)
next_batch = iterator.get_next()
# Ops for initializing the two different iterators
tr_init_op = iterator.make_initializer(tr_data.dataset)
val_init_op = iterator.make_initializer(val_data.dataset)
tr_batches_per_epoch = int(tr_data.data_size / FLAGS.batch_size)
if tr_data.data_size % FLAGS.batch_size > 0:
tr_batches_per_epoch += 1
val_batches_per_epoch = int(val_data.data_size / FLAGS.batch_size)
if val_data.data_size % FLAGS.batch_size > 0:
val_batches_per_epoch += 1
############################
# Training
############################
print("{} Training start ... ".format(datetime.datetime.now()))
for epoch in xrange(start_epoch, FLAGS.epochs + 1):
print('{} Training epoch-{} start >> '.format(datetime.datetime.now(), epoch))
sess.run(tr_init_op)
for step in range(tr_batches_per_epoch):
X_train, y_train = sess.run(next_batch)
train_summary, accuracy, _, _ = \
sess.run([summary_op, iou_op, train_op, increment_global_step],
feed_dict={X: X_train,
GT: y_train,
mode: True}
)
train_summary_writer.add_summary(train_summary, (epoch-start_epoch)*tr_batches_per_epoch+step)
tf.logging.info('epoch #%d, step #%d/%d, accuracy(iou) %.5f%%' %
(epoch, step, tr_batches_per_epoch, accuracy))
print("{} Validation start ... ".format(datetime.datetime.now()))
total_val_accuracy = 0
val_count = 0
sess.run(val_init_op)
for n in range(val_batches_per_epoch):
X_val, y_val = sess.run(next_batch)
val_summary, val_accuracy = \
sess.run([summary_op, iou_op],
feed_dict={X: X_val,
GT: y_val,
mode: False}
)
# total_val_accuracy += val_step_iou * X_val.shape[0]
total_val_accuracy += val_accuracy
val_count += 1
val_summary_writer.add_summary(val_summary, (epoch-start_epoch)*val_batches_per_epoch+n)
tf.logging.info('step #%d/%d, accuracy(iou) %.5f%%' %
(n, val_batches_per_epoch, val_accuracy * 100))
total_val_accuracy /= val_count
tf.logging.info('step %d: Validation accuracy = %.2f%% (N=%d)' %
(epoch, total_val_accuracy * 100, raw.get_size('validation')))
# save checkpoint
checkpoint_path = os.path.join(FLAGS.train_dir, 'unet.ckpt')
tf.logging.info('Saving to "%s-%d"', checkpoint_path, epoch)
saver.save(sess, checkpoint_path, global_step=epoch)
# save best checkpoint
if FLAGS.best_train_dir:
best_ckpt_saver.handle(total_val_accuracy, sess, global_step, epoch)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--data_dir',
# default='../../dl_data/nucleus/stage1_train_valid_elas',
default='../../dl_data/nucleus/stage1_train_aug',
type=str,
help="Data directory")
parser.add_argument(
'--validation_percentage',
type=int,
default=10,
help='What percentage of wavs to use as a validation set.')
parser.add_argument(
'--logdir',
type=str,
default=os.getcwd() + '/models/retrain_logs',
help="Tensorboard log directory")
parser.add_argument(
'--train_dir',
type=str,
default=os.getcwd() + '/models',
help='Directory to write event logs and checkpoint.')
parser.add_argument(
'--best_train_dir',
type=str,
# default=os.getcwd() + '/models_best',
default=None,
help="Directory to write best checkpoint.")
# parser.add_argument(
# '--reg',
# type=float,
# default=0.1,
# help="L2 Regularizer Term")
parser.add_argument(
'--ckpt_path',
type=str,
# default=os.getcwd() + '/models/unet.ckpt-20',
default='',
help="Checkpoint directory")
parser.add_argument(
'--epochs',
type=int,
default=50,
help='Number of epochs')
parser.add_argument(
'--batch_size',
default=2,
type=int,
help="Batch size")
parser.add_argument(
'--img_size',
type=int,
# default=256,
default=512,
# default=572,
help="Image height and width")
parser.add_argument(
'--label_size',
type=int,
# default=256,
default=512,
# default=388,
help="Label height and width")
parser.add_argument(
'--conv_padding',
type=str,
default='same',
# default='valid',
help="conv padding. if your img_size is 572 and, conv_padding is valid then the label_size is 388")
parser.add_argument(
'--use_64_channel',
type=bool,
default=True,
# default=False,
help="If you set True then use the Unet_64_1024. otherwise use the Unet_32_512")
parser.add_argument(
'--gpu_index',
type=str,
# default=None,
default='0',
# default='1',
help="Set the gpu index. If you not sepcify then auto")
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)