Beispiel #1
0
def main(low_res_pkl: Path,  # Pickle file from which to take low res layers
         high_res_pkl: Path,  # Pickle file from which to take high res layers
         resolution: int,  # Resolution level at which to switch between models
         level: int = 0,  # Switch at Conv block 0 or 1?
         blend_width: Optional[float] = None,  # None = hard switch, float = smooth switch (logistic) with given width
         output_grid: Optional[Path] = "blended.jpg",  # Path of image file to save example grid (None = don't save)
         seed: int = 0,  # seed for random grid
         output_pkl: Optional[Path] = None,  # Output path of pickle (None = don't save)
         verbose: bool = False,  # Print out the exact blending fraction
         ):
    grid_size = (3, 3)

    tflib.init_tf()

    with tf.Session() as sess, tf.device('/gpu:0'):
        low_res_G, low_res_D, low_res_Gs = misc.load_pkl(low_res_pkl)
        high_res_G, high_res_D, high_res_Gs = misc.load_pkl(high_res_pkl)

        out = blend_models(low_res_Gs, high_res_Gs, resolution, level, blend_width=blend_width, verbose=verbose)

        if output_grid:
            rnd = np.random.RandomState(seed)
            grid_latents = rnd.randn(np.prod(grid_size), *out.input_shape[1:])
            grid_fakes = out.run(grid_latents, None, is_validation=True, minibatch_size=1)
            misc.save_image_grid(grid_fakes, output_grid, drange=[-1, 1], grid_size=grid_size)

        # TODO modify all the networks
        if output_pkl:
            misc.save_pkl((low_res_G, low_res_D, out), output_pkl)
