/
attrs_train.py
362 lines (306 loc) · 15.7 KB
/
attrs_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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: train.py
import argparse
import cv2
import shutil
import itertools
import tqdm
import numpy as np
import json
import six
import os
from concurrent.futures import ThreadPoolExecutor
from detection.tensorpacks.model_mrcnn import maskrcnn_upXconv_head
from detection.tensorpacks.model_rpn import rpn_head, generate_rpn_proposals
try:
import horovod.tensorflow as hvd
except ImportError:
pass
import tensorflow as tf
assert six.PY3, "FasterRCNN requires Python 3!"
from tensorpack import *
from tensorpack.tfutils.summary import add_moving_summary
from tensorpack.tfutils import optimizer
from detection.tensorpacks.coco import COCODetection
from detection.tensorpacks.basemodel import (
image_preprocess, resnet_c4_backbone, resnet_conv5, resnet_conv5_attr)
from detection.tensorpacks.model_frcnn import (
sample_fast_rcnn_targets, fastrcnn_outputs, attrs_head,
fastrcnn_predictions, BoxProposals, FastRCNNHead, attr_losses, attr_losses_v2, all_attrs_losses)
from detection.tensorpacks.model_box import (
clip_boxes, crop_and_resize, roi_align, RPNAnchors)
from detection.tensorpacks.data import (
get_train_dataflow, get_eval_dataflow,
get_all_anchors, get_all_anchors_fpn, get_attributes_dataflow)
from detection.tensorpacks.eval import (
eval_coco, detect_one_image, print_evaluation_scores, DetectionResult)
from detection.config.tensorpack_config import finalize_configs, config as cfg
class DetectionModel(ModelDesc):
def preprocess(self, image):
image = tf.expand_dims(image, 0)
image = image_preprocess(image, bgr=True)
return tf.transpose(image, [0, 3, 1, 2])
def optimizer(self):
lr = tf.get_variable('learning_rate', initializer=0.003, trainable=False)
tf.summary.scalar('learning_rate-summary', lr)
# The learning rate is set for 8 GPUs, and we use trainers with average=False.
lr = lr / 8.
# I haved changed the learning rate
opt = tf.train.MomentumOptimizer(lr, 0.9)
opt = optimizer.AccumGradOptimizer(opt, 8 // cfg.TRAIN.NUM_GPUS) # assume cfg.TRAIN.NUM_GPUS < 8:
return opt
class ResNetC4Model(DetectionModel):
def inputs(self): # OK
ret = [
tf.placeholder(tf.float32, (None, None, 3), 'image'),
# box of each ground truth
tf.placeholder(tf.float32, (None, 4), 'gt_boxes'),
# label of each anchor
tf.placeholder(tf.int32, (None, None, cfg.RPN.NUM_ANCHOR), 'anchor_labels'), # NUM_ANCHOR = 5*3
# box of each anchor
tf.placeholder(tf.float32, (None, None, cfg.RPN.NUM_ANCHOR, 4), 'anchor_boxes'),
# male_labels of each ground truth
tf.placeholder(tf.int64, (None,), 'male'),
# longhair_labels of each ground truth
tf.placeholder(tf.int64, (None,), 'longhair'),
# sunglass_labels of each ground truth
tf.placeholder(tf.int64, (None,), 'sunglass'),
# hat_labels of each ground truth
tf.placeholder(tf.int64, (None,), 'hat'),
# tshort_labels of each ground truth
tf.placeholder(tf.int64, (None,), 'tshirt'),
tf.placeholder(tf.int64, (None,), 'longsleeve'),
tf.placeholder(tf.int64, (None,), 'formal'),
tf.placeholder(tf.int64, (None,), 'shorts'),
tf.placeholder(tf.int64, (None,), 'jeans'),
tf.placeholder(tf.int64, (None,), 'longpants'),
tf.placeholder(tf.int64, (None,), 'skirt'),
tf.placeholder(tf.int64, (None,), 'facemask'),
tf.placeholder(tf.int64, (None,), 'logo'),
tf.placeholder(tf.int64, (None,), 'stripe')]
return ret
def build_graph(self, *inputs):
inputs = dict(zip(self.input_names, inputs))
image = self.preprocess(inputs['image']) # 1CHW
# build resnet c4
featuremap = resnet_c4_backbone(image, cfg.BACKBONE.RESNET_NUM_BLOCK[:3])
rpn_label_logits, rpn_box_logits = rpn_head('rpn', featuremap, cfg.RPN.HEAD_DIM, cfg.RPN.NUM_ANCHOR)
# HEAD_DIM = 1024, NUM_ANCHOR = 15
# rpn_label_logits: fHxfWxNA
# rpn_box_logits: fHxfWxNAx4
anchors = RPNAnchors(get_all_anchors(), inputs['anchor_labels'], inputs['anchor_boxes'])
# anchor_boxes is Groundtruth boxes corresponding to each anchor
anchors = anchors.narrow_to(featuremap) # ??
image_shape2d = tf.shape(image)[2:] # h,w
pred_boxes_decoded = anchors.decode_logits(rpn_box_logits) # fHxfWxNAx4, floatbox
# ProposalCreator (get the topk proposals)
proposal_boxes, proposal_scores = generate_rpn_proposals(
tf.reshape(pred_boxes_decoded, [-1, 4]),
tf.reshape(rpn_label_logits, [-1]),
image_shape2d,
cfg.RPN.TEST_PRE_NMS_TOPK, # 2000
cfg.RPN.TEST_POST_NMS_TOPK) # 1000
x, y, w, h = tf.split(inputs['gt_boxes'], 4, axis=1)
gt_boxes = tf.concat([x, y, x + w, y + h], axis=1)
boxes_on_featuremap = gt_boxes * (1.0 / cfg.RPN.ANCHOR_STRIDE) # ANCHOR_STRIDE = 16
roi_resized = roi_align(featuremap, boxes_on_featuremap, 14)
feature_fastrcnn = resnet_conv5(roi_resized,
cfg.BACKBONE.RESNET_NUM_BLOCK[
-1]) # nxcx7x7 # RESNET_NUM_BLOCK = [3, 4, 6, 3]
# Keep C5 feature to be shared with mask branch
feature_gap = GlobalAvgPooling('gap', feature_fastrcnn, data_format='channels_first') # ??
fastrcnn_label_logits, fastrcnn_box_logits = fastrcnn_outputs('fastrcnn', feature_gap,
cfg.DATA.NUM_CLASS) # ??
# Returns:
# cls_logits: Tensor("fastrcnn/class/output:0", shape=(n, 81), dtype=float32)
# reg_logits: Tensor("fastrcnn/output_box:0", shape=(n, 81, 4), dtype=float32)
# ------------------Fastrcnn_Head------------------------
fastrcnn_head = FastRCNNHead(proposal_boxes, fastrcnn_box_logits, fastrcnn_label_logits, #
tf.constant(cfg.FRCNN.BBOX_REG_WEIGHTS,
dtype=tf.float32)) # [10., 10., 5., 5.]
decoded_boxes = fastrcnn_head.decoded_output_boxes() # pre_boxes_on_images
decoded_boxes = clip_boxes(decoded_boxes, image_shape2d, name='fastrcnn_all_boxes')
label_scores = tf.nn.softmax(fastrcnn_label_logits, name='fastrcnn_all_scores')
# class scores, summed to one for each box.
final_boxes, final_scores, final_labels = fastrcnn_predictions(
decoded_boxes, label_scores, name_scope='output')
feature_maskrcnn = resnet_conv5(roi_resized,
cfg.BACKBONE.RESNET_NUM_BLOCK[
-1]) # nxcx7x7 # RESNET_NUM_BLOCK = [3, 4, 6, 3]
# Keep C5 feature to be shared with mask branch
mask_logits = maskrcnn_upXconv_head(
'maskrcnn', feature_maskrcnn, cfg.DATA.NUM_CATEGORY, 0) # #result x #cat x 14x14
# Assume only person here
person_labels = tf.ones_like(inputs['male'])
indices = tf.stack([tf.range(tf.size(person_labels)), tf.to_int32(person_labels) - 1], axis=1)
final_mask_logits = tf.gather_nd(mask_logits, indices) # #resultx14x14
final_mask_logits = tf.sigmoid(final_mask_logits, name='output/masks')
mask = False
if mask:
final_mask_logits_expand = tf.expand_dims(final_mask_logits, axis=1)
final_mask_logits_tile = tf.tile(final_mask_logits_expand, multiples=[1, 1024, 1, 1])
fg_roi_resized = tf.where(final_mask_logits_tile >= 0.5, roi_resized,
roi_resized * 1.0)
feature_attrs = resnet_conv5_attr(fg_roi_resized, cfg.BACKBONE.RESNET_NUM_BLOCK[-1])
else:
feature_attrs = resnet_conv5_attr(roi_resized, cfg.BACKBONE.RESNET_NUM_BLOCK[-1])
feature_attrs_gap = GlobalAvgPooling('gap', feature_attrs, data_format='channels_first')
attrs_logits = attrs_head('attrs', feature_attrs_gap)
attrs_loss = all_attrs_losses(inputs, attrs_logits, attr_losses_v2)
all_losses = [attrs_loss]
# male loss
wd_cost = regularize_cost(
'.*/W', l2_regularizer(cfg.TRAIN.WEIGHT_DECAY), name='wd_cost')
all_losses.append(wd_cost)
total_cost = tf.add_n(all_losses, 'total_cost')
add_moving_summary(wd_cost, total_cost)
return total_cost
class EvalCallback(Callback):
"""
A callback that runs COCO evaluation once a while.
It supports multi-gpu evaluation.
"""
_chief_only = False
def __init__(self, in_names, out_names):
self._in_names, self._out_names = in_names, out_names
def _setup_graph(self):
num_gpu = cfg.TRAIN.NUM_GPUS
if cfg.TRAINER == 'replicated':
# Use two predictor threads per GPU to get better throughput
self.num_predictor = num_gpu * 2
self.predictors = [self._build_coco_predictor(k % num_gpu) for k in range(self.num_predictor)]
self.dataflows = [get_eval_dataflow(shard=k, num_shards=self.num_predictor)
for k in range(self.num_predictor)]
else:
# Only eval on the first machine.
# Alternatively, can eval on all ranks and use allgather, but allgather sometimes hangs
self._horovod_run_eval = hvd.rank() == hvd.local_rank()
if self._horovod_run_eval:
self.predictor = self._build_coco_predictor(0)
self.dataflow = get_eval_dataflow(shard=hvd.local_rank(), num_shards=hvd.local_size())
self.barrier = hvd.allreduce(tf.random_normal(shape=[1]))
def _build_coco_predictor(self, idx):
graph_func = self.trainer.get_predictor(self._in_names, self._out_names, device=idx)
return lambda img: detect_one_image(img, graph_func)
def _before_train(self):
num_eval = cfg.TRAIN.NUM_EVALS
interval = max(self.trainer.max_epoch // (num_eval + 1), 1)
self.epochs_to_eval = set([interval * k for k in range(1, num_eval + 1)])
self.epochs_to_eval.add(self.trainer.max_epoch)
if len(self.epochs_to_eval) < 15:
logger.info("[EvalCallback] Will evaluate at epoch " + str(sorted(self.epochs_to_eval)))
else:
logger.info("[EvalCallback] Will evaluate every {} epochs".format(interval))
def _eval(self):
logdir = args.logdir
if cfg.TRAINER == 'replicated':
with ThreadPoolExecutor(max_workers=self.num_predictor) as executor, \
tqdm.tqdm(total=sum([df.size() for df in self.dataflows])) as pbar:
futures = []
for dataflow, pred in zip(self.dataflows, self.predictors):
futures.append(executor.submit(eval_coco, dataflow, pred, pbar))
all_results = list(itertools.chain(*[fut.result() for fut in futures]))
else:
if self._horovod_run_eval:
local_results = eval_coco(self.dataflow, self.predictor)
output_partial = os.path.join(
logdir, 'outputs{}-part{}.json'.format(self.global_step, hvd.local_rank()))
with open(output_partial, 'w') as f:
json.dump(local_results, f)
self.barrier.eval()
if hvd.rank() > 0:
return
all_results = []
for k in range(hvd.local_size()):
output_partial = os.path.join(
logdir, 'outputs{}-part{}.json'.format(self.global_step, k))
with open(output_partial, 'r') as f:
obj = json.load(f)
all_results.extend(obj)
os.unlink(output_partial)
output_file = os.path.join(
logdir, 'outputs{}.json'.format(self.global_step))
with open(output_file, 'w') as f:
json.dump(all_results, f)
try:
scores = print_evaluation_scores(output_file)
for k, v in scores.items():
self.trainer.monitors.put_scalar(k, v)
except Exception:
logger.exception("Exception in COCO evaluation.")
def _trigger_epoch(self):
if self.epoch_num in self.epochs_to_eval:
logger.info("Running evaluation ...")
self._eval()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--load', help='load a model for evaluation. Can overwrite BACKBONE.WEIGHTS')
parser.add_argument('--logdir', help='log directory', default='train_log/maskrcnn')
parser.add_argument('--config', help="A list of KEY=VALUE to overwrite those defined in tensorpack_config.py",
nargs='+')
args = parser.parse_args()
if args.config:
cfg.update_args(args.config)
MODEL = ResNetC4Model()
is_horovod = cfg.TRAINER == 'horovod'
if is_horovod:
hvd.init()
logger.info("Horovod Rank={}, Size={}".format(hvd.rank(), hvd.size()))
if not is_horovod or hvd.rank() == 0:
logger.set_logger_dir(args.logdir, 'd')
finalize_configs(is_training=True)
stepnum = cfg.TRAIN.STEPS_PER_EPOCH # STEPS_PER_EPOCH = 5000
# warmup is step based, lr is epoch based
init_lr = cfg.TRAIN.BASE_LR * 0.33 * min(8. / cfg.TRAIN.NUM_GPUS, 1.)
warmup_schedule = [(0, init_lr), (cfg.TRAIN.WARMUP, cfg.TRAIN.BASE_LR)]
warmup_end_epoch = cfg.TRAIN.WARMUP * 1. / stepnum # 1000/500
lr_schedule = [(int(warmup_end_epoch + 0.5), cfg.TRAIN.BASE_LR)]
factor = 8. / cfg.TRAIN.NUM_GPUS
for idx, steps in enumerate(cfg.TRAIN.LR_SCHEDULE[:-1]):
mult = 0.1 ** (idx + 1)
lr_schedule.append(
(steps * factor // stepnum, cfg.TRAIN.BASE_LR * mult))
logger.info("Warm Up Schedule (steps, value): " + str(warmup_schedule))
logger.info("LR Schedule (epochs, value): " + str(lr_schedule))
# train_dataflow = get_train_dataflow() # get the coco datasets
train_attrs_dataflow = get_attributes_dataflow() # get the wider datasets
# This is what's commonly referred to as "epochs"
total_passes = cfg.TRAIN.LR_SCHEDULE[-1] * factor / train_attrs_dataflow.size()
logger.info("Total passes of the training set is: {}".format(total_passes))
callbacks = [
PeriodicCallback(
ModelSaver(max_to_keep=10, keep_checkpoint_every_n_hours=1),
every_k_epochs=20),
# linear warmup
ScheduledHyperParamSetter(
'learning_rate', warmup_schedule, interp='linear', step_based=True),
ScheduledHyperParamSetter('learning_rate', lr_schedule),
# EvalCallback(*MODEL.get_inference_tensor_names()),
PeakMemoryTracker(),
EstimatedTimeLeft(median=True),
SessionRunTimeout(60000).set_chief_only(True), # 1 minute timeout
]
if not is_horovod:
callbacks.append(GPUUtilizationTracker())
if is_horovod and hvd.rank() > 0:
session_init = None
else:
if args.load:
session_init = get_model_loader(args.load)
else:
session_init = get_model_loader(cfg.BACKBONE.WEIGHTS) if cfg.BACKBONE.WEIGHTS else None
traincfg = TrainConfig(
model=MODEL,
data=QueueInput(train_attrs_dataflow),
callbacks=callbacks,
steps_per_epoch=stepnum,
max_epoch=cfg.TRAIN.LR_SCHEDULE[-1] * factor // stepnum,
session_init=session_init,
)
if is_horovod:
trainer = HorovodTrainer(average=False)
else:
# nccl mode has better speed than cpu mode
trainer = SyncMultiGPUTrainerReplicated(cfg.TRAIN.NUM_GPUS, average=False, mode='nccl')
launch_train_with_config(traincfg, trainer)