Esempio n. 1
0
    def _pool(
            self, h_cls_seg, h_ag_loc, rois, roi_indices):
        # PSROI Pooling
        # shape: (n_roi, n_class, 2, roi_size, roi_size)
        roi_cls_ag_seg_scores = psroi_pooling_2d(
            h_cls_seg, rois, roi_indices,
            self.n_class * 2, self.roi_size, self.roi_size,
            self.spatial_scale, self.group_size)
        roi_cls_ag_seg_scores = roi_cls_ag_seg_scores.reshape(
            (-1, self.n_class, 2, self.roi_size, self.roi_size))

        # shape: (n_roi, 2*4, roi_size, roi_size)
        roi_ag_loc_scores = psroi_pooling_2d(
            h_ag_loc, rois, roi_indices,
            2 * 4, self.roi_size, self.roi_size,
            self.spatial_scale, self.group_size)

        # shape: (n_roi, n_class)
        roi_cls_scores = _global_average_pooling_2d(
            F.max(roi_cls_ag_seg_scores, axis=2))

        # Bbox Regression
        # shape: (n_roi, 2*4)
        roi_ag_locs = _global_average_pooling_2d(roi_ag_loc_scores)
        roi_ag_locs = roi_ag_locs.reshape((-1, 2, 4))

        # Mask Regression
        # shape: (n_roi, n_class, 2, roi_size, roi_size)
        max_cls_indices = roi_cls_scores.array.argmax(axis=1)
        # shape: (n_roi, 2, roi_size, roi_size)
        roi_ag_seg_scores = roi_cls_ag_seg_scores[
            self.xp.arange(len(max_cls_indices)), max_cls_indices]

        return roi_ag_seg_scores, roi_ag_locs, roi_cls_scores
Esempio n. 2
0
 def apply_backward(self, x_data, roi_data, roi_index_data, y_grad_data):
     x = chainer.Variable(x_data)
     rois = chainer.Variable(roi_data)
     roi_indices = chainer.Variable(roi_index_data)
     y = functions.psroi_pooling_2d(x, rois, roi_indices, self.out_c,
                                    self.out_h, self.out_w,
                                    self.spatial_scale, self.group_size)
     x.cleargrad()
     y.grad = y_grad_data
     y.backward()
     return x, y
Esempio n. 3
0
 def check_forward(self, x_data, roi_data, roi_index_data):
     x = chainer.Variable(x_data)
     rois = chainer.Variable(roi_data)
     roi_indices = chainer.Variable(roi_index_data)
     y = functions.psroi_pooling_2d(x, rois, roi_indices, self.out_c,
                                    self.out_h, self.out_w,
                                    self.spatial_scale, self.group_size)
     self.assertEqual(y.data.dtype, np.float32)
     y_data = cuda.to_cpu(y.data)
     self.assertEqual((self.n_roi, self.out_c, self.out_h, self.out_w),
                      y_data.shape)
Esempio n. 4
0
    def _pool(self, h_cls_seg, h_ag_loc, rois, roi_indices, gt_roi_labels):
        # PSROI Pooling
        # shape: (n_roi, n_class, 2, roi_size, roi_size)
        roi_cls_ag_seg_scores = psroi_pooling_2d(h_cls_seg, rois, roi_indices,
                                                 self.n_class * 2,
                                                 self.roi_size, self.roi_size,
                                                 self.spatial_scale,
                                                 self.group_size)
        roi_cls_ag_seg_scores = F.reshape(
            roi_cls_ag_seg_scores,
            (-1, self.n_class, 2, self.roi_size, self.roi_size))

        # shape: (n_roi, 2*4, roi_size, roi_size)
        roi_ag_loc_scores = psroi_pooling_2d(h_ag_loc, rois, roi_indices,
                                             2 * 4, self.roi_size,
                                             self.roi_size, self.spatial_scale,
                                             self.group_size)

        # shape: (n_roi, n_class)
        roi_cls_scores = F.average(F.max(roi_cls_ag_seg_scores, axis=2),
                                   axis=(2, 3))

        # Bbox Regression
        # shape: (n_roi, 2, 4)
        roi_ag_locs = F.average(roi_ag_loc_scores, axis=(2, 3))
        roi_ag_locs = F.reshape(roi_ag_locs, (-1, 2, 4))

        # Mask Regression
        # shape: (n_roi, n_class, 2, roi_size, roi_size)
        if gt_roi_labels is None:
            max_cls_indices = roi_cls_scores.array.argmax(axis=1)
        else:
            max_cls_indices = gt_roi_labels

        # shape: (n_roi, 2, roi_size, roi_size)
        roi_ag_seg_scores = roi_cls_ag_seg_scores[
            self.xp.arange(len(max_cls_indices)), max_cls_indices]

        return roi_ag_seg_scores, roi_ag_locs, roi_cls_scores