def infer(self, y_labels=None): tf.global_variables_initializer().run() #saver to save the model self.saver = tf.train.Saver(max_to_keep=2) could_load, checkpoint_counter = self.load(self.checkpoint_dir) if could_load: z_sample = np.random.uniform(low = -1., high = 1., size = (self.sample_num, self.z_dim)) if y_labels is None: if self.dataset_name == 'mnist' or self.dataset_name == 'fashion-mnist': y_labels = np.zeros((self.sample_num, self.y_dim)) for i in range(self.y_dim): y_labels[i*self.y_dim:(i+1)*self.y_dim, i] = 1 elif self.dataset_name == 'anime': y_labels = np.zeros((self.sample_num, self.y_dim)) #blue hair, blue eye; blue hair, green eye; green hair, blue eye; green hair, red eye; pink hair, aqua eye; pink hair, purple eye; red hair, blue eye; red hair, brown eye indices = [[8, 21], [8, 18], [4, 21], [4, 20], [7, 16], [7, 17], [5, 21], [5, 19]] for i in range(8): y_labels[i*8 : (i+1)*8, indices[i]] = 1 samples = self.sess.run(self.predict_images, feed_dict={self.z: z_sample, self.y_gen: y_labels}) samples = montage(samples) image_path = check_folder(self.sample_dir + '/' + self.model_dir) + '/' + 'sample.jpg' imsave(image_path, samples) print("[*] Load Successfully") else: print("[!] Load failed...")
def visualize_results(self, epoch, z_sample): samples = self.sess.run(self.predict_images, feed_dict={self.z: z_sample}) samples = montage(samples) image_path = check_folder(self.sample_dir + '/' + self.model_dir) + '/' + 'epoch_%d' % epoch + '_test.jpg' imsave(image_path, samples) #show the images plt.axis('off') if self.c_dim == 1: plt.imshow(samples, cmap = 'gray') else: plt.imshow(samples) plt.show() plt.close()
def infer(self): tf.global_variables_initializer().run() #saver to save the model self.saver = tf.train.Saver(max_to_keep=2) could_load, checkpoint_counter = self.load(self.checkpoint_dir) if could_load: z_sample = np.random.uniform(low = -1., high = 1., size = (self.sample_num, self.z_dim)) samples = self.sess.run(self.predict_images, feed_dict={self.z: z_sample}) samples = montage(samples) image_path = check_folder(self.sample_dir + '/' + self.model_dir) + '/' + 'sample.jpg' imsave(image_path, samples) print("[*] Load Successfully") else: print("[!] Load failed...")
def main(argv): # turn off log message tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.FATAL) # initializer init_op = tf.group(tf.initializers.global_variables(), tf.initializers.local_variables()) with tf.Session(config=utils.config(index=FLAGS.gpu_index)) as sess: # set network kwars = { 'sess': sess, 'noise_dim': FLAGS.noise_dim, 'image_size': FLAGS.image_size, 'generator_layer': generator_layer, 'discriminator_layer': discriminator_layer, 'is_training': False } Model = DCGAN(**kwars) # initialize sess.run(init_op) # test Model.restore_model( os.path.join(FLAGS.indir, 'model', 'model_{}'.format(FLAGS.model_index))) noise = np.random.uniform(-1., 1., size=[FLAGS.batch_size, FLAGS.noise_dim]) samples = Model.gen_sample(noise) samples = samples[:, :, :, 0] m = utils.montage(samples) gen_img = m plt.axis('off') plt.imshow(gen_img, cmap='gray') plt.show()
def test_mnist(on_cloud=0): """Train an autoencoder on MNIST. This function will train an autoencoder on MNIST and also save many image files during the training process, demonstrating the latent space of the inner most dimension of the encoder, as well as reconstructions of the decoder. """ # load MNIST n_code = 2 mnist = MNIST(split=[0.8, 0.1, 0.1]) ae = VAE(input_shape=[None, 784], n_filters=[512, 256], n_hidden=64, n_code=n_code, activation=tf.nn.sigmoid, convolutional=False, variational=True) n_examples = 100 zs = np.random.uniform(-1.0, 1.0, [4, n_code]).astype(np.float32) zs = utils.make_latent_manifold(zs, n_examples) learning_rate = 0.02 optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize( ae['cost']) # We create a session to use the graph sess = tf.Session() sess.run(tf.global_variables_initializer()) # Fit all training data t_i = 0 batch_i = 0 batch_size = 200 n_epochs = 10 test_xs = mnist.test.images[:n_examples] if (on_cloud == 0): utils.montage(test_xs.reshape((-1, 28, 28)), 'test_xs.png') else: utils.montage(test_xs.reshape((-1, 28, 28)), '/output/test_xs.png') for epoch_i in range(n_epochs): train_i = 0 train_cost = 0 for batch_xs, _ in mnist.train.next_batch(batch_size): train_cost += sess.run([ae['cost'], optimizer], feed_dict={ ae['x']: batch_xs, ae['train']: True, ae['keep_prob']: 1.0 })[0] train_i += 1 if batch_i % 10 == 0: # Plot example reconstructions from latent layer recon = sess.run(ae['y'], feed_dict={ ae['z']: zs, ae['train']: False, ae['keep_prob']: 1.0 }) m = utils.montage(recon.reshape((-1, 28, 28)), 'manifold_%08d.png' % t_i) # Plot example reconstructions recon = sess.run(ae['y'], feed_dict={ ae['x']: test_xs, ae['train']: False, ae['keep_prob']: 1.0 }) m = utils.montage(recon.reshape((-1, 28, 28)), 'reconstruction_%08d.png' % t_i) t_i += 1 batch_i += 1 valid_i = 0 valid_cost = 0 for batch_xs, _ in mnist.valid.next_batch(batch_size): valid_cost += sess.run([ae['cost']], feed_dict={ ae['x']: batch_xs, ae['train']: False, ae['keep_prob']: 1.0 })[0] valid_i += 1 print('train:', train_cost / train_i, 'valid:', valid_cost / valid_i)
def train_vae( files, input_shape, learning_rate=0.0001, batch_size=100, n_epochs=50, n_examples=10, crop_shape=[64, 64, 3], crop_factor=0.8, n_filters=[100, 100, 100, 100], n_hidden=0, # n_hidden=256, n_code=50, convolutional=True, variational=True, filter_sizes=[3, 3, 3, 3], dropout=False, #dropout=True, keep_prob=0.8, activation=tf.nn.relu, img_step=300, save_step=300, ckpt_name="vae.ckpt", ckpt_load_dir="./checkpoints/", on_cloud=0): """General purpose training of a (Variational) (Convolutional) Autoencoder. Supply a list of file paths to images, and this will do everything else. Parameters ---------- files : list of strings List of paths to images. input_shape : list Must define what the input image's shape is. learning_rate : float, optional Learning rate. batch_size : int, optional Batch size. n_epochs : int, optional Number of epochs. n_examples : int, optional Number of example to use while demonstrating the current training iteration's reconstruction. Creates a square montage, so make sure int(sqrt(n_examples))**2 = n_examples, e.g. 16, 25, 36, ... 100. crop_shape : list, optional Size to centrally crop the image to. crop_factor : float, optional Resize factor to apply before cropping. n_filters : list, optional Same as VAE's n_filters. n_hidden : int, optional Same as VAE's n_hidden. n_code : int, optional Same as VAE's n_code. convolutional : bool, optional Use convolution or not. variational : bool, optional Use variational layer or not. filter_sizes : list, optional Same as VAE's filter_sizes. dropout : bool, optional Use dropout or not keep_prob : float, optional Percent of keep for dropout. activation : function, optional Which activation function to use. img_step : int, optional How often to save training images showing the manifold and reconstruction. save_step : int, optional How often to save checkpoints. ckpt_name : str, optional Checkpoints will be named as this, e.g. 'model.ckpt' """ batch = create_input_pipeline(files=files, batch_size=batch_size, n_epochs=n_epochs, crop_shape=crop_shape, crop_factor=crop_factor, shape=input_shape) ae = VAE(input_shape=[None] + crop_shape, convolutional=convolutional, variational=variational, n_filters=n_filters, n_hidden=n_hidden, n_code=n_code, dropout=dropout, filter_sizes=filter_sizes, activation=activation, on_cloud=0) # Create a manifold of our inner most layer to show # example reconstructions. This is one way to see # what the "embedding" or "latent space" of the encoder # is capable of encoding, though note that this is just # a random hyperplane within the latent space, and does not # encompass all possible embeddings. zs = np.random.uniform(-1.0, 1.0, [4, n_code]).astype(np.float32) zs = utils.make_latent_manifold(zs, n_examples) optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize( ae['cost']) # We create a session to use the graph sess = tf.Session() saver = tf.train.Saver() sess.run(tf.global_variables_initializer()) # This will handle our threaded image pipeline coord = tf.train.Coordinator() # Ensure no more changes to graph tf.get_default_graph().finalize() # Start up the queues for handling the image pipeline threads = tf.train.start_queue_runners(sess=sess, coord=coord) #if os.path.exists(ckpt_name_load + '.index') or os.path.exists(ckpt_name_load): # saver.restore(sess, ckpt_name_load) could_load, checkpoint_counter = load(ckpt_load_dir, sess, saver) if could_load: counter = checkpoint_counter print(" [*] Load SUCCESS") else: print(" [!] Load failed...") # Fit all training data t_i = 0 batch_i = 0 epoch_i = 0 cost = 0 n_files = len(files) test_xs = sess.run(batch) / 255.0 if (on_cloud == 0): utils.montage(test_xs, 'test_xs.png') else: utils.montage(test_xs, '/output/test_xs.png') try: while not coord.should_stop() and epoch_i < n_epochs: batch_i += 1 batch_xs = sess.run(batch) / 255.0 train_cost = sess.run([ae['cost'], optimizer], feed_dict={ ae['x']: batch_xs, ae['train']: True, ae['keep_prob']: keep_prob })[0] print(batch_i, train_cost) cost += train_cost if batch_i % n_files == 0: print('epoch:', epoch_i) print('average cost:', cost / batch_i) cost = 0 batch_i = 0 epoch_i += 1 if batch_i % img_step == 0: # Plot example reconstructions from latent layer recon = sess.run(ae['y'], feed_dict={ ae['z']: zs, ae['train']: False, ae['keep_prob']: 1.0 }) if (on_cloud == 0): utils.montage(recon.reshape([-1] + crop_shape), 'manifold_%08d.png' % t_i) else: utils.montage(recon.reshape([-1] + crop_shape), '/output/manifolds/manifold_%08d.png' % t_i) # Plot example reconstructions recon = sess.run(ae['y'], feed_dict={ ae['x']: test_xs, ae['train']: False, ae['keep_prob']: 1.0 }) print('reconstruction (min, max, mean):', recon.min(), recon.max(), recon.mean()) if (on_cloud == 0): utils.montage(recon.reshape([-1] + crop_shape), 'reconstruction_%08d.png' % t_i) else: utils.montage( recon.reshape([-1] + crop_shape), '/output/reconstruct/reconstruction_%08d.png' % t_i) t_i += 1 if batch_i % save_step == 0: # Save the variables to disk. saver.save(sess, ckpt_name, global_step=batch_i, write_meta_graph=False) except tf.errors.OutOfRangeError: print('Done.') finally: # One of the threads has issued an exception. So let's tell all the # threads to shutdown. coord.request_stop() # Wait until all threads have finished. coord.join(threads) # Clean up the session. sess.close()
############################################################################### # Print the evaluation results. Note that they are saved. So, they can be also # accessed when the model is loaded. print('[EVALUATION RESULTS]') loopModel.print_evaluation() # Get the first batch # The data in X contains two nparrays. Each array is a batch of images. Each # images in one array relates to the corresponding image in the other array # depending on the values of y, which state the class (in this case, class=0 # means low or no overlap and class 1 means large overlap) [X,y]=testGenerator.__getitem__(0) # Let's predict the first batch. The output is rounded since we have 2 classes. # That is, we round the prediction to be 0 or 1. thePredictions=np.round(loopModel.predict(X)) # To provide a "clear" representation, let's change the red channel of the # images to their predicted class (so, class 1 will have be sort of red and # the other ones sort of... non-red). Note that LoopGenerator can also work # with grayscale images. So, this "approach" to visualize loop information # would not work with grayscale images. Obviously. for i in range(X[0].shape[0]): X[0][i,:,:,0]=thePredictions[i] X[1][i,:,:,0]=thePredictions[i] # Now plot the modified images using montage montage(X[0]) montage(X[1])
training_cost / n_batches)) # Also, every 20 iterations, we'll draw the prediction of our # input xs, which should try to recreate our image! if (it_i + 1) % gif_step == 0: costs.append(training_cost / n_batches) ys_pred = model['Y_pred'].eval(feed_dict={model['X']: xs}, session=sess) img = ys_pred.reshape(imgs.shape) gifs.append(img) return gifs celeb_imgs = utils.get_celeb_imgs() plt.figure(figsize=(10, 10)) plt.imshow(utils.montage(celeb_imgs).astype(np.uint8)) # It doesn't have to be 100 images, explore! imgs = np.array(celeb_imgs).copy() gifs = train(imgs=imgs) montage_gifs = [ np.clip(utils.montage((m * 127.5) + 127.5), 0, 255).astype(np.uint8) for m in gifs ] _ = gif.build_gif(montage_gifs, saveto='multiple.gif') final = gifs[-1] final_gif = [ np.clip(((m * 127.5) + 127.5), 0, 255).astype(np.uint8) for m in final ] gif.build_gif(final_gif, saveto='final.gif')
W_fc2 = tf.Variable(tf.random_normal([1024, 10], mean=0.0, stddev=0.01)) b_fc2 = tf.Variable(tf.random_normal([10], mean=0.0, stddev=0.01)) y_pred = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2) # %% Define loss/eval/training functions cross_entropy = -tf.reduce_sum(y*tf.log(y_pred)) optimizer = tf.train.AdamOptimizer().minimize(cross_entropy) # %% Monitor accuracy correct_prediction = tf.equal( tf.argmax(y_pred,1), tf.argmax(y,1) ) accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float")) # Train n_epoch = 5 n_batch = 100 sess = tf.Session() sess.run(tf.initialize_all_variables()) for i in range(n_epoch): batch_xs, batch_ys = mnist.train.next_batch(n_batch) sess.run(optimizer,{x: batch_xs, y: batch_ys, keep_prob: 0.5}) # validation error print(sess.run(accuracy,{x: mnist.validation.images, y: mnist.validation.labels, keep_prob: 1.0})) # final error print(sess.run(accuracy,{x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0})) # %% Let's take a look at the kernels we've learned W = sess.run(W_conv1) plt.imshow(utils.montage(W / np.max(W)), cmap='coolwarm') plt.waitforbuttonpress()
# #Gets files and crops image files = [ os.path.join('img_align_celeba', file_i) for file_i in os.listdir('img_align_celeba') if file_i.endswith('.jpg') ] imgs = [] for file_i in files: img = plt.imread(file_i) square = utils.imcrop_tosquare(img) rsz = np.array( Image.fromarray(square).resize((100, 100), resample=Image.NEAREST)) imgs.append(rsz) imgs = np.array(imgs).astype(np.float32) plt.figure(figsize=(10, 10)) plt.imshow(utils.montage(imgs, saveto='dataset.png')) #calculates mean of images and saves into picture mean_img = np.mean(imgs, axis=0).astype(np.float32) assert (mean_img.shape == (100, 100, 3)) plt.figure(figsize=(10, 10)) plt.imshow(mean_img) plt.imsave(arr=mean_img, fname='mean.png') #calculates standard deviation of images and saves into picture std_img = np.std(imgs, axis=0).astype(np.float32) assert (std_img.shape == (100, 100) or std_img.shape == (100, 100, 3)) plt.figure(figsize=(10, 10)) std_img_show = std_img / np.max(std_img) plt.imshow(std_img_show) plt.imsave(arr=std_img_show, fname='std.png')
from utils import montage batch_size = 100 z_dim = 100 # dataset = 'lfw_new_imgs' dataset = 'celeba' sess = tf.Session() sess.run(tf.global_variables_initializer()) saver = tf.train.import_meta_graph( os.path.join('samples_' + dataset, 'wgan_' + dataset + '-60000.meta')) saver.restore(sess, tf.train.latest_checkpoint('samples_' + dataset)) # 获取计算图generator graph = tf.get_default_graph() g = graph.get_tensor_by_name('generator/g/Tanh:0') noise = graph.get_tensor_by_name('noise:0') is_training = graph.get_tensor_by_name('is_training:0') n = np.random.uniform(-1.0, 1.0, [batch_size, z_dim]).astype(np.float32) gen_imgs = sess.run(g, feed_dict={noise: n, is_training: False}) gen_imgs = (gen_imgs + 1) / 2 imgs = [img[:, :, :] for img in gen_imgs] gen_imgs = montage(imgs) gen_imgs = np.clip(gen_imgs, 0, 1) plt.figure(figsize=(8, 8)) plt.axis('off') plt.imshow(gen_imgs) plt.show()
im = utils.im_resize(im, (100, 100)) image_size = im.shape[:2] XF = vgg.get_Deep_Feature([im]) #求一个图片的list的平均的特征向量# original.append(im) # for each transform for j, (a, b) in enumerate(attribute_pairs): _, P, Q = make_manifolds(b, [a], im_path=path) PF = vgg.get_Deep_Feature(utils.im_generator(P[:K], image_size)) QF = vgg.get_Deep_Feature(utils.im_generator(Q[:K], image_size)) if True: WF = (QF - PF) / ((QF - PF)**2).mean() else: WF = (QF - PF) # for each interpolation step for delta in delta_list: print(path, b, delta) Y = vgg.Deep_Feature_inverse(XF + WF * delta, max_iter=max_iter, initial_image=im) result[-1].append(Y) original = numpy.array(original) result = numpy.array(result) if color_postprocess: result = utils.color_match(numpy.expand_dims(original, 1), result) m = utils.montage( numpy.concatenate([numpy.expand_dims(original, 1), result], axis=1)) utils.im_write('results/demo1.png', m) print('Output is results/demo1.png')