Exemplo n.º 1
0
def test_gradients():
    ha = qshift('qshift_c')[0]
    hb = qshift('qshift_c')[1]
    im_t = torch.unsqueeze(torch.tensor(barbara, dtype=torch.float32,
                                        requires_grad=True), dim=0)
    y_t = coldfilt(im_t, prep_filt(ha, 1), prep_filt(hb, 1), np.sum(ha*hb) > 0)
    dy = np.random.randn(*tuple(y_t.shape)).astype('float32')
    torch.autograd.grad(y_t, im_t, grad_outputs=torch.tensor(dy))
Exemplo n.º 2
0
def test_equal_numpy_qshift1(hp):
    if hp:
        ha = qshift('qshift_a')[4]
        hb = qshift('qshift_a')[5]
    else:
        ha = qshift('qshift_a')[0]
        hb = qshift('qshift_a')[1]
    ref = ref_coldfilt(barbara, ha, hb)
    y = coldfilt(barbara_t, prep_filt(ha, 1).to(dev), prep_filt(hb, 1).to(dev),
                 highpass=hp)
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
Exemplo n.º 3
0
 def __init__(self, biort='near_sym_a', mode='symmetric', magbias=1e-2):
     super().__init__()
     self.biort = biort
     # Have to convert the string to an int as the grad checks don't work
     # with string inputs
     self.mode_str = mode
     self.mode = mode_to_int(mode)
     self.magbias = magbias
     h0o, _, h1o, _ = _biort(biort)
     self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
     self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
     self.lp_pool = nn.AvgPool2d(2)
Exemplo n.º 4
0
def test_equal_small_in(hp):
    if hp:
        ha = qshift('qshift_a')[4]
        hb = qshift('qshift_a')[5]
    else:
        ha = qshift('qshift_a')[0]
        hb = qshift('qshift_a')[1]
    im = barbara[:,0:4,0:4]
    im_t = torch.unsqueeze(torch.tensor(im, dtype=torch.float32), dim=0).to(dev)
    ref = ref_coldfilt(im, ha, hb)
    y = coldfilt(im_t, prep_filt(ha, 1).to(dev), prep_filt(hb, 1).to(dev),
                 highpass=hp)
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
def test_equal_numpy_biort2():
    h = biort('near_sym_b')[0]
    im = barbara[:, 52:407, 30:401]
    im_t = torch.unsqueeze(torch.tensor(im, dtype=torch.float32), dim=0).to(dev)
    ref = ref_colfilter(im, h)
    y = colfilter(im_t, prep_filt(h, 1).to(dev))
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
def test_gradients():
    h = biort('near_sym_b')[0]
    im_t = torch.unsqueeze(torch.tensor(barbara, dtype=torch.float32,
                                        requires_grad=True), dim=0)
    y_t = colfilter(im_t, prep_filt(h, 1))
    dy = np.random.randn(*tuple(y_t.shape)).astype('float32')
    torch.autograd.grad(y_t, im_t, grad_outputs=torch.tensor(dy))
Exemplo n.º 7
0
def test_gradients(hp):
    if hp:
        ha = qshift('qshift_b')[4]
        hb = qshift('qshift_b')[5]
    else:
        ha = qshift('qshift_b')[0]
        hb = qshift('qshift_b')[1]
    im_t = torch.unsqueeze(torch.tensor(barbara,
                                        dtype=torch.float32,
                                        requires_grad=True),
                           dim=0).to(dev)
    y_t = rowdfilt(im_t,
                   prep_filt(ha, 1).to(dev),
                   prep_filt(hb, 1).to(dev),
                   highpass=hp)
    np.random.randn(*tuple(y_t.shape)).astype('float32')
