ops.py 16.9 KB
Newer Older
F
FDInSky 已提交
1 2 3 4 5 6 7 8 9 10 11
import numpy as np
from numbers import Integral
import paddle.fluid as fluid
from paddle.fluid.dygraph.base import to_variable
from ppdet.core.workspace import register, serializable
from ppdet.py_op.target import generate_rpn_anchor_target, generate_proposal_target, generate_mask_target
from ppdet.py_op.post_process import bbox_post_process


@register
@serializable
12
class AnchorGeneratorRPN(object):
F
FDInSky 已提交
13 14 15 16
    def __init__(self,
                 anchor_sizes=[32, 64, 128, 256, 512],
                 aspect_ratios=[0.5, 1.0, 2.0],
                 stride=[16.0, 16.0],
17 18
                 variance=[1.0, 1.0, 1.0, 1.0],
                 anchor_start_size=None):
19
        super(AnchorGeneratorRPN, self).__init__()
F
FDInSky 已提交
20 21 22 23
        self.anchor_sizes = anchor_sizes
        self.aspect_ratios = aspect_ratios
        self.stride = stride
        self.variance = variance
24 25 26 27 28 29 30 31 32 33 34 35
        self.anchor_start_size = anchor_start_size

    def __call__(self, input, level=None):
        anchor_sizes = self.anchor_sizes if (
            level is None or self.anchor_start_size is None) else (
                self.anchor_start_size * 2**level)
        stride = self.stride if (
            level is None or self.anchor_start_size is None) else (
                self.stride[0] * (2.**level), self.stride[1] * (2.**level))
        anchor, var = fluid.layers.anchor_generator(
            input=input,
            anchor_sizes=anchor_sizes,
F
FDInSky 已提交
36
            aspect_ratios=self.aspect_ratios,
37
            stride=stride,
F
FDInSky 已提交
38
            variance=self.variance)
39
        return anchor, var
F
FDInSky 已提交
40 41 42 43


@register
@serializable
44
class AnchorTargetGeneratorRPN(object):
F
FDInSky 已提交
45 46 47 48 49 50 51
    def __init__(self,
                 batch_size_per_im=256,
                 straddle_thresh=0.,
                 fg_fraction=0.5,
                 positive_overlap=0.7,
                 negative_overlap=0.3,
                 use_random=True):
52
        super(AnchorTargetGeneratorRPN, self).__init__()
F
FDInSky 已提交
53 54 55 56 57 58 59
        self.batch_size_per_im = batch_size_per_im
        self.straddle_thresh = straddle_thresh
        self.fg_fraction = fg_fraction
        self.positive_overlap = positive_overlap
        self.negative_overlap = negative_overlap
        self.use_random = use_random

60 61
    def __call__(self, cls_logits, bbox_pred, anchor_box, gt_boxes, is_crowd,
                 im_info):
F
FDInSky 已提交
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
        anchor_box = anchor_box.numpy()
        gt_boxes = gt_boxes.numpy()
        is_crowd = is_crowd.numpy()
        im_info = im_info.numpy()
        loc_indexes, score_indexes, tgt_labels, tgt_bboxes, bbox_inside_weights = generate_rpn_anchor_target(
            anchor_box, gt_boxes, is_crowd, im_info, self.straddle_thresh,
            self.batch_size_per_im, self.positive_overlap,
            self.negative_overlap, self.fg_fraction, self.use_random)

        loc_indexes = to_variable(loc_indexes)
        score_indexes = to_variable(score_indexes)
        tgt_labels = to_variable(tgt_labels)
        tgt_bboxes = to_variable(tgt_bboxes)
        bbox_inside_weights = to_variable(bbox_inside_weights)

        loc_indexes.stop_gradient = True
        score_indexes.stop_gradient = True
        tgt_labels.stop_gradient = True

        cls_logits = fluid.layers.reshape(x=cls_logits, shape=(-1, ))
        bbox_pred = fluid.layers.reshape(x=bbox_pred, shape=(-1, 4))
        pred_cls_logits = fluid.layers.gather(cls_logits, score_indexes)
        pred_bbox_pred = fluid.layers.gather(bbox_pred, loc_indexes)

        return pred_cls_logits, pred_bbox_pred, tgt_labels, tgt_bboxes, bbox_inside_weights


