Esempio n. 1
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)
Esempio n. 2
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))
Esempio n. 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)
Esempio n. 4
0
 def generate_bbox(self):
     valid_x_range = 0, self.img_size[0] - self.width_range[1] - 1
     valid_y_range = 0, self.img_size[1] - self.height_range[1] - 1
     
     x = rand_between(*valid_x_range, as_int=True)
     y = rand_between(*valid_y_range, as_int=True)
     
     width = rand_between(*self.width_range, as_int=True)
     height = rand_between(*self.height_range, as_int=True)
     
     return BBox(x, y, width, height)
Esempio n. 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
Esempio n. 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]))
Esempio n. 7
0
 def test_negative_height(self):
     with self.assertRaises(AssertionError):
         BBox(100, 100, 10, -200)
Esempio n. 8
0
 def test_negative_width(self):
     with self.assertRaises(AssertionError):
         BBox(100, 100, -10, 200)
Esempio n. 9
0
 def setUp(self) -> None:
     self.bbox = BBox(10, 20, 300, 400)