Exemplo n.º 1
0
    def sample_from_z(self, config, z):
        counter = 0
        if config.rotate_azimuth:
            low = cfg['azi_low']
            high = cfg['azi_high']
            step = STEP
        elif config.rotate_elevation:
            low = cfg['ele_low']
            high = cfg['ele_high']
            step = STEP
        else:
            low = 0
            high = 90
            step = 30

        for i in range(low, high, step):
            if config.rotate_azimuth:
                sample_view = np.tile(
                    np.array([
                        i * math.pi / 180.0, 0 * math.pi / 180.0, 1.0, 0, 0, 0
                    ]), (cfg['batch_size'], 1))
            elif config.rotate_azimuth:
                sample_view = np.tile(
                    np.array([
                        270 * math.pi / 180.0, (90 - i) * math.pi / 180.0, 1.0,
                        0, 0, 0
                    ]), (cfg['batch_size'], 1))
            else:
                sample_view = self.gen_view_func(
                    cfg['batch_size'],
                    cfg['ele_low'],
                    cfg['ele_high'],
                    cfg['azi_low'],
                    cfg['azi_high'],
                    cfg['scale_low'],
                    cfg['scale_high'],
                    cfg['x_low'],
                    cfg['x_high'],
                    cfg['y_low'],
                    cfg['y_high'],
                    cfg['z_low'],
                    cfg['z_high'],
                    with_translation=False,
                    with_scale=to_bool(str(cfg['with_translation'])))

            feed_eval = {self.z: z, self.view_in: sample_view}

            samples = self.sess.run(self.G, feed_dict=feed_eval)
            ren_img = np.clip(255 * samples, 0, 255).astype(np.uint8)

            try:
                cv2.imwrite(
                    os.path.join(self.sample_dir,
                                 "{0}_samples_{1}.jpg".format(counter, i)),
                    merge(ren_img, [cfg['batch_size'] // 4, 4]))
            except:
                cv2.imwrite(
                    os.path.join(self.sample_dir,
                                 "{0}_samples_{1}.jpg".format(counter, i)),
                    ren_img[0])
Exemplo n.º 2
0
    def _classification(self, sequence_x, ann_x, sequence_y, ann_y):
        def state(i):
            if sequence_x[i] == '-' and sequence_y[i] == '-':
                return 0
            if sequence_x[i] == '-':
                return 2
            if sequence_y[i] == '-':
                return 1
            return 0

        def get_pos():
            def state(i):
                if sequence_x[i] == '-' and sequence_y[i] == '-':
                    return -1
                if sequence_x[i] == '-':
                    return 2
                if sequence_y[i] == '-':
                    return 1
                return 0

            pos_x, pos_y = 0, 0
            pos = list()
            for i in xrange(len(sequence_x)):
                pos.append((pos_x, pos_y))
                s = state(i)
                if s == 0:
                    pos_x += 1
                    pos_y += 1
                if s == 1:
                    pos_x += 1
                if s == 2:
                    pos_y += 1

            return pos

        assert len(sequence_y) == len(sequence_x)
        l = len(sequence_x)

        sequence_xs = Fasta.alnToSeq(sequence_x)
        sequence_ys = Fasta.alnToSeq(sequence_y)

        positions = get_pos()

        ret_match = (
            0 for _ in filter(lambda x: state(x) == 0, xrange(l))
        )

        ret_insertX = self.clf.multi_prepare_predict(
            (sequence_xs, positions[pos][0], ann_x, sequence_ys, positions[pos][1], ann_y)
            for pos in filter(lambda x: state(x) == 1, xrange(l))
        )

        ret_insertY = self.clf.multi_prepare_predict(
            (sequence_ys, positions[pos][1], ann_y, sequence_xs, positions[pos][0], ann_x)
            for pos in filter(lambda x: state(x) == 2, xrange(l))
        )

        ret = merge((state(x) for x in xrange(l)), ret_match, ret_insertX, ret_insertY)
        return ret
Exemplo n.º 3
0
    def _classification(self, sequence_x, ann_x, sequence_y, ann_y):
        def state(i):
            if sequence_x[i] == '-' and sequence_y[i] == '-':
                return 0
            if sequence_x[i] == '-':
                return 2
            if sequence_y[i] == '-':
                return 1
            return 0

        def get_pos():
            def state(i):
                if sequence_x[i] == '-' and sequence_y[i] == '-':
                    return -1
                if sequence_x[i] == '-':
                    return 2
                if sequence_y[i] == '-':
                    return 1
                return 0

            pos_x, pos_y = 0, 0
            pos = list()
            for i in xrange(len(sequence_x)):
                pos.append((pos_x, pos_y))
                s = state(i)
                if s == 0:
                    pos_x += 1
                    pos_y += 1
                if s == 1:
                    pos_x += 1
                if s == 2:
                    pos_y += 1

            return pos

        assert len(sequence_y) == len(sequence_x)
        l = len(sequence_x)

        sequence_xs = Fasta.alnToSeq(sequence_x)
        sequence_ys = Fasta.alnToSeq(sequence_y)

        positions = get_pos()

        ret_match = (0 for _ in filter(lambda x: state(x) == 0, xrange(l)))

        ret_insertX = self.clf.multi_prepare_predict(
            (sequence_xs, positions[pos][0], ann_x, sequence_ys,
             positions[pos][1], ann_y)
            for pos in filter(lambda x: state(x) == 1, xrange(l)))

        ret_insertY = self.clf.multi_prepare_predict(
            (sequence_ys, positions[pos][1], ann_y, sequence_xs,
             positions[pos][0], ann_x)
            for pos in filter(lambda x: state(x) == 2, xrange(l)))

        ret = merge((state(x) for x in xrange(l)), ret_match, ret_insertX,
                    ret_insertY)
        return ret
Exemplo n.º 4
0
  def sample_HoloGAN(self, config):
      could_load, checkpoint_counter = self.load(self.checkpoint_dir)
      if could_load:
          counter = checkpoint_counter
          print(" [*] Load SUCCESS")
      else:
          print(" [!] Load failed...")
          return
      SAMPLE_DIR = os.path.join(OUTPUT_DIR, "samples")
      if not os.path.exists(SAMPLE_DIR):
          os.makedirs(SAMPLE_DIR)
      sample_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
      if config.rotate_azimuth:
          low  = cfg['azi_low']
          high = cfg['azi_high']
          step = 10
      elif config.rotate_elevation:
          low  = cfg['ele_low']
          high = cfg['ele_high']
          step = 5
      else:
          low  = 0
          high = 10
          step = 1

      for i in range(low, high, step):
          if config.rotate_azimuth:
              sample_view = np.tile(
                  np.array([i * math.pi / 180.0, 0 * math.pi / 180.0, 1.0, 0, 0, 0]), (cfg['batch_size'], 1))
          elif config.rotate_azimuth:
              sample_view = np.tile(
                  np.array([270 * math.pi / 180.0, (90 - i) * math.pi / 180.0, 1.0, 0, 0, 0]), (cfg['batch_size'], 1))
          else:
              sample_view = self.gen_view_func(cfg['batch_size'],
                                               cfg['ele_low'], cfg['ele_high'],
                                               cfg['azi_low'], cfg['azi_high'],
                                               cfg['scale_low'], cfg['scale_high'],
                                               cfg['x_low'], cfg['x_high'],
                                               cfg['y_low'], cfg['y_high'],
                                               cfg['z_low'], cfg['z_high'],
                                               with_translation=False,
                                               with_scale=to_bool(str(cfg['with_translation'])))

          feed_eval = {self.z: sample_z,
                       self.view_in: sample_view}

          samples = self.sess.run(self.G, feed_dict=feed_eval)
          ren_img = inverse_transform(samples)
          ren_img = np.clip(255 * ren_img, 0, 255).astype(np.uint8)
          try:
              scipy.misc.imsave(
                  os.path.join(SAMPLE_DIR, "{0}_samples_{1}.png".format(counter, i)),
                  merge(ren_img, [cfg['batch_size'] // 4, 4]))
          except:
              scipy.misc.imsave(
                  os.path.join(SAMPLE_DIR, "{0}_samples_{1}.png".format(counter, i)),
                  ren_img[0])
Exemplo n.º 5
0
  def train_HoloGAN(self, config):
      self.d_lr_in = tf.placeholder(tf.float32, None, name='d_eta')
      self.g_lr_in = tf.placeholder(tf.float32, None, name='d_eta')

      d_optim = tf.train.AdamOptimizer(cfg['d_eta'], beta1=cfg['beta1'], beta2=cfg['beta2']).minimize(self.d_loss, var_list=self.d_vars)
      g_optim = tf.train.AdamOptimizer(cfg['g_eta'], beta1=cfg['beta1'], beta2=cfg['beta2']).minimize(self.g_loss, var_list=self.g_vars)

      tf.global_variables_initializer().run()

      shutil.copyfile(sys.argv[1], os.path.join(LOGDIR, 'config.json'))
      self.g_sum = merge_summary([self.d_loss_fake_sum, self.g_loss_sum])
      self.d_sum = merge_summary([self.d_loss_real_sum, self.d_loss_sum])
      self.writer = SummaryWriter(os.path.join(LOGDIR, str(dt.now())), self.sess.graph)

      num_objs = 1
      # Sample noise Z and view parameters to test during training
      sample_z_bg = self.sampling_Z(cfg['z_dim_bg'], num_objs=1, type=str(cfg['sample_z']))
      sample_z_fg = self.sampling_Z(cfg['z_dim_fg'], num_objs=num_objs, type=str(cfg['sample_z']))
      sample_view_bg = self.gen_view_func(cfg['batch_size'] * 1,
                                       cfg['ele_low'], cfg['ele_high'],
                                       cfg['azi_low'], cfg['azi_high'],
                                       cfg['scale_low'], cfg['scale_high'],
                                       cfg['x_low'], cfg['x_high'],
                                       cfg['y_low'], cfg['y_high'],
                                       cfg['z_low'], cfg['z_high'],
                                       with_translation=to_bool(str(cfg['with_translation'])),
                                       with_scale=to_bool(str(cfg['with_scale'])))
      sample_view_fg = self.gen_view_func(cfg['batch_size'] * num_objs,
                                       cfg['ele_low'], cfg['ele_high'],
                                       cfg['azi_low'], cfg['azi_high'],
                                       cfg['scale_low'], cfg['scale_high'],
                                       cfg['x_low'], cfg['x_high'],
                                       cfg['y_low'], cfg['y_high'],
                                       cfg['z_low'], cfg['z_high'],
                                       with_translation=to_bool(str(cfg['with_translation'])),
                                       with_scale=to_bool(str(cfg['with_scale'])))
      # sample_view_bg = tf.reshape(sample_view_bg, (cfg['batch_size'], 1, *sample_view_bg.shape[1:]))
      # sample_view_fg = tf.reshape(sample_view_fg, (cfg['batch_size'], num_objs, *sample_view_fg.shape[1:]))
      sample_files = self.data[0:cfg['batch_size']]

      if config.dataset == "cats" or config.dataset == "cars":
          sample_images = [get_image(sample_file,
                                    input_height=self.input_height,
                                    input_width=self.input_width,
                                    resize_height=self.output_height,
                                    resize_width=self.output_width,
                                    crop=self.crop) for sample_file in sample_files]
      else:
          sample_images = [get_image(sample_file,
                                    input_height=self.input_height,
                                    input_width=self.input_width,
                                    resize_height=self.output_height,
                                    resize_width=self.output_width,
                                    crop=self.crop) for sample_file in sample_files]

      ## TEMP
      batch_images = sample_images
      # batch_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
      # batch_view = self.gen_view_func(cfg['batch_size'],
      #                           cfg['ele_low'], cfg['ele_high'],
      #                           cfg['azi_low'], cfg['azi_high'],
      #                           cfg['scale_low'], cfg['scale_high'],
      #                           cfg['x_low'], cfg['x_high'],
      #                           cfg['y_low'], cfg['y_high'],
      #                           cfg['z_low'], cfg['z_high'],
      #                           with_translation=False,
      #                           with_scale=to_bool(str(cfg['with_translation'])))

      batch_z_bg = self.sampling_Z(cfg['z_dim_bg'], num_objs=1, type=str(cfg['sample_z']))
      batch_z_fg = self.sampling_Z(cfg['z_dim_fg'], num_objs=num_objs, type=str(cfg['sample_z']))
      batch_view_bg = self.gen_view_func(cfg['batch_size'] * 1,
                                       cfg['ele_low'], cfg['ele_high'],
                                       cfg['azi_low'], cfg['azi_high'],
                                       cfg['scale_low'], cfg['scale_high'],
                                       cfg['x_low'], cfg['x_high'],
                                       cfg['y_low'], cfg['y_high'],
                                       cfg['z_low'], cfg['z_high'],
                                       with_translation=to_bool(str(cfg['with_translation'])),
                                       with_scale=to_bool(str(cfg['with_scale'])))
      batch_view_fg = self.gen_view_func(cfg['batch_size'] * num_objs,
                                       cfg['ele_low'], cfg['ele_high'],
                                       cfg['azi_low'], cfg['azi_high'],
                                       cfg['scale_low'], cfg['scale_high'],
                                       cfg['x_low'], cfg['x_high'],
                                       cfg['y_low'], cfg['y_high'],
                                       cfg['z_low'], cfg['z_high'],
                                       with_translation=to_bool(str(cfg['with_translation'])),
                                       with_scale=to_bool(str(cfg['with_scale'])))
      # batch_view_bg = tf.reshape(batch_view_bg, (cfg['batch_size'], 1, *batch_view_bg.shape[1:]))
      # batch_view_fg = tf.reshape(batch_view_fg, (cfg['batch_size'], num_objs, *batch_view_fg.shape[1:]))

      counter = 0
      start_time = time.time()
      could_load, checkpoint_counter = self.load(self.checkpoint_dir)
      if could_load:
          counter = checkpoint_counter
          print(" [*] Load SUCCESS")
      else:
          print(" [!] Load failed...")

      self.data = glob.glob(os.path.join(IMAGE_PATH, self.input_fname_pattern))
      d_lr = cfg['d_eta']
      g_lr = cfg['g_eta']
      for epoch in range(cfg['max_epochs']):
          d_lr = d_lr if epoch < cfg['epoch_step'] else d_lr * (cfg['max_epochs'] - epoch) / (cfg['max_epochs'] - cfg['epoch_step'])
          g_lr = g_lr if epoch < cfg['epoch_step'] else g_lr * (cfg['max_epochs'] - epoch) / (cfg['max_epochs'] - cfg['epoch_step'])

          random.shuffle(self.data)
          batch_idxs = min(len(self.data), config.train_size) // cfg['batch_size']

          for idx in range(0, batch_idxs):
              batch_files = self.data[idx * cfg['batch_size']:(idx + 1) * cfg['batch_size']]
              # if config.dataset == "cats" or config.dataset == "cars":
              #     batch_images = [get_image(batch_file,
              #                       input_height=self.input_height,
              #                       input_width=self.input_width,
              #                       resize_height=self.output_height,
              #                       resize_width=self.output_width,
              #                       crop=self.crop) for batch_file in batch_files]
              # else:
              #     batch_images = [get_image(batch_file,
              #                       input_height=self.input_height,
              #                       input_width=self.input_width,
              #                       resize_height=self.output_height,
              #                       resize_width=self.output_width,
              #                       crop=self.crop) for batch_file in batch_files]

              # batch_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
              # batch_view = self.gen_view_func(cfg['batch_size'],
              #                          cfg['ele_low'], cfg['ele_high'],
              #                          cfg['azi_low'], cfg['azi_high'],
              #                          cfg['scale_low'], cfg['scale_high'],
              #                          cfg['x_low'], cfg['x_high'],
              #                          cfg['y_low'], cfg['y_high'],
              #                          cfg['z_low'], cfg['z_high'],
              #                          with_translation=False,
              #                          with_scale=to_bool(str(cfg['with_translation'])))

              feed = {self.inputs: batch_images,
                      self.z_bg: batch_z_bg,
                      self.z_fg: batch_z_fg,
                      # self.z: batch_z,
                      self.view_in_bg: batch_view_bg,
                      self.view_in_fg: batch_view_fg,
                      self.d_lr_in: d_lr,
                      self.g_lr_in: g_lr}
              # Update D network
              _, summary_str = self.sess.run([d_optim, self.d_sum],feed_dict=feed)
              self.writer.add_summary(summary_str, counter)
              # Update G network
              _, summary_str = self.sess.run([g_optim, self.g_sum], feed_dict=feed)
              self.writer.add_summary(summary_str, counter)
              # Run g_optim twice
              _, summary_str = self.sess.run([g_optim, self.g_sum],  feed_dict=feed)
              self.writer.add_summary(summary_str, counter)

              errD_fake = self.d_loss_fake.eval(feed)
              errD_real = self.d_loss_real.eval(feed)
              errG = self.g_loss.eval(feed)
              # errQ = self.q_loss.eval(feed)
              errQ = 0.

              print("Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f, q_loss: %.8f" \
                    % (epoch, idx, batch_idxs,
                       time.time() - start_time, errD_fake + errD_real, errG, errQ))

              if np.mod(counter, 100) == 0:
                  # self.save(LOGDIR, counter)
                  feed_eval = {self.inputs: sample_images,
                               self.z_bg: sample_z_bg,
                               self.z_fg: sample_z_fg,
                               # self.z: batch_z,
                               self.view_in_bg: sample_view_bg,
                               self.view_in_fg: sample_view_fg,
                               self.d_lr_in: d_lr,
                               self.g_lr_in: g_lr}

                  samples, d_loss, g_loss = self.sess.run(
                      [self.G, self.d_loss, self.g_loss],
                      feed_dict=feed_eval)


                  print('generated samples')
                  print(samples)

                  ren_img = inverse_transform(samples)
                  ren_img = np.clip(255 * ren_img, 0, 255).astype(np.uint8)
                  # real_img = inverse_transform(np.array(sample_images))
                  # real_img = np.clip(255 * real_img, 0, 255).astype(np.uint8)

                  try:
                      self.writer.add_summary(tf.summary.image("{0}_GAN.png".format(counter), ren_img).eval(), counter)
                      imsave(
                          os.path.join(OUTPUT_DIR, "{0}_GAN.png".format(counter)),
                          merge(ren_img, [cfg['batch_size'] // 4, 4]))
                      # imsave(
                      #     os.path.join(OUTPUT_DIR, "{0}_reals_GAN.png".format(counter)),
                      #     merge(real_img, [cfg['batch_size'] // 4, 4]))
                      # print("[Sample] d_loss: %.8f, g_loss: %.8f" % (d_loss, g_loss))
                  except:
                      self.writer.add_summary(tf.summary.image("{0}_GAN.png".format(counter), ren_img[:1]).eval(), counter)
                      imsave(
                          os.path.join(OUTPUT_DIR, "{0}_GAN.png".format(counter)),
                          ren_img[0])
                      # imsave(
                      #     os.path.join(OUTPUT_DIR, "{0}_reals_GAN.png".format(counter)),
                      #     real_img[0])
                      # print("[Sample] d_loss: %.8f, g_loss: %.8f" % (d_loss, g_loss))
              counter += 1
          return
      self.save(LOGDIR, counter)
Exemplo n.º 6
0
    def train_HoloGAN(self, config):
        self.d_lr_in = tf.placeholder(tf.float32, None, name='d_eta')
        self.g_lr_in = tf.placeholder(tf.float32, None, name='d_eta')

        d_optim = tf.train.AdamOptimizer(cfg['d_eta'], beta1=cfg['beta1'], beta2=cfg['beta2']).minimize(self.d_loss, var_list=self.d_vars)
        g_optim = tf.train.AdamOptimizer(cfg['g_eta'], beta1=cfg['beta1'], beta2=cfg['beta2']).minimize(self.g_loss, var_list=self.g_vars)

        tf.global_variables_initializer().run()

        shutil.copyfile(sys.argv[1], os.path.join(LOGDIR, 'config.json'))
        self.g_sum = merge_summary([self.d_loss_fake_sum, self.g_loss_sum])
        self.d_sum = merge_summary([self.d_loss_real_sum, self.d_loss_sum])
        self.writer = SummaryWriter(LOGDIR, self.sess.graph)

        # Sample noise Z and view parameters to test during training
        sample_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
        sample_view = self.gen_view_func(cfg['batch_size'],
                                         cfg['ele_low'], cfg['ele_high'],
                                         cfg['azi_low'], cfg['azi_high'],
                                         cfg['scale_low'], cfg['scale_high'],
                                         cfg['x_low'], cfg['x_high'],
                                         cfg['y_low'], cfg['y_high'],
                                         cfg['z_low'], cfg['z_high'],
                                         with_translation=False,
                                         with_scale=to_bool(str(cfg['with_translation'])))
        sample_files = self.data[0:cfg['batch_size']]

        if config.dataset in ['cats', 'cars']:
            sample_images = [get_image(sample_file,
                                       input_height=self.input_height,
                                       input_width=self.input_width,
                                       resize_height=self.output_height,
                                       resize_width=self.output_width,
                                       crop=False)
                             for sample_file in sample_files]
        else:
            sample_images = [get_image(sample_file,
                                       input_height=self.input_height,
                                       input_width=self.input_width,
                                       resize_height=self.output_height,
                                       resize_width=self.output_width,
                                       crop=True)
                             for sample_file in sample_files]

        counter = 1
        start_time = time.time()
        could_load, checkpoint_counter = self.load(self.checkpoint_dir)
        if could_load:
            counter = checkpoint_counter
            print(' [*] Load SUCCESS')
        else:
            print(' [!] Load failed...')

        self.data = glob.glob(os.path.join(IMAGE_PATH, self.input_fname_pattern))
        d_lr = cfg['d_eta']
        g_lr = cfg['g_eta']
        for epoch in range(cfg['max_epochs']):
            d_lr = d_lr if epoch < cfg['epoch_step'] else d_lr * (cfg['max_epochs'] - epoch) / (cfg['max_epochs'] - cfg['epoch_step'])
            g_lr = g_lr if epoch < cfg['epoch_step'] else g_lr * (cfg['max_epochs'] - epoch) / (cfg['max_epochs'] - cfg['epoch_step'])

            random.shuffle(self.data)
            batch_idxs = min(len(self.data), config.train_size) // cfg['batch_size']

            for idx in range(0, batch_idxs):
                batch_files = self.data[idx * cfg['batch_size']:(idx + 1) * cfg['batch_size']]
                if config.dataset == 'cats' or config.dataset == 'cars':
                    batch_images = [get_image(batch_file,
                                              input_height=self.input_height,
                                              input_width=self.input_width,
                                              resize_height=self.output_height,
                                              resize_width=self.output_width,
                                              crop=False)
                                    for batch_file in batch_files]
                else:
                    batch_images = [get_image(batch_file,
                                              input_height=self.input_height,
                                              input_width=self.input_width,
                                              resize_height=self.output_height,
                                              resize_width=self.output_width,
                                              crop=self.crop)
                                    for batch_file in batch_files]

                batch_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
                batch_view = self.gen_view_func(cfg['batch_size'],
                                                cfg['ele_low'], cfg['ele_high'],
                                                cfg['azi_low'], cfg['azi_high'],
                                                cfg['scale_low'], cfg['scale_high'],
                                                cfg['x_low'], cfg['x_high'],
                                                cfg['y_low'], cfg['y_high'],
                                                cfg['z_low'], cfg['z_high'],
                                                with_translation=False,
                                                with_scale=to_bool(str(cfg['with_translation'])))

                feed = {self.inputs: batch_images,
                        self.z: batch_z,
                        self.view_in: batch_view,
                        self.d_lr_in: d_lr,
                        self.g_lr_in: g_lr}
                # Update D network
                _, summary_str = self.sess.run([d_optim, self.d_sum], feed_dict=feed)
                self.writer.add_summary(summary_str, counter)
                # Update G network
                _, summary_str = self.sess.run([g_optim, self.g_sum], feed_dict=feed)
                self.writer.add_summary(summary_str, counter)
                # Run g_optim twice
                _, summary_str = self.sess.run([g_optim, self.g_sum], feed_dict=feed)
                self.writer.add_summary(summary_str, counter)

                errD_fake = self.d_loss_fake.eval(feed)
                errD_real = self.d_loss_real.eval(feed)
                errG = self.g_loss.eval(feed)
                errQ = self.q_loss.eval(feed)

                counter += 1
                print('Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f, q_loss: %.8f' \
                      % (epoch, idx, batch_idxs, time.time() - start_time, errD_fake + errD_real, errG, errQ))

                if np.mod(counter, 1000) == 1:
                    self.save(LOGDIR, counter)
                    feed_eval = {self.inputs: sample_images,
                                 self.z: sample_z,
                                 self.view_in: sample_view,
                                 self.d_lr_in: d_lr,
                                 self.g_lr_in: g_lr}
                    samples, d_loss, g_loss = self.sess.run([self.G, self.d_loss, self.g_loss], feed_dict=feed_eval)
                    ren_img = inverse_transform(samples)
                    ren_img = np.clip(255 * ren_img, 0, 255).astype(np.uint8)
                    try:
                        scipy.misc.imsave(
                            os.path.join(OUTPUT_DIR, '{0}_GAN.png'.format(counter)),
                            merge(ren_img, [cfg['batch_size'] // 4, 4]))
                        print('[Sample] d_loss: %.8f, g_loss: %.8f' % (d_loss, g_loss))
                    except:
                        scipy.misc.imsave(
                            os.path.join(OUTPUT_DIR, '{0}_GAN.png'.format(counter)),
                            ren_img[0])
                        print('[Sample] d_loss: %.8f, g_loss: %.8f' % (d_loss, g_loss))
Exemplo n.º 7
0
    def sample_HoloGAN(self):
        could_load, checkpoint_counter = self.load(self.checkpoint_dir)
        if could_load:
            counter = checkpoint_counter
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")
            return
        SAMPLE_DIR = os.path.join(OUTPUT_DIR, "samples")
        if not os.path.exists(SAMPLE_DIR):
            os.makedirs(SAMPLE_DIR)

        # sample_z = self.sampling_Z(cfg.z_dim, str(cfg.sample_z))
        if cfg.rotate_azimuth:
            low = cfg.azi_low
            high = cfg.azi_high
            step = 10
        elif cfg.rotate_elevation:
            low = cfg.ele_low
            high = cfg.ele_high
            step = 5
        else:
            low = 0
            high = 10
            step = 1

        for i in range(low, high, step):
            if cfg.rotate_azimuth:
                sample_view = np.tile(
                    np.array([
                        i * math.pi / 180.0, 0 * math.pi / 180.0, 1.0, 0, 0, 0
                    ]), (cfg.batch_size, 1))
            elif cfg.rotate_elevation:
                sample_view = np.tile(
                    np.array([
                        270 * math.pi / 180.0, (90 - i) * math.pi / 180.0, 1.0,
                        0, 0, 0
                    ]), (cfg.batch_size, 1))
            else:
                sample_view = self.gen_view_func(
                    cfg.batch_size,
                    cfg.ele_low,
                    cfg.ele_high,
                    cfg.azi_low,
                    cfg.azi_high,
                    cfg.scale_low,
                    cfg.scale_high,
                    cfg.x_low,
                    cfg.x_high,
                    cfg.y_low,
                    cfg.y_high,
                    cfg.z_low,
                    cfg.z_high,
                    with_translation=False,
                    with_scale=to_bool(str(cfg.with_translation)))

            # Use global vectors
            feed_eval = {self.z: SAMPLE_Z, self.view_in: SAMPLE_VIEW}

            samples = self.sess.run(self.G, feed_dict=feed_eval)
            ren_img = inverse_transform(samples)
            ren_img = np.clip(255 * ren_img, 0, 255).astype(np.uint8)
            grid_x = GRID_X
            if ADD_EMBEDDING:
                ren_img = np.concatenate([self.images, ren_img], axis=0)
                grid_x += 1
            Image.fromarray(merge(ren_img, [grid_x, GRID_Y]).astype(
                np.uint8)).save(
                    os.path.join(SAMPLE_DIR,
                                 "{0}_samples_{1}.png".format(counter, i)))
Exemplo n.º 8
0
    def train_HoloGAN(self):
        self.d_lr_in = tf.placeholder(tf.float32, None, name='d_eta')
        self.g_lr_in = tf.placeholder(tf.float32, None, name='d_eta')

        d_optim = tf.train_on_loader.AdamOptimizer(cfg.d_eta,
                                                   beta1=cfg.beta1,
                                                   beta2=cfg.beta2).minimize(
                                                       self.d_loss,
                                                       var_list=self.d_vars)
        g_optim = tf.train_on_loader.AdamOptimizer(cfg.g_eta,
                                                   beta1=cfg.beta1,
                                                   beta2=cfg.beta2).minimize(
                                                       self.g_loss,
                                                       var_list=self.g_vars)

        tf.global_variables_initializer().run()

        self.g_sum = merge_summary([self.d_loss_fake_sum, self.g_loss_sum])
        self.d_sum = merge_summary([self.d_loss_real_sum, self.d_loss_sum])
        self.writer = SummaryWriter(LOGDIR, self.sess.graph)

        # Sample noise Z and view parameters to test during training
        # sample_z = self.sampling_Z(cfg.z_dim - cfg.emb_dim, str(cfg.sample_z))
        # sample_view = self.gen_view_func(cfg.batch_size,
        #                                  cfg.ele_low, cfg.ele_high,
        #                                  cfg.azi_low, cfg.azi_high,
        #                                  cfg.scale_low, cfg.scale_high,
        #                                  cfg.x_low, cfg.x_high,
        #                                  cfg.y_low, cfg.y_high,
        #                                  cfg.z_low, cfg.z_high,
        #                                  with_translation=False,
        #                                  with_scale=to_bool(str(cfg.with_translation)))
        sample_files = self.data[0:cfg.batch_size]

        if cfg.dataset == "cats" or cfg.dataset == "cars":
            sample_images = [
                get_image(sample_file,
                          input_height=self.input_height,
                          input_width=self.input_width,
                          resize_height=self.output_height,
                          resize_width=self.output_width,
                          crop=False) for sample_file in sample_files
            ]
        else:
            sample_images = [
                get_image(sample_file,
                          input_height=self.input_height,
                          input_width=self.input_width,
                          resize_height=self.output_height,
                          resize_width=self.output_width,
                          crop=self.crop) for sample_file in sample_files
            ]

        counter = 1
        start_time = time.time()
        could_load, checkpoint_counter = self.load(self.checkpoint_dir)
        if could_load:
            counter = checkpoint_counter
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")

        self.data = glob.glob(
            os.path.join(IMAGE_PATH, self.input_fname_pattern))
        d_lr = cfg.d_eta
        g_lr = cfg.g_eta
        for epoch in range(cfg.max_epochs):
            d_lr = d_lr if epoch < cfg.epoch_step else d_lr * (
                cfg.max_epochs - epoch) / (cfg.max_epochs - cfg.epoch_step)
            g_lr = g_lr if epoch < cfg.epoch_step else g_lr * (
                cfg.max_epochs - epoch) / (cfg.max_epochs - cfg.epoch_step)

            random.shuffle(self.data)
            batch_idxs = min(len(self.data), cfg.train_size) // cfg.batch_size

            for idx in tqdm(range(0, batch_idxs)):
                batch_files = self.data[idx * cfg.batch_size:(idx + 1) *
                                        cfg.batch_size]
                if cfg.dataset == "cats" or cfg.dataset == "cars":
                    batch_images = [
                        get_image(batch_file,
                                  input_height=self.input_height,
                                  input_width=self.input_width,
                                  resize_height=self.output_height,
                                  resize_width=self.output_width,
                                  crop=False) for batch_file in batch_files
                    ]
                else:
                    batch_images = [
                        get_image(batch_file,
                                  input_height=self.input_height,
                                  input_width=self.input_width,
                                  resize_height=self.output_height,
                                  resize_width=self.output_width,
                                  crop=self.crop) for batch_file in batch_files
                    ]

                if ADD_EMBEDDING:
                    batch_z = self.sampling_Z(cfg.z_dim - cfg.emb_dim,
                                              str(cfg.sample_z))
                else:
                    batch_z = self.sampling_Z(cfg.z_dim, str(cfg.sample_z))
                # batch_z = self.sampling_Z(cfg.z_dim, str(cfg.sample_z))
                # batch_emb = torch.stack([self.emb_transforms(Image.open(file).convert('RGB')) for file in batch_files])
                # batch_emb = self.embedder(batch_emb.to(torch.float32).cuda()).cpu().detach().numpy()

                if ADD_EMBEDDING:
                    batch_emb = [
                        self.emb_transforms(Image.open(file).convert('RGB'))
                        for file in batch_files
                    ]
                    if len(batch_emb) != cfg.batch_size:
                        batch_emb = batch_emb * cfg.batch_size
                        batch_emb = batch_emb[:cfg.batch_size]
                    batch_emb = torch.stack(batch_emb).numpy()
                    batch_emb = self.sess_emb.run(self.emb_output,
                                                  {self.emb_input: batch_emb})
                    batch_z = np.hstack((batch_z, batch_emb))

                batch_view = self.gen_view_func(cfg.batch_size,
                                                cfg.ele_low,
                                                cfg.ele_high,
                                                cfg.azi_low,
                                                cfg.azi_high,
                                                cfg.scale_low,
                                                cfg.scale_high,
                                                cfg.x_low,
                                                cfg.x_high,
                                                cfg.y_low,
                                                cfg.y_high,
                                                cfg.z_low,
                                                cfg.z_high,
                                                with_translation=False,
                                                with_scale=to_bool(
                                                    str(cfg.with_translation)))

                # control_weight = [
                #     op for op in tf.get_default_graph().get_operations()
                #     if op.name == 'layer3.20.conv1.weight']
                # control_weight = control_weight[0].values()[0].eval(session=self.sess)
                # print(np.mean(control_weight), np.std(control_weight))

                feed = {
                    self.inputs: batch_images,
                    self.z: batch_z,
                    self.view_in: batch_view,
                    self.d_lr_in: d_lr,
                    self.g_lr_in: g_lr
                }
                # Update D network
                _, summary_str = self.sess.run([d_optim, self.d_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)

                # Update G network
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)

                # Run g_optim twice
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)

                errD_fake = self.d_loss_fake.eval(feed)
                errD_real = self.d_loss_real.eval(feed)
                errG = self.g_loss.eval(feed)
                errQ = self.q_loss.eval(feed)
                if ADD_EMBEDDING:
                    errE = self.e_loss.eval(feed)
                else:
                    errE = 0

                counter += 1
                print("Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f, q_loss: %.8f, e_loss: %.8f" \
                      % (epoch, idx, batch_idxs,
                         time.time() - start_time, errD_fake + errD_real, errG, errQ, errE))

                if np.mod(counter, cfg.refresh_rate) == 1:
                    assert 'log' in str(LOGDIR)
                    shutil.rmtree(LOGDIR, ignore_errors=True)
                    shutil.copyfile(sys.argv[1],
                                    os.path.join(LOGDIR, 'config.py'))
                    self.save(LOGDIR, counter)
                    # Use global vectors
                    feed_eval = {
                        self.inputs: sample_images,
                        self.z: SAMPLE_Z,
                        self.view_in: SAMPLE_VIEW,
                        self.d_lr_in: d_lr,
                        self.g_lr_in: g_lr
                    }
                    samples, d_loss, g_loss = self.sess.run(
                        [self.G, self.d_loss, self.g_loss],
                        feed_dict=feed_eval)
                    ren_img = inverse_transform(samples)
                    ren_img = np.clip(255 * ren_img, 0, 255).astype(np.uint8)
                    grid_x = GRID_X
                    if ADD_EMBEDDING:
                        ren_img = np.concatenate([self.images, ren_img],
                                                 axis=0)
                        grid_x += 1
                    # try:
                    Image.fromarray(
                        merge(ren_img,
                              [grid_x, GRID_Y]).astype(np.uint8)).save(
                                  os.path.join(OUTPUT_DIR,
                                               "{0}_GAN.png".format(counter)))
                    print("[Sample] d_loss: %.8f, g_loss: %.8f" %
                          (d_loss, g_loss))
Exemplo n.º 9
0
    def sample_HoloGAN(self, config):
        sample_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
        count = 0
        could_load, checkpoint_counter = self.load()
        if could_load:
            counter = checkpoint_counter
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")
            return
        if not os.path.exists(self.sample_dir):
            os.makedirs(self.sample_dir)

        if config.rotate_azimuth:
            low = cfg['azi_low']
            high = cfg['azi_high']
            step = STEP
        elif config.rotate_elevation:
            low = cfg['ele_low']
            high = cfg['ele_high']
            step = STEP
        else:
            low = 0
            high = 10
            step = 1

        for i in range(low, high, step):
            if config.rotate_azimuth:
                sample_view = np.tile(
                    np.array([
                        i * math.pi / 180.0, 0 * math.pi / 180.0, 1.0, 0, 0, 0
                    ]), (cfg['batch_size'], 1))
                #print(sample_view)
            elif config.rotate_elevation:
                sample_view = np.tile(
                    np.array([
                        270 * math.pi / 180.0, (90 - i) * math.pi / 180.0, 1.0,
                        0, 0, 0
                    ]), (cfg['batch_size'], 1))
            else:
                sample_view = self.gen_view_func(
                    cfg['batch_size'],
                    cfg['ele_low'],
                    cfg['ele_high'],
                    cfg['azi_low'],
                    cfg['azi_high'],
                    cfg['scale_low'],
                    cfg['scale_high'],
                    cfg['x_low'],
                    cfg['x_high'],
                    cfg['y_low'],
                    cfg['y_high'],
                    cfg['z_low'],
                    cfg['z_high'],
                    with_translation=to_bool(str(cfg['with_translation'])),
                    with_scale=to_bool(str(cfg['with_scale'])))

            feed_eval = {self.z: sample_z, self.view_in: sample_view}

            samples = self.sess.run(self.G, feed_dict=feed_eval)
            ren_img = np.clip(255 * samples, 0, 255).astype(np.uint8)
            """
            cv2.imshow('image',ren_img[0])
            cv2.waitKey(0)
            raise Exception('å')
            """
            if count < 10:
                count_str = 'a' + str(count)
            elif count > 99:
                count_str = 'c' + str(count)
            else:
                count_str = 'b' + str(count)

            try:
                cv2.imwrite(
                    os.path.join(self.sample_dir, "{0}.jpg".format(count_str)),
                    merge(ren_img, [cfg['batch_size'] // 4, 4]))
            except:
                cv2.imwrite(
                    os.path.join(self.sample_dir, "{0}.jpg".format(count_str)),
                    ren_img[0])
            count = count + 1
Exemplo n.º 10
0
    def train_HoloGAN(self, config):

        self.d_lr_in = tf.compat.v1.placeholder(tf.float32, None, name='d_eta')
        self.g_lr_in = tf.compat.v1.placeholder(tf.float32, None, name='d_eta')

        d_optim = tf.train.AdamOptimizer(cfg['d_eta'],
                                         beta1=cfg['beta1'],
                                         beta2=cfg['beta2']).minimize(
                                             self.d_loss, var_list=self.d_vars)
        g_optim = tf.train.AdamOptimizer(cfg['g_eta'],
                                         beta1=cfg['beta1'],
                                         beta2=cfg['beta2']).minimize(
                                             self.g_loss, var_list=self.g_vars)

        tf.global_variables_initializer().run()

        shutil.copyfile(sys.argv[1], os.path.join(self.log_dir, 'config.json'))
        self.g_sum = merge_summary([self.d_loss_fake_sum, self.g_loss_sum])
        self.d_sum = merge_summary([self.d_loss_real_sum, self.d_loss_sum])
        self.writer = SummaryWriter(self.log_dir, self.sess.graph)

        # Sample noise Z and view parameters to test during training
        sample_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
        sample_view = self.gen_view_func(cfg['batch_size'],
                                         cfg['ele_low'],
                                         cfg['ele_high'],
                                         cfg['azi_low'],
                                         cfg['azi_high'],
                                         cfg['scale_low'],
                                         cfg['scale_high'],
                                         cfg['x_low'],
                                         cfg['x_high'],
                                         cfg['y_low'],
                                         cfg['y_high'],
                                         cfg['z_low'],
                                         cfg['z_high'],
                                         with_translation=False,
                                         with_scale=to_bool(
                                             str(cfg['with_translation'])))
        sample_files = self.data[0:cfg['batch_size']]
        sample_images = [
            get_image(sample_file,
                      input_height=self.input_height,
                      input_width=self.input_width,
                      resize_height=self.output_height,
                      resize_width=self.output_width,
                      crop=False) for sample_file in sample_files
        ]

        counter = 1
        start_time = time.time()
        could_load, checkpoint_counter = self.load()
        if could_load:
            counter = checkpoint_counter
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")

        d_lr = cfg['d_eta']
        g_lr = cfg['g_eta']
        for epoch in range(cfg['max_epochs']):
            d_lr = d_lr if epoch < cfg['epoch_step'] else d_lr * (
                cfg['max_epochs'] - epoch) / (cfg['max_epochs'] -
                                              cfg['epoch_step'])
            g_lr = g_lr if epoch < cfg['epoch_step'] else g_lr * (
                cfg['max_epochs'] - epoch) / (cfg['max_epochs'] -
                                              cfg['epoch_step'])
            random.shuffle(self.data)
            batch_idxs = min(len(self.data),
                             config.train_size) // cfg['batch_size']
            batch_idxs = int(batch_idxs)
            for idx in range(0, batch_idxs):
                batch_files = self.data[idx * cfg['batch_size']:(idx + 1) *
                                        cfg['batch_size']]

                batch_images = [
                    get_image(batch_file,
                              input_height=self.input_height,
                              input_width=self.input_width,
                              resize_height=self.output_height,
                              resize_width=self.output_width,
                              crop=self.crop) for batch_file in batch_files
                ]
                """
                img = np.clip(255 * batch_images[0], 0, 255).astype(np.uint8)
                cv2.imshow('image',img)
                cv2.waitKey(0)
                raise Exception('å')
                """

                batch_z = self.sampling_Z(cfg['z_dim'], str(cfg['sample_z']))
                batch_view = self.gen_view_func(
                    cfg['batch_size'],
                    cfg['ele_low'],
                    cfg['ele_high'],
                    cfg['azi_low'],
                    cfg['azi_high'],
                    cfg['scale_low'],
                    cfg['scale_high'],
                    cfg['x_low'],
                    cfg['x_high'],
                    cfg['y_low'],
                    cfg['y_high'],
                    cfg['z_low'],
                    cfg['z_high'],
                    with_translation=False,
                    with_scale=to_bool(str(cfg['with_translation'])))

                feed = {
                    self.inputs: batch_images,
                    self.z: batch_z,
                    self.view_in: batch_view,
                    self.d_lr_in: d_lr,
                    self.g_lr_in: g_lr
                }
                # Update D network
                _, summary_str = self.sess.run([d_optim, self.d_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)
                # Update G network
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)
                # Run g_optim twice
                _, summary_str = self.sess.run([g_optim, self.g_sum],
                                               feed_dict=feed)
                self.writer.add_summary(summary_str, counter)

                errD_fake = self.d_loss_fake.eval(feed)
                errD_real = self.d_loss_real.eval(feed)
                errG = self.g_loss.eval(feed)
                errQ = self.q_loss.eval(feed)

                counter += 1
                print(
                    "Epoch: [%2d] [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f, q_loss: %.8f"
                    % (epoch, idx, batch_idxs, time.time() - start_time,
                       errD_fake + errD_real, errG, errQ))

                if np.mod(counter, SAVE_STEP) == 1:
                    self.save(counter)
                    feed_eval = {
                        self.inputs: sample_images,
                        self.z: sample_z,
                        self.view_in: sample_view,
                        self.d_lr_in: d_lr,
                        self.g_lr_in: g_lr
                    }
                    samples, d_loss, g_loss = self.sess.run(
                        [self.G, self.d_loss, self.g_loss],
                        feed_dict=feed_eval)
                    ren_img = np.clip(255 * samples, 0, 255).astype(np.uint8)
                    try:
                        cv2.imwrite(
                            os.path.join(self.img_dir,
                                         "{0}_GAN.jpg".format(counter)),
                            merge(ren_img, [cfg['batch_size'] // 4, 4]))
                        print("[Sample] d_loss: %.8f, g_loss: %.8f" %
                              (d_loss, g_loss))
                    except:
                        cv2.imwrite(
                            os.path.join(self.img_dir,
                                         "{0}_GAN.jpg".format(counter)),
                            ren_img[0])
                        print("[Sample] d_loss: %.8f, g_loss: %.8f" %
                              (d_loss, g_loss))