def inverse(self): if not self._can_invert(): raise InvertibleError('Cannot invert a random transformation before it is applied.') return T.Lambda( lambd=lambda x: x, tf_inv=self._transform, repr_str='ColorJitterInverse()' )
def inverse(self): if not self._can_invert(): raise InvertibleError('Cannot invert a transformation before it is applied' ' (size before cropping is unknown).') five_crop = self._five_crop five_crop_flip = self._five_crop_flip return T.Lambda( lambd=lambda imgs: five_crop.inverse()(imgs[:5]) + self._flip(five_crop_flip.inverse()(imgs[5:])), tf_inv=lambda imgs: five_crop.inverse().invert(imgs[:5]) + five_crop_flip.inverse().invert( self._flip(imgs[5:])), repr_str='TenCropInverse()', )
def __call__(self, img): """ Args: img (Tensor): Tensor image of size (C, H, W) to be erased. Returns: img (Tensor): Erased Tensor image. """ self._transform = T.Identity() if flip_coin(self.p): x, y, h, w, v = self.get_params(img, scale=self.scale, ratio=self.ratio, value=self.value) self._transform = T.Lambda( lambd=lambda img: F.erase(img, x, y, h, w, v, self.inplace), tf_inv=lambda img: img, repr_str='RandomErasing()' ) return img
def inverse(self): if not self._can_invert(): raise InvertibleError('Cannot invert a transformation before it is applied' ' (size before the cropping is unknown).') crop_h, crop_w = self.size pad_w = self._img_w - crop_w pad_h = self._img_h - crop_h pad_tl = (0, 0, pad_w, pad_h) pad_tr = (pad_w, 0, 0, pad_h) pad_bl = (0, pad_h, pad_w, 0) pad_br = (pad_w, pad_h, 0, 0) half_w = (self._img_w - crop_w) // 2 half_h = (self._img_h - crop_h) // 2 pad_center = (half_w, half_h, self._img_w - crop_w - half_w, self._img_h - crop_h - half_h) tfs = [] for padding in [pad_tl, pad_tr, pad_bl, pad_br, pad_center]: tf = T.Pad(padding) tf._img_h = crop_h tf._img_w = crop_w tfs.append(tf) def invert_crops(crops): tl, tr, bl, br, center = crops return [tf(img) for tf, img in zip(tfs, [tl, tr, bl, br, center])] def invert_invert_crops(crops): tl, tr, bl, br, center = crops return [tf.inverse()(img) for tf, img in zip(tfs, [tl, tr, bl, br, center])] return T.Lambda( lambd=invert_crops, tf_inv=invert_invert_crops, repr_str=f'FiveCropInverse()', )
def __randaug_imagenet(self, img_pil, to_tensor, norm): # TODO: turn this code less cryptic... policy = [('FlipLR', 0.5, random.randint(1, 9)), ('FlipUD', 0.5, random.randint(1, 9)), ('Rotate', 0.5, random.randint(1, 9))] \ + random.choice(self._all_policies) img_shape = img_pil.size[::-1] + (3, ) for xform in policy: assert len(xform) == 3 name, probability, level = xform xform_fn = NAME_TO_TRANSFORM[name].pil_transformer( probability, level, img_shape) img_pil = xform_fn(img_pil) return T.Compose([ T.Lambda(lambda img: img.convert('RGB')), T.TransformIf(T.ToTensor(), to_tensor), T.TransformIf( T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), to_tensor and norm), ])(img_pil)
def inverse(self): return T.Lambda( lambd=lambda x: x, tf_inv=Grayscale(self.num_output_channels), repr_str='GrayscaleInverse()' )
def setUp(self) -> None: super().setUp() self.tf = T.Lambda(lambd=lambda x: x * 2, tf_inv=lambda x: x * 0.5)
def test_invert_none(self): tf = T.Lambda(lambda x: self.n) self.assertEqual(self.n, tf(None)) with self.assertRaises(InvertibleError): tf.inverse()
def test_invert_invertible(self): tf_inv = T.Lambda(lambda x: x * 0.5) tf = T.Lambda(lambd=lambda x: x * 2, tf_inv=tf_inv) self.assertEqual(tf_inv, tf.inverse()) self.assertTrue( torch.allclose(self.img_tensor, tf.invert(tf(self.img_tensor))))
def test_call(self): tf = T.Lambda(lambd=lambda x: self.n) self.assertEqual(tf(None), self.n)