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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from keras.datasets import mnist
|
| 4 |
+
from keras.utils import np_utils
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
import numpy as np
|
| 7 |
+
from tensorflow.keras import datasets
|
| 8 |
+
import os
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
|
| 11 |
+
|
| 12 |
+
# Adversarial attacks mnist
|
| 13 |
+
def create_pattern_mnist(image, label, model):
|
| 14 |
+
# Define loss function
|
| 15 |
+
loss_function = tf.keras.losses.CategoricalCrossentropy()
|
| 16 |
+
# Reshape image
|
| 17 |
+
image = image.reshape((1,image.shape[0]))
|
| 18 |
+
image = tf.cast(image, tf.float32)
|
| 19 |
+
# Reshape label
|
| 20 |
+
label = label.reshape(((1,label.shape[0])))
|
| 21 |
+
with tf.GradientTape() as tape:
|
| 22 |
+
tape.watch(image)
|
| 23 |
+
prediction = model(image)
|
| 24 |
+
loss = loss_function(label, prediction)
|
| 25 |
+
|
| 26 |
+
# Get the gradients of the loss w.r.t to the input image.
|
| 27 |
+
gradient = tape.gradient(loss, image)
|
| 28 |
+
# Get the sign of the gradients to create the perturbation
|
| 29 |
+
signed_grad = tf.sign(gradient)
|
| 30 |
+
return signed_grad.numpy()
|
| 31 |
+
|
| 32 |
+
def fgsm_mnist(image, label, model, epsilon):
|
| 33 |
+
pattern = create_pattern_mnist(image, label, model)
|
| 34 |
+
adv_x = image + epsilon*pattern
|
| 35 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 36 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 37 |
+
return adv_x.numpy()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def iterative_fgsm_mnist(image, label, model, epsilon, alpha, niter):
|
| 42 |
+
adv_x = image
|
| 43 |
+
for _ in range(niter):
|
| 44 |
+
pattern = create_pattern_mnist(adv_x, label, model)
|
| 45 |
+
adv_x = adv_x + alpha * pattern
|
| 46 |
+
adv_x = tf.clip_by_value(adv_x, image - epsilon, image+epsilon)
|
| 47 |
+
adv_x = adv_x.numpy()
|
| 48 |
+
adv_x = adv_x.reshape(adv_x.shape[1])
|
| 49 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 50 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 51 |
+
return adv_x.numpy()
|
| 52 |
+
|
| 53 |
+
def iterative_least_likely_fgsm_mnist(image, model, epsilon, alpha, niter, nb_classes):
|
| 54 |
+
adv_x = image
|
| 55 |
+
image = image.reshape((1,image.shape[0]))
|
| 56 |
+
label = np_utils.to_categorical(np.argmin(model(image)), nb_classes)
|
| 57 |
+
image = image.reshape(image.shape[1])
|
| 58 |
+
for _ in range(niter):
|
| 59 |
+
pattern = create_pattern_mnist(adv_x, label, model)
|
| 60 |
+
adv_x = adv_x - alpha * pattern
|
| 61 |
+
adv_x = tf.clip_by_value(adv_x, image - epsilon, image+epsilon)
|
| 62 |
+
adv_x = adv_x.numpy()
|
| 63 |
+
adv_x = adv_x.reshape(adv_x.shape[1])
|
| 64 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 65 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 66 |
+
return adv_x.numpy()
|
| 67 |
+
|
| 68 |
+
# Attack functions cifar10
|
| 69 |
+
def create_pattern_cifar10(image, label, model):
|
| 70 |
+
# Define loss function
|
| 71 |
+
loss_function = tf.keras.losses.CategoricalCrossentropy()
|
| 72 |
+
# Reshape image
|
| 73 |
+
image = image.reshape((1,32,32,3))
|
| 74 |
+
image = tf.cast(image, tf.float32)
|
| 75 |
+
# Reshape label
|
| 76 |
+
label = label.reshape(((1,10)))
|
| 77 |
+
with tf.GradientTape() as tape:
|
| 78 |
+
tape.watch(image)
|
| 79 |
+
prediction = model(image)
|
| 80 |
+
loss = loss_function(label, prediction)
|
| 81 |
+
|
| 82 |
+
# Get the gradients of the loss w.r.t to the input image.
|
| 83 |
+
gradient = tape.gradient(loss, image)
|
| 84 |
+
# Get the sign of the gradients to create the perturbation
|
| 85 |
+
signed_grad = tf.sign(gradient)
|
| 86 |
+
return signed_grad.numpy()
|
| 87 |
+
|
| 88 |
+
def fgsm_cifar10(image, label, model, epsilon):
|
| 89 |
+
pattern = create_pattern_cifar10(image, label, model)
|
| 90 |
+
adv_x = image + epsilon*pattern
|
| 91 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 92 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 93 |
+
return adv_x.numpy()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def iterative_fgsm_cifar10(image, label, model, epsilon, alpha, niter):
|
| 98 |
+
adv_x = image
|
| 99 |
+
for _ in range(niter):
|
| 100 |
+
pattern = create_pattern_cifar10(adv_x, label, model)
|
| 101 |
+
adv_x = adv_x + alpha * pattern
|
| 102 |
+
adv_x = tf.clip_by_value(adv_x, image - epsilon, image+epsilon)
|
| 103 |
+
adv_x = adv_x.numpy()
|
| 104 |
+
adv_x = adv_x.reshape((32,32,3))
|
| 105 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 106 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 107 |
+
return adv_x.numpy()
|
| 108 |
+
|
| 109 |
+
def iterative_least_likely_fgsm_cifar10(image, model, epsilon, alpha, niter, nb_classes):
|
| 110 |
+
adv_x = image
|
| 111 |
+
image = image.reshape((1,32,32,3))
|
| 112 |
+
label = np_utils.to_categorical(np.argmin(model(image)), nb_classes)
|
| 113 |
+
image = image.reshape((32,32,3))
|
| 114 |
+
for _ in range(niter):
|
| 115 |
+
pattern = create_pattern_cifar10(adv_x, label, model)
|
| 116 |
+
adv_x = adv_x - alpha * pattern
|
| 117 |
+
adv_x = tf.clip_by_value(adv_x, image - epsilon, image+epsilon)
|
| 118 |
+
adv_x = adv_x.numpy()
|
| 119 |
+
adv_x = adv_x.reshape((32,32,3))
|
| 120 |
+
adv_x = tf.clip_by_value(adv_x, -1, 1)
|
| 121 |
+
adv_x = adv_x * 0.5 + 0.5
|
| 122 |
+
return adv_x.numpy()
|
| 123 |
+
|
| 124 |
+
def fn(dataset, attack):
|
| 125 |
+
epsilon=10/255
|
| 126 |
+
alpha=1
|
| 127 |
+
niter = int(min(4 + epsilon*255, 1.25 * epsilon * 255))
|
| 128 |
+
nb_classes = 10
|
| 129 |
+
classes = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
|
| 130 |
+
|
| 131 |
+
if dataset == "MNIST":
|
| 132 |
+
idx = np.random.randint(0, len(X_test_mnist))
|
| 133 |
+
image1 = X_test_mnist[idx]
|
| 134 |
+
label1 = Y_test_mnist[idx]
|
| 135 |
+
pred1 = np.argmax(label1)
|
| 136 |
+
if attack == "FGSM":
|
| 137 |
+
image2 = fgsm_mnist(image1, label1, model_mnist, epsilon)
|
| 138 |
+
elif attack == "I_FGSM":
|
| 139 |
+
image2 = iterative_fgsm_mnist(image1, label1, model_mnist, epsilon, alpha, niter)
|
| 140 |
+
else:
|
| 141 |
+
image2 = iterative_least_likely_fgsm_mnist(image1, model_mnist, epsilon, alpha, niter, nb_classes)
|
| 142 |
+
|
| 143 |
+
pred2 = np.argmax(model_mnist(image2.reshape((1,784))))
|
| 144 |
+
image1 = image1.reshape((28,28))
|
| 145 |
+
image2 = image2.reshape((28,28))
|
| 146 |
+
else:
|
| 147 |
+
idx = np.random.randint(0, len(X_test_cifar10))
|
| 148 |
+
image1 = X_test_cifar10[idx]
|
| 149 |
+
label1 = Y_test_cifar10[idx]
|
| 150 |
+
pred1 = classes[np.argmax(label1)]
|
| 151 |
+
print(pred1)
|
| 152 |
+
if attack == "FGSM":
|
| 153 |
+
image2 = fgsm_cifar10(image1, label1, model_cifar10, epsilon)
|
| 154 |
+
elif attack == "I_FGSM":
|
| 155 |
+
image2 = iterative_fgsm_cifar10(image1, label1, model_cifar10, epsilon, alpha, niter)
|
| 156 |
+
else:
|
| 157 |
+
image2 = iterative_least_likely_fgsm_cifar10(image1, model_cifar10, epsilon, alpha, niter, nb_classes)
|
| 158 |
+
|
| 159 |
+
pred2 = classes[np.argmax(model_cifar10(image2.reshape((1,32,32,3))))]
|
| 160 |
+
print(pred2)
|
| 161 |
+
image1 = image1.reshape((32,32,3))
|
| 162 |
+
image2 = image2.reshape((32,32,3))
|
| 163 |
+
|
| 164 |
+
return image1, pred1, image2, pred2
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
model_mnist = keras.models.load_model('mnist.h5')
|
| 168 |
+
model_cifar10 = keras.models.load_model('cifar10.h5')
|
| 169 |
+
|
| 170 |
+
# Load MNIST data
|
| 171 |
+
(_, _), (X_test_mnist, Y_test_mnist) = mnist.load_data()
|
| 172 |
+
X_test_mnist = X_test_mnist.astype('float32')
|
| 173 |
+
X_test_mnist = X_test_mnist.reshape(10000, 784)
|
| 174 |
+
X_test_mnist /= 255
|
| 175 |
+
nb_classes = 10
|
| 176 |
+
Y_test_mnist = np_utils.to_categorical(Y_test_mnist, nb_classes)
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
# Load CIFAR10 data
|
| 180 |
+
(_, _), (X_test_cifar10, Y_test_cifar10) = datasets.cifar10.load_data()
|
| 181 |
+
X_test_cifar10 = X_test_cifar10 / 255.0
|
| 182 |
+
Y_test_cifar10 = np_utils.to_categorical(Y_test_cifar10, nb_classes)
|
| 183 |
+
|
| 184 |
+
demo = gr.Interface(
|
| 185 |
+
fn=fn,
|
| 186 |
+
allow_flagging="never",
|
| 187 |
+
title="Adversarial attack demo",
|
| 188 |
+
description="A random image from the chosen dataset will be perturbated with the chosen attack type and both the original image and the perturbated image will be displayed.",
|
| 189 |
+
inputs=[
|
| 190 |
+
gr.Radio(choices=["MNIST", "CIFAR10"], label="Pick a dataset"),
|
| 191 |
+
gr.Radio(choices=["FGSM", "I-FGSM", "I-LL-FGSM"], label="Pick an attack")
|
| 192 |
+
],
|
| 193 |
+
outputs=[
|
| 194 |
+
gr.Image(label="Original Image").style(height=256,width=256),
|
| 195 |
+
gr.Textbox(label="Predicted class"),
|
| 196 |
+
gr.Image(label="Perturbated image").style(height=256,width=256),
|
| 197 |
+
gr.Textbox(label="Predicted class")],
|
| 198 |
+
)
|
| 199 |
+
demo.launch()
|