<jupyter_start><jupyter_text>Data<jupyter_code>cleandata = np.load("./data/clean100data.npy").astype(np.float32)
cleanlabel = np.load("./data/clean100label.npy")
targets = np.load("./data/random_targets.npy")
targets_expanded = np.concatenate([np.zeros((100, 1)), targets], axis=1)<jupyter_output><empty_output><jupyter_text>Tensorflow inception v3<jupyter_code>tf.flags.DEFINE_string(
    'checkpoint_path',
    './data/inception_v3.ckpt', 'Path to checkpoint for inception network.')
FLAGS = tf.flags.FLAGS
tf.app.flags.DEFINE_string('f', '', 'kernel') 
class InceptionModel(Model):
    """Model class for CleverHans library."""

    def __init__(self, nb_classes):
        super(InceptionModel, self).__init__(nb_classes=nb_classes,
                                             needs_dummy_fprop=True)
        self.built = False

    def __call__(self, x_input, return_logits=False):
        """Constructs model and return probabilities for given input."""
        reuse = True if self.built else None
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            x_input = x_input * 2.0 - 1.0
            _, end_points = inception.inception_v3(
                x_input, num_classes=self.nb_classes, is_training=False,
                reuse=reuse)
        self.built = True
        self.logits = end_points['Logits']
        # Strip off the extra reshape op at the output
        self.probs = end_points['Predictions'].op.inputs[0]
        if return_logits:
            return self.logits
        else:
            return self.probs

    def get_logits(self, x_input):
        return self(x_input, return_logits=True)

    def get_probs(self, x_input):
        return self(x_input)
def _top_1_accuracy(logits, labels):
    return tf.reduce_mean(tf.cast(tf.nn.in_top_k(logits[:, 1:], labels, 1), tf.float32))
with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)

    saver = tf.train.Saver(slim.get_model_variables())

    session_creator = tf.train.ChiefSessionCreator(
        scaffold=tf.train.Scaffold(saver=saver),
        checkpoint_filename_with_path=FLAGS.checkpoint_path)

    with tf.train.MonitoredSession(session_creator=session_creator) as sess:
        acc_val = sess.run(acc, feed_dict={x_input: cleandata, y_label: cleanlabel})
        tf.logging.info('Accuracy: %s', acc_val)<jupyter_output>WARNING:tensorflow:From /home/stanislas/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py:1354: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
INFO:tensorflow:Graph was finalized.
WARNING:tensorflow:From /home/stanislas/anaconda3/lib/python3.7/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
Instructions for updating:
Use standard file APIs to check for files with this prefix.
INFO:tensorflow:Restoring parameters from ./data/inception_v3.ckpt
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Accuracy: 1.0<jupyter_text>Generate AEs<jupyter_code>def getabatch(dataset,labelset,numbatch,batchsize):
    databatch = dataset[numbatch*batchsize:(numbatch*batchsize+batchsize)]
    labelbatch = labelset[numbatch*batchsize:(numbatch*batchsize+batchsize)]
    return databatch,labelbatch
def get_random_targets(label):
    targets = label.copy()
    nb_samples = label.shape[0]
    nb_classes = label.shape[1]

    for i in range(nb_samples):
        targets[i, :] = np.roll(targets[i, :], random.randint(1, nb_classes - 1))

    return targets
def one_hot(raw_label):
    nb_samples = len(raw_label)
    nb_classes = 1000
    label = np.zeros((nb_samples, nb_classes))
    for i in range(nb_samples):
        label[i, raw_label[i]] = 1
        
    return label<jupyter_output><empty_output><jupyter_text>FGSM<jupyter_code>eps = 0.01
order = np.inf
with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)
    
    fgsm_params = {'y_target': targets_expanded, 'eps': eps,
                   'ord': order, 'clip_min': 0., 'clip_max': 1.}
    fgsm = attacks.FastGradientMethod(model)
    # adv_fgsm = np.copy(cleandata)
    adv_fgsm = fgsm.generate(x_input, **fgsm_params)

    saver = tf.train.Saver(slim.get_model_variables())

    session_creator = tf.train.ChiefSessionCreator(
        scaffold=tf.train.Scaffold(saver=saver),
        checkpoint_filename_with_path=FLAGS.checkpoint_path)

    with tf.train.MonitoredSession(session_creator=session_creator) as sess:
        adv_image = sess.run(adv_fgsm, feed_dict={x_input: cleandata})
