Exemplo n.º 1
0
    def __call__(self, x, im_info, gpu=-1, gt_boxes=None):
        n = x.data.shape[0]
        h = F.relu(self.rpn_conv_3x3(x))
        rpn_cls_score = self.rpn_cls_score(h)
        rpn_bbox_pred = self.rpn_bbox_pred(h)
        rpn_cls_score_reshape = F.reshape(rpn_cls_score, (n, 2, -1))

        # RoI Proposal
        rpn_cls_prob = F.softmax(rpn_cls_score_reshape)
        c, hh, ww = rpn_cls_score.data.shape[1:]
        rpn_cls_prob_reshape = F.reshape(rpn_cls_prob, (n, c, hh, ww))
        rois = self.proposal_layer(
            rpn_cls_prob_reshape, rpn_bbox_pred, im_info, gt_boxes is not None)

        if gt_boxes is not None:
            rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, \
                rpn_bbox_outside_weights = self.anchor_target_layer(
                    rpn_cls_score, gt_boxes, im_info)
            rpn_labels = rpn_labels.reshape((1, -1))

            # make it into Variable
            if gpu >= 0:
                tg = lambda x: to_gpu(x, device=gpu)
                rpn_labels = tg(rpn_labels)
                rpn_bbox_targets = tg(rpn_bbox_targets)
                rpn_bbox_inside_weights = tg(rpn_bbox_inside_weights)
                rpn_bbox_outside_weights = tg(rpn_bbox_outside_weights)
            volatile = 'off' if gt_boxes is not None else 'on'
            rpn_labels = Variable(rpn_labels, volatile=volatile)
            rpn_bbox_targets = Variable(rpn_bbox_targets, volatile=volatile)
            # rpn_bbox_inside_weights = Variable(
            #     rpn_bbox_inside_weights, volatile=volatile)
            # rpn_bbox_outside_weights = Variable(
            #     rpn_bbox_outside_weights, volatile=volatile)

            rpn_cls_loss = F.softmax_cross_entropy(
                rpn_cls_score_reshape, rpn_labels)
            rpn_loss_bbox = smooth_l1_loss(
                rpn_bbox_pred, rpn_bbox_targets, rpn_bbox_inside_weights,
                rpn_bbox_outside_weights, self.rpn_sigma)

            return rpn_cls_loss, rpn_loss_bbox, rois
        else:
            return rois
Exemplo n.º 2
0
    def __call__(self, x, im_info, gpu=-1, gt_boxes=None):
        n = x.data.shape[0]
        h = F.relu(self.rpn_conv_3x3(x))
        rpn_cls_score = self.rpn_cls_score(h)
        rpn_bbox_pred = self.rpn_bbox_pred(h)
        rpn_cls_score_reshape = F.reshape(rpn_cls_score, (n, 2, -1))

        # RoI Proposal
        rpn_cls_prob = F.softmax(rpn_cls_score_reshape)
        c, hh, ww = rpn_cls_score.data.shape[1:]
        rpn_cls_prob_reshape = F.reshape(rpn_cls_prob, (n, c, hh, ww))
        rois = self.proposal_layer(
            rpn_cls_prob_reshape, rpn_bbox_pred, im_info, gt_boxes is not None)

        if gt_boxes is not None:
            rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, \
                rpn_bbox_outside_weights = self.anchor_target_layer(
                    rpn_cls_score, gt_boxes, im_info)
            rpn_labels = rpn_labels.reshape((1, -1))

            # make it into Variable
            if gpu >= 0:
                tg = lambda x: to_gpu(x, device=gpu)
                rpn_labels = tg(rpn_labels)
                rpn_bbox_targets = tg(rpn_bbox_targets)
                rpn_bbox_inside_weights = tg(rpn_bbox_inside_weights)
                rpn_bbox_outside_weights = tg(rpn_bbox_outside_weights)
            rpn_labels = Variable(rpn_labels, volatile='auto')
            rpn_bbox_targets = Variable(rpn_bbox_targets, volatile='auto')
            # rpn_bbox_inside_weights = Variable(
            #     rpn_bbox_inside_weights, volatile=volatile)
            # rpn_bbox_outside_weights = Variable(
            #     rpn_bbox_outside_weights, volatile=volatile)

            rpn_cls_loss = F.softmax_cross_entropy(
                rpn_cls_score_reshape, rpn_labels)
            rpn_loss_bbox = smooth_l1_loss(
                rpn_bbox_pred, rpn_bbox_targets, rpn_bbox_inside_weights,
                rpn_bbox_outside_weights, self.rpn_sigma)

            return rpn_cls_loss, rpn_loss_bbox, rois
        else:
            return rois