def test_equal_small_in():
    h = qshift('qshift_b')[0]
    im = barbara[:,0:4,0:4]
    im_t = torch.unsqueeze(torch.tensor(im, dtype=torch.float32), dim=0).to(dev)
    ref = ref_colfilter(im, h)
    y = colfilter(im_t, prep_filt(h, 1).to(dev))
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
Exemplo n.º 9
0
def test_equal_numpy_qshift2():
    h = _qshift('qshift_c')[0]
    im = barbara[:, 52:407, 30:401]
    im_t = torch.unsqueeze(torch.tensor(im, dtype=torch.float32),
                           dim=0).to(dev)
    ref = ref_rowfilter(im, h)
    y = rowfilter(im_t, prep_filt(h, 1).to(dev))
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
Exemplo n.º 10
0
 def __init__(self, biort='near_sym_a', mode='symmetric', magbias=1e-2,
              combine_colour=False):
     super().__init__()
     self.biort = biort
     # Have to convert the string to an int as the grad checks don't work
     # with string inputs
     self.mode_str = mode
     self.mode = mode_to_int(mode)
     self.magbias = magbias
     self.combine_colour = combine_colour
     if biort == 'near_sym_b_bp':
         self.bandpass_diag = True
         h0o, _, h1o, _, h2o, _ = _biort(biort)
         self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
         self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
         self.h2o = torch.nn.Parameter(prep_filt(h2o, 1), False)
     else:
         self.bandpass_diag = False
         h0o, _, h1o, _ = _biort(biort)
         self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
         self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
Exemplo n.º 11
0
def test_odd_size():
    h = [-1, 2, -1]
    y_op = rowfilter(barbara_t, prep_filt(h, 1).to(dev))
    assert list(y_op.shape)[1:] == bshape
Exemplo n.º 12
0
def test_equal_numpy_qshift1():
    h = _qshift('qshift_c')[0]
    ref = ref_rowfilter(barbara, h)
    y = rowfilter(barbara_t, prep_filt(h, 1).to(dev))
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
Exemplo n.º 13
0
def test_qshift():
    h = qshift('qshift_a')[0]
    y_op = colfilter(barbara_t, prep_filt(h, 1).to(dev))
    assert list(y_op.shape)[1:] == bshape_extrarow
Exemplo n.º 14
0
    def __init__(self,
                 biort='near_sym_a',
                 qshift='qshift_a',
                 J=3,
                 o_dim=2,
                 ri_dim=-1):
        super().__init__()
        self.biort = biort
        self.qshift = qshift
        self.o_dim = o_dim
        self.ri_dim = ri_dim
        self.J = J
        if isinstance(biort, str):
            _, g0o, _, g1o = _biort(biort)
            self.g0o = torch.nn.Parameter(prep_filt(g0o, 1), False)
            self.g1o = torch.nn.Parameter(prep_filt(g1o, 1), False)
        else:
            self.g0o = torch.nn.Parameter(prep_filt(biort[0], 1), False)
            self.g1o = torch.nn.Parameter(prep_filt(biort[1], 1), False)
        if isinstance(qshift, str):
            _, _, g0a, g0b, _, _, g1a, g1b = _qshift(qshift)
            self.g0a = torch.nn.Parameter(prep_filt(g0a, 1), False)
            self.g0b = torch.nn.Parameter(prep_filt(g0b, 1), False)
            self.g1a = torch.nn.Parameter(prep_filt(g1a, 1), False)
            self.g1b = torch.nn.Parameter(prep_filt(g1b, 1), False)
        else:
            self.g0a = torch.nn.Parameter(prep_filt(qshift[0], 1), False)
            self.g0b = torch.nn.Parameter(prep_filt(qshift[1], 1), False)
            self.g1a = torch.nn.Parameter(prep_filt(qshift[2], 1), False)
            self.g1b = torch.nn.Parameter(prep_filt(qshift[3], 1), False)

        # Create the function to do the DTCWT
        self.dtcwt_func = getattr(tf, 'ifm{J}'.format(J=J))