89 90 91 92 93 94 95 96 97 98 99 100 101
@register
@serializable
class AnchorGeneratorYOLO(object):
    def __init__(self,
                 anchors=[
                     10, 13, 16, 30, 33, 23, 30, 61, 62, 45, 59, 119, 116, 90,
                     156, 198, 373, 326
                 ],
                 anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]]):
        super(AnchorGeneratorYOLO, self).__init__()
        self.anchors = anchors
        self.anchor_masks = anchor_masks

W
wangguanzhong 已提交
102 103
    def __call__(self):
        anchor_num = len(self.anchors)
104
        mask_anchors = []
W
wangguanzhong 已提交
105
        for i in range(len(self.anchor_masks)):
106 107
            mask_anchor = []
            for m in self.anchor_masks[i]:
W
wangguanzhong 已提交
108 109
                assert m < anchor_num, "anchor mask index overflow"
                mask_anchor.extend(self.anchors[2 * m:2 * m + 2])
110
            mask_anchors.append(mask_anchor)
W
wangguanzhong 已提交
111
        return self.anchors, self.anchor_masks, mask_anchors
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135


@register
@serializable
class AnchorTargetGeneratorYOLO(object):
    def __init__(self,
                 ignore_thresh=0.7,
                 downsample_ratio=32,
                 label_smooth=True):
        super(AnchorTargetGeneratorYOLO, self).__init__()
        self.ignore_thresh = ignore_thresh
        self.downsample_ratio = downsample_ratio
        self.label_smooth = label_smooth

    def __call__(self, ):
        # TODO: split yolov3_loss into here 
        outs = {
            'ignore_thresh': self.ignore_thresh,
            'downsample_ratio': self.downsample_ratio,
            'label_smooth': self.label_smooth
        }
        return outs


F
FDInSky 已提交
136 137 138 139 140 141 142 143 144 145 146 147
@register
@serializable
class ProposalGenerator(object):
    __append_doc__ = True

    def __init__(self,
                 train_pre_nms_top_n=12000,
                 train_post_nms_top_n=2000,
                 infer_pre_nms_top_n=6000,
                 infer_post_nms_top_n=1000,
                 nms_thresh=.5,
                 min_size=.1,
148
                 eta=1.):
F
FDInSky 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        super(ProposalGenerator, self).__init__()
        self.train_pre_nms_top_n = train_pre_nms_top_n
        self.train_post_nms_top_n = train_post_nms_top_n
        self.infer_pre_nms_top_n = infer_pre_nms_top_n
        self.infer_post_nms_top_n = infer_post_nms_top_n
        self.nms_thresh = nms_thresh
        self.min_size = min_size
        self.eta = eta

    def __call__(self,
                 scores,
                 bbox_deltas,
                 anchors,
                 variances,
                 im_info,
                 mode='train'):
        pre_nms_top_n = self.train_pre_nms_top_n if mode == 'train' else self.infer_pre_nms_top_n
        post_nms_top_n = self.train_post_nms_top_n if mode == 'train' else self.infer_post_nms_top_n
167
        rpn_rois, rpn_rois_prob, rpn_rois_num = fluid.layers.generate_proposals(
F
FDInSky 已提交
168 169 170 171 172 173 174 175 176 177
            scores,
            bbox_deltas,
            im_info,
            anchors,
            variances,
            pre_nms_top_n=pre_nms_top_n,
            post_nms_top_n=post_nms_top_n,
            nms_thresh=self.nms_thresh,
            min_size=self.min_size,
            eta=self.eta,
178 179
            return_rois_num=True)
        return rpn_rois, rpn_rois_prob, rpn_rois_num, post_nms_top_n
