Exemple #1
0
def main(args):

    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(
        np.array([3941.30175781, 2856.94287109, 2519.35791016]))

    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)

        images = tf.placeholder(tf.float32,
                                shape=(None, gen_image_size, gen_image_size,
                                       3),
                                name='input')

        # Normalize
        images_norm = (images - img_mean) / img_stddev

        # Resize to appropriate size for the encoder
        images_norm_resize = tf.image.resize_images(
            images_norm, (gen_image_size, gen_image_size))

        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)

        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance / 2)
        latent_var = mean + epsilon * std

        # Create decoder
        reconstructed_norm = vae.decoder(latent_var, False)

        # Un-normalize
        reconstructed = (reconstructed_norm * img_stddev) + img_mean

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options,
                                                log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            vae_checkpoint = os.path.expanduser(args.vae_checkpoint)
            print('Restoring VAE checkpoint: %s' % vae_checkpoint)
            saver.restore(sess, vae_checkpoint)

            filename = os.path.expanduser(args.attributes_filename)
            with h5py.File(filename, 'r') as f:
                latent_vars = np.array(f.get('latent_vars'))
                attributes = np.array(f.get('attributes'))
                #fields = np.array(f.get('fields'))
                attribute_vectors = np.array(f.get('attribute_vectors'))

            # Reconstruct faces while adding varying amount of the selected attribute vector
            attribute_index = 31  # 31: 'Smiling'
            image_indices = [
                8, 11, 13, 18, 19, 26, 31, 39, 47, 54, 56, 57, 58, 59, 60, 73
            ]
            nrof_images = len(image_indices)
            nrof_interp_steps = 10
            sweep_latent_var = np.zeros(
                (nrof_interp_steps * nrof_images, args.latent_var_size),
                np.float32)
            for j in range(nrof_images):
                image_index = image_indices[j]
                idx = np.argwhere(
                    attributes[:, attribute_index] == -1)[image_index, 0]
                for i in range(nrof_interp_steps):
                    sweep_latent_var[i + nrof_interp_steps * j, :] = latent_vars[
                        idx, :] + 5.0 * i / nrof_interp_steps * attribute_vectors[
                            attribute_index, :]

            recon = sess.run(reconstructed,
                             feed_dict={latent_var: sweep_latent_var})

            img = facenet.put_images_on_grid(
                recon,
                shape=(nrof_interp_steps * 2, int(math.ceil(nrof_images / 2))))

            image_filename = os.path.expanduser(args.output_image_filename)
            print('Writing generated image to %s' % image_filename)
            misc.imsave(image_filename, img)
