-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
254 lines (197 loc) · 9.21 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
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import os
import sys
import tensorflow as tf
import deeplabv3plus_hed_model
import preprocessing
from tensorflow.python import debug as tf_debug
import pdb
import shutil
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
parser = argparse.ArgumentParser()
parser.add_argument('--model_dir', type=str, default='./model',
help='Base directory for the model.')
parser.add_argument('--clean_model_dir', action='store_true',
help='Whether to clean up the model directory if present.')
parser.add_argument('--train_epochs', type=int, default=20,
help='Number of training epochs: '
'For 30K iteration with batch size 6, train_epoch = 17.01 (= 30K * 6 / 10,582). '
'For 30K iteration with batch size 8, train_epoch = 22.68 (= 30K * 8 / 10,582). '
'For 30K iteration with batch size 10, train_epoch = 25.52 (= 30K * 10 / 10,582). '
'For 30K iteration with batch size 11, train_epoch = 31.19 (= 30K * 11 / 10,582). '
'For 30K iteration with batch size 15, train_epoch = 42.53 (= 30K * 15 / 10,582). '
'For 30K iteration with batch size 16, train_epoch = 45.36 (= 30K * 16 / 10,582).')
parser.add_argument('--epochs_per_eval', type=int, default=1,
help='The number of training epochs to run between evaluations.')
parser.add_argument('--tensorboard_images_max_outputs', type=int, default=6,
help='Max number of batch elements to generate for Tensorboard.')
parser.add_argument('--batch_size', type=int, default=5,
help='Number of examples per batch.')
parser.add_argument('--learning_rate_policy', type=str, default='poly',
choices=['poly', 'piecewise'],
help='Learning rate policy to optimize loss.')
parser.add_argument('--max_iter', type=int, default=30000,
help='Number of maximum iteration used for "poly" learning rate policy.')
parser.add_argument('--data_dir', type=str, default='./',
help='Path to the directory containing the PASCAL VOC data tf record.')
parser.add_argument('--base_architecture', type=str, default='resnet_v2_101',
choices=['resnet_v2_50', 'resnet_v2_101'],
help='The architecture of base Resnet building block.')
parser.add_argument('--pre_trained_model', type=str, default='../tensorflow-deeplab-v3-plus/ini_checkpoints/resnet_v2_101/resnet_v2_101.ckpt',
help='Path to the pre-trained model checkpoint.')
parser.add_argument('--output_stride', type=int, default=16,
choices=[8, 16],
help='Output stride for DeepLab v3. Currently 8 or 16 is supported.')
parser.add_argument('--freeze_batch_norm', action='store_true',
help='Freeze batch normalization parameters during the training.')
parser.add_argument('--initial_learning_rate', type=float, default=7e-3,
help='Initial learning rate for the optimizer.')
parser.add_argument('--end_learning_rate', type=float, default=1e-6,
help='End learning rate for the optimizer.')
parser.add_argument('--initial_global_step', type=int, default=0,
help='Initial global step for controlling learning rate when fine-tuning model.')
parser.add_argument('--weight_decay', type=float, default=2e-4,
help='The weight decay to use for regularizing the model.')
parser.add_argument('--debug', action='store_true',
help='Whether to use debugger to track down bad values during training.')
_NUM_CLASSES = 11
_HEIGHT = 513
_WIDTH = 513
_DEPTH = 3
_MIN_SCALE = 0.5
_MAX_SCALE = 2.0
_IGNORE_LABEL = 255
_POWER = 0.9
_MOMENTUM = 0.9
_BATCH_NORM_DECAY = 0.9997
_NUM_IMAGES = {
'train': 57132,
'validation': 6348,
}
def get_filenames(is_training, data_dir):
if is_training:
return [os.path.join(data_dir, 'railway_train.record')]
else:
return [os.path.join(data_dir, 'railway_val.record')]
def parse_record(raw_record):
keys_to_features = {
'image/height':
tf.FixedLenFeature((), tf.int64),
'image/width':
tf.FixedLenFeature((), tf.int64),
'image/encoded':
tf.FixedLenFeature((), tf.string, default_value=''),
'image/format':
tf.FixedLenFeature((), tf.string, default_value='png'),
'label/encoded':
tf.FixedLenFeature((), tf.string, default_value=''),
'label/format':
tf.FixedLenFeature((), tf.string, default_value='png'),
'edge/encoded':
tf.FixedLenFeature((), tf.string, default_value=''),
'edge/format':
tf.FixedLenFeature((), tf.string, default_value='png'),
}
parsed = tf.parse_single_example(raw_record, keys_to_features)
# height = tf.cast(parsed['image/height'], tf.int32)
# width = tf.cast(parsed['image/width'], tf.int32)
image = tf.image.decode_image(
tf.reshape(parsed['image/encoded'], shape=[]), _DEPTH)
image = tf.to_float(tf.image.convert_image_dtype(image, dtype=tf.uint8))
image.set_shape([None, None, 3])
label = tf.image.decode_image(
tf.reshape(parsed['label/encoded'], shape=[]), 1)
label = tf.to_int32(tf.image.convert_image_dtype(label, dtype=tf.uint8))
label.set_shape([None, None, 1])
edge = tf.image.decode_image(
tf.reshape(parsed['edge/encoded'], shape=[]), 1)
edge = tf.to_int32(tf.image.convert_image_dtype(edge, dtype=tf.uint8))
edge.set_shape([None, None, 1])
return image, label, edge
def preprocess_image(image, label, edge, is_training):
image = preprocessing.mean_image_subtraction(image)
return image, label, edge
def input_fn(is_training, data_dir, batch_size, num_epochs=1):
dataset = tf.data.Dataset.from_tensor_slices(get_filenames(is_training, data_dir))
dataset = dataset.flat_map(tf.data.TFRecordDataset)
if is_training:
dataset = dataset.shuffle(buffer_size=_NUM_IMAGES['train'])
dataset = dataset.map(parse_record)
dataset = dataset.map(
lambda image, label, edge: preprocess_image(image, label, edge, is_training))
dataset = dataset.prefetch(batch_size)
dataset = dataset.repeat(num_epochs)
dataset = dataset.batch(batch_size)
iterator = dataset.make_one_shot_iterator()
images, labels, edges = iterator.get_next()
_inputs={'images':images,'labels':labels,'edges':edges}
return _inputs,None
def main(unused_argv):
os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'
if FLAGS.clean_model_dir:
shutil.rmtree(FLAGS.model_dir, ignore_errors=True)
run_config = tf.estimator.RunConfig().replace(save_checkpoints_secs=1e9)
model = tf.estimator.Estimator(
model_fn=deeplabv3plus_hed_model.deeplabv3_plus_model_fn,
model_dir=FLAGS.model_dir,
config=run_config,
params={
'output_stride': FLAGS.output_stride,
'batch_size': FLAGS.batch_size,
'base_architecture': FLAGS.base_architecture,
'pre_trained_model': FLAGS.pre_trained_model,
'batch_norm_decay': _BATCH_NORM_DECAY,
'num_classes': _NUM_CLASSES,
'tensorboard_images_max_outputs': FLAGS.tensorboard_images_max_outputs,
'weight_decay': FLAGS.weight_decay,
'learning_rate_policy': FLAGS.learning_rate_policy,
'num_train': _NUM_IMAGES['train'],
'initial_learning_rate': FLAGS.initial_learning_rate,
'max_iter': FLAGS.max_iter,
'end_learning_rate': FLAGS.end_learning_rate,
'power': _POWER,
'momentum': _MOMENTUM,
'freeze_batch_norm': FLAGS.freeze_batch_norm,
'initial_global_step': FLAGS.initial_global_step
})
for _ in range(FLAGS.train_epochs // FLAGS.epochs_per_eval):
tensors_to_log = {
'learning_rate': 'learning_rate',
'cross_entropy': 'cross_entropy',
'train_px_accuracy': 'train_px_accuracy',
'train_mean_iou': 'train_mean_iou',
'accuracy_hed_side1': 'accuracy_hed_side1',
'accuracy_hed_side2': 'accuracy_hed_side2',
'accuracy_hed_side3': 'accuracy_hed_side3',
'accuracy_hed_side4': 'accuracy_hed_side4',
'accuracy_hed_side5': 'accuracy_hed_side5',
'accuracy_hed_fuse': 'accuracy_hed_fuse',
}
logging_hook = tf.train.LoggingTensorHook(
tensors=tensors_to_log, every_n_iter=10)
train_hooks = [logging_hook]
eval_hooks = None
if FLAGS.debug:
debug_hook = tf_debug.LocalCLIDebugHook()
debug_hook.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
train_hooks.append(debug_hook)
eval_hooks = [debug_hook]
tf.logging.info("Start training.")
model.train(
input_fn=lambda: input_fn(True, FLAGS.data_dir, FLAGS.batch_size, FLAGS.epochs_per_eval),
hooks=train_hooks,
)
tf.logging.info("Start evaluation.")
eval_results = model.evaluate(
input_fn=lambda: input_fn(False, FLAGS.data_dir, 1),
hooks=eval_hooks,
)
print(eval_results)
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)