F
FDInSky 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194


@register
@serializable
class ProposalTargetGenerator(object):
    __shared__ = ['num_classes']

    def __init__(self,
                 batch_size_per_im=512,
                 fg_fraction=.25,
                 fg_thresh=[.5, ],
                 bg_thresh_hi=[.5, ],
                 bg_thresh_lo=[0., ],
                 bbox_reg_weights=[[0.1, 0.1, 0.2, 0.2]],
                 num_classes=81,
195
                 use_random=True,
F
FDInSky 已提交
196 197 198 199 200 201 202 203 204 205
                 is_cls_agnostic=False,
                 is_cascade_rcnn=False):
        super(ProposalTargetGenerator, self).__init__()
        self.batch_size_per_im = batch_size_per_im
        self.fg_fraction = fg_fraction
        self.fg_thresh = fg_thresh
        self.bg_thresh_hi = bg_thresh_hi
        self.bg_thresh_lo = bg_thresh_lo
        self.bbox_reg_weights = bbox_reg_weights
        self.num_classes = num_classes
206
        self.use_random = use_random
207
        self.is_cls_agnostic = is_cls_agnostic
F
FDInSky 已提交
208 209 210 211
        self.is_cascade_rcnn = is_cascade_rcnn

    def __call__(self,
                 rpn_rois,
212
                 rpn_rois_num,
F
FDInSky 已提交
213 214 215 216
                 gt_classes,
                 is_crowd,
                 gt_boxes,
                 im_info,
217
                 stage=0):
F
FDInSky 已提交
218
        rpn_rois = rpn_rois.numpy()
219
        rpn_rois_num = rpn_rois_num.numpy()
F
FDInSky 已提交
220 221 222 223 224
        gt_classes = gt_classes.numpy()
        gt_boxes = gt_boxes.numpy()
        is_crowd = is_crowd.numpy()
        im_info = im_info.numpy()
        outs = generate_proposal_target(
225
            rpn_rois, rpn_rois_num, gt_classes, is_crowd, gt_boxes, im_info,
F
FDInSky 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
            self.batch_size_per_im, self.fg_fraction, self.fg_thresh[stage],
            self.bg_thresh_hi[stage], self.bg_thresh_lo[stage],
            self.bbox_reg_weights[stage], self.num_classes, self.use_random,
            self.is_cls_agnostic, self.is_cascade_rcnn)
        outs = [to_variable(v) for v in outs]
        for v in outs:
            v.stop_gradient = True
        return outs


@register
@serializable
class MaskTargetGenerator(object):
239
    __shared__ = ['num_classes', 'mask_resolution']
F
FDInSky 已提交
240

241
    def __init__(self, num_classes=81, mask_resolution=14):
F
FDInSky 已提交
242 243
        super(MaskTargetGenerator, self).__init__()
        self.num_classes = num_classes
244
        self.mask_resolution = mask_resolution
F
FDInSky 已提交
245

246
    def __call__(self, im_info, gt_classes, is_crowd, gt_segms, rois, rois_num,
F
FDInSky 已提交
247 248 249 250 251 252
                 labels_int32):
        im_info = im_info.numpy()
        gt_classes = gt_classes.numpy()
        is_crowd = is_crowd.numpy()
        gt_segms = gt_segms.numpy()
        rois = rois.numpy()
253
        rois_num = rois_num.numpy()
F
FDInSky 已提交
254 255
        labels_int32 = labels_int32.numpy()
        outs = generate_mask_target(im_info, gt_classes, is_crowd, gt_segms,
256 257
                                    rois, rois_num, labels_int32,
                                    self.num_classes, self.mask_resolution)
F
FDInSky 已提交
258 259 260 261 262 263 264 265 266 267

        outs = [to_variable(v) for v in outs]
        for v in outs:
            v.stop_gradient = True
        return outs


