def inspect(self, image_id=0):
     self.restore()
     data = next(self.test_set, image_id + 1)[image_id]
     self.test_set.reset()
     sample_x = self.latent_inspect(data)
     visualize_samples(sample_x,
                       os.path.join(
                           "results", 'inspect_%s_sample_%d.png' %
                           (self.data_set, image_id)),
                       layout=(self.z_dim, self.num_traversal_step),
                       vrange=self.vrange)
 def examine_reg(self, mgen, image_id=0):
     self.restore()
     data = self.test_set.next()  #next(self.test_set, image_id+1)[image_id]
     # data = np.stack([data for i in range(self.batch_size)], axis=0)
     self.test_set.reset()
     _, _, sample_x = self.controlled_sample(data, mgen)
     visualize_samples(sample_x,
                       os.path.join(
                           "results", 'examine_%s_sample_%d.png' %
                           (self.data_set, image_id)),
                       layout=(8, 4),
                       vrange=self.vrange)
 def inpainting(self,
                eval_mgen,
                layout=(5, 5),
                same_inputs=False,
                use_mask_at=None):
     self.restore()
     #
     data = next(self.test_set)
     self.test_set.reset()
     ori_x, masked_x, sample_x = self.sample(data,
                                             eval_mgen,
                                             same_inputs=same_inputs,
                                             use_mask_at=use_mask_at)
     visualize_samples(ori_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_gt.png' % (self.data_set)),
                       layout=layout,
                       vrange=self.vrange)
     visualize_samples(masked_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_masked.png' % (self.data_set)),
                       layout=layout,
                       vrange=self.vrange)
     visualize_samples(sample_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_sample.png' % (self.data_set)),
                       layout=layout,
                       vrange=self.vrange)
Exemplo n.º 4
0
 def eval(self,
          which_set,
          mgen=None,
          generate_samples=False,
          restore=True,
          layout=(5, 5),
          same_inputs=False,
          use_mask_at=None):
     if restore:
         self.restore()
     self.eval_epoch(mgen, which_set=which_set)
     self.monitor.summarise_epoch(time=0., log=False)
     if which_set == 'train':
         data_set = self.train_set
     elif which_set == 'eval':
         data_set = self.eval_set
     elif which_set == 'test':
         data_set = self.test_set
     if generate_samples:
         data = next(data_set)
         data_set.reset()
         ori_x, masked_x, sample_x = self.sample(data,
                                                 mgen,
                                                 same_inputs=same_inputs,
                                                 use_mask_at=use_mask_at)
         visualize_samples(
             ori_x,
             os.path.join("results", self.exp_name,
                          'gen_%s_gt_%s.png' % (self.data_set, which_set)),
             layout=layout,
             vrange=self.vrange)
         visualize_samples(
             masked_x,
             os.path.join(
                 "results", self.exp_name,
                 'gen_%s_masked_%s.png' % (self.data_set, which_set)),
             layout=layout,
             vrange=self.vrange)
         visualize_samples(
             sample_x,
             os.path.join(
                 "results", self.exp_name,
                 'gen_%s_sample_%s.png' % (self.data_set, which_set)),
             layout=layout,
             vrange=self.vrange)
Exemplo n.º 5
0
    def train(self,
              train_mgen,
              sample_mgen,
              max_num_epoch=100,
              save_interval=None,
              restore=False):
        if restore:
            self.restore()
        for epoch in range(max_num_epoch + 1):
            tt = time.time()
            self.train_epoch(train_mgen, which_set='train')
            self.eval_epoch(train_mgen, which_set='eval')
            self.monitor.summarise_epoch(time=time.time() - tt, log=True)

            if save_interval is not None and epoch % save_interval == 0:
                self.save()
                data = next(self.test_set)  # note that test set is used here
                self.test_set.reset()
                ori_x, masked_x, sample_x = self.sample(data, sample_mgen)
                visualize_samples(ori_x,
                                  os.path.join(
                                      "results", self.exp_name,
                                      'train_%s_gt_%d.png' %
                                      (self.data_set, epoch)),
                                  layout=(5, 5),
                                  vrange=self.vrange)
                visualize_samples(
                    masked_x,
                    os.path.join(
                        "results", self.exp_name,
                        'train_%s_masked_%d.png' % (self.data_set, epoch)),
                    layout=(5, 5),
                    vrange=self.vrange)
                visualize_samples(
                    sample_x,
                    os.path.join(
                        "results", self.exp_name,
                        'train_%s_sample_%d.png' % (self.data_set, epoch)),
                    layout=(5, 5),
                    vrange=self.vrange)
                print("------------ saved")
                sys.stdout.flush()
 def traverse(self, eval_mgen, image_id=0):
     self.restore()
     data = next(self.test_set, image_id + 1)[image_id]
     self.test_set.reset()
     ori_x, masked_x, sample_x = self.latent_traversal(data, mgen=eval_mgen)
     visualize_samples(ori_x,
                       os.path.join(
                           "results", 'traverse_%s_gt_%d_%s.png' %
                           (self.data_set, image_id, eval_mgen.name)),
                       layout=(self.z_dim, self.num_traversal_step),
                       vrange=self.vrange)
     visualize_samples(masked_x,
                       os.path.join(
                           "results", 'traverse_%s_masked_%d_%s.png' %
                           (self.data_set, image_id, eval_mgen.name)),
                       layout=(self.z_dim, self.num_traversal_step),
                       vrange=self.vrange)
     visualize_samples(sample_x,
                       os.path.join(
                           "results", 'traverse_%s_sample_%d_%s.png' %
                           (self.data_set, image_id, eval_mgen.name)),
                       layout=(self.z_dim, self.num_traversal_step),
                       vrange=self.vrange)
 def inpainting(self, eval_mgen):
     self.restore()
     #
     data = next(self.test_set)
     self.test_set.reset()
     ori_x, masked_x, sample_x = self.sample(data, eval_mgen)
     visualize_samples(ori_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_gt.png' % (self.data_set)),
                       layout=(5, 5),
                       vrange=self.vrange)
     visualize_samples(masked_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_masked_%s.png' % (self.data_set)),
                       layout=(5, 5),
                       vrange=self.vrange)
     visualize_samples(sample_x,
                       os.path.join(
                           "results", self.exp_name,
                           'inpainting_%s_sample_%s.png' % (self.data_set)),
                       layout=(5, 5),
                       vrange=self.vrange)