Exemple #2
0
def main(args):

    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(
        np.array([3941.30175781, 2856.94287109, 2519.35791016]))

    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(
            model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)
    log_file_name = os.path.join(model_dir, 'logs.h5')

    # Write arguments to a text file
    facenet.write_arguments_to_file(args,
                                    os.path.join(model_dir, 'arguments.txt'))

    # Store some git revision info in a text file in the log directory
    src_path, _ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, model_dir, ' '.join(sys.argv))

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)

        train_set = facenet.get_dataset(args.data_dir)
        image_list, _ = facenet.get_image_paths_and_labels(train_set)

        # Create the input queue
        input_queue = tf.train.string_input_producer(image_list, shuffle=True)

        nrof_preprocess_threads = 4
        image_per_thread = []
        for _ in range(nrof_preprocess_threads):
            file_contents = tf.read_file(input_queue.dequeue())
            image = tf.image.decode_image(file_contents, channels=3)
            image = tf.image.resize_image_with_crop_or_pad(
                image, args.input_image_size, args.input_image_size)
            image.set_shape((args.input_image_size, args.input_image_size, 3))
            image = tf.cast(image, tf.float32)
            #pylint: disable=no-member
            image_per_thread.append([image])

        images = tf.train.batch_join(image_per_thread,
                                     batch_size=args.batch_size,
                                     capacity=4 * nrof_preprocess_threads *
                                     args.batch_size,
                                     allow_smaller_final_batch=False)

        # Normalize
        images_norm = (images - img_mean) / img_stddev

        # Resize to appropriate size for the encoder
        images_norm_resize = tf.image.resize_images(
            images_norm, (gen_image_size, gen_image_size))

        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)

        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance / 2)
        latent_var = mean + epsilon * std

        # Create decoder network
        reconstructed_norm = vae.decoder(latent_var, True)

        # Un-normalize
        reconstructed = (reconstructed_norm * img_stddev) + img_mean

        # Create reconstruction loss
        if args.reconstruction_loss_type == 'PLAIN':
            images_resize = tf.image.resize_images(
                images, (gen_image_size, gen_image_size))
            reconstruction_loss = tf.reduce_mean(
                tf.reduce_sum(tf.pow(images_resize - reconstructed, 2)))
        elif args.reconstruction_loss_type == 'PERCEPTUAL':
            network = importlib.import_module(args.model_def)

            reconstructed_norm_resize = tf.image.resize_images(
                reconstructed_norm,
                (args.input_image_size, args.input_image_size))

            # Stack images from both the input batch and the reconstructed batch in a new tensor
            shp = [-1] + images_norm.get_shape().as_list()[1:]
            input_images = tf.reshape(
                tf.stack([images_norm, reconstructed_norm_resize], axis=0),
                shp)
            _, end_points = network.inference(input_images,
                                              1.0,
                                              phase_train=False,
                                              bottleneck_layer_size=128,
                                              weight_decay=0.0)

            # Get a list of feature names to use for loss terms
            feature_names = args.loss_features.replace(' ', '').split(',')

            # Calculate L2 loss between original and reconstructed images in feature space
            reconstruction_loss_list = []
            for feature_name in feature_names:
                feature_flat = slim.flatten(end_points[feature_name])
                image_feature, reconstructed_feature = tf.unstack(tf.reshape(
                    feature_flat, [2, args.batch_size, -1]),
                                                                  num=2,
                                                                  axis=0)
                reconstruction_loss = tf.reduce_mean(tf.reduce_sum(
                    tf.pow(image_feature - reconstructed_feature, 2)),
                                                     name=feature_name +
                                                     '_loss')
                reconstruction_loss_list.append(reconstruction_loss)
            # Sum up the losses in for the different features
            reconstruction_loss = tf.add_n(reconstruction_loss_list,
                                           'reconstruction_loss')
        else:
            pass

        # Create KL divergence loss
        kl_loss = kl_divergence_loss(mean, log_variance)
        kl_loss_mean = tf.reduce_mean(kl_loss)

        total_loss = args.alfa * kl_loss_mean + args.beta * reconstruction_loss

        learning_rate = tf.train.exponential_decay(
            args.initial_learning_rate,
            global_step,
            args.learning_rate_decay_steps,
            args.learning_rate_decay_factor,
            staircase=True)

        # Calculate gradients and make sure not to include parameters for the perceptual loss model
        opt = tf.train.AdamOptimizer(learning_rate)
        grads = opt.compute_gradients(total_loss,
                                      var_list=get_variables_to_train())

        # Apply gradients
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
        with tf.control_dependencies([apply_gradient_op]):
            train_op = tf.no_op(name='train')

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        facenet_saver = tf.train.Saver(get_facenet_variables_to_restore())

        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.compat.v1.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(
            gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():

            if args.reconstruction_loss_type == 'PERCEPTUAL':
                if not args.pretrained_model:
                    raise ValueError(
                        'A pretrained model must be specified when using perceptual loss'
                    )
                pretrained_model_exp = os.path.expanduser(
                    args.pretrained_model)
                print('Restoring pretrained model: %s' % pretrained_model_exp)
                facenet_saver.restore(sess, pretrained_model_exp)

            log = {
                'total_loss': np.zeros((0, ), np.float),
                'reconstruction_loss': np.zeros((0, ), np.float),
                'kl_loss': np.zeros((0, ), np.float),
                'learning_rate': np.zeros((0, ), np.float),
            }

            step = 0
            print('Running training')
            while step < args.max_nrof_steps:
                start_time = time.time()
                step += 1
                save_state = step > 0 and (step % args.save_every_n_steps == 0
                                           or step == args.max_nrof_steps)
                if save_state:
                    _, reconstruction_loss_, kl_loss_mean_, total_loss_, learning_rate_, rec_ = sess.run(
                        [
                            train_op, reconstruction_loss, kl_loss_mean,
                            total_loss, learning_rate, reconstructed
                        ])
                    img = facenet.put_images_on_grid(rec_, shape=(16, 8))
                    misc.imsave(
                        os.path.join(model_dir,
                                     'reconstructed_%06d.png' % step), img)
                else:
                    _, reconstruction_loss_, kl_loss_mean_, total_loss_, learning_rate_ = sess.run(
                        [
                            train_op, reconstruction_loss, kl_loss_mean,
                            total_loss, learning_rate
                        ])
                log['total_loss'] = np.append(log['total_loss'], total_loss_)
                log['reconstruction_loss'] = np.append(
                    log['reconstruction_loss'], reconstruction_loss_)
                log['kl_loss'] = np.append(log['kl_loss'], kl_loss_mean_)
                log['learning_rate'] = np.append(log['learning_rate'],
                                                 learning_rate_)

                duration = time.time() - start_time
                print(
                    'Step: %d \tTime: %.3f \trec_loss: %.3f \tkl_loss: %.3f \ttotal_loss: %.3f'
                    % (step, duration, reconstruction_loss_, kl_loss_mean_,
                       total_loss_))

                if save_state:
                    print('Saving checkpoint file')
                    checkpoint_path = os.path.join(model_dir, 'model.ckpt')
                    saver.save(sess,
                               checkpoint_path,
                               global_step=step,
                               write_meta_graph=False)
                    print('Saving log')
                    with h5py.File(log_file_name, 'w') as f:
                        for key, value in iteritems(log):
                            f.create_dataset(key, data=value)
def main(args):
    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(
        np.array([3941.30175781, 2856.94287109, 2519.35791016])
    )

    vae_checkpoint = os.path.expanduser(args.vae_checkpoint)
    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    with tf.Graph().as_default():
        filename = args.image
        # Read image and convert to 3 channels
        image = Image.open(filename)
        image = image.convert('RGB')
        image = image.resize((gen_image_size, gen_image_size), Image.ANTIALIAS)
        image = np.array(image).reshape(1, gen_image_size, gen_image_size, 3)

        images = tf.placeholder(
            tf.float32,
            shape=(None, gen_image_size, gen_image_size, 3),
            name='input'
        )

        # Normalize
        images_norm = (images - img_mean) / img_stddev

        # Resize to appropriate size for the encoder
        images_norm_resize = tf.image.resize_images(
            images_norm, (gen_image_size, gen_image_size)
        )

        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)

        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance / 2)
        latent_var = mean + epsilon * std

        # Create decoder
        reconstructed_norm = vae.decoder(latent_var, False)

        # Un-normalize
        reconstructed = (reconstructed_norm * img_stddev) + img_mean

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)

        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.GPUOptions(
            per_process_gpu_memory_fraction=gpu_memory_fraction
        )
        sess = tf.Session(
            config=tf.ConfigProto(
                gpu_options=gpu_options,
                log_device_placement=False
            )
        )
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        # coord = tf.train.Coordinator()
        # tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():
            if vae_checkpoint:
                print('Restoring VAE checkpoint: %s' % vae_checkpoint)
                saver.restore(sess, vae_checkpoint)

            start_time = time.time()
            latent_var_ = sess.run(latent_var, feed_dict={images: image})
            duration = time.time() - start_time
            print('Sess run: %.3f seconds' % duration)
            # NOTE: This will print the 'Out of range' warning if the last
            # batch is not full, as described by
            # https://github.com/tensorflow/tensorflow/issues/8330

            print('Got latent var shape {}'.format(latent_var_.shape))
            # Reconstruct faces while adding varying amount of
            # the selected attribute vector
            attribute_index = 31  # 31: 'Smiling'
            with h5py.File(args.attributes_filename, 'r') as f:
                # latent_vars = np.array(f.get('latent_vars'))
                # attributes = np.array(f.get('attributes'))
                # fields = np.array(f.get('fields'))
                attribute_vectors = np.array(f.get('attribute_vectors'))

            nrof_interp_steps = 10
            sweep_latent_var = np.zeros(
                (nrof_interp_steps, latent_var.shape[1]), np.float32
            )
            for i in range(nrof_interp_steps):
                sweep_latent_var[i, :] = (
                    latent_var_[0, :] + 5.0 * i /
                    nrof_interp_steps * attribute_vectors[attribute_index, :]
                )

            print('Reconstruct image')
            start_time = time.time()
            recon = sess.run(
                reconstructed,
                feed_dict={latent_var: sweep_latent_var}
            )
            duration = time.time() - start_time
            print('Sess run: %.3f seconds' % duration)

            img = facenet.put_images_on_grid(
                recon, shape=(nrof_interp_steps, int(math.ceil(1 / 2)))
            )

            image_filename = os.path.expanduser(args.output_image_filename)
            print('Writing generated image to %s' % image_filename)
            misc.imsave(image_filename, img)