@register
class RoIExtractor(object):
    def __init__(self,
268
                 resolution=14,
F
FDInSky 已提交
269
                 sampling_ratio=0,
270 271 272 273
                 canconical_level=4,
                 canonical_size=224,
                 start_level=0,
                 end_level=3):
F
FDInSky 已提交
274 275 276
        super(RoIExtractor, self).__init__()
        self.resolution = resolution
        self.sampling_ratio = sampling_ratio
277 278 279 280
        self.canconical_level = canconical_level
        self.canonical_size = canonical_size
        self.start_level = start_level
        self.end_level = end_level
F
FDInSky 已提交
281

282 283
    def __call__(self, feats, rois, spatial_scale):
        roi, rois_num = rois
F
FDInSky 已提交
284
        cur_l = 0
285
        if self.start_level == self.end_level:
F
FDInSky 已提交
286
            rois_feat = fluid.layers.roi_align(
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
                feats[self.start_level],
                roi,
                self.resolution,
                self.resolution,
                spatial_scale,
                rois_num=rois_num)
            return rois_feat
        offset = 2
        k_min = self.start_level + offset
        k_max = self.end_level + offset
        rois_dist, restore_index, rois_num_dist = fluid.layers.distribute_fpn_proposals(
            roi,
            k_min,
            k_max,
            self.canconical_level,
            self.canonical_size,
            rois_num=rois_num)
W
wangguanzhong 已提交
304

305 306 307 308 309 310 311 312 313 314 315 316 317
        rois_feat_list = []
        for lvl in range(self.start_level, self.end_level + 1):
            roi_feat = fluid.layers.roi_align(
                feats[lvl],
                rois_dist[lvl],
                self.resolution,
                self.resolution,
                spatial_scale[lvl],
                sampling_ratio=self.sampling_ratio,
                rois_num=rois_num_dist[lvl])
            rois_feat_list.append(roi_feat)
        rois_feat_shuffle = fluid.layers.concat(rois_feat_list)
        rois_feat = fluid.layers.gather(rois_feat_shuffle, restore_index)
F
FDInSky 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

        return rois_feat


@register
@serializable
class DecodeClipNms(object):
    __shared__ = ['num_classes']

    def __init__(
            self,
            num_classes=81,
            keep_top_k=100,
            score_threshold=0.05,
            nms_threshold=0.5, ):
        super(DecodeClipNms, self).__init__()
        self.num_classes = num_classes
        self.keep_top_k = keep_top_k
        self.score_threshold = score_threshold
        self.nms_threshold = nms_threshold

339 340 341 342
    def __call__(self, bboxes, bbox_prob, bbox_delta, im_info):
        bboxes_np = (i.numpy() for i in bboxes)
        # bbox, bbox_num
        outs = bbox_post_process(bboxes_np,
F
FDInSky 已提交
343 344
                                 bbox_prob.numpy(),
                                 bbox_delta.numpy(),
345
                                 im_info.numpy(), self.keep_top_k,
F
FDInSky 已提交
346 347 348 349 350 351 352 353
                                 self.score_threshold, self.nms_threshold,
                                 self.num_classes)
        outs = [to_variable(v) for v in outs]
        for v in outs:
            v.stop_gradient = True
        return outs


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
@register
@serializable
class MultiClassNMS(object):
    __op__ = fluid.layers.multiclass_nms
    __append_doc__ = True

    def __init__(self,
                 score_threshold=.05,
                 nms_top_k=-1,
                 keep_top_k=100,
                 nms_threshold=.5,
                 normalized=False,
                 nms_eta=1.0,
                 background_label=0):
        super(MultiClassNMS, self).__init__()
        self.score_threshold = score_threshold
        self.nms_top_k = nms_top_k
        self.keep_top_k = keep_top_k
        self.nms_threshold = nms_threshold
        self.normalized = normalized
        self.nms_eta = nms_eta
        self.background_label = background_label


