-
Notifications
You must be signed in to change notification settings - Fork 38
/
extract_characteristics.py
417 lines (372 loc) · 17.5 KB
/
extract_characteristics.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from __future__ import absolute_import
from __future__ import print_function
import os
import argparse
import warnings
import numpy as np
from sklearn.neighbors import KernelDensity
from keras.models import load_model
from util import (get_data, get_noisy_samples, get_mc_predictions,
get_deep_representations, score_samples, normalize,
get_lids_random_batch, get_kmeans_random_batch)
# In the original paper, the author used optimal KDE bandwidths dataset-wise
# that were determined from CV tuning
BANDWIDTHS = {'mnist': 3.7926, 'cifar': 0.26, 'svhn': 1.00}
# Here we further tune bandwidth for each of the 10 classes in mnist, cifar and svhn
# Run tune_kernal_density.py to get the following settings.
# BANDWIDTHS = {'mnist': [0.2637, 0.1274, 0.2637, 0.2637, 0.2637, 0.2637, 0.2637, 0.2069, 0.3360, 0.2637],
# 'cifar': [0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000],
# 'svhn': [0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1000, 0.1274, 0.1000, 0.1000]}
PATH_DATA = "data/"
PATH_IMAGES = "plots/"
def merge_and_generate_labels(X_pos, X_neg):
"""
merge positve and nagative artifact and generate labels
:param X_pos: positive samples
:param X_neg: negative samples
:return: X: merged samples, 2D ndarray
y: generated labels (0/1): 2D ndarray same size as X
"""
X_pos = np.asarray(X_pos, dtype=np.float32)
print("X_pos: ", X_pos.shape)
X_pos = X_pos.reshape((X_pos.shape[0], -1))
X_neg = np.asarray(X_neg, dtype=np.float32)
print("X_neg: ", X_neg.shape)
X_neg = X_neg.reshape((X_neg.shape[0], -1))
X = np.concatenate((X_pos, X_neg))
y = np.concatenate((np.ones(X_pos.shape[0]), np.zeros(X_neg.shape[0])))
y = y.reshape((X.shape[0], 1))
return X, y
def get_kd(model, X_train, Y_train, X_test, X_test_noisy, X_test_adv):
"""
Get kernel density scores
:param model:
:param X_train:
:param Y_train:
:param X_test:
:param X_test_noisy:
:param X_test_adv:
:return: artifacts: positive and negative examples with kd values,
labels: adversarial (label: 1) and normal/noisy (label: 0) examples
"""
# Get deep feature representations
print('Getting deep feature representations...')
X_train_features = get_deep_representations(model, X_train,
batch_size=args.batch_size)
X_test_normal_features = get_deep_representations(model, X_test,
batch_size=args.batch_size)
X_test_noisy_features = get_deep_representations(model, X_test_noisy,
batch_size=args.batch_size)
X_test_adv_features = get_deep_representations(model, X_test_adv,
batch_size=args.batch_size)
# Train one KDE per class
print('Training KDEs...')
class_inds = {}
for i in range(Y_train.shape[1]):
class_inds[i] = np.where(Y_train.argmax(axis=1) == i)[0]
kdes = {}
warnings.warn("Using pre-set kernel bandwidths that were determined "
"optimal for the specific CNN models of the paper. If you've "
"changed your model, you'll need to re-optimize the "
"bandwidth.")
print('bandwidth %.4f for %s' % (BANDWIDTHS[args.dataset], args.dataset))
for i in range(Y_train.shape[1]):
kdes[i] = KernelDensity(kernel='gaussian',
bandwidth=BANDWIDTHS[args.dataset]) \
.fit(X_train_features[class_inds[i]])
# Get model predictions
print('Computing model predictions...')
preds_test_normal = model.predict_classes(X_test, verbose=0,
batch_size=args.batch_size)
preds_test_noisy = model.predict_classes(X_test_noisy, verbose=0,
batch_size=args.batch_size)
preds_test_adv = model.predict_classes(X_test_adv, verbose=0,
batch_size=args.batch_size)
# Get density estimates
print('computing densities...')
densities_normal = score_samples(
kdes,
X_test_normal_features,
preds_test_normal
)
densities_noisy = score_samples(
kdes,
X_test_noisy_features,
preds_test_noisy
)
densities_adv = score_samples(
kdes,
X_test_adv_features,
preds_test_adv
)
print("densities_normal:", densities_normal.shape)
print("densities_adv:", densities_adv.shape)
print("densities_noisy:", densities_noisy.shape)
## skip the normalization, you may want to try different normalizations later
## so at this step, just save the raw values
# densities_normal_z, densities_adv_z, densities_noisy_z = normalize(
# densities_normal,
# densities_adv,
# densities_noisy
# )
densities_pos = densities_adv
densities_neg = np.concatenate((densities_normal, densities_noisy))
artifacts, labels = merge_and_generate_labels(densities_pos, densities_neg)
return artifacts, labels
def get_bu(model, X_test, X_test_noisy, X_test_adv):
"""
Get Bayesian uncertainty scores
:param model:
:param X_train:
:param Y_train:
:param X_test:
:param X_test_noisy:
:param X_test_adv:
:return: artifacts: positive and negative examples with bu values,
labels: adversarial (label: 1) and normal/noisy (label: 0) examples
"""
print('Getting Monte Carlo dropout variance predictions...')
uncerts_normal = get_mc_predictions(model, X_test,
batch_size=args.batch_size) \
.var(axis=0).mean(axis=1)
uncerts_noisy = get_mc_predictions(model, X_test_noisy,
batch_size=args.batch_size) \
.var(axis=0).mean(axis=1)
uncerts_adv = get_mc_predictions(model, X_test_adv,
batch_size=args.batch_size) \
.var(axis=0).mean(axis=1)
print("uncerts_normal:", uncerts_normal.shape)
print("uncerts_noisy:", uncerts_noisy.shape)
print("uncerts_adv:", uncerts_adv.shape)
## skip the normalization, you may want to try different normalizations later
## so at this step, just save the raw values
# uncerts_normal_z, uncerts_adv_z, uncerts_noisy_z = normalize(
# uncerts_normal,
# uncerts_adv,
# uncerts_noisy
# )
uncerts_pos = uncerts_adv
uncerts_neg = np.concatenate((uncerts_normal, uncerts_noisy))
artifacts, labels = merge_and_generate_labels(uncerts_pos, uncerts_neg)
return artifacts, labels
def get_lid(model, X_test, X_test_noisy, X_test_adv, k=10, batch_size=100, dataset='mnist'):
"""
Get local intrinsic dimensionality
:param model:
:param X_train:
:param Y_train:
:param X_test:
:param X_test_noisy:
:param X_test_adv:
:return: artifacts: positive and negative examples with lid values,
labels: adversarial (label: 1) and normal/noisy (label: 0) examples
"""
print('Extract local intrinsic dimensionality: k = %s' % k)
lids_normal, lids_noisy, lids_adv = get_lids_random_batch(model, X_test, X_test_noisy,
X_test_adv, dataset, k, batch_size)
print("lids_normal:", lids_normal.shape)
print("lids_noisy:", lids_noisy.shape)
print("lids_adv:", lids_adv.shape)
## skip the normalization, you may want to try different normalizations later
## so at this step, just save the raw values
# lids_normal_z, lids_adv_z, lids_noisy_z = normalize(
# lids_normal,
# lids_adv,
# lids_noisy
# )
lids_pos = lids_adv
lids_neg = np.concatenate((lids_normal, lids_noisy))
artifacts, labels = merge_and_generate_labels(lids_pos, lids_neg)
return artifacts, labels
def get_kmeans(model, X_test, X_test_noisy, X_test_adv, k=10, batch_size=100, dataset='mnist'):
"""
Calculate the average distance to k nearest neighbours as a feature.
This is used to compare density vs LID. Why density doesn't work?
:param model:
:param X_train:
:param Y_train:
:param X_test:
:param X_test_noisy:
:param X_test_adv:
:return: artifacts: positive and negative examples with lid values,
labels: adversarial (label: 1) and normal/noisy (label: 0) examples
"""
print('Extract k means feature: k = %s' % k)
kms_normal, kms_noisy, kms_adv = get_kmeans_random_batch(model, X_test, X_test_noisy,
X_test_adv, dataset, k, batch_size,
pca=True)
print("kms_normal:", kms_normal.shape)
print("kms_noisy:", kms_noisy.shape)
print("kms_adv:", kms_adv.shape)
## skip the normalization, you may want to try different normalizations later
## so at this step, just save the raw values
# kms_normal_z, kms_noisy_z, kms_adv_z = normalize(
# kms_normal,
# kms_noisy,
# kms_adv
# )
kms_pos = kms_adv
kms_neg = np.concatenate((kms_normal, kms_noisy))
artifacts, labels = merge_and_generate_labels(kms_pos, kms_neg)
return artifacts, labels
def main(args):
assert args.dataset in ['mnist', 'cifar', 'svhn'], \
"Dataset parameter must be either 'mnist', 'cifar' or 'svhn'"
assert args.attack in ['fgsm', 'bim-a', 'bim-b', 'jsma', 'cw-l2', 'all'], \
"Attack parameter must be either 'fgsm', 'bim-a', 'bim-b', " \
"'jsma' or 'cw-l2'"
assert args.characteristic in ['kd', 'bu', 'lid', 'km', 'all'], \
"Characteristic(s) to use 'kd', 'bu', 'lid', 'km', 'all'"
model_file = os.path.join(PATH_DATA, "model_%s.h5" % args.dataset)
assert os.path.isfile(model_file), \
'model file not found... must first train model using train_model.py.'
adv_file = os.path.join(PATH_DATA, "Adv_%s_%s.npy" % (args.dataset, args.attack))
assert os.path.isfile(adv_file), \
'adversarial sample file not found... must first craft adversarial ' \
'samples using craft_adv_samples.py'
print('Loading the data and model...')
# Load the model
model = load_model(model_file)
# Load the dataset
X_train, Y_train, X_test, Y_test = get_data(args.dataset)
# Check attack type, select adversarial and noisy samples accordingly
print('Loading noisy and adversarial samples...')
if args.attack == 'all':
# TODO: implement 'all' option
# X_test_adv = ...
# X_test_noisy = ...
raise NotImplementedError("'All' types detector not yet implemented.")
else:
# Load adversarial samples
X_test_adv = np.load(adv_file)
print("X_test_adv: ", X_test_adv.shape)
# as there are some parameters to tune for noisy example, so put the generation
# step here instead of the adversarial step which can take many hours
noisy_file = os.path.join(PATH_DATA, 'Noisy_%s_%s.npy' % (args.dataset, args.attack))
if os.path.isfile(noisy_file):
X_test_noisy = np.load(noisy_file)
else:
# Craft an equal number of noisy samples
print('Crafting %s noisy samples. ' % args.dataset)
X_test_noisy = get_noisy_samples(X_test, X_test_adv, args.dataset, args.attack)
np.save(noisy_file, X_test_noisy)
# Check model accuracies on each sample type
for s_type, dataset in zip(['normal', 'noisy', 'adversarial'],
[X_test, X_test_noisy, X_test_adv]):
_, acc = model.evaluate(dataset, Y_test, batch_size=args.batch_size,
verbose=0)
print("Model accuracy on the %s test set: %0.2f%%" %
(s_type, 100 * acc))
# Compute and display average perturbation sizes
if not s_type == 'normal':
l2_diff = np.linalg.norm(
dataset.reshape((len(X_test), -1)) -
X_test.reshape((len(X_test), -1)),
axis=1
).mean()
print("Average L-2 perturbation size of the %s test set: %0.2f" %
(s_type, l2_diff))
# Refine the normal, noisy and adversarial sets to only include samples for
# which the original version was correctly classified by the model
preds_test = model.predict_classes(X_test, verbose=0,
batch_size=args.batch_size)
inds_correct = np.where(preds_test == Y_test.argmax(axis=1))[0]
print("Number of correctly predict images: %s" % (len(inds_correct)))
X_test = X_test[inds_correct]
X_test_noisy = X_test_noisy[inds_correct]
X_test_adv = X_test_adv[inds_correct]
print("X_test: ", X_test.shape)
print("X_test_noisy: ", X_test_noisy.shape)
print("X_test_adv: ", X_test_adv.shape)
if args.characteristic == 'kd':
# extract kernel density
characteristics, labels = get_kd(model, X_train, Y_train, X_test, X_test_noisy, X_test_adv)
print("KD: [characteristic shape: ", characteristics.shape, ", label shape: ", labels.shape)
# save to file
bandwidth = BANDWIDTHS[args.dataset]
file_name = os.path.join(PATH_DATA, 'kd_%s_%s_%.4f.npy' % (args.dataset, args.attack, bandwidth))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
elif args.characteristic == 'bu':
# extract Bayesian uncertainty
characteristics, labels = get_bu(model, X_test, X_test_noisy, X_test_adv)
print("BU: [characteristic shape: ", characteristics.shape, ", label shape: ", labels.shape)
# save to file
file_name = os.path.join(PATH_DATA, 'bu_%s_%s.npy' % (args.dataset, args.attack))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
elif args.characteristic == 'lid':
# extract local intrinsic dimensionality
characteristics, labels = get_lid(model, X_test, X_test_noisy, X_test_adv,
args.k_nearest, args.batch_size, args.dataset)
print("LID: [characteristic shape: ", characteristics.shape, ", label shape: ", labels.shape)
# save to file
# file_name = os.path.join(PATH_DATA, 'lid_%s_%s.npy' % (args.dataset, args.attack))
file_name = os.path.join('../data_grid_search/lid_large_batch/', 'lid_%s_%s_%s.npy' %
(args.dataset, args.attack, args.k_nearest))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
elif args.characteristic == 'km':
# extract k means distance
characteristics, labels = get_kmeans(model, X_test, X_test_noisy, X_test_adv,
args.k_nearest, args.batch_size, args.dataset)
print("K-Mean: [characteristic shape: ", characteristics.shape, ", label shape: ", labels.shape)
# save to file
file_name = os.path.join(PATH_DATA, 'km_pca_%s_%s.npy' % (args.dataset, args.attack))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
elif args.characteristic == 'all':
# extract kernel density
characteristics, labels = get_kd(model, X_train, Y_train, X_test, X_test_noisy, X_test_adv)
file_name = os.path.join(PATH_DATA, 'kd_%s_%s.npy' % (args.dataset, args.attack))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
# extract Bayesian uncertainty
characteristics, labels = get_bu(model, X_test, X_test_noisy, X_test_adv)
file_name = os.path.join(PATH_DATA, 'bu_%s_%s.npy' % (args.dataset, args.attack))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
# extract local intrinsic dimensionality
characteristics, labels = get_lid(model, X_test, X_test_noisy, X_test_adv,
args.k_nearest, args.batch_size, args.dataset)
file_name = os.path.join(PATH_DATA, 'lid_%s_%s.npy' % (args.dataset, args.attack))
data = np.concatenate((characteristics, labels), axis=1)
np.save(file_name, data)
# extract k means distance
# artifcharacteristics, labels = get_kmeans(model, X_test, X_test_noisy, X_test_adv,
# args.k_nearest, args.batch_size, args.dataset)
# file_name = os.path.join(PATH_DATA, 'km_%s_%s.npy' % (args.dataset, args.attack))
# data = np.concatenate((characteristics, labels), axis=1)
# np.save(file_name, data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'-d', '--dataset',
help="Dataset to use; either 'mnist', 'cifar' or 'svhn'",
required=True, type=str
)
parser.add_argument(
'-a', '--attack',
help="Attack to use; either 'fgsm', 'jsma', 'bim-b', 'jsma', 'cw-l2' "
"or 'all'",
required=True, type=str
)
parser.add_argument(
'-r', '--characteristic',
help="Characteristic(s) to use 'kd', 'bu', 'lid' 'km' or 'all'",
required=True, type=str
)
parser.add_argument(
'-k', '--k_nearest',
help="The number of nearest neighbours to use; either 10, 20, 100 ",
required=False, type=int
)
parser.add_argument(
'-b', '--batch_size',
help="The batch size to use for training.",
required=False, type=int
)
parser.set_defaults(batch_size=100)
parser.set_defaults(k_nearest=20)
args = parser.parse_args()
main(args)