np.save("./AE/FGSM_001_AE_data.npy", adv_image)<jupyter_output><empty_output><jupyter_text>BIM<jupyter_code>eps = 0.01
order = np.inf
with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)
    
    ifgsm_params = {'y_target':targets_expanded, 'eps': eps, 'eps_iter': eps / 10,
                'ord': order, 'clip_min': 0., 'clip_max': 1.}
    ifgsm = attacks.BasicIterativeMethod(model)
    adv = ifgsm.generate(x_input, **ifgsm_params)

    saver = tf.train.Saver(slim.get_model_variables())

    session_creator = tf.train.ChiefSessionCreator(
        scaffold=tf.train.Scaffold(saver=saver),
        checkpoint_filename_with_path=FLAGS.checkpoint_path)

    with tf.train.MonitoredSession(session_creator=session_creator) as sess:
        adv_image = sess.run(adv, feed_dict={x_input: cleandata})
np.save("./AE/IFGSM_001_AE_data.npy", adv_image)<jupyter_output><empty_output><jupyter_text>LBFGS<jupyter_code>with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)
    
    lbfgs_params = {'y_target': targets_expanded, 'batch_size':100,
                'binary_search_steps': 4, 'max_iterations': 1000,
                'clip_min': 0., 'clip_max': 1.}

    saver = tf.train.Saver(slim.get_model_variables())

    session_creator = tf.train.ChiefSessionCreator(
        scaffold=tf.train.Scaffold(saver=saver),
        checkpoint_filename_with_path=FLAGS.checkpoint_path)
    
    # with tf.train.MonitoredSession(session_creator=session_creator) as sess:
    with tf.Session() as sess:
        saver.restore(sess, FLAGS.checkpoint_path)
        lbfgs = attacks.LBFGS(model, sess)
        adv = np.copy(cleandata)
        adv = lbfgs.generate_np(adv, **lbfgs_params)
        # adv_image = sess.run(adv, feed_dict={x_input: cleandata})
np.save("./AE/LBFGS_AE_data.npy",adv)<jupyter_output><empty_output><jupyter_text>CW<jupyter_code>with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)

    saver = tf.train.Saver(slim.get_model_variables())
    
    # with tf.train.MonitoredSession(session_creator=session_creator) as sess:
    with tf.Session() as sess:
        saver.restore(sess, FLAGS.checkpoint_path)
        cw = attacks.CarliniWagnerL2(model, sess)
        # carlini and wagner
        cw_params = {'y_target': targets_expanded,
                     'batch_size': 100,
                     'confidence': 10,
                     'learning_rate': 0.1,
                     'binary_search_steps': 5,
                     'max_iterations': 1000,
                     'abort_early': True,
                     'initial_const': 0.01,
                     'clip_min': 0,
                     'clip_max': 1}
        
        adv = np.copy(cleandata)
        adv = cw.generate_np(adv, **cw_params)
np.save("./AE/CW_AE_data.npy",adv)<jupyter_output><empty_output><jupyter_text>Deepfool<jupyter_code>with tf.Graph().as_default():
    x_input = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
    y_label = tf.placeholder(tf.int32, shape=(None,))
    model = InceptionModel(nb_classes=1001)
    logits = model.get_logits(x_input)
    acc = _top_1_accuracy(logits, y_label)

    saver = tf.train.Saver(slim.get_model_variables())
    
    # with tf.train.MonitoredSession(session_creator=session_creator) as sess:
    with tf.Session() as sess:
        saver.restore(sess, FLAGS.checkpoint_path)
        deepfool_params = {'nb_candidate': 10, 'overshoot': 0.02, 'clip_min': 0., 'clip_max': 1.}
        deepfool = attacks.DeepFool(model, sess)
        totalbatch = 10
        batchsize = 10

        adv = np.copy(cleandata)
        
        for numbatch in range(totalbatch):
            adv_df_batch,labelbatch = getabatch(adv,cleanlabel,numbatch,batchsize)
            adv_df_batch = deepfool.generate_np(adv_df_batch, **deepfool_params)
            adv[numbatch*batchsize:(numbatch*batchsize+batchsize)] = adv_df_batch
np.save("./AE/Deepfool_AE_data.npy", adv)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12241343986887933, 'fraction_numerical': 0.021307109198934644, 'mean_word_length': 3.642415596766524, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '94406', 'license': 'MIT', 'n_tokens_mistral': 3634, 'n_tokens_neox': 3385, 'n_words': 556, 'path': 'remove_code/sotas/Advanced-Gradient-Obfuscating-master/AE_generation.ipynb', 'repo_name': 'JohnZhang000/adaptive-jpeg-compression', 'size': '9762'}