def test_bit_transform(): DataLoader = cifar10_data.DataLoader train_data = DataLoader('/tmp/pxpp-bitwise/data', subset='train', batch_size=20) x = train_data.next(20) x_scaled = U.scale_function(x) d_ = 32 x_3bit = (np.floor(x.astype(np.float32) / d_) * d_) / ((255 // d_) * d_) x_3bit = 2. * x_3bit - 1. bits = U.num_to_bit(x, 3) recov = U.bit_to_num(bits, 3) img_tile = plotting.img_tile(x_scaled, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile) save_dir = '/tmp/paralel-px/save' plotting.plt.savefig(os.path.join(save_dir, 'test1.png')) plotting.plt.close('all') img_tile = plotting.img_tile(x_3bit, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile) save_dir = '/tmp/paralel-px/save' plotting.plt.savefig(os.path.join(save_dir, 'test2.png')) plotting.plt.close('all') recov_scaled = recov / 255. img_tile = plotting.img_tile(recov_scaled, aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile) save_dir = '/tmp/paralel-px/save' plotting.plt.savefig(os.path.join(save_dir, 'test3.png')) plotting.plt.close('all')
def Gen_Images(self, sess, epoch): sample_x = self._sample_from_model(sess) img_tile = plotting.img_tile(sample_x[:int( np.floor(np.sqrt(self.args.batch_size * self.args.nr_gpu))**2)], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=self.args.data_set + ' samples') plotting.plt.savefig( os.path.join(self.args.save_dir, '%s_sample%d.png' % (self.args.data_set, epoch))) plotting.plt.close('all')
def sample_from_model(sess, n_samples=args.nr_gpu * args.batch_size): sample_x = np.zeros((0,) + obs_shape, dtype=np.float32) while len(sample_x) < n_samples: x_gen = [np.zeros((args.batch_size,) + obs_shape, dtype=np.float32) for i in range(args.nr_gpu)] for yi in range(obs_shape[0]): for xi in range(obs_shape[1]): new_x_gen_np = sess.run(new_x_gen, {xs[i]: x_gen[i] for i in range(args.nr_gpu)}) for i in range(args.nr_gpu): x_gen[i][:, yi, xi, :] = new_x_gen_np[i][:, yi, xi, :] sample_x = np.concatenate([sample_x] + x_gen, axis=0) img_tile = plotting.img_tile( sample_x[:int(np.floor(np.sqrt(n_samples))**2)], 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(save_dir, '%s_samples.png' % args.data_set)) np.save(os.path.join(save_dir, '%s_samples.npy' % args.data_set), sample_x) plotting.plt.close('all')
test_losses.append(l) test_loss_gen = np.mean(test_losses) test_bpd.append(test_loss_gen) # 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 == 0: # generate samples from the model sample_x = sample_from_model(sess) img_tile = plotting.img_tile(sample_x[:int( np.floor(np.sqrt(args.batch_size * args.nr_gpu))**2)], 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('starting training') test_bpd = [] lr = args.learning_rate with tf.Session() as sess: ckpt_file = args.save_dir + '/params_' + args.data_set + '.ckpt' print('restoring parameters from', ckpt_file) saver.restore(sess, ckpt_file) td = next(test_data) from PIL import Image for i in range(64): Image.fromarray(td[i]).save("/data/ziz/jxu/CelebA/celeba_cropped_test_samples/c"+str(i)+".png") td = np.cast[np.float32]((td - 127.5) / 127.5) imgs = [td[i*args.batch_size:(i+1)*args.batch_size, :, :, :] for i in range(args.nr_gpu)] img_tile = plotting.img_tile(ret_original_images(imgs)[:64], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.data_set + ' original') plotting.plt.savefig(os.path.join( args.save_dir, '%s_original.png' % (args.data_set, ))) plotting.plt.close('all') img_tile = plotting.img_tile(ret_masked_images(imgs)[:64], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.data_set + ' masked') plotting.plt.savefig(os.path.join( args.save_dir, '%s_masked.png' % (args.data_set, ))) plotting.plt.close('all') sample_x = complete(imgs, sess) img_tile = plotting.img_tile(sample_x[:64], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title=args.data_set + ' completion') plotting.plt.savefig(os.path.join(
losses.append(train_loss) np.savez(args.save_dir + '/train_losses_' + args.data_set + '.npz', train_loss=losses) save_dir = args.save_dir + '/params_' + args.data_set + '.ckpt' """ sampling """ if epoch % args.save_interval == 0 and epoch > 0: count = 0 for d in test_data: if count == 4: break count += 1 inputs, labels = d gen_begin = time.time() outputs = model.sample() outputs_scaled = outputs / 255. img_tile = plotting.img_tile( outputs_scaled[:int(np.floor(np.sqrt(args.batch_size))**2)], 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_iter%d.png' % (args.data_set, epoch, count))) plotting.plt.close('all') model.save_model(save_dir)
print 'Creating Phi and measurments' np.random.seed(args.seed) Phi = np.zeros((m, N**2, 3)) for i in range(3): Phi[:, :, i] = orth(np.random.randn(m, N**2).T).T test_img_r = np.cast[np.float64](test_img / 255.0) y = np.einsum('mnr,ndr->mdr', Phi, test_img_r.reshape(-1, 1, 3)) #Initialization np.random.seed(3) init_imgs = np.random.uniform(low=0.0, high=255.0, size=test_imgs.shape) init_imgs = np.cast[np.float32]((init_imgs - 127.5) / 127.5) out_img = plotting.img_tile(test_imgs, border=0, stretch=True, tile_shape=tile_shape) mplimg.imsave(path + '/original_img', out_img) prev_update = np.zeros_like(init_imgs) l = 0 print 'Optimization started' for itr in range(args.max_epochs): # Backpropagation to inputs x = np.split(init_imgs, args.nr_gpu) grads_n, loss_gen_n = sess.run([grads_sum, loss_gen_sum], {xs[i]: x[i] for i in range(args.nr_gpu)}) select = np.random.choice(2, grads_n.shape, p=[args.p_dropout, 1 - args.p_dropout])
l = sess.run(bits_per_dim_test, feed_dict) test_losses.append(l) test_loss_gen = np.mean(test_losses) test_bpd.append(test_loss_gen) # 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 == 0: # generate samples from the model sample_x = sample_from_model(sess) img_tile = plotting.img_tile(sample_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))
# compute likelihood over test data test_losses = [] for d in test_data: feed_dict = make_feed_dict(d) l = sess.run(bits_per_dim_test, feed_dict) test_losses.append(l) test_loss_gen = np.mean(test_losses) test_bpd.append(test_loss_gen) # 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 == 0: # generate samples from the model sample_x = sample_from_model(sess) img_tile = plotting.img_tile(sample_x[:int(np.floor(np.sqrt( args.batch_size * args.nr_gpu))**2)], 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))
def test_model(): dim_max = 16 batch_size_init = 5 batch_size = 5 nr_filters = 64 nr_resnet = 1 dropout_p = 0.5 channel = 3 nr_gpu = 8 polyak_decay = 0.9995 max_epoch = 20 bit_len = 3 # test initializer print('test initialization') imgs_init = np.random.randint(0, 2**bit_len, size=(batch_size_init, dim_max, dim_max, channel)) imgs = np.random.randint(0, 2**bit_len, size=(batch_size, dim_max, dim_max, channel)) labels_init = np.random.randint(0, 10, size=(batch_size_init)) labels = np.random.randint(0, 10, size=(batch_size)) imgs_init, labels_init = U.make_feed_data((imgs_init, labels_init), bit_len) imgs, labels = U.make_feed_data((imgs, labels), bit_len) model = PxCNN_bw(nr_resnet=nr_resnet, nr_filters=nr_filters, resnet_nonlinearity='concat_tanh', class_conditional=False, dim=dim_max, channel=3, bit_len=bit_len, batch_size=batch_size, batch_size_init=batch_size_init, dropout_p=dropout_p, nr_gpu=nr_gpu, num_labels=10, polyak_decay=polyak_decay) model.initialize(x_init=imgs_init) # , y_init = labels_init) lr = 0.001 print('test training') for i in range(max_epoch): lr *= polyak_decay accu = model.train(x=imgs, lr=lr) # y=labels) print('Iteration %d: loss is %.4f.' % (i, accu)) print('test sampling') begin = time.time() sample = model.sample() #y=labels) print('sample time is %.4f' % (time.time() - begin)) print('sample shape') print(sample.shape) print('test plotting') save_dir = '/tmp/pxpp-bitwise/bitwise/save' img_tile = plotting.img_tile( sample[:int(np.floor(np.sqrt(batch_size))**2)], aspect_ratio=1.0, border_color=1.0, stretch=True) img = plotting.plot_img(img_tile, title='test samples') plotting.plt.savefig(os.path.join(save_dir, 'test_sample%d.png')) plotting.plt.close('all') return