Exemplo n.º 8
0
    max_num_epoch = 200
    for epoch in range(max_num_epoch + 1):
        tt = time.time()
        for data in train_data:
            feed_dict = make_feed_dict(data, is_training=True, dropout_p=0.5)
            sess.run(train_step, feed_dict=feed_dict)

        for data in eval_data:
            feed_dict = make_feed_dict(data, is_training=False, dropout_p=0.)
            recorder.evaluate(sess, feed_dict)

        recorder.finish_epoch_and_display(time=time.time() - tt, log=True)

        if epoch % args.save_interval == 0:
            saver.save(sess,
                       args.save_dir + '/params_' + args.data_set + '.ckpt')
            data = next(test_data)
            test_data.reset()
            sample_x = sample_from_model(sess,
                                         data,
                                         fill_region=fill_region,
                                         mgen=sample_mgen)
            visualize_samples(sample_x,
                              os.path.join(
                                  args.save_dir,
                                  '%s_sample%d.png' % (args.data_set, epoch)),
                              layout=(4, 4))
            print("------------ saved")
            sys.stdout.flush()
initializer = tf.global_variables_initializer()
saver = tf.train.Saver()

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:

    ckpt_file = args.save_dir + '/params_' + args.data_set + '.ckpt'
    print('restoring parameters from', ckpt_file)
    saver.restore(sess, ckpt_file)

    data = next(test_data)
    test_data.reset()
    vdata = np.cast[np.float32]((data - 127.5) / 127.5)
    visualize_samples(vdata,
                      "/data/ziz/jxu/gpu-results/show_original.png",
                      layout=(10, 10))
    img = []
    for i in [4, 5, 8, 42, 47]:  #[2, 3, 5, 40, 55]:
        # sample_x = latent_traversal(sess, data, use_image_id=i)
        sample_x = latent_traversal(sess, data[i])
        view = visualize_samples(sample_x,
                                 None,
                                 layout=(args.z_dim,
                                         sample_x.shape[0] // args.z_dim))
        img.append(view.copy())
    img = np.concatenate(img, axis=1)
    from PIL import Image
    img = img.astype(np.uint8)
    img = Image.fromarray(img, 'RGB')
    img.save("/data/ziz/jxu/gpu-results/show_celeba32_z32_b8.png")
Exemplo n.º 10
0
    fill_region = sample_mgen.gen(1)[0]
    # sample_mgen = get_generator('transparent', args.img_size)
    # fill_region = get_generator('full', args.img_size).gen(1)[0]
    data = next(test_data)

    from blocks.helpers import broadcast_masks_np
    data = data.astype(np.float32) * broadcast_masks_np(fill_region, 3)

    test_data.reset()
    # vdata = np.cast[np.float32]((data - 127.5) / 127.5)
    # visualize_samples(vdata, "/data/ziz/jxu/gpu-results/show_original.png", layout=[8,8])

    img = []
    for i in [7]:  #[5,7,8]: #[5, 7, 8, 18, 27, 44, 74, 77]:
        sample_x = latent_traversal(sess,
                                    data[i],
                                    traversal_range=[-6, 6],
                                    num_traversal_step=13,
                                    fill_region=fill_region,
                                    mgen=sample_mgen)
        view = visualize_samples(sample_x,
                                 None,
                                 layout=(args.z_dim,
                                         sample_x.shape[0] // args.z_dim))
        img.append(view.copy())
    img = np.concatenate(img, axis=1)
    from PIL import Image
    img = img.astype(np.uint8)
    img = Image.fromarray(img, 'RGB')
    img.save("/data/ziz/jxu/gpu-results/bottom_quarter_traversal_b2e6_ce.png")