Exemple #1
0
    def test_output_shape(self):
        bbox = BBox(0, 0, 100, 100)
        width, height = 127, 255
        patch = center_crop_and_resize(self.img, bbox, (width, height))
        patch = np.asarray(patch)

        self.assertEqual(patch.shape, (height, width, 3))
Exemple #2
0
    def init(self, img: ImageT, bbox: np.ndarray) -> None:
        self.model.eval()

        bbox = assure_int_bbox(bbox)
        self.target_bbox = BBox(*bbox)
        self.curr_instance_side_size = calc_bbox_side_size_with_context(
            self.target_bbox)
        size_ratio = self.cfg.exemplar_size / self.cfg.instance_size
        exemplar_side_size = int(
            round(self.curr_instance_side_size * size_ratio))

        exemplar_bbox = BBox.build_from_center_and_size(
            self.target_bbox.center,
            np.asarray((exemplar_side_size, exemplar_side_size)))
        exemplar_img = center_crop_and_resize(
            img, exemplar_bbox,
            (self.cfg.exemplar_size, self.cfg.exemplar_size))

        if self.on_exemplar_img_extract:
            self.on_exemplar_img_extract(exemplar_img)

        exemplar_img_tensor = torch.unsqueeze(pil_to_tensor(exemplar_img), 0)
        exemplar_img_tensor = exemplar_img_tensor.to(self.device)
        self.kernel_reg, self.kernel_cls = self.model.learn_kernels(
            exemplar_img_tensor)
Exemple #3
0
    def test_bbox_outsize_image_needs_padding_constant_border(self):
        bbox = BBox(-50, -10, 200, 300)
        border = 127  # Value to fill the border with.
        patch = center_crop_and_resize(self.img, bbox, (127, 255), border)
        patch_flat = np.asarray(patch).flatten()
        values = np.unique(patch_flat)

        self.assertTrue(len(values) > 1)
        self.assertTrue(border in values)
        self.assertTrue(self.PIX_VALUE in values)
Exemple #4
0
    def update(self, img: ImageT) -> np.ndarray:
        self.model.eval()

        side_size = int(round(self.curr_instance_side_size))
        bbox = BBox.build_from_center_and_size(
            self.target_bbox.center, np.asarray((side_size, side_size)))
        instance_img = center_crop_and_resize(
            img, bbox, (self.cfg.instance_size, self.cfg.instance_size))

        if self.on_instance_img_extract:
            self.on_instance_img_extract(instance_img)

        instance_img = pil_to_tensor(instance_img).to(self.device)
        pred_reg, pred_cls = self.model.inference(instance_img,
                                                  self.kernel_reg,
                                                  self.kernel_cls)

        pred_reg = pred_reg.squeeze()
        pred_cls = pred_cls.squeeze()

        pred_cls = F.softmax(pred_cls, dim=1)
        pred_cls_max = pred_cls.argmax(dim=1)
        # TODO Store the range somewhere as it may be faster.
        scores = pred_cls[list(range(len(pred_cls))), pred_cls_max]
        scores[pred_cls_max == 0] = 0  # The 0-th position is the background.

        # TODO Think of modifying the regression predictions in place.
        xy_vals = pred_reg[:, :2] * self.anchors[:, 2:] + self.anchors[:, :2]
        wh_vals = torch.exp(pred_reg[:, 2:]) * self.anchors[:, 2:]
        boxes = torch.hstack((xy_vals, wh_vals))
        boxes = ops.box_convert(boxes, 'cxcywh', 'xyxy')
        boxes = ops.clip_boxes_to_image(
            boxes, (self.cfg.instance_size, self.cfg.instance_size))

        response = (1 - self.cfg.cosine_win_influence) * response + \
                   self.cfg.cosine_win_influence * self.cosine_win

        # The assumption is that the peak response value is in the center of the
        # response map. Thus, we compute the change with respect to the center
        # and convert it back to the pixel coordinates in the image.
        peak_response_pos = np.asarray(
            np.unravel_index(response.argmax(), response.shape))

        # Update target scale.
        self.curr_instance_side_size *= new_scale

        # Change from [row, col] to [x, y] coordinates.
        self.target_bbox.shift(disp_in_image[::-1])
        self.target_bbox.rescale(new_scale, new_scale)

        return self.target_bbox.as_xywh()
Exemple #5
0
    def _read_image_and_transform(
            img_path: str,
            anno: np.ndarray,
            output_side_size: int,
            transform: Callable[[Image.Image], torch.Tensor],
            size_with_context_scale: float = 1.0) -> torch.Tensor:
        bbox = BBox(*anno)
        side_size_with_context = calc_bbox_side_size_with_context(bbox)
        side_size_scaled = side_size_with_context * size_with_context_scale
        new_size = (side_size_scaled, side_size_scaled)
        bbox.size = np.asarray(new_size).round().astype(np.int)  # !!!

        img = Image.open(img_path)
        output_side = (output_side_size, output_side_size)
        patch = center_crop_and_resize(img, bbox, output_side)

        if patch.mode == 'L':
            patch = patch.convert('RGB')
        patch_tensor = transform(patch)

        return patch_tensor
Exemple #6
0
    def test_bbox_within_image_no_padding(self):
        bbox = BBox(0, 0, 100, 100)
        patch = center_crop_and_resize(self.img, bbox, (127, 255))
        patch_flat = np.asarray(patch).flatten()

        self.assertTrue(np.all(patch_flat == patch_flat[0]))