def save_png(self, save_dir, epoch): if torch.cuda.is_available(): noise = self.noise.cuda() with torch.no_grad(): gen_data = self.G(noise) gen_data = gen_data.cpu().detach().numpy() img_bhwc = np.transpose(gen_data, (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='CIFAR10 samples ' + str(epoch) + " epochs") plotting.plt.savefig(save_dir + "cifar_sample_feature_match_" + str(epoch) + ".png")
parser.add_argument('--data_dir', type=str, default='~/temp/data') parser.add_argument('--plot_title', type=str, default=None) args = parser.parse_args() print(args) data_dir = args.data_dir trainx, trainy = cifar10_data.load(data_dir) ids = [[] for i in range(10)] for i in range(trainx.shape[0]): if len(ids[trainy[i]]) < 10: ids[trainy[i]].append(i) if np.alltrue(np.asarray([len(_ids) >= 10 for _ids in ids])): break images = np.zeros((10 * 10, 32, 32, 3), dtype='uint8') for i in range(len(ids)): for j in range(len(ids[i])): images[10 * j + i] = trainx[ids[i][j]].transpose([1, 2, 0]) print(ids) img_tile = plotting.img_tile(images, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img( img_tile, title=args.plot_title if args.plot_title != 'None' else None) plotting.plt.savefig(args.save_dir + '/cifar10_orig_images.png') plotting.plt.close('all')
import plotting import numpy as np from data_reader import load from settings import DATA_DIR, DATA_SEED trainx, trainy = load(DATA_DIR, subset='train') rng_data = np.random.RandomState(DATA_SEED) inds = rng_data.permutation(trainx.shape[0]) trainx = trainx[inds] trainy = trainy[inds] txs = [] for j in range(10): txs.append(trainx[trainy == j][:10]) txs = np.concatenate(txs, axis=0) img_bhwc = np.transpose(txs, (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='CIFAR10 samples') plotting.plt.savefig("cifar_sample.png")
c += 1 if c == pat: break # log progress to console print( "Iteration %d, time = %ds, train bits_per_dim = %.4f, test bits_per_dim = %.4f" % (epoch, time.time() - begin, train_loss_gen, test_loss_gen)) sys.stdout.flush() if epoch % args.save_interval == -1: print("Generating samples...") # generate samples from the model sampled_x = sample_from_model(sess) img_tile = plotting.img_tile(sampled_x, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.data_set + ' samples') plotting.plt.savefig( os.path.join(args.save_dir, '%s_sample%d.png' % (args.data_set, epoch))) plotting.plt.close('all') # save params saver.save(sess, args.save_dir + '/params_' + args.data_set + '.ckpt') np.savez(args.save_dir + '/test_bpd_' + args.data_set + '.npz', test_bpd=np.array(test_bpd)) print("online: ", first) print("offline: ", testl)
loss_unl += lu train_err += te train_err2 +=te2 e = train_batch_gen(trainx_unl2[t*args.batch_size:(t+1)*args.batch_size],lr) # disc and gen for unlabeled data are different gen_loss += float(e) #clamp_D_fn() loss_lab /= nr_batches_train loss_unl /= nr_batches_train train_err /= nr_batches_train train_err2 /= nr_batches_train # test test_err = 0. for t in range(nr_batches_test): test_err += test_batch(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size]) test_err /= nr_batches_test # report print("Iteration %d, time = %ds, loss_lab = %.4f, loss_unl = %.4f, train err = %.4f, train err2 = %.4f,gen loss = %.4f,test err = %.4f" % (epoch, time.time()-begin, loss_lab, loss_unl, train_err,train_err2,gen_loss,test_err)) sys.stdout.flush() # generate samples from the model sample_x = samplefun() img_bhwc = np.transpose(sample_x[:100,], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='CIFAR10 samples') plotting.plt.savefig("cifar_sample_feature_match_50_pai.png") # save params np.savez('disc_params.npz', *[p.get_value() for p in disc_params]) np.savez('gen_params.npz', *[p.get_value() for p in gen_params])
x_temp = T.tensor4() # Test generator in sampling procedure samplefun = th.function(inputs=[], outputs=gen_dat) sample_x = [] for k in range(20): sample_x.append(samplefun()) sample_x = np.concatenate(sample_x, axis=0) img_bhwc = np.transpose(sample_x[:100, ], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='') plotting.plt.savefig(args.data_name + "_patches_gen.png") img_bhwc = np.transpose(trainx[:100, ], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='') plotting.plt.savefig(args.data_name + "_patches_train.png") evens = [x for x in range(int(test_matched.shape[0] / 2)) if x % 2 == 0] odds = [x for x in range(int(test_matched.shape[0] / 2)) if x % 2 == 1] disc_layers.append(ll.GlobalPoolLayer(disc_layers[f_low_dim])) batch_size_test = 100
test_err = 0. for t in range(nr_batches_test): #if t % 100 == 0: #print(t) #print(test_batch_out(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size])) test_err += test_batch(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size]) test_err /= nr_batches_test test_err2 = 0. for t in range(nr_batches_test): #if t % 100 == 0: #print(t) #print(test_batch_out(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size])) test_err2 += test_batch2(testx[t*args.batch_size:(t+1)*args.batch_size],testy[t*args.batch_size:(t+1)*args.batch_size]) test_err2 /= nr_batches_test # report print("Iteration %d, time = %ds, loss_lab = %.4f, loss_unl = %.4f, train err = %.4f, test err = %.4f, test err avg. = %.4f" % (epoch, time.time()-begin, loss_lab, loss_unl, train_err, test_err, test_err2)) sys.stdout.flush() # generate samples from the model sample_x = samplefun() img_bhwc = np.transpose(sample_x[:100,], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='CIFAR10 samples', rgb=False) plotting.plt.savefig("cxr_sample_feature_match{:}.png".format(epoch)) # save params #np.savez('disc_params.npz', *[p.get_value() for p in disc_params]) #np.savez('gen_params.npz', *[p.get_value() for p in gen_params])
NNdiff = np.sum(np.sum(np.sum(np.square( np.expand_dims(sample_x, axis=1) - np.expand_dims(trainx, axis=0)), axis=2), axis=2), axis=2) NN = trainx[np.argmin(NNdiff, axis=1)] NN = np.transpose(NN[:100], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) NN_tile = plotting.img_tile(NN, aspect_ratio=1.0, border_color=1.0, stretch=True) img_tile = np.concatenate((img_tile, NN_tile), axis=1) img_tile = img_tile.reshape(img_tile.shape[0], img_tile.shape[1]) img = plotting.plot_img(img_tile, title='sketch samples') plotting.plt.savefig(args.results_dir + '/bg_sketch_sample_minibatch.png') if epoch % 50 == 0: plotting.plt.savefig(args.results_dir + '/' + str(epoch) + '.png') # save params np.savez(args.results_dir + '/train/disc_params' + str(epoch) + '.npz', *[p.get_value() for p in disc_params]) np.savez(args.results_dir + '/train/gen_params' + str(epoch) + '.npz', *[p.get_value() for p in gen_params]) np.save(args.results_dir + '/train/errors.npy', a) np.save(args.results_dir + '/train/sig.npy', sigm) plotting.plt.close('all')
parser = argparse.ArgumentParser() parser.add_argument('--save_dir', type=str, default='./log') parser.add_argument('--data_dir', type=str, default='/home/tim/data') parser.add_argument('--plot_title', type=str, default=None) args = parser.parse_args() print(args) data_dir = args.data_dir trainx, trainy = cifar10_data.load(data_dir) ids = [[] for i in range(10)] for i in range(trainx.shape[0]): if len(ids[trainy[i]]) < 10: ids[trainy[i]].append(i) if np.alltrue(np.asarray([len(_ids) >= 10 for _ids in ids])): break images = np.zeros((10*10,32,32,3),dtype='uint8') for i in range(len(ids)): for j in range(len(ids[i])): images[10*j+i] = trainx[ids[i][j]].transpose([1,2,0]) print(ids) img_tile = plotting.img_tile(images, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.plot_title if args.plot_title != 'None' else None) plotting.plt.savefig(args.save_dir + '/cifar10_orig_images.png') plotting.plt.close('all')
# report print( "Epoch %d, time = %ds, loss_lab = %.3f, learning_rate = %.6f,train err= %.4f, test err = %.4f" % (epoch, time.time() - begin, loss_lab, learning_rate_var.get_value(), train_err, test_err)) sys.stdout.flush() # generate samples from the model sample_y = np.int32(np.random.choice(num_classes, size=(args.batch_size))) sample_x = samplefun(sample_y) img_bhwc = np.transpose(sample_x[:100, ], (0, 2, 3, 1)) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='SVHN samples') plotting.plt.savefig(args.results_dir + args.dataset + '/svhn_sample.png') if epoch % 20 == 0: NNdiff = np.sum(np.sum(np.sum(np.square( np.expand_dims(sample_x, axis=1) - np.expand_dims(trainx, axis=0)), axis=2), axis=2), axis=2) NN = trainx[np.argmin(NNdiff, axis=1)] NN = np.transpose(NN[:100], (0, 2, 3, 1)) NN_tile = plotting.img_tile(NN, aspect_ratio=1.0, border_color=1.0, stretch=True) img_tile = np.concatenate((img_tile, NN_tile), axis=1) img = plotting.plot_img(img_tile, title='SVHN samples')
def measure_all(i): # Read in image bcg_im3 = fits.getdata(bcgdir+bcg_list[i].replace('maps','bcg')) bcg_im = np.zeros((bcg_im3.shape[0]+2,bcg_im3.shape[1]+2),dtype=bcg_im3.dtype) bcg_im[1:-1,1:-1]=bcg_im3 # Image size in pixels and Mpc tsy,tsx = bcg_im.shape mpc = tsx/0.4 # pix/Mpc xctr=int(tsx/2); yctr=int(tsy/2) t0=time.time() the_res=[] ; colnames=[] # Compute moments -- input param w_guess # Weighted moments full image---------------------------------------------- q20,q11,q02 = sl.weighted_higher_order_moments(bcg_im,w_guess,2,xctr,yctr) q00 = sl.weighted_higher_order_moments(bcg_im,w_guess,0,xctr,yctr)[0] Tchi = np.around(np.sqrt((q20+q02)/q00),decimals=2) size_a,size_b,pos_angle = sl.axes_lengths_from_chi_moments(Tchi,(q20-q02)/(q20+q02),2*q11/(q20+q02)) # Save results the_res += [xctr,yctr,Tchi,size_b/size_a,pos_angle+90] colnames += ['moment_wghtd_'+str(w_guess)+'_xc','moment_wghtd_'+str(w_guess)+'_yc','moment_wghtd_'+str(w_guess)+'_T','moment_wghtd_'+str(w_guess)+'_q','mom_wgh_'+str(w_guess)+'_pos_ang'] mom_p=((xctr,yctr),Tchi, np.around((q20-q02)/(q20+q02),decimals=4), np.around((2*q11)/(q20+q02),decimals=4)) # # Fit Sersic----------------------------------------------------- # fit_morph = sl.fit_sersic(bcg_im,verbose=False,plot_mod=False) # q_sersic= (1-fit_morph.sersic_ellip)/(1+fit_morph.sersic_ellip) # # Save results # if q_sersic==-1: # the_res+=[-1,-1,-1,q_sersic,-1] # else: # the_res+=[fit_morph.sersic_xc,fit_morph.sersic_yc,fit_morph.sersic_rhalf, # q_sersic,fit_morph.sersic_theta*180/np.pi + 90] # colnames+=['sersic_fit_xc','sersic_fit_yc','sersic_fit_r','sersic_fit_q','sersic_fit_pos_ang'] # Measure (longest) contour and its properties -- ((percentile variable used here))----------- longcon,longcon_props = sl.get_longest_contour_props(bcg_im,contour_intensity=np.percentile(bcg_im,percentile)) a_c,b_c = longcon_props[0][0].major_axis_length,longcon_props[0][0].minor_axis_length theta_c = 180 - longcon_props[0][0].orientation*180/np.pi # Save results if a_c==0: the_res+=[longcon_props[0][0].centroid[0],longcon_props[0][0].centroid[1],np.sqrt(b_c*a_c),-1, -99,] else: the_res+=[longcon_props[0][0].centroid[0],longcon_props[0][0].centroid[1],np.sqrt(b_c*a_c),b_c/a_c, theta_c] colnames+=['contour_'+str(percentile)+'ptile_xc','contour_'+str(percentile)+'ptile_yc','contour_'+str(percentile)+'ptile_r','contour_'+str(percentile)+'ptile_q','contour_'+str(percentile)+'ptile_pos_ang'] #make visual check images ----------------------------------------------- pl.plot_img(bcg_im, mom_p,fit_morph, contour=longcon, contour_props=longcon_props, zoomins=[0,100,150], save=args.save_data_dir+'TEST_shapes_%s.pdf' %(bcg_list[i]), show=False) print(i,bcg_list[i], time.time()-t0) return the_res,colnames
test_list.append(test_err) # report print("Epoch %d, time = %ds, loss_lab = %.3f, learning_rate = %.6f,train err= %.4f, test err = %.4f" % (epoch, time.time() - begin, loss_lab, learning_rate_var.get_value(),train_err, test_err)) sys.stdout.flush() # generate samples from the model if epoch % 20 == 0: sample_y = np.int32(np.random.choice(num_classes, size=(args.batch_size))) sample_x = samplefun(sample_y) img_bhwc = np.transpose(sample_x[:100, ], (0, 2, 3, 1)) # print(img_bhwc.shape) img_tile = plotting.img_tile(img_bhwc, aspect_ratio=1.0, border_color=1.0, stretch=True) # print(img_tile.shape) img_tile = np.squeeze(img_tile) img = plotting.plot_img(img_tile, title='ddsm samples') plotting.plt.savefig(args.results_dir + args.dataset + '/ddsm_sample_'+str(epoch)+'.png') plotting.plt.close('all') if epoch % 100 == 0: np.savez(args.results_dir + args.dataset + '/disc_params' + str(epoch) + '.npz', *[p.get_value() for p in disc_params]) np.savez(args.results_dir + args.dataset + '/gen_params' + str(epoch) + '.npz', *[p.get_value() for p in gen_params]) sns.set() plotting.plt.plot(train_list) plotting.plt.plot(test_list) plotting.plt.title('training and testing error') plotting.plt.ylabel('err') plotting.plt.xlabel('epoch') plotting.plt.legend(['training', 'testing'], loc='upper right') plotting.plt.savefig(args.results_dir + args.dataset + '/error.png')