-
Notifications
You must be signed in to change notification settings - Fork 0
/
AttackModel.py
executable file
·215 lines (165 loc) · 9.9 KB
/
AttackModel.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Run this to attack a trained model via TrainModel.
Use the "loadFullModel" submethod to load in an already trained model (trained via TrainModel)
The main attack function is "runAttacks" which runs attacks on trained models
"""
import warnings
warnings.filterwarnings("ignore")
import os
import tensorflow as tf
#tf.compat.v1.enable_eager_execution
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from cleverhans.attacks import Noise, CarliniWagnerL2, MaxConfidence, FastGradientMethod, BasicIterativeMethod, DeepFool, MomentumIterativeMethod, ProjectedGradientDescent
from Model_Implementations import Model_Softmax_Baseline, Model_Logistic_Baseline, Model_Logistic_Ensemble, Model_Tanh_Ensemble, Model_Tanh_Baseline
from tensorflow.keras.datasets import mnist, cifar10
from tensorflow.keras import backend
import numpy as np
import scipy.linalg
from scipy import stats
import matplotlib.pyplot as plt
model_path = 'models/' #path with saved model parameters
sess = backend.get_session()
backend.set_learning_phase(0) #need to do this to get CleverHans to work with batchnorm
#Dataset-specific parameters - should be same as those used in TrainModel
DATA_DESC = 'CIFAR10'; (X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
epochs=None; weight_save_freq=None
num_classes=10 #how many classes (categories) are in this dataset?
Y_train = np.squeeze(Y_train); Y_test = np.squeeze(Y_test)
num_filters_std = [32, 64, 128]; num_filters_ens=[32, 64, 128]; num_filters_ens_2=16; dropout_rate_std=0.0; dropout_rate_ens=0.0; weight_decay = 0
model_rep_baseline=2; model_rep_ens=2; DATA_AUGMENTATION_FLAG=1; BATCH_NORMALIZATION_FLAG=1
num_channels = 3; inp_shape = (32,32,3); lr=1e-4; batch_size=80;
noise_stddev = 0.032; blend_factor = .032
#MNIST
# DATA_DESC = 'MNIST'; (X_train, Y_train), (X_test, Y_test) = mnist.load_data()
# Y_train = np.squeeze(Y_train); Y_test = np.squeeze(Y_test)
# num_channels = 1; inp_shape = (28,28,1); num_classes=10
# #MODEL-SPECIFIC PARAMETERS: MNIST
# #PARAMETERS RELATED TO SGD OPTIMIZATION
# epochs=None; weight_save_freq=None; batch_size=80; lr=3e-4;
# #MODEL DEFINTION PARAMETERS
# num_filters_std = [64, 64, 64]; num_filters_ens=[32, 32, 32]; num_filters_ens_2=4;
# dropout_rate_std=0.0; dropout_rate_ens=0.0; weight_decay = 0
# noise_stddev = 0.3; blend_factor=0.3;
# model_rep_baseline=1; model_rep_ens=2;
# DATA_AUGMENTATION_FLAG=0; BATCH_NORMALIZATION_FLAG=0
#Attack parameters
eps_val = 8/255.0; PGD_iters = 200; eps_iter=(2/3)*eps_val;
eps_range = np.linspace(0, 0.33, 10)
noise_eps=0.1
# DATA PRE-PROCESSING
X_train = (X_train/255).astype(np.float32); X_test = (X_test/255).astype(np.float32)
#reshape (add third (image) channel)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2],num_channels); X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2],num_channels)
X_valid = X_test[1000:2000]; Y_valid = Y_test[1000:2000]; #validation data, used to attack model
X_train = X_train-0.5; X_test = X_test-0.5; X_valid = X_valid-0.5; #map to range (-0.5,0.5)
data_dict = {'X_train':X_train, 'Y_train_cat':Y_train, 'X_test':X_test, 'Y_test_cat':Y_test}
X_random = np.random.rand(X_valid.shape[0],X_valid.shape[1],X_valid.shape[2],X_valid.shape[3])-0.5; X_random = X_random.astype(np.float32)
# def output_activation(x):
# return tf.nn.softmax(x)
#Model definition of the model we want to attack; should be same as the definition used in TrainModel
name = 'tanh_32_diverse'+'_'+DATA_DESC; seed = 59; code_length=32; num_codes=code_length; num_chunks=4; base_model=None;
def output_activation(x):
return tf.nn.tanh(x)
M = scipy.linalg.hadamard(code_length).astype(np.float32)
M[np.arange(0, num_codes,2), 0]= -1#replace first col, which for scipy's Hadamard construction is always 1, hence not a useful classifier; this change still ensures all codewords have dot product <=0; since our decoder ignores negative correlations anyway, this has no net effect on probability estimation
np.random.seed(seed)
np.random.shuffle(M)
idx=np.random.permutation(code_length)
M = M[0:num_codes, idx[0:code_length]]
params_dict = {'BATCH_NORMALIZATION_FLAG':BATCH_NORMALIZATION_FLAG, 'DATA_AUGMENTATION_FLAG':DATA_AUGMENTATION_FLAG, 'M':M, 'base_model':base_model, 'num_chunks':num_chunks, 'model_rep': model_rep_ens, 'output_activation':output_activation, 'num_filters_ens':num_filters_ens, 'num_filters_ens_2':num_filters_ens_2,'batch_size':batch_size, 'epochs':epochs, 'dropout_rate':dropout_rate_ens, 'lr':lr, 'blend_factor':blend_factor, 'inp_shape':inp_shape, 'noise_stddev':noise_stddev, 'weight_save_freq':weight_save_freq, 'name':name, 'model_path':model_path}
m4 = Model_Tanh_Ensemble(data_dict, params_dict)
m4.loadFullModel() #load in the saved model, which should have already been trained first via TrainModel
m4.legend = 'TEns16';
m4.X_valid = X_valid; m4.Y_valid = Y_valid;
m4.X_test = X_test; m4.Y_test = Y_test;
m4.X_random = X_random;
m4.minval = -0.5; m4.maxval = 0.5
def benignAccuracy(model, X, Y):
acc_vec=[]; probs_benign_list=[]
for rep in np.arange(0, X.shape[0], 1000):
x = X[rep:rep+1000]
probs_benign = sess.run(model.predict(tf.convert_to_tensor(x)))
acc= np.mean(np.argmax(probs_benign, 1)==Y[rep:rep+1000])
acc_vec += [acc]
probs_benign_list += list(np.max(probs_benign, 1))
acc = np.mean(acc_vec)
print("Accuracy for model " + model.params_dict['name'] + " : ", acc)
return probs_benign_list
def wbAttack(model, attack, att_params, X, Y):
sess = backend.get_session()
modelCH = model.modelCH()
adv_model = attack(modelCH, sess=sess)
acc_vec=[]; probs_adv_list=[]
inc=250
for rep in np.arange(0, X.shape[0], inc):
x = X[rep:rep+inc]
y = Y[rep:rep+inc]
X_adv = adv_model.generate(tf.convert_to_tensor(x), **att_params).eval(session=sess)
preds = np.argmax(sess.run(model.predict(tf.convert_to_tensor(X_adv))), 1)
acc = np.mean(np.equal(preds, y))
probs_adv = np.max(sess.run(model.predict(tf.convert_to_tensor(X_adv))), 1)
probs_adv = probs_adv[preds != y]
acc= np.mean(np.equal(preds, y))
acc_vec += [acc]
probs_adv_list += list(probs_adv)
acc = np.mean(acc_vec)
print("Adv accuracy for model " + model.params_dict['name'] + " : ", acc)
return probs_adv_list, acc, X_adv
def runAttacks(models_list):
#CW attack
for model in models_list:
print(""); print(""); print("");
print("Running tests on model: ", model.params_dict['name'])
print("Clean accuracy of model:")
probs_benign = benignAccuracy(model, model.X_test, model.Y_test)
print("")
print("Running PGD attack:")
att_params = {'clip_min': model.minval, 'clip_max':model.maxval, 'eps':eps_val, 'eps_iter':eps_iter, 'nb_iter':PGD_iters,'ord':np.inf}
probs_adv_pgd, junk, X_adv = wbAttack(model, ProjectedGradientDescent, att_params, model.X_valid, model.Y_valid)
print("")
print("Running CW attack:")
att_params = {'clip_min': model.minval, 'clip_max':model.maxval, 'binary_search_steps':10, 'learning_rate':1e-3}
probs_adv_cw, junk, X_adv = wbAttack(model, CarliniWagnerL2, att_params, model.X_valid[0:100], model.Y_valid[0:100])
print("")
print("Running Blind Spot attack, alpha=0.8:")
att_params = {'clip_min': model.minval, 'clip_max':model.maxval, 'binary_search_steps':10, 'learning_rate':1e-3}
probs_adv_bsa, junk, X_adv = wbAttack(model, CarliniWagnerL2, att_params, 0.8*model.X_valid[0:100], model.Y_valid[0:100])
print("")
#Random ATTACK (0 SNR inputs)
print("Running random attack:")
probs_random = np.max(sess.run(model.predict(tf.convert_to_tensor(model.X_random))), 1)
print('Prob. that ', model.params_dict['name'], ' < 0.9 on random data: ', np.mean(probs_random<0.9))
#Noise ATTACK (low SNR inputs)
print("Running Noise attack:")
att_params = {'clip_min': model.minval, 'clip_max':model.maxval, 'eps':noise_eps}
probs_noise, junk, X_adv = wbAttack(model, Noise, att_params, model.X_valid, model.Y_valid)
print("")
return probs_benign, probs_adv_pgd, probs_adv_cw, probs_adv_bsa, probs_random, probs_noise
models_list = [m4]
probs_benign, probs_adv_pgd, probs_adv_cw, probs_adv_bsa, probs_random, probs_noise = runAttacks(models_list)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_probs_benign.npy', probs_benign)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_probs_adv_pgd.npy', probs_adv_pgd)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_probs_adv_cw.npy', probs_adv_cw)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_adv_bsa.npy', probs_adv_bsa)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_probs_random.npy', probs_random)
np.save('plots/CIFAR/tanh_32_diverse_CIFAR/tanh_32_diverse_probs_noise.npy', probs_noise)
plt.figure(1)
kernel = stats.gaussian_kde(probs_benign, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)
plt.figure(2)
kernel = stats.gaussian_kde(probs_adv_pgd, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)
plt.figure(3)
kernel = stats.gaussian_kde(probs_adv_cw, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)
plt.figure(4)
kernel = stats.gaussian_kde(probs_adv_bsa, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)
plt.figure(5)
kernel = stats.gaussian_kde(probs_random, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)
plt.figure(6)
kernel = stats.gaussian_kde(probs_noise, bw_method=0.5)
plt.plot(np.arange(0, 1, .01), kernel.pdf(np.arange(0, 1, .01)), linewidth=4)