def test_exception(self, device, dtype): with pytest.raises(TypeError): assert elastic_transform2d([0.]) with pytest.raises(TypeError): assert elastic_transform2d(torch.tensor(), 1) with pytest.raises(ValueError): img = torch.ones(1, 1, 1, device=device, dtype=dtype) noise = torch.ones(1, 2, 1, 1, device=device, dtype=dtype) assert elastic_transform2d(img, noise) with pytest.raises(ValueError): img = torch.ones(1, 1, 1, 1, device=device, dtype=dtype) noise = torch.ones(1, 3, 1, 1, device=device, dtype=dtype) assert elastic_transform2d(img, noise)
def test_cardinality(self, device, dtype, batch, channels, height, width): shape = batch, channels, height, width img = torch.ones(shape, device=device, dtype=dtype) noise = torch.ones((batch, 2, height, width), device=device, dtype=dtype) assert elastic_transform2d(img, noise).shape == shape
def inner(x): a = np.random.rand(2) k = np.random.randint(8,64) * 2 + 1 # 63 s = k / (np.random.rand()+2.) # 2-3 times less than k # s = float(np.random.randint(8,64)) # 32 noise = torch.zeros([1, 2, x.shape[2], x.shape[3]]).cuda() return K.elastic_transform2d(x, noise, (k,k), (s,s), tuple(a))
def test_valid_paramters(self, device, dtype, kernel_size, sigma, alpha): image = torch.rand(1, 4, 5, 5, device=device, dtype=dtype) noise = torch.rand(1, 2, 5, 5, device=device, dtype=dtype) if isinstance(sigma, torch.Tensor): sigma = sigma.to(device, dtype) if isinstance(alpha, torch.Tensor): alpha = alpha.to(device, dtype) assert elastic_transform2d(image, noise, kernel_size, sigma, alpha) is not None
def apply_transform(self, input: Tensor, params: Dict[str, Tensor], transform: Optional[Tensor] = None) -> Tensor: return elastic_transform2d( input, params["noise"].to(input), self.flags["kernel_size"], self.flags["sigma"], self.flags["alpha"], self.flags["align_corners"], self.flags["mode"], self.flags["padding_mode"], )
def test_values(self, device, dtype): image = torch.tensor( [[[[0.0018, 0.7521, 0.7550], [0.2053, 0.4249, 0.1369], [0.1027, 0.3992, 0.8773]]]], device=device, dtype=dtype) noise = torch.ones(1, 2, 3, 3, device=device, dtype=dtype) expected = torch.tensor( [[[[0.2193, 0.2193, 0.2193], [0.2193, 0.2193, 0.2193], [0.2193, 0.2193, 0.2193]]]], device=device, dtype=dtype) actual = elastic_transform2d(image, noise) assert_allclose(actual, expected, atol=1e-3, rtol=1e-3)
def test_smoke(self, device, dtype): image = torch.rand(1, 4, 5, 5, device=device, dtype=dtype) noise = torch.rand(1, 2, 5, 5, device=device, dtype=dtype) assert elastic_transform2d(image, noise) is not None