def test_scattering2d_errors(): S = Scattering2D(3, (32, 32)) if backend.NAME == 'skcuda': S.cuda() with pytest.raises(TypeError) as record: S(None) assert ('input should be' in record.value.args[0]) x = torch.randn(4, 4) y = x[::2, ::2] with pytest.raises(RuntimeError) as record: S(y) assert ('must be contiguous' in record.value.args[0]) x = torch.randn(31, 31) with pytest.raises(RuntimeError) as record: S(x) assert ('Tensor must be of spatial size' in record.value.args[0]) S = Scattering2D(3, (32, 32), pre_pad=True) with pytest.raises(RuntimeError) as record: S(x) assert ('Padded tensor must be of spatial size' in record.value.args[0])
def test_Scattering2D(): test_data_dir = os.path.dirname(__file__) data = torch.load(os.path.join(test_data_dir, 'test_data_2d.pt')) x = data['x'] S = data['Sx'] J = data['J'] # we need to reorder S from interleaved (how it's saved) to o0, o1, o2 # (which is how it's now computed) o0, o1, o2 = reorder_coefficients_from_interleaved(J, L=8) reorder = torch.from_numpy(np.concatenate((o0, o1, o2))) S = S[..., reorder, :, :] pre_pad = data['pre_pad'] M = x.shape[2] N = x.shape[3] import kymatio.scattering2d.backend as backend if backend.NAME == 'skcuda': print('skcuda backend tested!') # First, let's check the Jit scattering = Scattering2D(J, shape=(M, N), pre_pad=pre_pad) scattering.cuda() x = x.cuda() S = S.cuda() y = scattering(x) assert ((S - y)).abs().max() < 1e-6 elif backend.NAME == 'torch': # Then, let's check when using pure pytorch code scattering = Scattering2D(J, shape=(M, N), pre_pad=pre_pad) Sg = [] for device in devices: if device == 'gpu': print('torch-gpu backend tested!') x = x.cuda() scattering.cuda() S = S.cuda() Sg = scattering(x) else: print('torch-cpu backend tested!') x = x.cpu() S = S.cpu() scattering.cpu() Sg = scattering(x) assert (Sg - S).abs().max() < 1e-6
def test_input_size_agnostic(): for N in [31, 32, 33]: for J in [2, 4]: scattering = Scattering2D(J, shape=(N, N)) x = torch.zeros(3, 3, N, N) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x) scattering = Scattering2D(J, shape=(N, N), pre_pad=True) x = torch.zeros(3, 3, scattering.M_padded, scattering.N_padded) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x)
def test_input_size_agnostic(): for N in [31, 32, 33]: for J in [1, 2, 4]: scattering = Scattering2D(J, shape=(N, N)) x = torch.zeros(3, 3, N, N) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x) scattering = Scattering2D(J, shape=(N, N), pre_pad=True) x = torch.zeros(3, 3, scattering.M_padded, scattering.N_padded) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x) N = 32 J = 5 scattering = Scattering2D(J, shape=(N, N)) x = torch.zeros(3, 3, N, N) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x) assert (S.shape[-2:] == (1, 1)) N = 32 J = 5 scattering = Scattering2D(J, shape=(N + 5, N)) x = torch.zeros(3, 3, N + 5, N) if backend.NAME == 'skcuda': x = x.cuda() scattering.cuda() S = scattering(x) assert (S.shape[-2:] == (1, 1))
def test_Scattering2D(): test_data_dir = os.path.dirname(__file__) data = torch.load(os.path.join(test_data_dir, 'test_data_2d.pt')) x = data['x'] S = data['Sx'] J = data['J'] pre_pad = data['pre_pad'] M = x.shape[2] N = x.shape[3] import kymatio.scattering2d.backend as backend if backend.NAME == 'skcuda': print('skcuda backend tested!') # First, let's check the Jit scattering = Scattering2D(M, N, J, pre_pad=pre_pad) scattering.cuda() x = x.cuda() S = S.cuda() y = scattering(x) assert ((S - y)).abs().max() < 1e-6 elif backend.NAME == 'torch': # Then, let's check when using pure pytorch code scattering = Scattering2D(M, N, J, pre_pad=pre_pad) Sg = [] for device in devices: if device == 'gpu': print('torch-gpu backend tested!') x = x.cuda() scattering.cuda() S = S.cuda() Sg = scattering(x) else: print('torch-cpu backend tested!') x = x.cpu() S = S.cpu() scattering.cpu() Sg = scattering(x) assert (Sg - S).abs().max() < 1e-6
def test_Scattering2D(): test_data_dir = os.path.dirname(__file__) data = torch.load(os.path.join(test_data_dir, 'test_data_2d.pt'), map_location='cpu') x = data['x'].view(7, 3, 128, 128) S = data['S'].view(7, 3, 417, 8, 8) import kymatio.scattering2d.backend as backend if backend.NAME == 'skcuda': print('skcuda backend tested!') # First, let's check the Jit scattering = Scattering2D(128, 128, 4, pre_pad=False) scattering.cuda() x = x.cuda() S = S.cuda() y = scattering(x) assert ((S - y)).abs().max() < 1e-6 elif backend.NAME == 'torch': # Then, let's check when using pure pytorch code scattering = Scattering2D(128, 128, 4, pre_pad=False) Sg = [] for device in devices: if device == 'gpu': print('torch-gpu backend tested!') x = x.cuda() scattering.cuda() S = S.cuda() Sg = scattering(x) else: print('torch-cpu backend tested!') x = x.cpu() S = S.cpu() scattering.cpu() Sg = scattering(x) assert (Sg - S).abs().max() < 1e-6
def test_batch_shape_agnostic(): J = 3 L = 8 shape = (32, 32) shape_ds = tuple(n // 2**J for n in shape) S = Scattering2D(J, shape, L) with pytest.raises(RuntimeError) as ve: S(torch.zeros(())) assert "at least two" in ve.value.args[0] with pytest.raises(RuntimeError) as ve: S(torch.zeros((32, ))) assert "at least two" in ve.value.args[0] x = torch.zeros(shape) if backend.NAME == 'skcuda': x = x.cuda() S.cuda() Sx = S(x) assert len(Sx.shape) == 3 assert Sx.shape[-2:] == shape_ds n_coeffs = Sx.shape[-3] test_shapes = ((1, ) + shape, (2, ) + shape, (2, 2) + shape, (2, 2, 2) + shape) for test_shape in test_shapes: x = torch.zeros(test_shape) if backend.NAME == 'skcuda': x = x.cuda() Sx = S(x) assert len(Sx.shape) == len(test_shape) + 1 assert Sx.shape[-2:] == shape_ds assert Sx.shape[-3] == n_coeffs assert Sx.shape[:-3] == test_shape[:-2]