Exemple #1
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)
from tools import utils
from tools.inception_v3_imagenet import model
import tensorflow as tf
import pickle
import sys

IMAGENET_PATH=""
if __name__=="__main__":
    if IMAGENET_PATH == "":
        raise ValueError("Please open precompute.py and set IMAGENET_PATH")
    s = (299, 299, 3)
    dataset = sys.argv[1]
    last_j = 0
    sess = tf.InteractiveSession()
    x = tf.placeholder(tf.float32, s)
    _, preds = model(sess, tf.expand_dims(x, 0))
    label_dict = {}
    for i in range(1,1000):
        print("Looking for %d" % (i,))
        if i in label_dict:
            continue
        for j in range(last_j, 50000):
            im, lab = utils.get_image(j, IMAGENET_PATH)
            if sess.run(preds, {x: im})[0] == lab:
                label_dict[lab] = j
            if lab == i:
                label_dict[i] = j
                break
        last_j = j
    pickle.dump(label_dict, open("tools/data/imagenet.pickle", "wb"))
        # Iterate over the samples batch-by-batch
        num_eval_examples = params.sample_size
        eval_batch_size = 1
        #num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
        num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
        x_adv = []  # adv accumulator

        bstart = 0
        x_full_batch = []
        y_full_batch = []
        print('loading image data')
        while (True):
            x_candid = []
            y_candid = []
            for i in range(100):
                img_batch, y_batch = get_image(target_indices[bstart + i],
                                               IMAGENET_PATH)
                img_batch = np.reshape(img_batch, (-1, *img_batch.shape))
                if i == 0:
                    x_candid = img_batch
                    y_candid = np.array([y_batch])
                else:
                    x_candid = np.concatenate([x_candid, img_batch])
                    y_candid = np.concatenate([y_candid, [y_batch]])
            logits, preds = sess.run([tester.logits, tester.predictions],
                                     feed_dict={
                                         tester.model_x: x_candid,
                                         tester.model_y: y_candid
                                     })
            idx = np.where(preds == y_candid)
            x_masked = x_candid[idx]
            y_masked = y_candid[idx]
    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))
Exemple #5
0
        num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
        x_adv = []  # adv accumulator

        attack_set = []

        bstart = 0
        x_full_batch = []
        y_full_batch = []
        print('loading image data')
        while (True):
            x_candid = []
            y_candid = []
            for i in range(100):
                img_batch, y_batch = get_image(
                    target_indices[args.img_index_start + bstart + i],
                    IMAGENET_PATH)
                #img_batch, y_batch = get_image(bstart+i, IMAGENET_PATH)
                img_batch = np.reshape(img_batch, (-1, *img_batch.shape))
                x_candid.append(img_batch)
                y_candid.append(y_batch)
            x_candid = np.concatenate(x_candid, axis=0)
            y_candid = np.array(y_candid)
            logits, preds = sess.run([attack.logits, attack.predictions],
                                     feed_dict={
                                         attack.x_input: x_candid,
                                         attack.y_input: y_candid
                                     })
            idx = np.where(preds == y_candid)
            for i in idx[0]:
                attack_set.append(bstart + i)
Exemple #6
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))
Exemple #7
0
        num_eval_examples = params.sample_size
        eval_batch_size = min(params.batch_size, num_eval_examples)
        assert num_eval_examples % eval_batch_size == 0
        #num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
        num_batches = int(math.ceil(num_eval_examples / eval_batch_size))
        x_adv = []  # adv accumulator

        bstart = 0
        x_full_batch = []
        y_full_batch = []
        print('loading image data')
        while (True):
            x_candid = []
            y_candid = []
            for i in range(100):
                img_batch, y_batch = get_image(bstart + i, IMAGENET_PATH)
                img_batch = np.reshape(img_batch, (-1, *img_batch.shape))
                if i == 0:
                    x_candid = img_batch
                    y_candid = np.array([y_batch])
                else:
                    x_candid = np.concatenate([x_candid, img_batch])
                    y_candid = np.concatenate([y_candid, [y_batch]])
            logits, preds = sess.run([attack.logits, attack.predictions],
                                     feed_dict={
                                         attack.model_x: x_candid,
                                         attack.model_y: y_candid
                                     })
            idx = np.where(preds == y_candid)
            x_masked = x_candid[idx]
            y_masked = y_candid[idx]
