-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmscoco.py
487 lines (431 loc) · 16.9 KB
/
mscoco.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
from __future__ import absolute_import
from __future__ import division
import os
from nvidia import dali
from nvidia.dali.pipeline import Pipeline
import numpy as np
import mxnet as mx
class DALICOCODetection(object):
"""DALI partial pipeline with COCO Reader and loader. To be passed as
a parameter of a DALI transform pipeline.
Parameters
----------
num_shards: int
DALI pipeline arg - Number of pipelines used, indicating to the reader
how to split/shard the dataset.
shard_id: int
DALI pipeline arg - Shard id of the pipeline must be in [0, num_shards).
file_root
Directory containing the COCO dataset.
annotations_file
The COCO annotation file to read from.
"""
def __init__(self, split, num_shards, shard_id, root_dir='~/.mxnet/datasets/coco'):
file_root = os.path.expanduser(os.path.join(root_dir, split))
anno_file_name = 'instances_{}.json'.format(split)
annotations_file = os.path.expanduser(os.path.join(root_dir, 'annotations', anno_file_name))
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file,
skip_empty=True,
shard_id=shard_id,
num_shards=num_shards,
ratio=True,
ltrb=True,
shuffle_after_epoch=True,
save_img_ids=True)
self.decode = dali.ops.ImageDecoder(device="cpu", output_type=dali.types.RGB)
# We need to build the COCOReader ops to parse the annotations
# and have acces to the dataset size.
# TODO(spanev): Replace by DALI standalone ops when available
class DummyMicroPipe(Pipeline):
""" Dummy pipeline which sole purpose is to build COCOReader
and get the epoch size. To be replaced by DALI standalone op, when available.
"""
def __init__(self):
super(DummyMicroPipe, self).__init__(batch_size=1,
device_id=0,
num_threads=1)
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file)
def define_graph(self):
inputs, bboxes, labels = self.input(name="Reader")
return (inputs, bboxes, labels)
micro_pipe = DummyMicroPipe()
micro_pipe.build()
self._size = micro_pipe.epoch_size(name="Reader")
del micro_pipe
def __call__(self):
"""Returns three DALI graph nodes: inputs, bboxes, labels.
To be called in `define_graph`.
"""
inputs, bboxes, labels, img_ids = self.input(name="Reader")
images = self.decode(inputs)
return (images, bboxes, labels, img_ids)
def size(self):
"""Returns size of COCO dataset
"""
return self._size
class SSDTrainPipeline(Pipeline):
def __init__(self, split, batch_size, data_shape, num_shards, device_id, anchors,
num_workers, root_dir='~/.mxnet/datasets/coco'):
super(SSDTrainPipeline, self).__init__(
batch_size=batch_size,
device_id=device_id,
num_threads=num_workers)
file_root = os.path.expanduser(os.path.join(root_dir, split))
anno_file_name = 'instances_{}.json'.format(split)
annotations_file = os.path.expanduser(os.path.join(root_dir, 'annotations', anno_file_name))
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file,
skip_empty=True,
shard_id=device_id,
num_shards=num_shards,
ratio=True,
ltrb=True,
shuffle_after_epoch=True,
save_img_ids=True)
self.decode = dali.ops.ImageDecoder(device="cpu", output_type=dali.types.RGB)
# Augumentation techniques
self.crop = dali.ops.RandomBBoxCrop(
device="cpu",
aspect_ratio=[0.5, 2.0],
thresholds=[0, 0.1, 0.3, 0.5, 0.7, 0.9],
scaling=[0.3, 1.0],
ltrb=True,
allow_no_crop=True,
num_attempts=1)
self.slice = dali.ops.Slice(device="cpu")
self.twist = dali.ops.ColorTwist(device="gpu")
self.resize = dali.ops.Resize(
device="cpu",
resize_x=data_shape,
resize_y=data_shape,
min_filter=dali.types.DALIInterpType.INTERP_TRIANGULAR)
# output_dtype = types.FLOAT16 if args.fp16 else types.FLOAT
output_dtype = dali.types.FLOAT
self.normalize = dali.ops.CropMirrorNormalize(
device="gpu",
crop=(data_shape, data_shape),
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
mirror=0,
output_dtype=output_dtype,
output_layout=dali.types.NCHW,
pad_output=False)
# Random variables
self.rng1 = dali.ops.Uniform(range=[0.5, 1.5])
self.rng2 = dali.ops.Uniform(range=[0.875, 1.125])
self.rng3 = dali.ops.Uniform(range=[-0.5, 0.5])
self.flip = dali.ops.Flip(device="cpu")
self.bbflip = dali.ops.BbFlip(device="cpu", ltrb=True)
self.flip_coin = dali.ops.CoinFlip(probability=0.5)
self.box_encoder = dali.ops.BoxEncoder(
device="cpu",
criteria=0.5,
anchors=self._to_normalized_ltrb_list(anchors, data_shape),
offset=True,
stds=[0.1, 0.1, 0.2, 0.2],
scale=data_shape)
# We need to build the COCOReader ops to parse the annotations
# and have acces to the dataset size.
# TODO(spanev): Replace by DALI standalone ops when available
class DummyMicroPipe(Pipeline):
""" Dummy pipeline which sole purpose is to build COCOReader
and get the epoch size. To be replaced by DALI standalone op, when available.
"""
def __init__(self):
super(DummyMicroPipe, self).__init__(batch_size=1,
device_id=0,
num_threads=1)
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file)
def define_graph(self):
inputs, bboxes, labels = self.input(name="Reader")
return (inputs, bboxes, labels)
micro_pipe = DummyMicroPipe()
micro_pipe.build()
self._size = micro_pipe.epoch_size(name="Reader")
print ('train dataset size {} for split {}'.format(self._size, split))
del micro_pipe
def _to_normalized_ltrb_list(self, anchors, size):
"""Prepare anchors into ltrb (normalized DALI anchors format list)"""
if isinstance(anchors, list):
return anchors
anchors_np = anchors.squeeze().asnumpy()
anchors_np_ltrb = anchors_np.copy()
anchors_np_ltrb[:, 0] = anchors_np[:, 0] - 0.5 * anchors_np[:, 2]
anchors_np_ltrb[:, 1] = anchors_np[:, 1] - 0.5 * anchors_np[:, 3]
anchors_np_ltrb[:, 2] = anchors_np[:, 0] + 0.5 * anchors_np[:, 2]
anchors_np_ltrb[:, 3] = anchors_np[:, 1] + 0.5 * anchors_np[:, 3]
anchors_np_ltrb /= size
return anchors_np_ltrb.flatten().tolist()
def define_graph(self):
saturation = self.rng1()
contrast = self.rng1()
brightness = self.rng2()
hue = self.rng3()
coin_rnd = self.flip_coin()
inputs, bboxes, labels, _ = self.input(name="Reader")
images = self.decode(inputs)
crop_begin, crop_size, bboxes, labels = self.crop(bboxes, labels)
images = self.slice(images, crop_begin, crop_size)
images = self.flip(images, horizontal=coin_rnd)
bboxes = self.bbflip(bboxes, horizontal=coin_rnd)
images = self.resize(images)
images = images.gpu()
images = self.twist(
images,
saturation=saturation,
contrast=contrast,
brightness=brightness,
hue=hue)
images = self.normalize(images)
bboxes, labels = self.box_encoder(bboxes, labels)
return (images, bboxes.gpu(), labels.gpu())
def size(self):
"""Returns size of COCO dataset
"""
return self._size
class ValPipeline(Pipeline):
def __init__(self, split, batch_size, data_shape, num_shards, device_id, num_workers,
root_dir='~/.mxnet/datasets/coco'):
super(ValPipeline, self).__init__(
batch_size=batch_size,
device_id=device_id,
num_threads=num_workers)
file_root = os.path.expanduser(os.path.join(root_dir, split))
anno_file_name = 'instances_{}.json'.format(split)
annotations_file = os.path.expanduser(os.path.join(root_dir, 'annotations', anno_file_name))
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file,
skip_empty=False,
shard_id=device_id,
num_shards=num_shards,
ratio=True,
ltrb=True,
shuffle_after_epoch=False,
save_img_ids=True)
self.decode = dali.ops.ImageDecoder(device="mixed", output_type=dali.types.RGB)
self.resize = dali.ops.Resize(
device="gpu",
resize_x=data_shape,
resize_y=data_shape,
min_filter=dali.types.DALIInterpType.INTERP_TRIANGULAR)
self.normalize = dali.ops.CropMirrorNormalize(
device="gpu",
mean=[0.485 * 255, 0.456 * 255, 0.406 * 255],
std=[0.229 * 255, 0.224 * 255, 0.225 * 255],
mirror=0,
output_dtype=dali.types.FLOAT,
output_layout=dali.types.NCHW,
pad_output=False)
# We need to build the COCOReader ops to parse the annotations
# and have acces to the dataset size.
# TODO(spanev): Replace by DALI standalone ops when available
class DummyMicroPipe(Pipeline):
""" Dummy pipeline which sole purpose is to build COCOReader
and get the epoch size. To be replaced by DALI standalone op, when available.
"""
def __init__(self):
super(DummyMicroPipe, self).__init__(batch_size=1,
device_id=0,
num_threads=1)
self.input = dali.ops.COCOReader(
file_root=file_root,
annotations_file=annotations_file)
def define_graph(self):
inputs, bboxes, labels = self.input(name="Reader")
return (inputs, bboxes, labels)
micro_pipe = DummyMicroPipe()
micro_pipe.build()
self._size = micro_pipe.epoch_size(name="Reader")
del micro_pipe
def define_graph(self):
inputs, bboxes, labels, img_ids = self.input(name="Reader")
images = self.decode(inputs)
images = self.resize(images)
images = self.normalize(images)
return (images, bboxes.gpu(), labels.gpu(), img_ids.gpu())
def size(self):
"""Returns size of COCO dataset
"""
return self._size
class ValLoader(object):
def __init__(self, pipelines, size, batch_size, data_shape=None):
self.pipelines = pipelines
self.size = size
print ('size {}'.format(size))
self.batch_size = batch_size
self.width, self.height = data_shape
self.num_worker = len(pipelines)
self.batch_size = pipelines[0].batch_size
for pipeline in self.pipelines:
pipeline.build()
self.count = 0
def __next__(self):
if self.count >= self.size:
self.reset()
raise StopIteration
batch_data = []
batch_img_ids = []
for idx, pipe in enumerate(self.pipelines):
data, bboxes, labels, img_ids = pipe.run()
data, labels = self.format_data(data, bboxes, labels, idx)
data_batch = mx.io.DataBatch(data=[data], label=[labels])
img_ids = [int(img_ids.as_cpu().at(idx)) for idx in range(self.batch_size)]
img_ids = mx.nd.array(img_ids)
batch_data.append(data_batch)
batch_img_ids.append(img_ids)
self.count += self.num_worker * self.batch_size
if self.count > self.size:
overflow = self.count - self.size
overflow_per_device = overflow // self.num_worker
last_batch_data = []
last_img_ids = []
for data_batch, img_ids in zip(batch_data, batch_img_ids):
data = data_batch.data[0][0: self.batch_size-overflow_per_device, :, :, :]
label = data_batch.label[0][0: self.batch_size-overflow_per_device, :, :]
data_batch = mx.io.DataBatch(data=[data], label=[labels])
img_ids = img_ids[0: self.batch_size-overflow_per_device]
last_batch_data.append(data_batch)
last_img_ids.append(img_ids)
batch_data = last_batch_data
batch_img_ids = last_img_ids
return batch_data, batch_img_ids
def format_data(self, data, bboxes, labels, idx):
ctx = mx.gpu(idx)
data = [data.as_cpu().at(idx) for idx in range(self.batch_size)]
data = [mx.nd.array(d).expand_dims(axis=0) for d in data]
data = mx.nd.concat(*data, dim=0)
data = data.as_in_context(ctx)
num_boxes = [bboxes.as_cpu().at(idx).shape[0] for idx in range(self.batch_size)]
max_num_boxes = max(num_boxes)
# for empty image
max_num_boxes = max(max_num_boxes, 1)
box_dim = bboxes.as_cpu().at(0).shape[1]
format_bboxes = []
format_labels = []
for idx in range(self.batch_size):
box_container = mx.nd.zeros((1, max_num_boxes, box_dim))
label_dim = labels.as_cpu().at(0).shape[1]
assert label_dim == 1, 'Expected label dim to be 1 but got {}.'.format(label_dim)
label_container = mx.nd.ones((1, max_num_boxes, 1))*-1
bbox = bboxes.as_cpu().at(idx)
label = labels.as_cpu().at(idx)
num_box = bbox.shape[0]
num_label = label.shape[0]
assert num_box == num_label, 'Expected same length of boxes and labels,\
got {} and {}'.format(num_box, num_label)
# for empty image
if num_box == 0:
bbox = mx.nd.zeros((1, 4))
label = mx.nd.ones((1, 1))*-1
num_box = 1
num_label = 1
box_container[:, 0: num_box, :] = bbox
label_container[:, 0: num_label, :] = label
format_bboxes.append(box_container)
format_labels.append(label_container)
format_bboxes = mx.nd.concat(*format_bboxes, dim=0)
format_bboxes[:, :, 0] *= self.width
format_bboxes[:, :, 1] *= self.height
format_bboxes[:, :, 2] *= self.width
format_bboxes[:, :, 3] *= self.height
format_labels = mx.nd.concat(*format_labels, dim=0)
labels = mx.nd.concat(format_bboxes, format_labels, dim=-1)
labels = labels.as_in_context(ctx)
return data, labels
def next(self):
return self.__next__()
def __iter__(self):
return self
def reset(self):
for pipe in self.pipelines:
pipe.reset()
self.count = 0
class_names = {0: u'__background__',
1: u'person',
2: u'bicycle',
3: u'car',
4: u'motorcycle',
5: u'airplane',
6: u'bus',
7: u'train',
8: u'truck',
9: u'boat',
10: u'traffic light',
11: u'fire hydrant',
12: u'stop sign',
13: u'parking meter',
14: u'bench',
15: u'bird',
16: u'cat',
17: u'dog',
18: u'horse',
19: u'sheep',
20: u'cow',
21: u'elephant',
22: u'bear',
23: u'zebra',
24: u'giraffe',
25: u'backpack',
26: u'umbrella',
27: u'handbag',
28: u'tie',
29: u'suitcase',
30: u'frisbee',
31: u'skis',
32: u'snowboard',
33: u'sports ball',
34: u'kite',
35: u'baseball bat',
36: u'baseball glove',
37: u'skateboard',
38: u'surfboard',
39: u'tennis racket',
40: u'bottle',
41: u'wine glass',
42: u'cup',
43: u'fork',
44: u'knife',
45: u'spoon',
46: u'bowl',
47: u'banana',
48: u'apple',
49: u'sandwich',
50: u'orange',
51: u'broccoli',
52: u'carrot',
53: u'hot dog',
54: u'pizza',
55: u'donut',
56: u'cake',
57: u'chair',
58: u'couch',
59: u'potted plant',
60: u'bed',
61: u'dining table',
62: u'toilet',
63: u'tv',
64: u'laptop',
65: u'mouse',
66: u'remote',
67: u'keyboard',
68: u'cell phone',
69: u'microwave',
70: u'oven',
71: u'toaster',
72: u'sink',
73: u'refrigerator',
74: u'book',
75: u'clock',
76: u'vase',
77: u'scissors',
78: u'teddy bear',
79: u'hair drier',
80: u'toothbrush'}