Exemplo n.º 15
0
    def __init__(self,
                 biort='near_sym_a',
                 qshift='qshift_a',
                 J=3,
                 skip_hps=False,
                 include_scale=False,
                 downsample=False,
                 o_dim=2,
                 ri_dim=-1):
        super().__init__()
        if o_dim == ri_dim:
            raise ValueError("Orientations and real/imaginary parts must be "
                             "in different dimensions.")

        self.biort = biort
        self.qshift = qshift
        self.J = J
        self.downsample = downsample
        self.o_dim = o_dim
        self.ri_dim = ri_dim
        if isinstance(biort, str):
            h0o, _, h1o, _ = _biort(biort)
            self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
            self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
        else:
            self.h0o = torch.nn.Parameter(prep_filt(biort[0], 1), False)
            self.h1o = torch.nn.Parameter(prep_filt(biort[1], 1), False)
        if isinstance(qshift, str):
            h0a, h0b, _, _, h1a, h1b, _, _ = _qshift(qshift)
            self.h0a = torch.nn.Parameter(prep_filt(h0a, 1), False)
            self.h0b = torch.nn.Parameter(prep_filt(h0b, 1), False)
            self.h1a = torch.nn.Parameter(prep_filt(h1a, 1), False)
            self.h1b = torch.nn.Parameter(prep_filt(h1b, 1), False)
        else:
            self.h0a = torch.nn.Parameter(prep_filt(qshift[0], 1), False)
            self.h0b = torch.nn.Parameter(prep_filt(qshift[1], 1), False)
            self.h1a = torch.nn.Parameter(prep_filt(qshift[2], 1), False)
            self.h1b = torch.nn.Parameter(prep_filt(qshift[3], 1), False)

        # Get the function to do the DTCWT
        if isinstance(skip_hps, (list, tuple, ndarray)):
            self.skip_hps = skip_hps
        else:
            self.skip_hps = [
                skip_hps,
            ] * self.J
        if isinstance(include_scale, (list, tuple, ndarray)):
            self.include_scale = include_scale
        else:
            self.include_scale = [
                include_scale,
            ] * self.J
        if True in self.include_scale:
            self.dtcwt_func = getattr(tf, 'xfm{J}scale'.format(J=J))
        else:
            self.dtcwt_func = getattr(tf, 'xfm{J}'.format(J=J))
Exemplo n.º 16
0
def test_equal_numpy_biort1():
    h = _biort('near_sym_b')[0]
    ref = ref_rowfilter(barbara, h)
    y = rowfilter(barbara_t, prep_filt(h, 1).to(dev))
    np.testing.assert_array_almost_equal(y[0].cpu(), ref, decimal=4)
Exemplo n.º 17
0
def test_good_input_size():
    ha = prep_filt((-1, 1), 1).to(dev)
    hb = prep_filt((1, -1), 1).to(dev)
    coldfilt(barbara_t[:,:,:,:511], ha, hb)
Exemplo n.º 18
0
def test_even_size():
    h = [-1, -1]
    y_op = rowfilter(barbara_t, prep_filt(h, 1).to(dev))
    assert list(y_op.shape)[1:] == bshape_extracol
Exemplo n.º 19
0
def test_biort():
    h = _biort('antonini')[0]
    y_op = rowfilter(barbara_t, prep_filt(h, 1).to(dev))
    assert list(y_op.shape)[1:] == bshape
Exemplo n.º 20
0
def test_good_input_size_non_orthogonal():
    ha = prep_filt((1, 1), 1).to(dev)
    hb = prep_filt((1, -1), 1).to(dev)
    rowdfilt(barbara_t[:, :, :511, :], ha, hb)
Exemplo n.º 21
0
def test_bad_input_size():
    with raises(ValueError):
        ha = prep_filt((-1, 1), 1).to(dev)
        hb = prep_filt((1, -1), 1).to(dev)
        rowdfilt(barbara_t[:, :, :, :511], ha, hb)
Exemplo n.º 22
0
def test_different_size():
    with raises(ValueError):
        ha = prep_filt((-0.5, -1, 2, 0.5), 1).to(dev)
        hb = prep_filt((-1, 2, 1), 1).to(dev)
        rowdfilt(barbara_t, ha, hb)
