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, :, :] img = tiled_gfs(grid, sym_cm=False, global_cm=False) img = img.resize((zoom * img.size[0], zoom * img.size[1])) img.save(out_fname + "-orig.png")
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()
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,:,:] img = tiled_gfs(grid, sym_cm=False, global_cm=False) img = img.resize( (zoom*img.size[0], zoom*img.size[1]) ) img.save(out_fname+"-orig.png")
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()