Exemple #8
0
def main(args):

    imagenet_loader = DataLoader(imagenet,
                                 batch_size=args.batch_size,
                                 shuffle=False)

    # data loader using code from NES
    num_eval_examples = args.sample_size
    eval_batch_size = min(args.batch_size, num_eval_examples)

    if args.targeted:
        target_indices = np.load(
            '/data/home/gaon/lazy-attack/data/indices_targeted.npy')
    else:
        target_indices = np.load(
            '/data/home/gaon/lazy-attack/data/indices_untargeted.npy')

    if args.shuffle:
        np.random.shuffle(target_indices)

    num_batches = int(math.ceil(num_eval_examples / eval_batch_size))

    #assert (num_eval_examples%eval_batch_size==0)
    bstart = 0
    x_full_batch = []
    y_full_batch = []

    attack_set = []

    print('loading image data')
    while (True):
        x_candid = []
        y_candid = []
        for i in range(100):
            if (args.img_index_start + bstart + i) < len(target_indices):
                img_batch, y_batch = get_image(
                    target_indices[args.img_index_start + bstart + i],
                    IMAGENET_PATH)
                img_batch = ch.Tensor(img_batch)
                img_batch = ch.transpose(img_batch, 0, 1)
                img_batch = ch.transpose(img_batch, 0, 2)
                x_candid.append(img_batch.view(-1, 3, 299, 299))

                if args.targeted:
                    target_class = pseudorandom_target(
                        target_indices[args.img_index_start + bstart + i],
                        1000, y_batch)
                    y_batch = target_class
                y_candid.append(y_batch)

        x_candid = ch.cat(x_candid)
        y_candid = np.array(y_candid)

        if args.targeted:
            preds = model_to_fool(
                batch_norm(x_candid).cuda()).argmax(1).cpu().numpy()
            idx = np.where(preds != y_candid)
        else:
            preds = model_to_fool(
                batch_norm(x_candid).cuda()).argmax(1).cpu().numpy()
            idx = np.where(preds == y_candid)

        for i in idx[0]:
            attack_set.append(bstart + i)
        x_candid = x_candid.cpu().numpy()
        x_masked = x_candid[idx]
        y_masked = y_candid[idx]
        if bstart == 0:
            x_full_batch = x_masked[:min(num_eval_examples, len(x_masked))]
            y_full_batch = y_masked[:min(num_eval_examples, len(y_masked))]
        else:
            index = min(num_eval_examples - len(x_full_batch), len(x_masked))
            x_full_batch = np.concatenate((x_full_batch, x_masked[:index]))
            y_full_batch = np.concatenate((y_full_batch, y_masked[:index]))
        bstart += 100
        print(len(attack_set), bstart, len(attack_set) / bstart)

        if len(x_full_batch) >= num_eval_examples or (bstart == 50000):
            break
    #np.save('./out/pytorch_{}.npy'.format(args.sample_size), attack_set)
    #exit()
    average_queries_per_success = 0.0
    total_correctly_classified_ims = 0.0
    success_rate_total = 0.0
    '''
    for i, (images, targets) in enumerate(imagenet_loader):
        
        if i*args.batch_size >= args.sample_size:
            return average_queries_per_success/total_correctly_classified_ims, \
                    success_rate_total/total_correctly_classified_ims
        res = make_adversarial_examples(images.cuda(), targets.cuda(), args)
        # The results can be analyzed here!
        average_queries_per_success += res['success_rate']*res['average_queries']*res['num_correctly_classified']
        success_rate_total += res['success_rate']*res['num_correctly_classified']
        total_correctly_classified_ims += res['num_correctly_classified']
        total_success_ims += res['success_rate']*res['num_correctly_classified']
    '''

    success_indices = []
    total_queries = []

    print("Iterating over {} batches\n".format(num_batches))
    for ibatch in range(num_batches):
        print('attacking {}th batch...'.format(ibatch))
        bstart = ibatch * eval_batch_size
        bend = min(bstart + eval_batch_size, num_eval_examples)
        images = x_full_batch[bstart:bend, :]
        targets = y_full_batch[bstart:bend]
        res = make_adversarial_examples(
            ch.Tensor(images).cuda(),
            ch.Tensor(targets).long().cuda(), args)
        average_queries_per_success += res['success_rate'] * res[
            'average_queries'] * res['num_correctly_classified']
        success_rate_total += res['success_rate'] * res[
            'num_correctly_classified']
        total_correctly_classified_ims += res['num_correctly_classified']

        for i in range(bend - bstart):
            success_indices.append(res['success'][i])
            total_queries.append(res['all_queries'][i])

    targeted = 'targeted' if args.targeted else 'untargeted'
    method = 'nes' if args.nes else 'bandit'

    np.save(
        '/data/home/gaon/lazy-attack/blackbox-attacks-bandits-priors/src/out/reb_queries_{}_{}_{}_{}.npy'
        .format(method, targeted, args.img_index_start,
                args.sample_size), total_queries)
    np.save(
        '/data/home/gaon/lazy-attack/blackbox-attacks-bandits-priors/src/out/reb_indices_{}_{}_{}_{}.npy'
        .format(method, targeted, args.img_index_start,
                args.sample_size), success_indices)
    #np.save('./out/queries_{}_{}_{}_{}_{}_{}_{}_{}_{}.npy'.format(method, targeted, args.img_index_start, args.sample_size, args.image_lr, args.online_lr, args.exploration, args.tile_size, args.fd_eta), total_queries)
    #np.save('./out/indices_{}_{}_{}_{}_{}_{}_{}_{}_{}.npy'.format(method, targeted, args.img_index_start, args.sample_size, args.image_lr, args.online_lr, args.exploration, args.tile_size, args.fd_eta), success_indices)

    return average_queries_per_success/success_rate_total, \
        success_rate_total/total_correctly_classified_ims \
    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))
    def train_z_map(self, config):
        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_file = self.z_map_image[0]
        sample_img = 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)
        sample_img = sample_img.reshape(1, 64, 64, 3).astype('float32')

        vars = tf.trainable_variables()
        z_var = [var for var in vars if 'z_weight' in var.name]
        #print('z var: ', self.sess.run(z_var))
        #raise Exception('hej')

        mae = tf.keras.losses.MeanAbsoluteError(reduction="sum")
        target_image_difference = mae(sample_img[0], self.G)
        regularizer = tf.abs(tf.norm(z_var) - np.sqrt(cfg['z_dim']))
        regularizer = tf.cast(regularizer, dtype=tf.float32)
        z_map_loss = target_image_difference + regularizer
        z_lr = tf.compat.v1.placeholder(tf.float32, None, name='z_lr')
        optimizer = tf.train.AdamOptimizer(learning_rate=z_lr,
                                           name="z_map_optimizer").minimize(
                                               z_map_loss, var_list=z_var)
        tf.global_variables_initializer().run()

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

        num_optimization_steps = 500
        losses = []
        print('START')
        feed = {
            self.view_in: sample_view,
            self.z: sample_z,
            self.inputs: sample_img
        }
        original_img = self.sess.run(self.G, feed_dict=feed)
        original_img = np.clip(255 * original_img, 0, 255).astype(np.uint8)

        losses = []
        lr = 0.001
        for step in range(num_optimization_steps):
            feed_z_map = {
                self.view_in: sample_view,
                self.z: sample_z,
                z_lr: lr
            }
            _, loss = self.sess.run([optimizer, z_map_loss],
                                    feed_dict=feed_z_map)
            print('loss: ', loss)
            losses.append(loss)
            if loss < 500:
                lr = 0.0005
        print()
        feed = {self.view_in: sample_view, self.z: sample_z}
        reconstructed_img = self.sess.run(self.G, feed_dict=feed)
        reconstructed_img = np.clip(255 * reconstructed_img, 0,
                                    255).astype(np.uint8)
        sample_img = np.clip(255 * sample_img, 0, 255).astype(np.uint8)
        start = original_img[0]
        target = sample_img[0]
        result = reconstructed_img[0]
        image = np.concatenate([start, target, result], axis=1)
        print(os.path.join(self.img_dir, "result.jpg"))
        cv2.imwrite(os.path.join(self.sample_dir, "result.jpg"), image)

        plt.plot(losses)
        plt.xlabel('steps')
        plt.ylabel('loss')
        plt.ylim(ymin=0)
        plt.xlim(xmin=0)
        plt.title("Loss per iteration for 50 samples of cars")
        #plt.show()

        if str.lower(str(cfg["sample_from_z"])) == "true":
            self.sample_from_z(config, sample_z)