Exemplo n.º 1
0
    def __test(w, h, m, n, rho, alpha, nonneg):
        # Generate several frames of data
        X_space = numpy.random.randn(h, w, m, n) * alpha / rho

        # Compute shrinkage point-wise
        Xshrunk = (X_space > alpha / rho) * (X_space - alpha / rho)

        if not nonneg:
            Xshrunk = Xshrunk + (X_space < -alpha / rho) * (X_space + alpha / rho)

        # Compute 2d-dfts
        X_freq  = numpy.fft.fft2(X_space, axes=(0, 1))

        # Reshape the data into columns
        X_cols  = X_freq.reshape((h * w * m, n), order='F')

        # Split real and complex
        X       = _cdl.complex_to_real2(X_cols)

        # First try without pre-allocation
        Xout    = _cdl.reg_l1_space(X, rho, alpha, width=w, height=h, nonneg=nonneg)

        # Convert back to complex
        Xout_cplx = _cdl.real2_to_complex(Xout)

        # reshape back into four dimensions
        Xout_freq = Xout_cplx.reshape((h, w, m, n), order='F')


        # Invert the fourier transform
        Xout_space = numpy.fft.ifft2(Xout_freq, axes=(0, 1)).real

        assert EQ(Xout_space, Xshrunk)

        # Now do it again with pre-allocation
        Xout_pre = numpy.empty_like(X, order='A')
        _cdl.reg_l1_space(X, rho, alpha, width=w, height=h, nonneg=nonneg, Xout=Xout_pre)

        # Convert back to complex
        Xout_cplx_pre = _cdl.real2_to_complex(Xout_pre)

        # reshape back into four dimensions
        Xout_freq_pre = Xout_cplx_pre.reshape((h, w, m, n), order='F')
        # Invert the fourier transform
        Xout_space_pre = numpy.fft.ifft2(Xout_freq_pre, axes=(0, 1)).real

        assert EQ(Xout_space_pre, Xshrunk)
        pass
Exemplo n.º 2
0
    def __test(d, n, rho, alpha):
        # Generate a random matrix, scale by alpha/rho to encourage
        # non-trivial solutions
        X = numpy.random.randn(2 * d, n) * alpha / rho

        # Compute magnitudes of complex values
        X_cplx  = _cdl.real2_to_complex(X)
        X_abs   = numpy.abs(X_cplx)

        # Compute shrinkage on X
        # x -> (1 - (alpha/rho) / |x|)_+ * x
        S       = X_abs.copy()

        # Avoid numerical instability here
        S[S < alpha / rho] = (alpha / rho)
        S   = (1 - (alpha/rho) / S)
        
        # Tile it to get real and complex
        S   = numpy.tile(S, (2, 1))

        # Compute shrinkage
        Xshrunk = X * S

        # First, test without pre-allocation
        Xout = _cdl.reg_l1_complex(X, rho, alpha)

        assert EQ(Xout, Xshrunk, rtol=1e-3, atol=1e-6)

        # Now test with pre-allocation
        Xout_pre = numpy.zeros_like(X, order='A')
        _cdl.reg_l1_complex(X, rho, alpha, Xout_pre)

        assert EQ(Xout_pre, Xshrunk, rtol=1e-3, atol=1e-6)
        pass
Exemplo n.º 3
0
def global_transform(X_batch, pad_data, encoder_, n_atoms, nonneg):
    X_batch = X_batch.swapaxes(0, 1).swapaxes(1, 2)

    h, w, n = X_batch.shape

    # Fourier transform
    X_new = _cdl.patches_to_vectors(X_batch, pad_data=pad_data)

    # Encode
    X_new = encoder_(X_new)

    X_new = _cdl.real2_to_complex(X_new)
    X_new = X_new.reshape((-1, X_new.shape[1] * n_atoms), order='F')
    X_new = _cdl.complex_to_real2(X_new)

    X_new = _cdl.vectors_to_patches(X_new, w, pad_data=pad_data, real=True)

    X_new = X_new.reshape((X_new.shape[0], X_new.shape[1], n_atoms, n),
                          order='F')

    X_new = X_new.swapaxes(3, 0).swapaxes(3, 2).swapaxes(3, 1)

    if nonneg:
        X_new = np.maximum(X_new, 0.0)

    return X_new
Exemplo n.º 4
0
Arquivo: CDL.py Projeto: CityU-HAN/CDL
def global_transform(X_batch, pad_data, encoder_, n_atoms, nonneg):
    X_batch = X_batch.swapaxes(0, 1).swapaxes(1, 2)

    h, w, n = X_batch.shape

    # Fourier transform
    X_new = _cdl.patches_to_vectors(X_batch, pad_data=pad_data)

    # Encode
    X_new = encoder_(X_new)

    X_new = _cdl.real2_to_complex(X_new)
    X_new = X_new.reshape( (-1, X_new.shape[1] * n_atoms), order='F')
    X_new = _cdl.complex_to_real2(X_new)   

    X_new = _cdl.vectors_to_patches(X_new, w, 
                                    pad_data=pad_data, 
                                    real=True)

    X_new = X_new.reshape( (X_new.shape[0], X_new.shape[1], 
                                n_atoms, n), 
                            order='F')

    X_new = X_new.swapaxes(3, 0).swapaxes(3, 2).swapaxes(3, 1)

    if nonneg:
        X_new = np.maximum(X_new, 0.0)

    return X_new
Exemplo n.º 5
0
    def __test(d, n):
        X_real2 = numpy.random.randn(2 * d, n)
        X_cplx  = _cdl.real2_to_complex(X_real2)

        # Verify shape match
        assert (X_cplx.shape[0] * 2  == X_real2.shape[0] and
                X_cplx.shape[1]      == X_real2.shape[1])

        # Verify numerical match
        assert (EQ(X_cplx.real, X_real2[:d, :]) and
                EQ(X_cplx.imag, X_real2[d:, :]))
        pass
Exemplo n.º 6
0
    def __test(d, m, n, rho, alpha):
        # Generate a random matrix 2*d*m-by-n matrix
        # scale by alpha/rho to encourage non-trivial solutions
        X = numpy.random.randn(2 * d * m, n) * alpha / rho

        # Compute the properly shrunk matrix
        X_cplx  = _cdl.real2_to_complex(X)
        S       = (X_cplx.conj() * X_cplx).real

        X_norms = numpy.zeros((m, n))

        for k in range(m):
            X_norms[k, :] = numpy.sum(S[k*d:(k+1)*d, :], axis=0)**0.5

        Xshrunk = numpy.zeros_like(X, order='A')

        for i in range(n):
            for k in range(m):
                if X_norms[k, i] > alpha / rho:
                    scale = 1.0 - alpha / (rho * X_norms[k, i])
                else:
                    scale = 0.0
                Xshrunk[(k*d):(k+1)*d, i] = scale * X[d*k:d*(k+1), i]
                Xshrunk[d*(m+k):(m+k+1)*d, i] = scale * X[d*(m+k):d*(m+k+1), i]
                pass
            pass

        # First, test without pre-allocation
        Xout = _cdl.reg_l2_group(X, rho, alpha, m)
        assert EQ(Xout, Xshrunk)

        # Now test with pre-allocation

        Xout_pre = numpy.zeros_like(X, order='A')
        _cdl.reg_l2_group(X, rho, alpha, m, Xout_pre)

        assert EQ(Xout_pre, Xshrunk)
        pass