Beispiel #2
0
    def _evaluate(self, Gs, Gs_kwargs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl(
            'http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/inception_v3_features.pkl'
        )
        activations = np.empty([self.num_images, inception.output_shape[1]],
                               dtype=np.float32)

        # Calculate statistics for reals.
        cache_file = self._get_cache_file_for_reals(num_images=self.num_images)
        os.makedirs(os.path.dirname(cache_file), exist_ok=True)
        if os.path.isfile(cache_file):
            mu_real, sigma_real = misc.load_pkl(cache_file)
        else:
            for idx, images in enumerate(
                    self._iterate_reals(minibatch_size=minibatch_size)):
                begin = idx * minibatch_size
                end = min(begin + minibatch_size, self.num_images)
                activations[begin:end] = inception.run(images[:end - begin],
                                                       num_gpus=num_gpus,
                                                       assume_frozen=True)
                if end == self.num_images:
                    break
            mu_real = np.mean(activations, axis=0)
            sigma_real = np.cov(activations, rowvar=False)
            misc.save_pkl((mu_real, sigma_real), cache_file)

        # Construct TensorFlow graph.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tf.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()
                inception_clone = inception.clone()
                latents = tf.random_normal([self.minibatch_per_gpu] +
                                           Gs_clone.input_shape[1:])
                labels = self._get_random_labels_tf(self.minibatch_per_gpu)
                images = Gs_clone.get_output_for(latents, labels, **Gs_kwargs)
                images = tflib.convert_images_to_uint8(images)
                print('shape before', images.shape)
                if images.shape[1] == 1:
                    #images = tf.repeat(images, 3, axis=1)
                    images = tf.concat([images, images, images], axis=1)
                    #images = tf.stack([images, images, images], axis=1)
                print('shape expanded ', images.shape)
                result_expr.append(inception_clone.get_output_for(images))

        # Calculate statistics for fakes.
        for begin in range(0, self.num_images, minibatch_size):
            self._report_progress(begin, self.num_images)
            end = min(begin + minibatch_size, self.num_images)
            activations[begin:end] = np.concatenate(tflib.run(result_expr),
                                                    axis=0)[:end - begin]
        mu_fake = np.mean(activations, axis=0)
        sigma_fake = np.cov(activations, rowvar=False)

        # Calculate FID.
        m = np.square(mu_fake - mu_real).sum()
        s, _ = scipy.linalg.sqrtm(np.dot(sigma_fake, sigma_real), disp=False)  # pylint: disable=no-member
        dist = m + np.trace(sigma_fake + sigma_real - 2 * s)
        self._report_result(np.real(dist))
    def _evaluate(self, Gs, Gs_kwargs, num_gpus, num_imgs, paths=None):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl(
            "http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/inception_v3_features.pkl"
        )

        # Compute statistics for reals
        cache_file = self._get_cache_file_for_reals(num_imgs)
        os.makedirs(os.path.dirname(cache_file), exist_ok=True)
        if os.path.isfile(cache_file):
            mu_real, sigma_real = misc.load_pkl(cache_file)
        else:
            imgs_iter = self._iterate_reals(minibatch_size=minibatch_size)
            feats_real = self._get_feats(imgs_iter, inception, minibatch_size)
            mu_real, sigma_real = self._feats_to_stats(feats_real)
            misc.save_pkl((mu_real, sigma_real), cache_file)

        if paths is not None:
            # Extract features for local sample image files (paths)
            feats = self._paths_to_feats(paths, inception_func, minibatch_size,
                                         num_imgs)
        else:
            # Extract features for newly generated fake images
            feats = self._gen_feats(Gs, inception, minibatch_size, num_imgs,
                                    num_gpus, Gs_kwargs)

        # Compute FID
        mu_fake, sigma_fake = _feats_to_stats(feats)
        self._report_result(
            self.compute_fid(mu_real, sigma_real, mu_fake, sigma_fake))
Beispiel #4
0
    def _evaluate(self, Gs, Gs_kwargs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        feature_net = misc.load_pkl(
            'https://drive.google.com/uc?id=1MzY4MFpZzE-mNS26pzhYlWN-4vMm2ytu',
            'vgg16.pkl')

        # Calculate features for reals.
        cache_file = self._get_cache_file_for_reals(num_images=self.num_images)
        os.makedirs(os.path.dirname(cache_file), exist_ok=True)
        if os.path.isfile(cache_file):
            ref_features = misc.load_pkl(cache_file)
        else:
            ref_features = np.empty(
                [self.num_images, feature_net.output_shape[1]],
                dtype=np.float32)
            for idx, images in enumerate(
                    self._iterate_reals(minibatch_size=minibatch_size)):
                begin = idx * minibatch_size
                end = min(begin + minibatch_size, self.num_images)
                ref_features[begin:end] = feature_net.run(images[:end - begin],
                                                          num_gpus=num_gpus,
                                                          assume_frozen=True)
                if end == self.num_images:
                    break
            misc.save_pkl(ref_features, cache_file)

        # Construct TensorFlow graph.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tflex.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()
                feature_net_clone = feature_net.clone()
                latents = tf.random_normal([self.minibatch_per_gpu] +
                                           Gs_clone.input_shape[1:])
                labels = self._get_random_labels_tf(self.minibatch_per_gpu)
                images = Gs_clone.get_output_for(latents, labels, **Gs_kwargs)
                images = tflib.convert_images_to_uint8(images)
                result_expr.append(feature_net_clone.get_output_for(images))

        # Calculate features for fakes.
        eval_features = np.empty(
            [self.num_images, feature_net.output_shape[1]], dtype=np.float32)
        for begin in range(0, self.num_images, minibatch_size):
            self._report_progress(begin, self.num_images)
            end = min(begin + minibatch_size, self.num_images)
            eval_features[begin:end] = np.concatenate(tflib.run(result_expr),
                                                      axis=0)[:end - begin]

        # Calculate precision and recall.
        state = knn_precision_recall_features(
            ref_features=ref_features,
            eval_features=eval_features,
            feature_net=feature_net,
            nhood_sizes=[self.nhood_size],
            row_batch_size=self.row_batch_size,
            col_batch_size=self.row_batch_size,
            num_gpus=num_gpus)
        self._report_result(state.knn_precision[0], suffix='_precision')
        self._report_result(state.knn_recall[0], suffix='_recall')
    def _evaluate(self, Gs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl(
            'https://drive.google.com/uc?id=1MzTY44rLToO5APn8TZmfR7_ENSe5aZUn'
        )  # inception_v3_features.pkl
        activations = np.empty([self.num_images, inception.output_shape[1]],
                               dtype=np.float32)

        # Calculate statistics for reals.
        cache_file = self._get_cache_file_for_reals(num_images=self.num_images)
        os.makedirs(os.path.dirname(cache_file), exist_ok=True)
        if os.path.isfile(cache_file):
            mu_real, sigma_real = misc.load_pkl(cache_file)
        else:
            for idx, images in enumerate(
                    self._iterate_reals(minibatch_size=minibatch_size)):
                begin = idx * minibatch_size
                end = min(begin + minibatch_size, self.num_images)
                activations[begin:end] = inception.run(images[:end - begin],
                                                       num_gpus=num_gpus,
                                                       assume_frozen=True)
                if end == self.num_images:
                    break
            mu_real = np.mean(activations, axis=0)
            sigma_real = np.cov(activations, rowvar=False)
            misc.save_pkl((mu_real, sigma_real), cache_file)

        # Construct TensorFlow graph.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tf.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()
                inception_clone = inception.clone()
                latents = tf.random_normal([self.minibatch_per_gpu] +
                                           Gs_clone.input_shape[1:])
                images = Gs_clone.get_output_for(
                    latents,
                    None,
                    is_validation=True,
                    randomize_noise=True,
                    truncation_psi_val=self.truncation_psi,
                    truncation_cutoff_val=8)
                images = tflib.convert_images_to_uint8(images)
                result_expr.append(inception_clone.get_output_for(images))

        # Calculate statistics for fakes.
        for begin in range(0, self.num_images, minibatch_size):
            end = min(begin + minibatch_size, self.num_images)
            activations[begin:end] = np.concatenate(tflib.run(result_expr),
                                                    axis=0)[:end - begin]
        mu_fake = np.mean(activations, axis=0)
        sigma_fake = np.cov(activations, rowvar=False)

        # Calculate FID.
        m = np.square(mu_fake - mu_real).sum()
        s, _ = scipy.linalg.sqrtm(np.dot(sigma_fake, sigma_real), disp=False)  # pylint: disable=no-member
        dist = m + np.trace(sigma_fake + sigma_real - 2 * s)
        self._report_result(np.real(dist))
Beispiel #6
0
def main():
    tflib.init_tf()
    baseline_network_pkl = '../../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-014526.pkl'
    no_minibatchstddev = '../../results/00031-sgan-ffhq256-2gpu-remove-minibatchstddev/network-snapshot-014526.pkl'

    _G, _D, Gs_baseline = misc.load_pkl(baseline_network_pkl)
    _G, _D, Gs_no_minibatchstddev = misc.load_pkl(no_minibatchstddev)

    # 888,1733
    draw_uncurated_result_figure('mode_collapse1.png', Gs_no_minibatchstddev, seed=13)
    draw_uncurated_result_figure('mode_collapse2.png', Gs_no_minibatchstddev, seed=15)
Beispiel #7
0
def plot_rot_fn(network,
                seeds,
                latent_pair,
                n_samples_per,
                bound,
                rot_start,
                rot_end,
                rot_interval,
                coord_adj,
                load_gan=False):
    tflib.init_tf()
    print('Loading networks from "%s"...' % network)

    if load_gan:
        _G, _D, I, G = misc.load_pkl(network)
    else:
        E, G = get_return_v(misc.load_pkl(network), 2)

    G_kwargs = dnnlib.EasyDict()
    G_kwargs.is_validation = True
    G_kwargs.randomize_noise = True
    G_kwargs.minibatch_size=8

    distance_measure = misc.load_pkl(
        'http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/vgg16_zhang_perceptual.pkl'
    )

    distance_rot_ls = []
    rot_ls = list(range(int(rot_start), int(rot_end) + 1, int(rot_interval)))
    mark_idxs = []
    for rot_idx, rot in enumerate(rot_ls):
        print('Generating images for rotation degree %d (%d/%d) ...' %
              (rot, rot_idx, len(rot_ls)))
        if rot in [-180, -90, 0, 90, 180]:
            mark_idxs.append(rot_idx)
        distance_ls = []
        for seed_idx, seed in enumerate(seeds):
            rnd = np.random.RandomState(seed)
            z = sample_grid_z(rnd, G, latent_pair, n_samples_per, bound, rot)
            images = get_return_v(
                G.run(z, None, **G_kwargs),
                1)  # [n_samples_per*n_samples_per, channel, height, width]

            distance_ls.append(
                measure_distance(images, n_samples_per, distance_measure))

        distance_rot_ls.append(np.mean(np.array(distance_ls)))
    plot_fn(rot_ls,
            distance_rot_ls,
            rot_start,
            rot_end,
            mark_idxs,
            coord_adj=coord_adj)
Beispiel #8
0
    def run(self,
            network_pkl,
            run_dir=None,
            data_dir=None,
            dataset_args=None,
            mirror_augment=None,
            num_gpus=1,
            tf_config=None,
            log_results=True,
            include_I=False,
            avg_mv_for_I=False,
            Gs_kwargs=dict(is_validation=True, return_atts=False),
            use_D=False,
            **kwargs):
        self._reset(network_pkl=network_pkl,
                    run_dir=run_dir,
                    data_dir=data_dir,
                    dataset_args=dataset_args,
                    mirror_augment=mirror_augment)
        time_begin = time.time()
        with tf.Graph().as_default(), tflib.create_session(
                tf_config).as_default():  # pylint: disable=not-context-manager
            self._report_progress(0, 1)
            if include_I:
                if avg_mv_for_I:
                    _G, _D, _I, Gs, I = misc.load_pkl(self._network_pkl)
                else:
                    _G, _D, I, Gs = misc.load_pkl(self._network_pkl)
                outs = self._evaluate(Gs=Gs,
                                      Gs_kwargs=Gs_kwargs,
                                      I_net=I,
                                      num_gpus=num_gpus,
                                      **kwargs)
            else:
                _G, _D, Gs = misc.load_pkl(self._network_pkl)
                outs = self._evaluate(Gs=Gs,
                                      Gs_kwargs=Gs_kwargs,
                                      num_gpus=num_gpus,
                                      **kwargs)
            self._report_progress(1, 1)
        self._eval_time = time.time() - time_begin  # pylint: disable=attribute-defined-outside-init

        if log_results:
            if run_dir is not None:
                log_file = os.path.join(run_dir, 'metric-%s.txt' % self.name)
                with dnnlib.util.Logger(log_file, 'a'):
                    print(self.get_result_str().strip())
            else:
                print(self.get_result_str().strip())
        return outs
Beispiel #9
0
    def run(self,
            network_pkl,
            run_dir=None,
            dataset_args=None,
            mirror_augment=None,
            num_gpus=1,
            tf_config=None,
            log_results=True,
            model_type="rignet"):

        create_dir(config.EVALUATION_DIR, exist_ok=True)

        self._network_pkl = network_pkl
        self._dataset_args = dataset_args
        self._mirror_augment = mirror_augment
        self._results = []
        self.model_type = model_type

        if (dataset_args is None
                or mirror_augment is None) and run_dir is not None:
            run_config = misc.parse_config_for_previous_run(run_dir)
            self._dataset_args = dict(run_config['dataset'])
            self._dataset_args['shuffle_mb'] = 0
            self._mirror_augment = run_config['train'].get(
                'mirror_augment', False)

        time_begin = time.time()
        with tf.Graph().as_default(), tflib.create_session(
                tf_config).as_default():  # pylint: disable=not-context-manager
            E, _G, _D, Gs = misc.load_pkl(self._network_pkl)
            print("Loaded Encoder")
            Inv, _, _, _ = misc.load_pkl(config.INVERSION_PICKLE_DIR)
            print("Loaded Inv")
            self._evaluate(Gs, E, Inv, num_gpus=num_gpus)
        self._eval_time = time.time() - time_begin

        if log_results:
            result_str = self.get_result_str()
            if run_dir is not None:
                log = os.path.join(run_dir, 'metric-%s.txt' % self.name)
                with dnnlib.util.Logger(log, 'a'):
                    print(result_str)
            else:
                print(result_str)

            result_path = os.path.join(
                config.EVALUATION_DIR, "result_" +
                convert_pickle_path_to_name(self._network_pkl) + ".txt")
            write_to_file(result_str + "\n\n\n", result_path)
Beispiel #10
0
def transitionAtoB(
    run_id          = 102,     # Run ID or network pkl to resume training from, None = start from scratch.
    snapshot        = None,
    num_gen_noise   = 100):
    #http://cedro3.com/ai/stylegan/

    # Initialize TensorFlow.
    tflib.init_tf()

    # Load pre-trained network.
    network_pkl = misc.locate_network_pkl(run_id, snapshot)
    print('Loading networks from "%s"...' % network_pkl)
    G, D, Gs = misc.load_pkl(network_pkl)

    # Print network details.
    Gs.print_layers()

    # Pick latent vector.
    rnd = np.random.RandomState(10)  # seed = 10
    latentsA = rnd.randn(1, Gs.input_shape[1])
    for _ in range(num_gen_noise):
        latents_ = rnd.randn(1, Gs.input_shape[1])
    latentsB = rnd.randn(1, Gs.input_shape[1])

    num_split = 39  # 2つのベクトルを39分割
    for i in range(30):
        latents = latentsB+(latentsA-latentsB)*i/num_split
        # Generate image.
        fmt = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
        images = Gs.run(latents, None, truncation_psi=0.7, randomize_noise=True, output_transform=fmt)

        # Save image.
        os.makedirs(os.path.join(config.result_dir, 'video'), exist_ok=True)
        png_filename = os.path.join(os.path.join(config.result_dir, 'video'), 'photo'+'{0:04d}'.format(i)+'.png')
        PIL.Image.fromarray(images[0], 'RGB').save(png_filename)
def get_model_and_average_w_id(model_name):
    G, D, Gs = misc.load_pkl(model_name)
    average_w_name = model_name.replace('.pkl', '-average_w_id.txt')
    if not os.path.isfile(average_w_name):
        print('Calculating average w id...\n')
        latents = tf.placeholder(tf.float32,
                                 name='latents',
                                 shape=[1, 128 + 32 + 16 + 3])
        noise = tf.placeholder(tf.float32, name='noise', shape=[1, 32])
        INPUTcoeff = z_to_lambda_mapping(latents)
        INPUTcoeff_id = INPUTcoeff[:, :160]
        INPUTcoeff_w_noise = tf.concat(
            [INPUTcoeff_id, tf.zeros([1, 64 + 27 + 3]), noise], axis=1)
        dlatent_out = Gs.components.mapping.get_output_for(INPUTcoeff_w_noise,
                                                           None,
                                                           is_training=False,
                                                           is_validation=True)
        restore_weights_and_initialize()
        np.random.seed(1)
        average_w_id = []
        for i in range(50000):
            lats = np.random.normal(size=[1, 128 + 32 + 16 + 3])
            noise_ = np.random.normal(size=[1, 32])
            w_out = tflib.run(dlatent_out, {latents: lats, noise: noise_})
            average_w_id.append(w_out)

        average_w_id = np.concatenate(average_w_id, axis=0)
        average_w_id = np.mean(average_w_id, axis=0)
        np.savetxt(average_w_name, average_w_id)
    else:
        average_w_id = np.loadtxt(average_w_name)

    return Gs, average_w_id
def main():
    """Main function."""
    args = parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    tf_config = {'rnd.np_random_seed': 1}
    tflib.init_tf(tf_config)
    assert os.path.exists(args.restore_path)
    _, _, _, Gs, _ = load_pkl(args.restore_path)
    latent_dim = Gs.components.synthesis.input_shape[2]
    print(f'Latent dimension shape: {latent_dim}')

    # Building graph
    Z = tf.placeholder('float32', [None, latent_dim], name='Gaussian')
    print(f'Z in tensorflow graph: {Z.shape}')
    sampling_from_z = Gs.get_output_for(Z, None, randomize_noise=True)
    sess = tf.get_default_session()

    save_dir = args.output_dir or './outputs/sampling'
    os.makedirs(save_dir, exist_ok=True)

    print('Sampling...')
    for it in tqdm(range(args.total_nums)):
        samples = sess.run(
            sampling_from_z,
            {Z: np.random.randn(args.batch_size * 2, latent_dim)})
        samples = samples.transpose(0, 2, 3, 1)
        print(f'shape of output: {samples.shape}')
        imwrite(immerge(samples, 2, args.batch_size),
                '%s/sampling_%06d_newseed.png' % (save_dir, it))
Beispiel #13
0
def main():
    """Main function."""
    args = parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    tf_config = {'rnd.np_random_seed': 1000}
    tflib.init_tf(tf_config)
    assert os.path.exists(args.restore_path)
    E, _, _, Gs, _ = load_pkl(args.restore_path)
    num_layers = Gs.components.synthesis.input_shape[1]

    # Building graph
    real = tf.placeholder('float32', [None, 3, args.image_size, args.image_size], name='real_image')
    encoder_w = E.get_output_for(real, phase=False)
    encoder_w_tile = tf.tile(encoder_w[:, np.newaxis], [1, num_layers, 1])
    reconstructor = Gs.components.synthesis.get_output_for(encoder_w_tile, randomize_noise=False)
    sess = tf.get_default_session()

    # Preparing data
    input_images, _ = preparing_data(im_path=args.data_dir_test, img_type=args.img_type)

    save_dir = args.output_dir or './outputs/reconstruction'
    os.makedirs(save_dir, exist_ok=True)

    print('Reconstructing...')
    for it, image_id in tqdm(enumerate(range(0, input_images.shape[0], args.batch_size))):
        batch_images = input_images[image_id:image_id+args.batch_size]
        rec = sess.run(reconstructor, feed_dict={real: batch_images})
        orin_recon = np.concatenate([batch_images, rec], axis=0)
        orin_recon = orin_recon.transpose(0, 2, 3, 1)
        imwrite(immerge(orin_recon, 2, batch_images.shape[0]),
                '%s/reconstruction_%06d.png' % (save_dir, it))
    def _evaluate(self, Gs, Gs_kwargs, num_gpus):
        classifier = misc.load_pkl(self.classifier_model)
        latent = tf.random_normal([self.minibatch_per_gpu, 512],
                                  dtype=tf.float32)
        label = self.get_filled_batch()
        image = Gs.get_output_for(latent, label)
        if self.selected_label != 'ratio':
            image = (image + 1) * (255 / 2)
        prediction = classifier.get_output_for(image)
        one_hot_prediction = tf.one_hot(indices=tf.argmax(prediction, axis=-1),
                                        depth=self.num_classes)
        current_label = label[:, self.offset:self.offset + self.num_classes]
        num_correct_pred = tf.reduce_sum(one_hot_prediction * current_label)
        num_current_label = tf.reduce_sum(current_label)

        num_correct = 0
        num_total = 0
        while num_total < self.num_images:
            num_correct_pred_out, num_rotation_label_out = tflib.run(
                [num_correct_pred, num_current_label])

            num_correct += num_correct_pred_out
            num_total += num_rotation_label_out

        self._report_result(num_correct / num_total)
Beispiel #15
0
def main():
    tflib.init_tf()
    baseline_network_pkl = '../../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-014526.pkl'
    no_style_mix_network_pkl = '../../results/00023-sgan-ffhq256-2gpu-remove-style-mix/network-snapshot-013726.pkl'

    _G, _D, Gs_baseline = misc.load_pkl(baseline_network_pkl)
    _G, _D, Gs_no_style_mix = misc.load_pkl(no_style_mix_network_pkl)

    # 888,1733
    psi = 0.0
    for _ in range(11):
        draw_uncurated_result_figure('truncation_example_' + str(psi) + '.png',
                                     Gs_baseline,
                                     seed=13,
                                     psi=psi)
        psi += 0.1
def generate_domain_shift(network_pkl, seeds, domain_dim):
    tflib.init_tf()
    print('Loading networks from "%s"...' % network_pkl)
    # _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    _G, _D, I, Gs = misc.load_pkl(network_pkl)

    Gs_kwargs = dnnlib.EasyDict()
    # Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = True

    for seed_idx, seed in enumerate(seeds):
        print('Generating image for seed %d (%d/%d) ...' %
              (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        # tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
        images_1, _ = Gs.run(z, None,
                             **Gs_kwargs)  # [minibatch, c, height, width]
        z[:, domain_dim] = -z[:, domain_dim]
        images_2, _ = Gs.run(z, None,
                             **Gs_kwargs)  # [minibatch, c, height, width]
        images = np.concatenate((images_1, images_2), axis=3)
        images = misc.adjust_dynamic_range(images, [-1, 1], [0, 255])
        # np.clip(images, 0, 255, out=images)
        images = np.transpose(images, [0, 2, 3, 1])
        images = np.rint(images).clip(0, 255).astype(np.uint8)
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('seed%04d.png' % seed))
    def _evaluate(self, Gs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl('http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/inception_v3_softmax.pkl')
        activations = np.empty([self.num_images, inception.output_shape[1]], dtype=np.float32)

        # Construct TensorFlow graph.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tf.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()
                inception_clone = inception.clone()
                latents_b = tf.random_normal([self.minibatch_per_gpu] + Gs_clone.input_shape[1:])
                latents_c = tf.random_normal([self.minibatch_per_gpu] + Gs_clone.input_shape[1:])
                images = Gs_clone.get_output_for(latents_b, latents_c, None, is_validation=True, randomize_noise=True)
                assert len(images) == 7
                images = tflib.convert_images_to_uint8(images[-1])
                assert images.shape[2] == images.shape[3] == 512
                result_expr.append(inception_clone.get_output_for(images))

        # Calculate activations for fakes.
        for begin in range(0, self.num_images, minibatch_size):
            end = min(begin + minibatch_size, self.num_images)
            activations[begin:end] = np.concatenate(tflib.run(result_expr), axis=0)[:end-begin]

        # Calculate IS.
        scores = []
        for i in range(self.num_splits):
            part = activations[i * self.num_images // self.num_splits : (i + 1) * self.num_images // self.num_splits]
            kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
            kl = np.mean(np.sum(kl, 1))
            scores.append(np.exp(kl))
        self._report_result(np.mean(scores), suffix='_mean')
        self._report_result(np.std(scores), suffix='_std')
def generate_images(network_pkl, seeds, create_new_G, new_func_name):
    tflib.init_tf()
    print('Loading networks from "%s"...' % network_pkl)
    # _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    _G, _D, I, Gs = misc.load_pkl(network_pkl)
    if create_new_G:
        Gs = Gs.convert(new_func_name=new_func_name)
    # noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    # Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = True

    for seed_idx, seed in enumerate(seeds):
        print('Generating image for seed %d (%d/%d) ...' %
              (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = rnd.randn(1, *Gs.input_shape[1:])  # [minibatch, component]
        # tflib.set_vars({var: rnd.randn(*var.shape.as_list()) for var in noise_vars}) # [height, width]
        images, _ = Gs.run(z, None,
                           **Gs_kwargs)  # [minibatch, height, width, channel]
        images = misc.adjust_dynamic_range(images, [-1, 1], [0, 255])
        # np.clip(images, 0, 255, out=images)
        images = np.transpose(images, [0, 2, 3, 1])
        images = np.rint(images).clip(0, 255).astype(np.uint8)
        PIL.Image.fromarray(images[0], 'RGB').save(
            dnnlib.make_run_dir_path('seed%04d.png' % seed))
def generate_gifs(network_pkl,
                  exist_imgs_dir,
                  used_imgs_ls,
                  used_semantics_ls,
                  attr2idx_dict,
                  create_new_G,
                  new_func_name,
                  traversal_frames=20):
    '''
    used_imgs_ls: ['img1.png', 'img2.png', ...]
    used_semantics_ls: ['azimuth', 'haircolor', ...]
    attr2idx_dict: {'azimuth': 10, 'haircolor': 17, 'smile': 6, ...}
    '''
    tflib.init_tf()
    print('Loading networks from "%s"...' % network_pkl)
    # _G, _D, Gs = pretrained_networks.load_networks(network_pkl)
    _G, _D, I, Gs = misc.load_pkl(network_pkl)
    if create_new_G:
        Gs = Gs.convert(new_func_name=new_func_name)
    # noise_vars = [var for name, var in Gs.components.synthesis.vars.items() if name.startswith('noise')]

    Gs_kwargs = dnnlib.EasyDict()
    # Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = True

    ori_imgs = []
    semantics_all_imgs_ls = []
    for in_img_name in used_imgs_ls:
        img_file = os.path.join(exist_imgs_dir, in_img_name)
        image = image_to_ready(img_file)
        attr_ori = I.run(image)
        ori_img, _ = Gs.run(attr_ori, None, **Gs_kwargs)
        _, c, img_h, img_w = ori_img.shape
        ori_imgs.append(ori_img)
        semantics_per_img_ls = []
        for i in range(len(used_semantics_ls)):
            attr = attr_ori.copy()
            attrs_trav = expand_traversal(
                attr, attr2idx_dict[used_semantics_ls[i]],
                traversal_frames)  # [n_trav, n_codes]
            imgs_trav, _ = Gs.run(attrs_trav, None,
                                  **Gs_kwargs)  # [n_trav, c, h, w]
            semantics_per_img_ls.append(imgs_trav)
        semantics_per_img_np = np.concatenate(
            tuple(semantics_per_img_ls),
            axis=3)  # [n_trav, c, h, w*n_used_attrs]
        semantics_all_imgs_ls.append(semantics_per_img_np)
    semantics_all_imgs_np = np.concatenate(
        tuple(semantics_all_imgs_ls),
        axis=2)  # [n_trav, c, h*n_imgs, w*n_used_attrs]
    imgs_to_save = [
        draw_imgs_and_text(semantics_all_imgs_np[i], used_semantics_ls, img_h,
                           img_w) for i in range(len(semantics_all_imgs_np))
    ]
    imgs_to_save[0].save(dnnlib.make_run_dir_path('traversals.gif'),
                         format='GIF',
                         append_images=imgs_to_save[1:],
                         save_all=True,
                         duration=100,
                         loop=0)
    def _evaluate(self, Gs, Gs_kwargs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu

        # Construct TensorFlow graph for each GPU.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tf.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()

                # Generate images.
                latents = tf.random_normal([self.minibatch_per_gpu] + Gs_clone.input_shape[1:])
                labels = self._get_random_labels_tf(self.minibatch_per_gpu)
                dlatents = Gs_clone.components.mapping.get_output_for(latents, labels, **Gs_kwargs)
                images = Gs_clone.get_output_for(latents, None, **Gs_kwargs)

                # Downsample to 256x256. The attribute classifiers were built for 256x256.
                if images.shape[2] > 256:
                    factor = images.shape[2] // 256
                    images = tf.reshape(images, [-1, images.shape[1], images.shape[2] // factor, factor, images.shape[3] // factor, factor])
                    images = tf.reduce_mean(images, axis=[3, 5])

                # Run classifier for each attribute.
                result_dict = dict(latents=latents, dlatents=dlatents[:,-1])
                for attrib_idx in self.attrib_indices:
                    classifier = misc.load_pkl(classifier_urls[attrib_idx])
                    logits = classifier.get_output_for(images, None)
                    predictions = tf.nn.softmax(tf.concat([logits, -logits], axis=1))
                    result_dict[attrib_idx] = predictions
                result_expr.append(result_dict)

        # Sampling loop.
        results = []
        for begin in range(0, self.num_samples, minibatch_size):
            self._report_progress(begin, self.num_samples)
            results += tflib.run(result_expr)
        results = {key: np.concatenate([value[key] for value in results], axis=0) for key in results[0].keys()}

        # Calculate conditional entropy for each attribute.
        conditional_entropies = defaultdict(list)
        for attrib_idx in self.attrib_indices:
            # Prune the least confident samples.
            pruned_indices = list(range(self.num_samples))
            pruned_indices = sorted(pruned_indices, key=lambda i: -np.max(results[attrib_idx][i]))
            pruned_indices = pruned_indices[:self.num_keep]

            # Fit SVM to the remaining samples.
            svm_targets = np.argmax(results[attrib_idx][pruned_indices], axis=1)
            for space in ['latents', 'dlatents']:
                svm_inputs = results[space][pruned_indices]
                try:
                    svm = sklearn.svm.LinearSVC()
                    svm.fit(svm_inputs, svm_targets)
                    svm.score(svm_inputs, svm_targets)
                    svm_outputs = svm.predict(svm_inputs)
                except:
                    svm_outputs = svm_targets # assume perfect prediction

                # Calculate conditional entropy.
                p = [[np.mean([case == (row, col) for case in zip(svm_outputs, svm_targets)]) for col in (0, 1)] for row in (0, 1)]
                conditional_entropies[space].append(conditional_entropy(p))
def main():
    """Main function."""
    args = parse_args()
    os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_id
    tf_config = {'rnd.np_random_seed': 1000}
    tflib.init_tf(tf_config)
    assert os.path.exists(args.restore_path)
    _, _, _, Gs, _ = load_pkl(args.restore_path)
    num_layers = Gs.components.synthesis.input_shape[1]

    batch_codes = np.load(args.data_dir_encode)
    print(batch_codes.shape)
    latent_dim = batch_codes.shape[1]
    print(f'Latent dimension shape: {latent_dim}')

    # Building graph
    w_vec = tf.placeholder('float32', [None, latent_dim], name='w_codes')
    print(f'W in tensorflow graph: {w_vec.shape}')
    encoder_w_tile = tf.tile(w_vec[:, np.newaxis], [1, num_layers, 1])
    print(f'encoder_w_tile size: {encoder_w_tile.shape}')
    reconstructor = Gs.components.synthesis.get_output_for(
        encoder_w_tile, randomize_noise=False)
    sess = tf.get_default_session()

    save_dir = args.output_dir or './outputs/rebuild_encodings'
    os.makedirs(save_dir, exist_ok=True)

    print('Creating Images...')
    samples = sess.run(reconstructor, {w_vec: batch_codes})
    samples = samples.transpose(0, 2, 3, 1)
    print(f'shape of output: {samples.shape}')
    imwrite(immerge(samples, 4, args.batch_size),
            '%s/decode_00000_new1.png' % (save_dir))
def run(resume, output, num_rows, num_cols, resolution, num_phases, transition_frames, static_frames, seed):
    tflib.init_tf({'rnd.np_random_seed': seed})
    _, _, Gs = misc.load_pkl(resume)
    output_seq = []
    batch_size = num_rows * num_cols
    latent_size = Gs.input_shape[1]
    latents = [np.random.randn(batch_size, latent_size) for _ in range(num_phases)]
    
    Gs_kwargs = dnnlib.EasyDict()
    Gs_kwargs.output_transform = dict(func=tflib.convert_images_to_uint8, nchw_to_nhwc=True)
    Gs_kwargs.randomize_noise = False

    def to_image_grid(outputs):
        outputs = np.reshape(outputs, [num_rows, num_cols, *outputs.shape[1:]])
        outputs = np.concatenate(outputs, axis=1)
        outputs = np.concatenate(outputs, axis=1)
        return Image.fromarray(outputs).resize((resolution * num_cols, resolution * num_rows), Image.ANTIALIAS)
    
    for i in range(num_phases):
        dlatents0 = Gs.components.mapping.run(latents[i - 1], None)
        dlatents1 = Gs.components.mapping.run(latents[i], None)
        for j in range(transition_frames):
            dlatents = (dlatents0 * (transition_frames - j) + dlatents1 * j) / transition_frames
            output_seq.append(to_image_grid(Gs.components.synthesis.run(dlatents, **Gs_kwargs)))
        output_seq.extend([to_image_grid(Gs.components.synthesis.run(dlatents1, **Gs_kwargs))] * static_frames)
    if not output.endswith('.gif'):
        output += '.gif'
    output_seq[0].save(output, save_all=True, append_images=output_seq[1:], optimize=False, duration=50, loop=0)
Beispiel #23
0
    def run(self, network_pkl, run_dir=None, dataset_args=None, mirror_augment=None, num_gpus=1, tf_config=None, log_results=True):
        self._network_pkl = network_pkl
        self._dataset_args = dataset_args
        self._mirror_augment = mirror_augment
        self._results = []

        if (dataset_args is None or mirror_augment is None) and run_dir is not None:
            run_config = misc.parse_config_for_previous_run(run_dir)
            self._dataset_args = dict(run_config['dataset'])
            self._dataset_args['shuffle_mb'] = 0
            self._mirror_augment = run_config['train'].get('mirror_augment', False)

        time_begin = time.time()
        with tf.Graph().as_default(), tflib.create_session(tf_config).as_default():  # pylint: disable=not-context-manager
            _G, _D, Gs = misc.load_pkl(self._network_pkl)
            self._evaluate(Gs, num_gpus=num_gpus)
        self._eval_time = time.time() - time_begin

        if log_results:
            result_str = self.get_result_str()
            if run_dir is not None:
                log = os.path.join(run_dir, 'metric-%s.txt' % self.name)
                with dnnlib.util.Logger(log, 'a'):
                    print(result_str)
            else:
                print(result_str)
Beispiel #24
0
def main():
    tflib.init_tf()
    baseline_network_pkl = '../results/00002-sgan-car512-2gpu/network-snapshot-023949.pkl'
    no_noise_network_pkl = '../../results/00022-sgan-ffhq256-2gpu-no-noise/network-snapshot-014926.pkl'

    _G, _D, Gs_baseline = misc.load_pkl(baseline_network_pkl)
    # _G, _D, Gs_no_nosie = misc.load_pkl(no_noise_network_pkl)

    # 888,1733
    # draw_uncurated_result_figure(os.path.join(config.result_dir, 'figure02-uncurated-ffhq.png'), Gs, cx=0, cy=0, cw=256, ch=256, rows=3, lods=[0,1,2,2,3,3], seed=5)
    #draw_style_mixing_figure(os.path.join(config.result_dir, 'figure03-style-mixing.png'), Gs, w=256, h=256, src_seeds=[639,701,687,615,2268], dst_seeds=[888,829,1898,1733,1614,845], style_ranges=[range(0,4)]*3+[range(4,8)]*2+[range(8,18)])
    # draw_style_mixing_figure(os.path.join(config.result_dir, 'figure03-style-mixing.png'), Gs, w=256, h=256, src_seeds=[123,456,789], dst_seeds=[888,1733]*2, style_ranges=[range(0,4)]*2+[range(4,8)]*2)

    # draw_style_mixing_figure_transition(os.path.join(config.result_dir, 'no-style-mixing.png'), Gs_baseline, w=256, h=256, style1_seeds=[222, 1733, 4], style2_seed=[888], style_ranges=[list(range(i-2, i)) for i in range(2, 16, 2)])
    #draw_style_mixing_figure_transition(os.path.join(config.result_dir, 'no-style-mixing.png'), Gs_no_style_mix, w=256, h=256, style1_seeds=[12, 23, 34], style2_seed=[45], style_ranges=[list(range(i-2, i)) for i in range(2, 16, 2)])

    draw_noise_detail_figure('images/noise_detail.png',
                             Gs_baseline,
                             w=512,
                             h=512,
                             num_samples=100,
                             seeds=[5, 4])
    draw_noise_components_figure('images/noise-components.png',
                                 Gs_baseline,
                                 w=512,
                                 h=512,
                                 seeds=[5, 4],
                                 noise_ranges=[range(0, 14),
                                               range(0, 0)],
                                 flips=[])
def transitions(png, w, h):
    baseline_network_pkl = [
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-007326.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-008526.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-009726.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-010926.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-012126.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-013326.pkl',
        '../results/00015-sgan-ffhq256-1gpu-baseline/network-snapshot-014526.pkl'
    ]

    col = 0
    canvas = PIL.Image.new('RGB', (w * 7, h * 2), 'white')
    for pkl in baseline_network_pkl:
        G, _D, Gs = misc.load_pkl(pkl)
        latents = np.random.RandomState([45]).randn(Gs.input_shape[1])

        dlatents = Gs.components.mapping.run(np.stack([latents]), None)
        image = Gs.components.synthesis.run(dlatents,
                                            randomize_noise=False,
                                            **synthesis_kwargs)[0]
        canvas.paste(PIL.Image.fromarray(image, 'RGB'), (col, 0))

        dlatents = G.components.mapping.run(np.stack([latents]), None)
        image = G.components.synthesis.run(dlatents,
                                           randomize_noise=False,
                                           **synthesis_kwargs)[0]
        canvas.paste(PIL.Image.fromarray(image, 'RGB'), (col, h))
        col += w
    canvas.save(png)
Beispiel #26
0
    def _evaluate(self, Gs, Gs_kwargs, num_gpus):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl('https://nvlabs-fi-cdn.nvidia.com/stylegan/networks/metrics/inception_v3_softmax.pkl')
        activations = np.empty([self.num_images, inception.output_shape[1]], dtype=np.float32)

        # Construct TensorFlow graph.
        result_expr = []
        for gpu_idx in range(num_gpus):
            with tf.device('/gpu:%d' % gpu_idx):
                Gs_clone = Gs.clone()
                inception_clone = inception.clone()
                latents = tf.random_normal([self.minibatch_per_gpu] + Gs_clone.input_shape[1:])
                labels = self._get_random_labels_tf(self.minibatch_per_gpu)
                images = Gs_clone.get_output_for(latents, labels, **Gs_kwargs)
                images = tflib.convert_images_to_uint8(images)
                result_expr.append(inception_clone.get_output_for(images))

        # Calculate activations for fakes.
        for begin in range(0, self.num_images, minibatch_size):
            self._report_progress(begin, self.num_images)
            end = min(begin + minibatch_size, self.num_images)
            activations[begin:end] = np.concatenate(tflib.run(result_expr), axis=0)[:end-begin]

        # Calculate IS.
        scores = []
        for i in range(self.num_splits):
            part = activations[i * self.num_images // self.num_splits : (i + 1) * self.num_images // self.num_splits]
            kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
            kl = np.mean(np.sum(kl, 1))
            scores.append(np.exp(kl))
        self._report_result(np.mean(scores), suffix='_mean')
        self._report_result(np.std(scores), suffix='_std')
Beispiel #27
0
    def _evaluate(self, Gs, Gs_kwargs, num_gpus, num_imgs, paths=None):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        inception = misc.load_pkl(
            "http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/inception_v3_softmax.pkl"
        )
        inception_func = lambda imgs: inception.run(
            imgs[:end - begin], num_gpus=num_gpus, assume_frozen=True)

        if paths is not None:
            # Extract features for local sample image files (paths)
            feats = self._paths_to_feats(paths, inception_func, minibatch_size,
                                         num_gpus, num_imgs)
        else:
            # Extract features for newly generated fake images
            feats = self._gen_feats(Gs, inception, minibatch_size, num_imgs,
                                    num_gpus, Gs_kwargs)

        # Compute IS
        scores = []
        for i in range(self.num_splits):
            part = feats[i * num_imgs // self.num_splits:(i + 1) * num_imgs //
                         self.num_splits]
            kl = part * (np.log(part) -
                         np.log(np.expand_dims(np.mean(part, 0), 0)))
            kl = np.mean(np.sum(kl, 1))
            scores.append(np.exp(kl))
        self._report_result(np.mean(scores), suffix="_mean")
        self._report_result(np.std(scores), suffix="_std")
Beispiel #28
0
def generate_grids(network,
                   seeds,
                   latent_pair,
                   n_samples_per=10,
                   bound=2,
                   rot=0,
                   load_gan=False):
    tflib.init_tf()
    print('Loading networks from "%s"...' % network)
    if load_gan:
        _G, _D, I, G = misc.load_pkl(network)
    else:
        E, G = get_return_v(misc.load_pkl(network), 2)

    G_kwargs = dnnlib.EasyDict()
    G_kwargs.is_validation = True
    G_kwargs.randomize_noise = True
    G_kwargs.minibatch_size = 8

    distance_measure = misc.load_pkl(
        'http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/vgg16_zhang_perceptual.pkl'
    )

    distance_ls = []
    for seed_idx, seed in enumerate(seeds):
        print('Generating images for seed %d (%d/%d) ...' %
              (seed, seed_idx, len(seeds)))
        rnd = np.random.RandomState(seed)
        z = sample_grid_z(rnd, G, latent_pair, n_samples_per, bound, rot)
        images = get_return_v(
            G.run(z, None, **G_kwargs),
            1)  # [n_samples_per*n_samples_per, channel, height, width]

        distance_ls.append(
            measure_distance(images, n_samples_per, distance_measure))

        images = add_outline(images, width=1)
        n_samples_square, c, h, w = np.shape(images)
        assert n_samples_square == n_samples_per * n_samples_per
        images = np.reshape(images, (n_samples_per, n_samples_per, c, h, w))
        images = np.transpose(images, [0, 3, 1, 4, 2])
        images = np.reshape(images, (n_samples_per * h, n_samples_per * w, c))
        images = misc.adjust_dynamic_range(images, [0, 1], [0, 255])
        images = np.rint(images).clip(0, 255).astype(np.uint8)
        PIL.Image.fromarray(images, 'RGB').save(
            dnnlib.make_run_dir_path('seed%04d.png' % seed))
    print('mean_distance:', np.mean(np.array(distance_ls)))
Beispiel #29
0
    def _evaluate(self,
                  Gs,
                  Gs_kwargs,
                  num_gpus,
                  num_imgs,
                  paths=None,
                  **kwargs):
        minibatch_size = num_gpus * self.minibatch_per_gpu
        feature_net = misc.load_pkl(
            "http://d36zk2xti64re0.cloudfront.net/stylegan1/networks/metrics/vgg16.pkl"
        )

        # Compute features for reals
        cache_file = self._get_cache_file_for_reals(num_imgs)
        os.makedirs(os.path.dirname(cache_file), exist_ok=True)
        if os.path.isfile(cache_file):
            ref_features = misc.load_pkl(cache_file)
        else:
            imgs_iter = self._iterate_reals(minibatch_size=minibatch_size)
            ref_features = self._get_feats(imgs_iter, feature_net,
                                           minibatch_size, num_gpus, num_imgs)

            misc.save_pkl(ref_features, cache_file)

        if paths is not None:
            # Extract features for local sample image files (paths)
            eval_features = self._paths_to_feats(paths, feature_net,
                                                 minibatch_size, num_gpus,
                                                 num_imgs)
        else:
            # Extract features for newly generated fake imgs
            eval_features = self._gen_feats(Gs, feature_net, minibatch_size,
                                            num_imgs, num_gpus, Gs_kwargs)

        # Compute precision and recall
        state = knn_precision_recall_features(
            ref_features=ref_features,
            eval_features=eval_features,
            feature_net=feature_net,
            nhood_sizes=[self.nhood_size],
            row_batch_size=self.row_batch_size,
            col_batch_size=self.row_batch_size,
            num_imgs=num_imgs,
            num_gpus=num_gpus)
        self._report_result(state.knn_precision[0], suffix="_precision")
        self._report_result(state.knn_recall[0], suffix="_recall")
Beispiel #30
0
    def get_vgg_loss(self, target_images, latents):
        '''
        target_images: np.ndarray
            (1, H, W, C)
        latents: np.ndarray
            (1, H, W, C)
        '''
        # Prepare target images.
        self._info('Preparing target images...')
        target_images = np.asarray(target_images, dtype='float32')
        #         print(target_images.shape)
        target_images = (target_images + 1) * (255 / 2)
        sh = target_images.shape
        assert sh[0] == self._minibatch_size
        #         print('target shahpe', self._target_images_var.shape)
        if sh[2] > self._target_images_var.shape[2]:
            factor = sh[2] // self._target_images_var.shape[2]
            target_images = np.reshape(
                target_images,
                [-1, sh[1], sh[2] // factor, factor, sh[3] // factor, factor
                 ]).mean((3, 5))
#         print('targ im shpae', target_images.shape)

# Initialize optimization state.
        self._info('Initializing optimization state...')
        tflib.set_vars({
            self._target_images_var:
            target_images,
            self._dlatents_var:
            np.tile(self._dlatent_avg, [self._minibatch_size, 1, 1])
        })
        tflib.run(self._noise_init_op)
        #         self._opt.reset_optimizer_state()
        #         self._cur_step = 0

        self._dlatents_expr = tf.convert_to_tensor(latents)
        self._images_expr = self._Gs.components.synthesis.get_output_for(
            self._dlatents_expr, randomize_noise=False)
        #         print(self._images_expr.shape)
        # Downsample image to 256x256 if it's larger than that. VGG was built for 224x224 images.
        proc_images_expr = (self._images_expr + 1) * (255 / 2)
        sh = proc_images_expr.shape.as_list()
        #         print('sh', sh)
        if sh[2] > 256:
            factor = sh[2] // 256
            proc_images_expr = tf.reduce_mean(tf.reshape(
                proc_images_expr,
                [-1, sh[1], sh[2] // factor, factor, sh[2] // factor, factor]),
                                              axis=[3, 5])

        self._info('Building loss graph...')
        if self._lpips is None:
            self._lpips = misc.load_pkl(
                self.vgg16_pkl)  # vgg16_zhang_perceptual.pkl
        self._dist = self._lpips.get_output_for(proc_images_expr,
                                                self._target_images_var)
        self._loss = tf.reduce_sum(self._dist)
        return tflib.run(self._loss)