P = in_oversized[n, :, :] P_ = convolve2d(P, dog, 'same') P_ = P_[left:right, left:right] # Normalize and mean-free if options.mf: P_ -= P_.mean() if options.norm: P_max = max(P_.max(), -P_.min()) P_ /= (P_max + 1e-5) if options.varnorm: P_var = np.var(P_) P_ /= (np.sqrt(P_var) + 1e-5) tbl_out.append("patches", P_) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6 grid = U.transpose().reshape((D, size, size)) img = tiled_gfs(grid, sym_cm=False, global_cm=True) img = img.resize((zoom * img.size[0], zoom * img.size[1])) img.save(out_fname + "-components.png") grid = P[:100, :, :]
P_max = np.maximum(P_.max(axis=1), -P_.min(axis=1)) print P_max.shape P_ /= P_max[:, None] + 1e-5 if options.varnorm: dlog.progress("Normalizing...") P_var = np.var(P_, axis=1) P_ /= np.sqrt(P_var[:, None]) + 1e-5 # Make patches to 2d images again... P_ = P_.reshape((N_patches, size, size)) # And safe resulting patches for n in xrange(0, N_patches): if n % batch_size == 0: dlog.progress("Saving whitened patches (%d)" % n, n / N_patches) tbl_out.append("patches", P_[n]) # Safe principal components & eigenvalues for d in xrange(D): tbl_out.append("eig_vec", U[:, d]) tbl_out.append("eig_val", S[d]) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6 grid = U.transpose().reshape((D, size, size))
P = in_oversized[n,:,:] P_ = convolve2d(P, dog, 'same') P_ = P_[left:right, left:right] # Normalize and mean-free if options.mf: P_ -= P_.mean() if options.norm: P_max = max(P_.max(), -P_.min()) P_ /= (P_max+1e-5) if options.varnorm: P_var = np.var(P_) P_ /= (np.sqrt(P_var)+1e-5) tbl_out.append("patches", P_) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6 grid = U.transpose().reshape( (D, size, size) ) img = tiled_gfs(grid, sym_cm=False, global_cm=True) img = img.resize( (zoom*img.size[0], zoom*img.size[1]) ) img.save(out_fname+"-components.png") grid = P[:100,:,:]
P_max = np.maximum(P_.max(axis=1), -P_.min(axis=1)) print P_max.shape P_ /= P_max[:, None]+1e-5 if options.varnorm: dlog.progress("Normalizing...") P_var = np.var(P_, axis=1) P_ /= np.sqrt(P_var[:, None])+1e-5 # Make patches to 2d images again... P_ = P_.reshape( (N_patches, size, size) ) # And safe resulting patches for n in xrange(0, N_patches): if n % batch_size == 0: dlog.progress("Saving whitened patches (%d)" % n, n/N_patches) tbl_out.append("patches", P_[n]) # Safe principal components & eigenvalues for d in xrange(D): tbl_out.append("eig_vec", U[:,d]) tbl_out.append("eig_val", S[d]) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6
Q = Q[left:right, left:right] # Normalize and mean-free if options.mf: Q -= Q.mean() if options.norm: Q_max = max(Q.max(), -Q.min()) Q /= (Q_max + 1e-5) if options.varnorm: Q_var = np.var(Q_) Q_ /= (np.sqrt(Q_var) + 1e-5) # Ensure sanity assert np.isfinite(Q).all() tbl_out.append("patches", Q) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6 grid = U.transpose().reshape((D, size, size)) img = tiled_gfs(grid, sym_cm=False, global_cm=True) img = img.resize((zoom * img.size[0], zoom * img.size[1])) img.save(out_fname + "-components.png") grid = P[:100, :, :]
# Normalize and mean-free if options.mf: Q -= Q.mean() if options.norm: Q_max = max(Q.max(), -Q.min()) Q /= (Q_max+1e-5) if options.varnorm: Q_var = np.var(Q_) Q_ /= (np.sqrt(Q_var)+1e-5) # Ensure sanity assert np.isfinite(Q).all() tbl_out.append("patches", Q) in_h5.close() tbl_out.close() exit(0) #============================================================ # Safe debug-output zoom = 6 grid = U.transpose().reshape( (D, size, size) ) img = tiled_gfs(grid, sym_cm=False, global_cm=True) img = img.resize( (zoom*img.size[0], zoom*img.size[1]) ) img.save(out_fname+"-components.png") grid = P[:100,:,:]
out_fname = "patches-%d" % size out_tbl = AutoTable(out_fname+".h5") images_h5 = tables.openFile(images_fname, "r") images = images_h5.root.images N_images = images.shape[0] #ppi = (N_patches // N_images // 10) + 1 ppi = 4 for n in xrange(N_patches): if n % 1000 == 0: dlog.progress("Extracting patch %d" % n, n/N_patches) if n % ppi == 0: while True: img = images[np.random.randint(N_images)] img = img / img.max() oversized_batch = pri.extract_patches_from_single_image(img, (oversize, oversize), ppi) patches_batch = oversized_batch[:, (size//2):(size//2+size), (size//2):(size//2+size)] variance = np.var( patches_batch.reshape([ppi, -1] ), axis=1) if np.alltrue(variance > min_var): break out_tbl.append('oversized', oversized_batch[n%ppi]) out_tbl.append('patches', patches_batch[n%ppi]) out_tbl.close()
out_tbl = AutoTable(out_fname + ".h5") images_h5 = tables.openFile(images_fname, "r") images = images_h5.root.images N_images = images.shape[0] #ppi = (N_patches // N_images // 10) + 1 ppi = 4 for n in xrange(N_patches): if n % 1000 == 0: dlog.progress("Extracting patch %d" % n, n / N_patches) if n % ppi == 0: while True: img = images[np.random.randint(N_images)] img = img / img.max() oversized_batch = pri.extract_patches_from_single_image( img, (oversize, oversize), ppi) patches_batch = oversized_batch[:, (size // 2):(size // 2 + size), (size // 2):(size // 2 + size)] variance = np.var(patches_batch.reshape([ppi, -1]), axis=1) if np.alltrue(variance > min_var): break out_tbl.append('oversized', oversized_batch[n % ppi]) out_tbl.append('patches', patches_batch[n % ppi]) out_tbl.close()