Exemplo n.º 23
0
def test_odd_filter():
    with raises(ValueError):
        ha = prep_filt((-1, 2, -1), 1).to(dev)
        hb = prep_filt((-1, 2, 1), 1).to(dev)
        rowdfilt(barbara_t, ha, hb)
Exemplo n.º 24
0
 def __init__(self,
              biort='near_sym_a',
              qshift='qshift_a',
              mode='symmetric',
              magbias=1e-2,
              combine_colour=False,
              J=2):
     super().__init__()
     self.biort = biort
     self.qshift = biort
     # Have to convert the string to an int as the grad checks don't work
     # with string inputs
     self.mode_str = mode
     self.mode = mode_to_int(mode)
     self.magbias = magbias
     self.combine_colour = combine_colour
     self.J = J
     if biort == 'near_sym_b_bp':
         assert qshift == 'qshift_b_bp'
         self.bandpass_diag = True
         h0o, _, h1o, _, h2o, _ = _biort(biort)
         self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
         self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
         self.h2o = torch.nn.Parameter(prep_filt(h2o, 1), False)
         h0a, h0b, _, _, h1a, h1b, _, _, h2a, h2b, _, _ = _qshift(
             'qshift_b_bp')
         self.h0a = torch.nn.Parameter(prep_filt(h0a, 1), False)
         self.h0b = torch.nn.Parameter(prep_filt(h0b, 1), False)
         self.h1a = torch.nn.Parameter(prep_filt(h1a, 1), False)
         self.h1b = torch.nn.Parameter(prep_filt(h1b, 1), False)
         self.h2a = torch.nn.Parameter(prep_filt(h2a, 1), False)
         self.h2b = torch.nn.Parameter(prep_filt(h2b, 1), False)
     else:
         self.bandpass_diag = False
         h0o, _, h1o, _ = _biort(biort)
         self.h0o = torch.nn.Parameter(prep_filt(h0o, 1), False)
         self.h1o = torch.nn.Parameter(prep_filt(h1o, 1), False)
         h0a, h0b, _, _, h1a, h1b, _, _ = _qshift(qshift)
         self.h0a = torch.nn.Parameter(prep_filt(h0a, 1), False)
         self.h0b = torch.nn.Parameter(prep_filt(h0b, 1), False)
         self.h1a = torch.nn.Parameter(prep_filt(h1a, 1), False)
         self.h1b = torch.nn.Parameter(prep_filt(h1b, 1), False)
Exemplo n.º 25
0
def test_qshift():
    h = _qshift('qshift_a')[0]
    x = barbara_t
    y_op = rowfilter(x, prep_filt(h, 1).to(dev))
    assert list(y_op.shape)[1:] == bshape_extracol
Exemplo n.º 26
0
    def __init__(self,
                 biort='near_sym_a',
                 qshift='qshift_a',
                 o_dim=2,
                 ri_dim=-1,
                 mode='symmetric'):
        super().__init__()
        self.biort = biort
        self.qshift = qshift
        self.o_dim = o_dim
        self.ri_dim = ri_dim
        self.mode = mode
        if isinstance(biort, str):
            _, g0o, _, g1o = _biort(biort)
            self.g0o = prep_filt(g0o, 1)
            self.g1o = prep_filt(g1o, 1)
        else:
            self.g0o = prep_filt(biort[0], 1)
            self.g1o = prep_filt(biort[1], 1)
        if isinstance(qshift, str):
            _, _, g0a, g0b, _, _, g1a, g1b = _qshift(qshift)
            self.g0a = prep_filt(g0a, 1)
            self.g0b = prep_filt(g0b, 1)
            self.g1a = prep_filt(g1a, 1)
            self.g1b = prep_filt(g1b, 1)
        else:
            self.g0a = prep_filt(qshift[0], 1)
            self.g0b = prep_filt(qshift[1], 1)
            self.g1a = prep_filt(qshift[2], 1)
            self.g1b = prep_filt(qshift[3], 1)

        self.g0o = nn.Parameter(self.g0o, requires_grad=True)
        self.g1o = nn.Parameter(self.g1o, requires_grad=True)
        self.g0a = nn.Parameter(self.g0a, requires_grad=True)
        self.g0b = nn.Parameter(self.g0b, requires_grad=True)
        self.g1a = nn.Parameter(self.g1a, requires_grad=True)
        self.g1b = nn.Parameter(self.g1b, requires_grad=True)
