Exemplo n.º 1
0
 def save_checkpoint_and_samples(self, step, freq=1000):
     if (np.mod(step, freq) == 0) and (self.d_counter == 0):
         self.save_checkpoint(step)
         samples = self.sess.run(self.sampler)
         self._ensure_dirs('sample')
         p = os.path.join(self.sample_dir, 'train_{:02d}.png'.format(step))
         misc.save_images(samples[:64, :, :, :], [8, 8], p)
Exemplo n.º 2
0
def test(model, data_loader, save_dir):
    dataset = data_loader.load_data()
    step = int(math.log2(dataset.dataset.get_resolution())) - 2

    for i, data in enumerate(dataset):
        generated = model.inference(data['style'], step=step)
        visuals = OrderedDict([
            ('input_label', misc.tensor2im(data['style'][0])),
            ('synthesized_image', misc.tensor2im(generated.data[0]))
        ])
        name = data['name'][0].split('/')[-1].split('.')[0]
        misc.save_images(visuals, name, save_dir)
Exemplo n.º 3
0
    def get_samples(self, n=None, save=True, layers=[]):
        if not (self.initialized_for_sampling or self.config.is_train):
            print('[*] Loading from ' + self.checkpoint_dir + '...')
            self.sess.run(tf.local_variables_initializer())
            self.sess.run(tf.global_variables_initializer())
            if self.load_checkpoint():
                print(" [*] Load SUCCESS, model trained up to epoch %d" % \
                      self.sess.run(self.global_step))
            else:
                print(" [!] Load failed...")
                return

        if len(layers) > 0:
            outputs = dict([(key + '_features', val)
                            for key, val in self.d_G_layers.items()])
            if not (layers == 'all'):
                keys = [sorted(list(outputs.keys()))[i] for i in layers]
                outputs = dict([(key, outputs[key]) for key in keys])
        else:
            outputs = {}
        outputs['samples'] = self.G

        values = self._evaluate_tensors(outputs, n=n)

        if not save:
            if len(layers) > 0:
                return values
            return values['samples']

        if not os.path.isdir(self.config.output_dir_of_test_samples):
            os.mkdir(self.config.output_dir_of_test_samples)
        for key, val in values.items():
            if key == 'samples':
                for idx in range(val.shape[0]):
                    print('Generating png to %s: %d / %d...' %
                          (self.config.output_dir_of_test_samples, idx,
                           val.shape[0]),
                          end='\r')
                    if self.config.model == 'mmd':
                        p = os.path.join(
                            self.config.output_dir_of_test_samples,
                            'MMD_{:08d}.png'.format(idx))
                    elif self.config.model == 'wgan_gp':
                        p = os.path.join(
                            self.config.output_dir_of_test_samples,
                            'WGAN-GP_{:08d}.png'.format(idx))
                    elif self.config.model == 'cramer':
                        p = os.path.join(
                            self.config.output_dir_of_test_samples,
                            'CRAMER_{:08d}.png'.format(idx))
                    misc.save_images(val[idx:idx + 1, :, :, :], [1, 1], p)