os.environ["CUDA_VISIBLE_DEVICES"] = gpu_device if discretize: codes = np.load(codes_path) if __name__ == '__main__': mnist = input_data.read_data_sets('MNIST_data', one_hot=False) with tf.Session() as sess: model = Model() saver = tf.train.Saver() checkpoint = tf.train.latest_checkpoint(model_dir) saver.restore(sess, checkpoint) if discretize: attack = CWAttack(model, attack_steps, step_size, epsilon, codes, eval_batch_size, alpha) else: attack = LinfPGDAttack(model, epsilon, attack_steps, step_size, random_start, loss_func) # Iterate over the samples batch-by-batch num_batches = int(math.ceil(num_eval_examples / eval_batch_size)) total_corr_nat = 0 total_corr_adv = 0 rate = [] for ibatch in range(num_batches): bstart = ibatch * eval_batch_size bend = min(bstart + eval_batch_size, num_eval_examples) x_batch = mnist.test.images[bstart:bend, :]
with tf.Session(config=tf_config) as sess: # Restore the checkpoint saver.restore(sess, model_path) # Iterate over the samples batch-by-batch num_batches = int(math.ceil(num_eval_examples / eval_batch_size)) total_xent_nat = 0. total_xent_adv = 0. total_corr_nat = 0 total_corr_adv = 0 if discretize: attack = CWAttack(model, num_steps=attack_steps, step_size=step_size, epsilon=epsilon, codes=codes, batch_size=eval_batch_size, alpha=alpha) else: attack = LinfPGDAttack(model, epsilon=epsilon, num_steps=attack_steps, step_size=step_size, random_start=random_start, loss_func=loss_func) for ibatch in range(num_batches): bstart = ibatch * eval_batch_size bend = min(bstart + eval_batch_size, num_eval_examples)