def test_diagonalization(self): """Test automatic diagonalization. """ var = Variable((2, 5)) K = np.array([[-1, 1]]) expr = 2 * vstack([conv(K, var), conv(K, var)]) assert expr.is_gram_diag(freq=True)
def test_diagonalization(self): """Test automatic diagonalization. """ var = Variable((2, 5)) K = np.array([[-1, 1]]) expr = 2*vstack([conv(K, var), conv(K, var)]) assert expr.is_gram_diag(freq=True)
def test_conv(self): """Test convolution lin op. """ # Forward. var = Variable((2, 3)) kernel = np.array([[1, 2, 3], [4, 5, 6]]) # 2x3 fn = conv(kernel, var) x = np.arange(6) * 1.0 x = np.reshape(x, (2, 3)) out = np.zeros(fn.shape) fn.forward([x], [out]) ks = np.array(kernel.shape) cc = np.floor((ks - 1) / 2.0).astype(np.int) # Center coordinate y = np.zeros((2, 3)) for i in range(2): for j in range(3): for s in range(2): for t in range(3): y[i, j] += kernel[ks[0] - 1 - s, ks[1] - 1 - t] * \ x[(s + i - cc[0]) % 2, (t + j - cc[1]) % 3] # For loop same as convolve # y = ndimage.convolve(x, kernel, mode='wrap') self.assertItemsAlmostEqual(out, y) # Adjoint.# x = np.arange(6) * 1.0 x = np.reshape(x, (2, 3)) out = np.zeros(var.shape) fn.adjoint([x], [out]) y = np.zeros((2, 3)) for i in range(2): for j in range(3): for s in range(2): for t in range(3): y[i, j] += kernel[s, t] * x[(s + i - (ks[0] - 1 - cc[0])) % 2, (t + j - (ks[1] - 1 - cc[1])) % 3] # For loop same as correlate # y = ndimage.correlate(x, kernel, mode='wrap') self.assertItemsAlmostEqual(out, y) # Diagonal form. x = Variable(5) kernel = np.arange(5) fn = conv(kernel, x) assert fn.is_diag(freq=True) assert not fn.is_diag(freq=False) forward_kernel = psf2otf(kernel, (5, ), 1) self.assertItemsAlmostEqual(fn.get_diag(freq=True)[x], forward_kernel)
def test_conv(self): """Test convolution lin op. """ # Forward. var = Variable((2, 3)) kernel = np.array([[1, 2, 3], [4, 5, 6]]) # 2x3 fn = conv(kernel, var) x = np.arange(6) * 1.0 x = np.reshape(x, (2, 3)) out = np.zeros(fn.shape) fn.forward([x], [out]) ks = np.array(kernel.shape) cc = np.floor((ks - 1) / 2.0).astype(np.int) # Center coordinate y = np.zeros((2, 3)) for i in range(2): for j in range(3): for s in range(2): for t in range(3): y[i, j] += kernel[ks[0] - 1 - s, ks[1] - 1 - t] * \ x[(s + i - cc[0]) % 2, (t + j - cc[1]) % 3] # For loop same as convolve #y = ndimage.convolve(x, kernel, mode='wrap') self.assertItemsAlmostEqual(out, y) # Adjoint.# x = np.arange(6) * 1.0 x = np.reshape(x, (2, 3)) out = np.zeros(var.shape) fn.adjoint([x], [out]) y = np.zeros((2, 3)) for i in range(2): for j in range(3): for s in range(2): for t in range(3): y[i, j] += kernel[s, t] * x[(s + i - (ks[0] - 1 - cc[0])) % 2, (t + j - (ks[1] - 1 - cc[1])) % 3] # For loop same as correlate #y = ndimage.correlate(x, kernel, mode='wrap') self.assertItemsAlmostEqual(out, y) # Diagonal form. x = Variable(5) kernel = np.arange(5) fn = conv(kernel, x) assert fn.is_diag(freq=True) assert not fn.is_diag(freq=False) forward_kernel = psf2otf(kernel, (5,), 1) self.assertItemsAlmostEqual(fn.get_diag(freq=True)[x], forward_kernel)
def test_combo(self): """Test subsampling followed by convolution. """ # Forward. var = Variable((2, 3)) kernel = np.array([[1, 2, 3]]) # 2x3 fn = vstack([conv(kernel, subsample(var, (2, 1)))]) fn = CompGraph(fn) x = np.arange(6) * 1.0 x = np.reshape(x, (2, 3)) out = np.zeros(fn.output_size) fn.forward(x.flatten(), out) y = np.zeros((1, 3)) xsub = x[::2, ::1] y = ndimage.convolve(xsub, kernel, mode='wrap') self.assertItemsAlmostEqual(np.reshape(out, y.shape), y) # Adjoint. x = np.arange(3) * 1.0 x = np.reshape(x, (1, 3)) out = np.zeros(var.size) fn.adjoint(x.flatten(), out) y = ndimage.correlate(x, kernel, mode='wrap') y2 = np.zeros((2, 3)) y2[::2, :] = y self.assertItemsAlmostEqual(np.reshape(out, y2.shape), y2) out = np.zeros(var.size) fn.adjoint(x.flatten(), out) self.assertItemsAlmostEqual(np.reshape(out, y2.shape), y2)