Exemplo n.º 27
0
def test_even_size_batch():
    zero_t = torch.zeros([1, *barbara.shape], dtype=torch.float32).to(dev)
    h = [-1, 1]
    y = rowfilter(zero_t, prep_filt(h, 1).to(dev))
    assert list(y.shape)[1:] == bshape_extracol
    assert not np.any(y.cpu().numpy()[:] != 0.0)
Exemplo n.º 28
0
    def __init__(self,
                 biort='near_sym_a',
                 qshift='qshift_a',
                 J=3,
                 skip_hps=False,
                 include_scale=False,
                 o_dim=2,
                 ri_dim=-1,
                 mode='symmetric'):
        super().__init__()
        if o_dim == ri_dim:
            raise ValueError("Orientations and real/imaginary parts must be "
                             "in different dimensions.")

        self.biort = biort
        self.qshift = qshift
        self.J = J
        self.o_dim = o_dim
        self.ri_dim = ri_dim
        self.mode = mode
        if isinstance(biort, str):
            h0o, _, h1o, _ = _biort(biort)
            self.h0o = prep_filt(h0o, 1)
            self.h1o = prep_filt(h1o, 1)
        else:
            self.h0o = prep_filt(biort[0], 1)
            self.h1o = prep_filt(biort[1], 1)
        if isinstance(qshift, str):
            h0a, h0b, _, _, h1a, h1b, _, _ = _qshift(qshift)
            self.h0a = prep_filt(h0a, 1)
            self.h0b = prep_filt(h0b, 1)
            self.h1a = prep_filt(h1a, 1)
            self.h1b = prep_filt(h1b, 1)
        else:
            self.h0a = prep_filt(qshift[0], 1)
            self.h0b = prep_filt(qshift[1], 1)
            self.h1a = prep_filt(qshift[2], 1)
            self.h1b = prep_filt(qshift[3], 1)

        self.h0o = nn.Parameter(self.h0o, requires_grad=True)
        self.h1o = nn.Parameter(self.h1o, requires_grad=True)
        self.h0a = nn.Parameter(self.h0a, requires_grad=True)
        self.h0b = nn.Parameter(self.h0b, requires_grad=True)
        self.h1a = nn.Parameter(self.h1a, requires_grad=True)
        self.h1b = nn.Parameter(self.h1b, requires_grad=True)

        # Get the function to do the DTCWT
        if isinstance(skip_hps, (list, tuple, ndarray)):
            self.skip_hps = skip_hps
        else:
            self.skip_hps = [
                skip_hps,
            ] * self.J
        if isinstance(include_scale, (list, tuple, ndarray)):
            self.include_scale = include_scale
        else:
            self.include_scale = [
                include_scale,
            ] * self.J
Exemplo n.º 29
0
def test_output_size():
    ha = prep_filt((-1, 1), 1).to(dev)
    hb = prep_filt((1, -1), 1).to(dev)
    y_op = rowdfilt(barbara_t, ha, hb)
    assert list(y_op.shape[1:]) == bshape_half
Exemplo n.º 30
0
def test_even_size_batch():
    zero_t = torch.zeros((1, *barbara.shape), dtype=torch.float32).to(dev)
    y = colfilter(zero_t, prep_filt([-1, 1], 1).to(dev))
    assert list(y.shape)[1:] == bshape_extrarow
    assert not np.any(y.cpu().numpy()[:] != 0.0)