Exemplo n.º 3
0
    def __call__(self, x, im_info, gt_boxes=None):
        h = self.trunk(x)
        if isinstance(im_info, chainer.cuda.cupy.ndarray):
            im_info = chainer.cuda.cupy.asnumpy(im_info)
        if self.train:
            im_info = im_info.data
            gt_boxes = gt_boxes.data
            if isinstance(gt_boxes, chainer.cuda.cupy.ndarray):
                im_info = chainer.cuda.cupy.asnumpy(im_info)
                gt_boxes = chainer.cuda.cupy.asnumpy(gt_boxes)
            rpn_cls_loss, rpn_loss_bbox, rois = self.RPN(
                h, im_info, self.gpu, gt_boxes)
        else:
            rois = self.RPN(h, im_info, self.gpu, gt_boxes)

        if self.train:
            rois, labels, bbox_targets, bbox_inside_weights, \
                bbox_outside_weights = self.proposal_target_layer(
                    rois, gt_boxes)

        # Convert rois
        if self.gpu >= 0:
            rois = to_gpu(rois, device=self.gpu)
            im_info = to_gpu(im_info, device=self.gpu)
            with chainer.cuda.Device(self.gpu):
                boxes = rois[:, 1:5] / im_info[0][2]
        else:
            boxes = rois[:, 1:5] / im_info[0][2]

        # RCNN
        pool5 = roi_pooling_2d(
            self.trunk.feature, rois, 7, 7, self.spatial_scale)
        fc6 = F.dropout(F.relu(self.fc6(pool5)), train=self.train)
        fc7 = F.dropout(F.relu(self.fc7(fc6)), train=self.train)

        # Per class probability
        cls_score = self.cls_score(fc7)
        cls_prob = F.softmax(cls_score)

        # BBox predictions
        bbox_pred = self.bbox_pred(fc7)
        box_deltas = bbox_pred.data

        if self.train:
            if self.gpu >= 0:
                tg = lambda x: to_gpu(x, device=self.gpu)
                labels = tg(labels)
                bbox_targets = tg(bbox_targets)
                bbox_inside_weights = tg(bbox_inside_weights)
                bbox_outside_weights = tg(bbox_outside_weights)
            loss_cls = F.softmax_cross_entropy(cls_score, labels)
            labels = Variable(labels, volatile='off')
            bbox_targets = Variable(bbox_targets, volatile='off')
            loss_bbox = smooth_l1_loss(
                bbox_pred, bbox_targets, bbox_inside_weights,
                bbox_outside_weights, self.sigma)

            reporter.report({'rpn_loss_cls': rpn_cls_loss,
                             'rpn_loss_bbox': rpn_loss_bbox,
                             'loss_bbox': loss_bbox,
                             'loss_cls': loss_cls}, self)

            return rpn_cls_loss, rpn_loss_bbox, loss_bbox, loss_cls
        else:
            pred_boxes = bbox_transform_inv(boxes, box_deltas, self.gpu)
            pred_boxes = clip_boxes(pred_boxes, im_info[0][:2], self.gpu)

            return cls_prob, pred_boxes
Exemplo n.º 4
0
    def __call__(self, x, im_info, gt_boxes=None):
        h = self.trunk(x)
        if chainer.cuda.available \
                and isinstance(im_info, chainer.cuda.cupy.ndarray):
            im_info = chainer.cuda.cupy.asnumpy(im_info)
        if self.train:
            im_info = im_info.data
            gt_boxes = gt_boxes.data
            if isinstance(gt_boxes, chainer.cuda.cupy.ndarray):
                im_info = chainer.cuda.cupy.asnumpy(im_info)
                gt_boxes = chainer.cuda.cupy.asnumpy(gt_boxes)
            rpn_cls_loss, rpn_loss_bbox, rois = self.RPN(
                h, im_info, self.gpu, gt_boxes)
        else:
            rois = self.RPN(h, im_info, self.gpu, gt_boxes)

        if self.train:
            rois, labels, bbox_targets, bbox_inside_weights, \
                bbox_outside_weights = self.proposal_target_layer(
                    rois, gt_boxes)

        # Convert rois
        if self.gpu >= 0:
            rois = to_gpu(rois, device=self.gpu)
            im_info = to_gpu(im_info, device=self.gpu)
            with chainer.cuda.Device(self.gpu):
                boxes = rois[:, 1:5] / im_info[0][2]
        else:
            boxes = rois[:, 1:5] / im_info[0][2]

        # RCNN
        pool5 = roi_pooling_2d(self.trunk.feature, rois, 7, 7,
                               self.spatial_scale)
        fc6 = F.dropout(F.relu(self.fc6(pool5)), train=self.train)
        fc7 = F.dropout(F.relu(self.fc7(fc6)), train=self.train)

        # Per class probability
        cls_score = self.cls_score(fc7)
        cls_prob = F.softmax(cls_score)

        # BBox predictions
        bbox_pred = self.bbox_pred(fc7)
        box_deltas = bbox_pred.data

        if self.train:
            if self.gpu >= 0:
                tg = lambda x: to_gpu(x, device=self.gpu)
                labels = tg(labels)
                bbox_targets = tg(bbox_targets)
                bbox_inside_weights = tg(bbox_inside_weights)
                bbox_outside_weights = tg(bbox_outside_weights)
            loss_cls = F.softmax_cross_entropy(cls_score, labels)
            labels = Variable(labels, volatile='auto')
            bbox_targets = Variable(bbox_targets, volatile='auto')
            loss_bbox = smooth_l1_loss(bbox_pred, bbox_targets,
                                       bbox_inside_weights,
                                       bbox_outside_weights, self.sigma)

            reporter.report(
                {
                    'rpn_loss_cls': rpn_cls_loss,
                    'rpn_loss_bbox': rpn_loss_bbox,
                    'loss_bbox': loss_bbox,
                    'loss_cls': loss_cls
                }, self)

            return rpn_cls_loss, rpn_loss_bbox, loss_bbox, loss_cls
        else:
            pred_boxes = bbox_transform_inv(boxes, box_deltas, self.gpu)
            pred_boxes = clip_boxes(pred_boxes, im_info[0][:2], self.gpu)

            return cls_prob, pred_boxes