-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
rcnn.py
468 lines (412 loc) · 20.4 KB
/
rcnn.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
"""Transforms for RCNN series."""
from __future__ import absolute_import
import copy
from random import randint
import mxnet as mx
from .. import bbox as tbbox
from .. import image as timage
from .. import mask as tmask
__all__ = ['transform_test', 'load_test',
'FasterRCNNDefaultTrainTransform', 'FasterRCNNDefaultValTransform',
'MaskRCNNDefaultTrainTransform', 'MaskRCNNDefaultValTransform']
def transform_test(imgs, short=600, max_size=1000, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)):
"""A util function to transform all images to tensors as network input by applying
normalizations. This function support 1 NDArray or iterable of NDArrays.
Parameters
----------
imgs : NDArray or iterable of NDArray
Image(s) to be transformed.
short : int, optional, default is 600
Resize image short side to this `short` and keep aspect ratio.
max_size : int, optional, default is 1000
Maximum longer side length to fit image.
This is to limit the input image shape, avoid processing too large image.
mean : iterable of float
Mean pixel values.
std : iterable of float
Standard deviations of pixel values.
Returns
-------
(mxnet.NDArray, numpy.ndarray) or list of such tuple
A (1, 3, H, W) mxnet NDArray as input to network, and a numpy ndarray as
original un-normalized color image for display.
If multiple image names are supplied, return two lists. You can use
`zip()`` to collapse it.
"""
if isinstance(imgs, mx.nd.NDArray):
imgs = [imgs]
for im in imgs:
assert isinstance(im, mx.nd.NDArray), "Expect NDArray, got {}".format(type(im))
tensors = []
origs = []
for img in imgs:
img = timage.resize_short_within(img, short, max_size)
orig_img = img.asnumpy().astype('uint8')
img = mx.nd.image.to_tensor(img)
img = mx.nd.image.normalize(img, mean=mean, std=std)
tensors.append(img.expand_dims(0))
origs.append(orig_img)
if len(tensors) == 1:
return tensors[0], origs[0]
return tensors, origs
def load_test(filenames, short=600, max_size=1000, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225)):
"""A util function to load all images, transform them to tensor by applying
normalizations. This function support 1 filename or list of filenames.
Parameters
----------
filenames : str or list of str
Image filename(s) to be loaded.
short : int, optional, default is 600
Resize image short side to this `short` and keep aspect ratio.
max_size : int, optional, default is 1000
Maximum longer side length to fit image.
This is to limit the input image shape, avoid processing too large image.
mean : iterable of float
Mean pixel values.
std : iterable of float
Standard deviations of pixel values.
Returns
-------
(mxnet.NDArray, numpy.ndarray) or list of such tuple
A (1, 3, H, W) mxnet NDArray as input to network, and a numpy ndarray as
original un-normalized color image for display.
If multiple image names are supplied, return two lists. You can use
`zip()`` to collapse it.
"""
if isinstance(filenames, str):
filenames = [filenames]
imgs = [mx.image.imread(f) for f in filenames]
return transform_test(imgs, short, max_size, mean, std)
class FasterRCNNDefaultTrainTransform(object):
"""Default Faster-RCNN training transform.
Parameters
----------
short : int/tuple, default is 600
Resize image shorter side to ``short``.
Resize the shorter side of the image randomly within the given range, if it is a tuple.
max_size : int, default is 1000
Make sure image longer side is smaller than ``max_size``.
net : mxnet.gluon.HybridBlock, optional
The faster-rcnn network.
.. hint::
If net is ``None``, the transformation will not generate training targets.
Otherwise it will generate training targets to accelerate the training phase
since we push some workload to CPU workers instead of GPUs.
mean : array-like of size 3
Mean pixel values to be subtracted from image tensor. Default is [0.485, 0.456, 0.406].
std : array-like of size 3
Standard deviation to be divided from image. Default is [0.229, 0.224, 0.225].
box_norm : array-like of size 4, default is (1., 1., 1., 1.)
Std value to be divided from encoded values.
num_sample : int, default is 256
Number of samples for RPN targets.
pos_iou_thresh : float, default is 0.7
Anchors larger than ``pos_iou_thresh`` is regarded as positive samples.
neg_iou_thresh : float, default is 0.3
Anchors smaller than ``neg_iou_thresh`` is regarded as negative samples.
Anchors with IOU in between ``pos_iou_thresh`` and ``neg_iou_thresh`` are
ignored.
pos_ratio : float, default is 0.5
``pos_ratio`` defines how many positive samples (``pos_ratio * num_sample``) is
to be sampled.
flip_p : float, default is 0.5
Probability to flip horizontally, by default is 0.5 for random horizontal flip.
You may set it to 0 to disable random flip or 1 to force flip.
ashape : int, default is 128
Defines shape of pre generated anchors for target generation
multi_stage : boolean, default is False
Whether the network output multi stage features.
"""
def __init__(self, short=600, max_size=1000, net=None, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225), box_norm=(1., 1., 1., 1.),
num_sample=256, pos_iou_thresh=0.7, neg_iou_thresh=0.3,
pos_ratio=0.5, flip_p=0.5, ashape=128, multi_stage=False, **kwargs):
self._short = short
self._max_size = max_size
self._mean = mean
self._std = std
self._anchors = None
self._multi_stage = multi_stage
self._random_resize = isinstance(self._short, (tuple, list))
self._flip_p = flip_p
if net is None:
return
# use fake data to generate fixed anchors for target generation
anchors = [] # [P2, P3, P4, P5]
# in case network has reset_ctx to gpu
anchor_generator = copy.deepcopy(net.rpn.anchor_generator)
anchor_generator.collect_params().reset_ctx(None)
if self._multi_stage:
for ag in anchor_generator:
anchor = ag(mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
ashape = max(ashape // 2, 16)
anchors.append(anchor)
else:
anchors = anchor_generator(
mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
self._anchors = anchors
# record feature extractor for infer_shape
if not hasattr(net, 'features'):
raise ValueError("Cannot find features in network, it is a Faster-RCNN network?")
self._feat_sym = net.features(mx.sym.var(name='data'))
from ....model_zoo.rcnn.rpn.rpn_target import RPNTargetGenerator
self._target_generator = RPNTargetGenerator(
num_sample=num_sample, pos_iou_thresh=pos_iou_thresh,
neg_iou_thresh=neg_iou_thresh, pos_ratio=pos_ratio,
stds=box_norm, **kwargs)
def __call__(self, src, label):
"""Apply transform to training image/label."""
# resize shorter side but keep in max_size
h, w, _ = src.shape
if self._random_resize:
short = randint(self._short[0], self._short[1])
else:
short = self._short
img = timage.resize_short_within(src, short, self._max_size, interp=1)
bbox = tbbox.resize(label, (w, h), (img.shape[1], img.shape[0]))
# random horizontal flip
h, w, _ = img.shape
img, flips = timage.random_flip(img, px=self._flip_p)
bbox = tbbox.flip(bbox, (w, h), flip_x=flips[0])
# to tensor
img = mx.nd.image.to_tensor(img)
img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
if self._anchors is None:
return img, bbox.astype(img.dtype)
# generate RPN target so cpu workers can help reduce the workload
# feat_h, feat_w = (img.shape[1] // self._stride, img.shape[2] // self._stride)
gt_bboxes = mx.nd.array(bbox[:, :4])
if self._multi_stage:
anchor_targets = []
oshapes = []
cls_targets, box_targets, box_masks = [], [], []
for anchor, feat_sym in zip(self._anchors, self._feat_sym):
oshape = feat_sym.infer_shape(data=(1, 3, img.shape[1], img.shape[2]))[1][0]
anchor = anchor[:, :, :oshape[2], :oshape[3], :]
oshapes.append(anchor.shape)
anchor_targets.append(anchor.reshape((-1, 4)))
anchor_targets = mx.nd.concat(*anchor_targets, dim=0)
cls_target, box_target, box_mask = self._target_generator(
gt_bboxes, anchor_targets, img.shape[2], img.shape[1])
start_ind = 0
for oshape in oshapes:
size = oshape[2] * oshape[3] * (oshape[4] // 4)
lvl_cls_target = cls_target[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
lvl_box_target = box_target[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
lvl_box_mask = box_mask[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
start_ind += size
cls_targets.append(lvl_cls_target)
box_targets.append(lvl_box_target)
box_masks.append(lvl_box_mask)
else:
oshape = self._feat_sym.infer_shape(data=(1, 3, img.shape[1], img.shape[2]))[1][0]
anchor = self._anchors[:, :, :oshape[2], :oshape[3], :]
oshape = anchor.shape
cls_target, box_target, box_mask = self._target_generator(
gt_bboxes, anchor.reshape((-1, 4)), img.shape[2], img.shape[1])
size = oshape[2] * oshape[3] * (oshape[4] // 4)
cls_targets = [cls_target[0:size].reshape(oshape[2], oshape[3], -1)]
box_targets = [box_target[0:size].reshape(oshape[2], oshape[3], -1)]
box_masks = [box_mask[0:size].reshape(oshape[2], oshape[3], -1)]
return img, bbox.astype(img.dtype), cls_targets, box_targets, box_masks
class FasterRCNNDefaultValTransform(object):
"""Default Faster-RCNN validation transform.
Parameters
----------
short : int, default is 600
Resize image shorter side to ``short``.
max_size : int, default is 1000
Make sure image longer side is smaller than ``max_size``.
mean : array-like of size 3
Mean pixel values to be subtracted from image tensor. Default is [0.485, 0.456, 0.406].
std : array-like of size 3
Standard deviation to be divided from image. Default is [0.229, 0.224, 0.225].
"""
def __init__(self, short=600, max_size=1000,
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self._mean = mean
self._std = std
self._short = short
self._max_size = max_size
def __call__(self, src, label):
"""Apply transform to validation image/label."""
# resize shorter side but keep in max_size
h, w, _ = src.shape
img = timage.resize_short_within(src, self._short, self._max_size, interp=1)
# no scaling ground-truth, return image scaling ratio instead
bbox = tbbox.resize(label, (w, h), (img.shape[1], img.shape[0]))
im_scale = h / float(img.shape[0])
img = mx.nd.image.to_tensor(img)
img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
return img, bbox.astype('float32'), mx.nd.array([im_scale])
class MaskRCNNDefaultTrainTransform(object):
"""Default Mask RCNN training transform.
Parameters
----------
short : int/tuple, default is 600
Resize image shorter side to ``short``.
Resize the shorter side of the image randomly within the given range, if it is a tuple.
max_size : int, default is 1000
Make sure image longer side is smaller than ``max_size``.
net : mxnet.gluon.HybridBlock, optional
The Mask R-CNN network.
.. hint::
If net is ``None``, the transformation will not generate training targets.
Otherwise it will generate training targets to accelerate the training phase
since we push some workload to CPU workers instead of GPUs.
mean : array-like of size 3
Mean pixel values to be subtracted from image tensor. Default is [0.485, 0.456, 0.406].
std : array-like of size 3
Standard deviation to be divided from image. Default is [0.229, 0.224, 0.225].
box_norm : array-like of size 4, default is (1., 1., 1., 1.)
Std value to be divided from encoded values.
num_sample : int, default is 256
Number of samples for RPN targets.
pos_iou_thresh : float, default is 0.7
Anchors larger than ``pos_iou_thresh`` is regarded as positive samples.
neg_iou_thresh : float, default is 0.3
Anchors smaller than ``neg_iou_thresh`` is regarded as negative samples.
Anchors with IOU in between ``pos_iou_thresh`` and ``neg_iou_thresh`` are
ignored.
pos_ratio : float, default is 0.5
``pos_ratio`` defines how many positive samples (``pos_ratio * num_sample``) is
to be sampled.
ashape : int, default is 128
Defines shape of pre generated anchors for target generation
multi_stage : boolean, default is False
Whether the network output multi stage features.
"""
def __init__(self, short=600, max_size=1000, net=None, mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225), box_norm=(1., 1., 1., 1.),
num_sample=256, pos_iou_thresh=0.7, neg_iou_thresh=0.3,
pos_ratio=0.5, ashape=128, multi_stage=False, **kwargs):
self._short = short
self._max_size = max_size
self._mean = mean
self._std = std
self._anchors = None
self._multi_stage = multi_stage
self._random_resize = isinstance(self._short, (tuple, list))
if net is None:
return
# use fake data to generate fixed anchors for target generation
anchors = [] # [P2, P3, P4, P5]
# in case network has reset_ctx to gpu
anchor_generator = copy.deepcopy(net.rpn.anchor_generator)
anchor_generator.collect_params().reset_ctx(None)
if self._multi_stage:
for ag in anchor_generator:
anchor = ag(mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
ashape = max(ashape // 2, 16)
anchors.append(anchor)
else:
anchors = anchor_generator(
mx.nd.zeros((1, 3, ashape, ashape))).reshape((1, 1, ashape, ashape, -1))
self._anchors = anchors
# record feature extractor for infer_shape
if not hasattr(net, 'features'):
raise ValueError("Cannot find features in network, it is a Mask RCNN network?")
self._feat_sym = net.features(mx.sym.var(name='data'))
from ....model_zoo.rcnn.rpn.rpn_target import RPNTargetGenerator
self._target_generator = RPNTargetGenerator(
num_sample=num_sample, pos_iou_thresh=pos_iou_thresh,
neg_iou_thresh=neg_iou_thresh, pos_ratio=pos_ratio,
stds=box_norm, **kwargs)
def __call__(self, src, label, segm):
"""Apply transform to training image/label."""
# resize shorter side but keep in max_size
h, w, _ = src.shape
if self._random_resize:
short = randint(self._short[0], self._short[1])
else:
short = self._short
img = timage.resize_short_within(src, short, self._max_size, interp=1)
bbox = tbbox.resize(label, (w, h), (img.shape[1], img.shape[0]))
segm = [tmask.resize(polys, (w, h), (img.shape[1], img.shape[0])) for polys in segm]
# random horizontal flip
h, w, _ = img.shape
img, flips = timage.random_flip(img, px=0.5)
bbox = tbbox.flip(bbox, (w, h), flip_x=flips[0])
segm = [tmask.flip(polys, (w, h), flip_x=flips[0]) for polys in segm]
# gt_masks (n, im_height, im_width) of uint8 -> float32 (cannot take uint8)
masks = [mx.nd.array(tmask.to_mask(polys, (w, h))) for polys in segm]
# n * (im_height, im_width) -> (n, im_height, im_width)
masks = mx.nd.stack(*masks, axis=0)
# to tensor
img = mx.nd.image.to_tensor(img)
img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
if self._anchors is None:
return img, bbox.astype(img.dtype), masks
# generate RPN target so cpu workers can help reduce the workload
# feat_h, feat_w = (img.shape[1] // self._stride, img.shape[2] // self._stride)
gt_bboxes = mx.nd.array(bbox[:, :4])
if self._multi_stage:
anchor_targets = []
oshapes = []
cls_targets, box_targets, box_masks = [], [], []
for anchor, feat_sym in zip(self._anchors, self._feat_sym):
oshape = feat_sym.infer_shape(data=(1, 3, img.shape[1], img.shape[2]))[1][0]
anchor = anchor[:, :, :oshape[2], :oshape[3], :]
oshapes.append(anchor.shape)
anchor_targets.append(anchor.reshape((-1, 4)))
anchor_targets = mx.nd.concat(*anchor_targets, dim=0)
cls_target, box_target, box_mask = self._target_generator(
gt_bboxes, anchor_targets, img.shape[2], img.shape[1])
start_ind = 0
for oshape in oshapes:
size = oshape[2] * oshape[3] * (oshape[4] // 4)
lvl_cls_target = cls_target[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
lvl_box_target = box_target[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
lvl_box_mask = box_mask[start_ind:start_ind + size] \
.reshape(oshape[2], oshape[3], -1)
start_ind += size
cls_targets.append(lvl_cls_target)
box_targets.append(lvl_box_target)
box_masks.append(lvl_box_mask)
else:
oshape = self._feat_sym.infer_shape(data=(1, 3, img.shape[1], img.shape[2]))[1][0]
anchor = self._anchors[:, :, :oshape[2], :oshape[3], :]
oshape = anchor.shape
cls_target, box_target, box_mask = self._target_generator(
gt_bboxes, anchor.reshape((-1, 4)), img.shape[2], img.shape[1])
size = oshape[2] * oshape[3] * (oshape[4] // 4)
cls_targets = [cls_target[0:size].reshape(oshape[2], oshape[3], -1)]
box_targets = [box_target[0:size].reshape(oshape[2], oshape[3], -1)]
box_masks = [box_mask[0:size].reshape(oshape[2], oshape[3], -1)]
return img, bbox.astype(img.dtype), cls_targets, box_targets, box_masks, masks
class MaskRCNNDefaultValTransform(object):
"""Default Mask RCNN validation transform.
Parameters
----------
short : int, default is 600
Resize image shorter side to ``short``.
max_size : int, default is 1000
Make sure image longer side is smaller than ``max_size``.
mean : array-like of size 3
Mean pixel values to be subtracted from image tensor. Default is [0.485, 0.456, 0.406].
std : array-like of size 3
Standard deviation to be divided from image. Default is [0.229, 0.224, 0.225].
"""
def __init__(self, short=600, max_size=1000,
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)):
self._mean = mean
self._std = std
self._short = short
self._max_size = max_size
def __call__(self, src, label, mask):
"""Apply transform to validation image/label."""
# resize shorter side but keep in max_size
h, _, _ = src.shape
img = timage.resize_short_within(src, self._short, self._max_size, interp=1)
# no scaling ground-truth, return image scaling ratio instead
im_scale = float(img.shape[0]) / h
img = mx.nd.image.to_tensor(img)
img = mx.nd.image.normalize(img, mean=self._mean, std=self._std)
return img, mx.nd.array([img.shape[-2], img.shape[-1], im_scale])