-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfast_gradient_sign.py
40 lines (27 loc) · 1.6 KB
/
fast_gradient_sign.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright (C) 2017, Nicholas Carlini <[email protected]>
# All rights reserved.
import tensorflow as tf
import numpy as np
class FGS:
def __init__(self, sess, model, batch_size=100, eps=0.2):
self.sess = sess
self.model = model
self.delta = tf.placeholder(tf.float32, (batch_size,model.image_size,model.image_size,model.num_channels))
self.img = tf.placeholder(tf.float32, (batch_size,model.image_size,model.image_size,model.num_channels))
self.lab = tf.placeholder(tf.float32, (batch_size,model.num_labels))
self.out = model.predict(tf.clip_by_value(self.img+self.delta, -0.5, 0.5))
self.loss = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=self.out,
labels=self.lab))
self.grads = tf.gradients(self.loss,[self.delta])[0]
self.batch_size = batch_size
self.eps = eps
def attack(self, imgs, labs):
r = []
for offset in range(0,len(imgs),self.batch_size):
batch_imgs,batch_labs = imgs[offset:offset+self.batch_size],labs[offset:offset+self.batch_size]
directions = np.sign(self.sess.run(self.grads, feed_dict={self.img: batch_imgs,
self.lab: batch_labs,
self.delta: np.zeros(batch_imgs.shape)}))
it = np.clip(batch_imgs+directions*self.eps, -.5, .5)
r.extend(it)
return np.array(r)