@register
@serializable
class YOLOBox(object):
    def __init__(
            self,
            conf_thresh=0.005,
            downsample_ratio=32,
            clip_bbox=True, ):
        self.conf_thresh = conf_thresh
        self.downsample_ratio = downsample_ratio
        self.clip_bbox = clip_bbox

W
wangguanzhong 已提交
390 391
    def __call__(self, x, img_size, anchors, num_classes, stage=0):
        outs = fluid.layers.yolo_box(x, img_size, anchors, num_classes,
392
                                     self.conf_thresh, self.downsample_ratio //
W
wangguanzhong 已提交
393
                                     2**stage, self.clip_bbox)
394 395 396
        return outs


F
FDInSky 已提交
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
@register
@serializable
class AnchorGrid(object):
    """Generate anchor grid

    Args:
        image_size (int or list): input image size, may be a single integer or
            list of [h, w]. Default: 512
        min_level (int): min level of the feature pyramid. Default: 3
        max_level (int): max level of the feature pyramid. Default: 7
        anchor_base_scale: base anchor scale. Default: 4
        num_scales: number of anchor scales. Default: 3
        aspect_ratios: aspect ratios. default: [[1, 1], [1.4, 0.7], [0.7, 1.4]]
    """

    def __init__(self,
                 image_size=512,
                 min_level=3,
                 max_level=7,
                 anchor_base_scale=4,
                 num_scales=3,
                 aspect_ratios=[[1, 1], [1.4, 0.7], [0.7, 1.4]]):
        super(AnchorGrid, self).__init__()
        if isinstance(image_size, Integral):
            self.image_size = [image_size, image_size]
        else:
            self.image_size = image_size
        for dim in self.image_size:
            assert dim % 2 ** max_level == 0, \
                "image size should be multiple of the max level stride"
        self.min_level = min_level
        self.max_level = max_level
        self.anchor_base_scale = anchor_base_scale
        self.num_scales = num_scales
        self.aspect_ratios = aspect_ratios

    @property
    def base_cell(self):
        if not hasattr(self, '_base_cell'):
            self._base_cell = self.make_cell()
        return self._base_cell

    def make_cell(self):
        scales = [2**(i / self.num_scales) for i in range(self.num_scales)]
        scales = np.array(scales)
        ratios = np.array(self.aspect_ratios)
        ws = np.outer(scales, ratios[:, 0]).reshape(-1, 1)
        hs = np.outer(scales, ratios[:, 1]).reshape(-1, 1)
        anchors = np.hstack((-0.5 * ws, -0.5 * hs, 0.5 * ws, 0.5 * hs))
        return anchors

    def make_grid(self, stride):
        cell = self.base_cell * stride * self.anchor_base_scale
        x_steps = np.arange(stride // 2, self.image_size[1], stride)
        y_steps = np.arange(stride // 2, self.image_size[0], stride)
        offset_x, offset_y = np.meshgrid(x_steps, y_steps)
        offset_x = offset_x.flatten()
        offset_y = offset_y.flatten()
        offsets = np.stack((offset_x, offset_y, offset_x, offset_y), axis=-1)
        offsets = offsets[:, np.newaxis, :]
        return (cell + offsets).reshape(-1, 4)

    def generate(self):
        return [
            self.make_grid(2**l)
            for l in range(self.min_level, self.max_level + 1)
        ]

    def __call__(self):
        if not hasattr(self, '_anchor_vars'):
            anchor_vars = []
            helper = LayerHelper('anchor_grid')
            for idx, l in enumerate(range(self.min_level, self.max_level + 1)):
                stride = 2**l
                anchors = self.make_grid(stride)
                var = helper.create_parameter(
                    attr=ParamAttr(name='anchors_{}'.format(idx)),
                    shape=anchors.shape,
                    dtype='float32',
                    stop_gradient=True,
                    default_initializer=NumpyArrayInitializer(anchors))
                anchor_vars.append(var)
                var.persistable = True
            self._anchor_vars = anchor_vars

        return self._anchor_vars