Esempio n. 1
0
    def check_mask_voting(self, seg_prob, bbox, cls_prob, size, bg_label,
                          roi_size):
        xp = chainer.cuda.get_array_module(seg_prob)
        seg_prob, bbox, label, cls_prob = mask_voting(seg_prob,
                                                      bbox,
                                                      cls_prob,
                                                      size,
                                                      0.5,
                                                      0.3,
                                                      0.5,
                                                      0.4,
                                                      bg_label=bg_label)

        n_roi = seg_prob.shape[0]
        self.assertIsInstance(seg_prob, xp.ndarray)
        self.assertEqual(seg_prob.shape[1:], (roi_size, roi_size))
        self.assertTrue(
            xp.all(xp.logical_and(seg_prob >= 0.0, seg_prob <= 1.0)))

        self.assertIsInstance(label, xp.ndarray)
        self.assertEqual(label.shape, (n_roi, ))

        self.assertIsInstance(cls_prob, xp.ndarray)
        self.assertEqual(cls_prob.shape, (n_roi, ))

        assert_is_bbox(bbox, size)
Esempio n. 2
0
    def predict(self, imgs):
        """Segment object instances from images.

        This method predicts instance-aware object regions for each image.

        Args:
            imgs (iterable of numpy.ndarray): Arrays holding images of shape
                :math:`(B, C, H, W)`.  All images are in CHW and RGB format
                and the range of their value is :math:`[0, 255]`.

        Returns:
           tuple of lists:
           This method returns a tuple of three lists,
           :obj:`(masks, labels, scores)`.

           * **masks**: A list of boolean arrays of shape :math:`(R, H, W)`, \
               where :math:`R` is the number of masks in a image. \
               Each pixel holds value if it is inside the object inside or not.
           * **labels** : A list of integer arrays of shape :math:`(R,)`. \
               Each value indicates the class of the masks. \
               Values are in range :math:`[0, L - 1]`, where :math:`L` is the \
               number of the foreground classes.
           * **scores** : A list of float arrays of shape :math:`(R,)`. \
               Each value indicates how confident the prediction is.

        """

        prepared_imgs = []
        sizes = []
        for img in imgs:
            size = img.shape[1:]
            img = self.prepare(img.astype(np.float32))
            prepared_imgs.append(img)
            sizes.append(size)

        masks = []
        labels = []
        scores = []

        for img, size in zip(prepared_imgs, sizes):
            with chainer.using_config('train', False), \
                    chainer.function.no_backprop_mode():
                # inference
                img_var = chainer.Variable(self.xp.array(img[None]))
                scale = img_var.shape[3] / size[1]
                roi_ag_seg_scores, _, roi_cls_scores, bboxes, _ = \
                    self.__call__(img_var, scale)

            # We are assuming that batch size is 1.
            roi_ag_seg_score = chainer.cuda.to_cpu(roi_ag_seg_scores.array)
            roi_cls_score = chainer.cuda.to_cpu(roi_cls_scores.array)
            bbox = chainer.cuda.to_cpu(bboxes)

            # filter bounding boxes with min_size
            height = bbox[:, 2] - bbox[:, 0]
            width = bbox[:, 3] - bbox[:, 1]
            keep_indices = np.where(
                (height >= self.min_drop_size) &
                (width >= self.min_drop_size))[0]
            roi_ag_seg_score = roi_ag_seg_score[keep_indices, :, :]
            roi_cls_score = roi_cls_score[keep_indices]
            bbox = bbox[keep_indices, :]

            # scale bbox
            bbox = bbox / scale

            # shape: (n_rois, 4)
            bbox[:, 0::2] = self.xp.clip(bbox[:, 0::2], 0, size[0])
            bbox[:, 1::2] = self.xp.clip(bbox[:, 1::2], 0, size[1])

            # shape: (n_roi, roi_size, roi_size)
            roi_seg_prob = F.softmax(roi_ag_seg_score).array[:, 1]
            roi_cls_prob = F.softmax(roi_cls_score).array

            roi_seg_prob, bbox, label, roi_cls_prob = mask_voting(
                roi_seg_prob, bbox, roi_cls_prob, size,
                self.score_thresh, self.nms_thresh,
                self.mask_merge_thresh, self.binary_thresh,
                limit=self.limit, bg_label=0)

            mask = np.zeros(
                (len(roi_seg_prob), size[0], size[1]), dtype=np.bool)
            for i, (roi_seg_pb, bb) in enumerate(zip(roi_seg_prob, bbox)):
                bb = np.round(bb).astype(np.int32)
                y_min, x_min, y_max, x_max = bb
                roi_msk_pb = resize(
                    roi_seg_pb.astype(np.float32)[None],
                    (y_max - y_min, x_max - x_min))
                roi_msk = (roi_msk_pb > self.binary_thresh)[0]
                mask[i, y_min:y_max, x_min:x_max] = roi_msk

            masks.append(mask)
            labels.append(label)
            scores.append(roi_cls_prob)

        return masks, labels, scores