Exemple #4
0
def main(args):
  
    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(np.array([3941.30175781, 2856.94287109, 2519.35791016]))
  
    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    subdir = datetime.strftime(datetime.now(), '%Y%m%d-%H%M%S')
    model_dir = os.path.join(os.path.expanduser(args.models_base_dir), subdir)
    if not os.path.isdir(model_dir):  # Create the model directory if it doesn't exist
        os.makedirs(model_dir)
    log_file_name = os.path.join(model_dir, 'logs.h5')
    
    # Write arguments to a text file
    facenet.write_arguments_to_file(args, os.path.join(model_dir, 'arguments.txt'))
        
    # Store some git revision info in a text file in the log directory
    src_path,_ = os.path.split(os.path.realpath(__file__))
    facenet.store_revision_info(src_path, model_dir, ' '.join(sys.argv))
    
    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        global_step = tf.Variable(0, trainable=False)
        
        train_set = facenet.get_dataset(args.data_dir)
        image_list, _ = facenet.get_image_paths_and_labels(train_set)
        
        # Create the input queue
        input_queue = tf.train.string_input_producer(image_list, shuffle=True)
    
        nrof_preprocess_threads = 4
        image_per_thread = []
        for _ in range(nrof_preprocess_threads):
            file_contents = tf.read_file(input_queue.dequeue())
            image = tf.image.decode_image(file_contents, channels=3)
            image = tf.image.resize_image_with_crop_or_pad(image, args.input_image_size, args.input_image_size)
            image.set_shape((args.input_image_size, args.input_image_size, 3))
            image = tf.cast(image, tf.float32)
            #pylint: disable=no-member
            image_per_thread.append([image])
    
        images = tf.train.batch_join(
            image_per_thread, batch_size=args.batch_size,
            capacity=4 * nrof_preprocess_threads * args.batch_size,
            allow_smaller_final_batch=False)
        
        # Normalize
        images_norm = (images-img_mean) / img_stddev

        # Resize to appropriate size for the encoder 
        images_norm_resize = tf.image.resize_images(images_norm, (gen_image_size,gen_image_size))
        
        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)
        
        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance/2)
        latent_var = mean + epsilon * std
        
        # Create decoder network
        reconstructed_norm = vae.decoder(latent_var, True)
        
        # Un-normalize
        reconstructed = (reconstructed_norm*img_stddev) + img_mean
        
        # Create reconstruction loss
        if args.reconstruction_loss_type=='PLAIN':
            images_resize = tf.image.resize_images(images, (gen_image_size,gen_image_size))
            reconstruction_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(images_resize - reconstructed,2)))
        elif args.reconstruction_loss_type=='PERCEPTUAL':
            network = importlib.import_module(args.model_def)

            reconstructed_norm_resize = tf.image.resize_images(reconstructed_norm, (args.input_image_size,args.input_image_size))

            # Stack images from both the input batch and the reconstructed batch in a new tensor 
            shp = [-1] + images_norm.get_shape().as_list()[1:]
            input_images = tf.reshape(tf.stack([images_norm, reconstructed_norm_resize], axis=0), shp)
            _, end_points = network.inference(input_images, 1.0, 
                phase_train=False, bottleneck_layer_size=128, weight_decay=0.0)

            # Get a list of feature names to use for loss terms
            feature_names = args.loss_features.replace(' ', '').split(',')

            # Calculate L2 loss between original and reconstructed images in feature space
            reconstruction_loss_list = []
            for feature_name in feature_names:
                feature_flat = slim.flatten(end_points[feature_name])
                image_feature, reconstructed_feature = tf.unstack(tf.reshape(feature_flat, [2,args.batch_size,-1]), num=2, axis=0)
                reconstruction_loss = tf.reduce_mean(tf.reduce_sum(tf.pow(image_feature-reconstructed_feature, 2)), name=feature_name+'_loss')
                reconstruction_loss_list.append(reconstruction_loss)
            # Sum up the losses in for the different features
            reconstruction_loss = tf.add_n(reconstruction_loss_list, 'reconstruction_loss')
        else:
            pass
        
        # Create KL divergence loss
        kl_loss = kl_divergence_loss(mean, log_variance)
        kl_loss_mean = tf.reduce_mean(kl_loss)
        
        total_loss = args.alfa*kl_loss_mean + args.beta*reconstruction_loss
        
        learning_rate = tf.train.exponential_decay(args.initial_learning_rate, global_step,
            args.learning_rate_decay_steps, args.learning_rate_decay_factor, staircase=True)
        
        # Calculate gradients and make sure not to include parameters for the perceptual loss model
        opt = tf.train.AdamOptimizer(learning_rate)
        grads = opt.compute_gradients(total_loss, var_list=get_variables_to_train())
        
        # Apply gradients
        apply_gradient_op = opt.apply_gradients(grads, global_step=global_step)
        with tf.control_dependencies([apply_gradient_op]):
            train_op = tf.no_op(name='train')

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        
        facenet_saver = tf.train.Saver(get_facenet_variables_to_restore())

        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)

        with sess.as_default():
            
            if args.reconstruction_loss_type=='PERCEPTUAL':
                if not args.pretrained_model:
                    raise ValueError('A pretrained model must be specified when using perceptual loss')
                pretrained_model_exp = os.path.expanduser(args.pretrained_model)
                print('Restoring pretrained model: %s' % pretrained_model_exp)
                facenet_saver.restore(sess, pretrained_model_exp)
          
            log = {
                'total_loss': np.zeros((0,), np.float),
                'reconstruction_loss': np.zeros((0,), np.float),
                'kl_loss': np.zeros((0,), np.float),
                'learning_rate': np.zeros((0,), np.float),
                }
            
            step = 0
            print('Running training')
            while step < args.max_nrof_steps:
                start_time = time.time()
                step += 1
                save_state = step>0 and (step % args.save_every_n_steps==0 or step==args.max_nrof_steps)
                if save_state:
                    _, reconstruction_loss_, kl_loss_mean_, total_loss_, learning_rate_, rec_ = sess.run(
                          [train_op, reconstruction_loss, kl_loss_mean, total_loss, learning_rate, reconstructed])
                    img = facenet.put_images_on_grid(rec_, shape=(16,8))
                    misc.imsave(os.path.join(model_dir, 'reconstructed_%06d.png' % step), img)
                else:
                    _, reconstruction_loss_, kl_loss_mean_, total_loss_, learning_rate_ = sess.run(
                          [train_op, reconstruction_loss, kl_loss_mean, total_loss, learning_rate])
                log['total_loss'] = np.append(log['total_loss'], total_loss_)
                log['reconstruction_loss'] = np.append(log['reconstruction_loss'], reconstruction_loss_)
                log['kl_loss'] = np.append(log['kl_loss'], kl_loss_mean_)
                log['learning_rate'] = np.append(log['learning_rate'], learning_rate_)

                duration = time.time() - start_time
                print('Step: %d \tTime: %.3f \trec_loss: %.3f \tkl_loss: %.3f \ttotal_loss: %.3f' % (step, duration, reconstruction_loss_, kl_loss_mean_, total_loss_))

                if save_state:
                    print('Saving checkpoint file')
                    checkpoint_path = os.path.join(model_dir, 'model.ckpt')
                    saver.save(sess, checkpoint_path, global_step=step, write_meta_graph=False)
                    print('Saving log')
                    with h5py.File(log_file_name, 'w') as f:
                        for key, value in log.iteritems():
                            f.create_dataset(key, data=value)
