def test_ten_crop(self): img = np.random.uniform(size=(3, 48, 32)) out = ten_crop(img, (32, 48)) self.assertEqual(out.shape, (10, 3, 48, 32)) for crop in out[:5]: np.testing.assert_equal(crop, img) for crop in out[5:]: np.testing.assert_equal(crop[:, :, ::-1], img) out = ten_crop(img, (12, 24)) self.assertEqual(out.shape, (10, 3, 24, 12))
def test_ten_crop(self): img = np.random.uniform(size=(3, 48, 32)) out = ten_crop(img, (48, 32)) self.assertEqual(out.shape, (10, 3, 48, 32)) for crop in out[:5]: np.testing.assert_equal(crop, img) for crop in out[5:]: np.testing.assert_equal(crop[:, :, ::-1], img) out = ten_crop(img, (24, 12)) self.assertEqual(out.shape, (10, 3, 24, 12))
def _prepare(self, img): """Prepare an image for feeding it to a model. This is a standard preprocessing scheme used by feature extraction models. First, the image is scaled or resized according to :math:`scale_size`. Note that this step is optional. Next, the image is cropped to :math:`crop_size`. Last, the image is mean subtracted by an array :obj:`mean`. Args: img (~numpy.ndarray): An image. This is in CHW format. The range of its value is :math:`[0, 255]`. Returns: ~numpy.ndarray: A preprocessed image. This is 4D array whose batch size is the number of crops. """ if self.scale_size is not None: if isinstance(self.scale_size, int): img = scale(img, size=self.scale_size) else: img = resize(img, size=self.scale_size) if self.crop == '10': imgs = ten_crop(img, self.crop_size) elif self.crop == 'center': imgs = center_crop(img, self.crop_size)[np.newaxis] imgs -= self.mean[np.newaxis] return imgs
def _prepare(self, img): """Prepare an image for feeding it to a model. This is a standard preprocessing scheme used by feature extraction models. First, the image is scaled or resized according to :math:`scale_size`. Note that this step is optional. Next, the image is cropped to :math:`crop_size`. Last, the image is mean subtracted by an array :obj:`mean`. Args: img (~numpy.ndarray): An image. This is in CHW format. The range of its value is :math:`[0, 255]`. Returns: ~numpy.ndarray: A preprocessed image. This is 4D array whose batch size is the number of crops. """ if self.scale_size is not None: if isinstance(self.scale_size, int): img = scale(img, size=self.scale_size) else: img = resize(img, size=self.scale_size) if self.crop == '10': imgs = ten_crop(img, self.crop_size) elif self.crop == 'center': imgs = center_crop(img, self.crop_size)[np.newaxis] imgs -= self.mean[np.newaxis] return imgs