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
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
def __test(d, n): X_cplx = numpy.random.randn(d, n) + 1.j * numpy.random.randn(d, n) X_real2 = _cdl.complex_to_real2(X_cplx) # 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
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