def reconstruct_faces( self, out_path, imgs_to_reconstruct, transformer=lambda x: img_ops.unbound_image_values(x)): out_original_path = fs_utils.add_suffix_to_path(out_path, "original") self.__save_original_images(imgs_to_reconstruct, out_original_path, transformer) img_size = imgs_to_reconstruct.shape[1] sess = tf.InteractiveSession() with tf.device(device_utils.get_device()): input_images = tf.placeholder(dtype=tf.float32, shape=[1, img_size, img_size, 3]) _, _, z_mean, z_cov_log_sq = self.load_enc_with_weights_func( sess, input_images) eps = tf.random_normal(shape=(1, self.z_dim), mean=0.0, stddev=1.0) z = z_mean + tf.multiply(tf.sqrt(tf.exp(z_cov_log_sq)), eps) gen_out_layer, _ = self.load_gen_with_weights_func(sess, z) x_reconstr = gen_out_layer.outputs reconstr_images = [] for i in xrange(imgs_to_reconstruct.shape[0]): reconstr_img = sess.run( x_reconstr, feed_dict={input_images: [imgs_to_reconstruct[i, :, :, :]]}) reconstr_images.append(reconstr_img[0]) self.logger.info("Reconstructed %d/%d images" % (i, imgs_to_reconstruct.shape[0])) img_ops.save_gen_images(reconstr_images, out_path, transformer)
def main(): args = parse_args() celeb_faces_generator = GanImgGenerator(args.gen_weights_path, cifar10_gan.load_gen_with_weights) out_vis_path = args.out_vis_path celeb_faces_generator.generate_images( args.how_many_samples, out_vis_path, transformer=lambda x: img_ops.unbound_image_values(x[:, :, ::-1]))
def __save_gen_images(gen_images_list, out_path, transformer): imgs = [ transformer(img_ops.unbound_image_values(gen_img)) for gen_img in gen_images_list ] imgs = np.array(imgs) grid_size = int(np.sqrt(imgs.shape[0])) big_img = img_ops.merge_images_to_grid(imgs, (grid_size, grid_size)) cv2.imwrite(out_path, big_img)
def main(): args = parse_args() reconstructor = VaeImgReconstructor(load_enc_weights(args.enc_params_path), load_gen_weights(args.gen_params_path), get_latent_dim(args.gen_params_path)) cifar_imgs = np.array( get_cifar_samples(args.cifar_ds_path, args.how_many_samples)) reconstructor.reconstruct_faces( args.out_vis_path, cifar_imgs, transformer=lambda x: img_ops.unbound_image_values(x[:, :, ::-1]))
def generate_images(self, samples_num, out_path, transformer=lambda x: img_ops.unbound_image_values(x)): sess = tf.InteractiveSession() with tf.device(device_utils.get_device()): z = tf.random_normal(shape=[1, self.z_dim], mean=0., stddev=1., dtype=tf.float32) gen_out_layer, _ = self.load_gen_with_weights_func(sess, z, self.gen_params_path) x_generated = gen_out_layer.outputs gen_images = [] for i in xrange(samples_num): x_gen = sess.run(x_generated)[0] gen_images.append(x_gen) self.logger.info("Generated %d/%d image" % (i, samples_num)) img_ops.save_gen_images(gen_images, out_path, transformer)
def inv_transform(transformed_img, scale_img_opt): if scale_img_opt == '0_to_1': return img_ops.unbound_images_values_01(transformed_img).astype( np.uint8) else: return img_ops.unbound_image_values(transformed_img).astype(np.uint8)