def main(args):
  
    img_mean = np.array([134.10714722, 102.52040863, 87.15436554])
    img_stddev = np.sqrt(np.array([3941.30175781, 2856.94287109, 2519.35791016]))
    
    vae_def = importlib.import_module(args.vae_def)
    vae = vae_def.Vae(args.latent_var_size)
    gen_image_size = vae.get_image_size()

    with tf.Graph().as_default():
        tf.set_random_seed(args.seed)
        
        images = tf.placeholder(tf.float32, shape=(None,gen_image_size,gen_image_size,3), name='input')
        
        # Normalize
        images_norm = (images-img_mean) / img_stddev

        # Resize to appropriate size for the encoder 
        images_norm_resize = tf.image.resize_images(images_norm, (gen_image_size,gen_image_size))
        
        # Create encoder network
        mean, log_variance = vae.encoder(images_norm_resize, True)
        
        epsilon = tf.random_normal((tf.shape(mean)[0], args.latent_var_size))
        std = tf.exp(log_variance/2)
        latent_var = mean + epsilon * std
        
        # Create decoder
        reconstructed_norm = vae.decoder(latent_var, False)
        
        # Un-normalize
        reconstructed = (reconstructed_norm*img_stddev) + img_mean

        # Create a saver
        saver = tf.train.Saver(tf.trainable_variables(), max_to_keep=3)
        
        # Start running operations on the Graph
        gpu_memory_fraction = 1.0
        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction)
        sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        tf.train.start_queue_runners(coord=coord, sess=sess)
        

        with sess.as_default():
          
            vae_checkpoint = os.path.expanduser(args.vae_checkpoint)
            print('Restoring VAE checkpoint: %s' % vae_checkpoint)
            saver.restore(sess, vae_checkpoint)
           
            filename = os.path.expanduser(args.attributes_filename)
            with h5py.File(filename,'r') as f:
                latent_vars = np.array(f.get('latent_vars'))
                attributes = np.array(f.get('attributes'))
                #fields = np.array(f.get('fields'))
                attribute_vectors = np.array(f.get('attribute_vectors'))

            # Reconstruct faces while adding varying amount of the selected attribute vector
            attribute_index = 31 # 31: 'Smiling'
            image_indices = [8,11,13,18,19,26,31,39,47,54,56,57,58,59,60,73]
            nrof_images = len(image_indices)
            nrof_interp_steps = 10
            sweep_latent_var = np.zeros((nrof_interp_steps*nrof_images, args.latent_var_size), np.float32)
            for j in range(nrof_images):
                image_index = image_indices[j]
                idx = np.argwhere(attributes[:,attribute_index]==-1)[image_index,0]
                for i in range(nrof_interp_steps):
                    sweep_latent_var[i+nrof_interp_steps*j,:] = latent_vars[idx,:] + 5.0*i/nrof_interp_steps*attribute_vectors[attribute_index,:]
                
            recon = sess.run(reconstructed, feed_dict={latent_var:sweep_latent_var})
            
            img = facenet.put_images_on_grid(recon, shape=(nrof_interp_steps*2,int(math.ceil(nrof_images/2))))
            
            image_filename = os.path.expanduser(args.output_image_filename)
            print('Writing generated image to %s' % image_filename)
            misc.imsave(image_filename, img)