-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain_models.py
executable file
·194 lines (165 loc) · 9.28 KB
/
train_models.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
## train_models.py -- train the neural network models for attacking
##
## Copyright (C) 2017, Yash Sharma <ysharma1126@gmail.com>.
## Copyright (C) 2016, Nicholas Carlini <nicholas@carlini.com>.
##
## This program is licenced under the BSD 2-Clause licence,
## contained in the LICENCE file in this directory.
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD
import tensorflow as tf
from setup_mnist import MNIST
from setup_cifar import CIFAR
import os
def train(data, file_name, params, num_epochs=50, batch_size=128, train_temp=1, init=None, adversarial=False, examples=None, labels=None):
"""
Standard neural network training procedure.
"""
if adversarial:
data.train_data = np.concatenate((data.train_data, examples), axis=0)
data.train_labels = np.concatenate((data.train_labels, labels), axis=0)
model = Sequential()
model.add(Conv2D(params[0], (3, 3),
input_shape=data.train_data.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(params[1], (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(params[2], (3, 3)))
model.add(Activation('relu'))
model.add(Conv2D(params[3], (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(params[4]))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(params[5]))
model.add(Activation('relu'))
model.add(Dense(10))
if init != None:
model.load_weights(init)
def fn(correct, predicted):
return tf.nn.softmax_cross_entropy_with_logits(labels=correct,
logits=predicted/train_temp)
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss=fn,
optimizer=sgd,
metrics=['accuracy'])
model.fit(data.train_data, data.train_labels,
batch_size=batch_size,
validation_data=(data.validation_data, data.validation_labels),
nb_epoch=num_epochs,
shuffle=True, verbose=1)
if file_name != None:
model.save(file_name)
return model
def train_distillation(data, file_name, params, num_epochs=50, batch_size=128, train_temp=1):
"""
Train a network using defensive distillation.
Distillation as a Defense to Adversarial Perturbations against Deep Neural Networks
Nicolas Papernot, Patrick McDaniel, Xi Wu, Somesh Jha, Ananthram Swami
IEEE S&P, 2016.
"""
if not os.path.exists(file_name+"_init"):
# Train for one epoch to get a good starting point.
train(data, file_name+"_init", params, 1, batch_size)
# now train the teacher at the given temperature
teacher = train(data, file_name+"_teacher", params, num_epochs, batch_size, train_temp,
init=file_name+"_init")
# evaluate the labels at temperature t
predicted = teacher.predict(data.train_data)
with tf.Session() as sess:
y = sess.run(tf.nn.softmax(predicted/train_temp))
#print(y)
data.train_labels = y
# train the student model at temperature t
student = train(data, file_name, params, num_epochs, batch_size, train_temp,
init=file_name+"_init")
# and finally we predict at temperature 1
predicted = teacher.predict(data.train_data)
#print(predicted)
def main(args):
if not os.path.isdir('models'):
os.makedirs('models')
if(not (args['adversarial'] or args['defensive'])):
if(args['dataset'] == "mnist" or args['dataset'] == "all"):
train(MNIST(), "models/mnist", [32, 32, 64, 64, 200, 200], num_epochs=50)
if(args['dataset'] == 'cifar' or args['dataset'] == 'all'):
train(CIFAR(), "models/cifar", [64, 64, 128, 128, 256, 256], num_epochs=50)
if (args['adversarial']):
if(args['dataset'] == "mnist" or args['dataset'] == "all"):
if(args['attack'] == "L2" or args['attack'] == "L2L1" or args['attack'] == "L2EN" or args['attack'] == "all"):
cwl2 = np.load('train/mnist_L2_train.npy')
if(args['attack'] == "L1" or args['attack'] == "L2L1" or args['attack'] == "all"):
cwl1 = np.load('train/mnist_L1_train.npy')
if(args['attack'] == "EN" or args['attack'] == "L2EN" or args['attack'] == "all"):
cwen = np.load('train/mnist_EN_train.npy')
labels = np.load('train/mnist_labels_train.npy')
if(args['attack'] == "L2L1" or args['attack'] == "all"):
cwl2l1 = np.concatenate((cwl2,cwl1), axis=0)
if(args['attack'] == "L2EN" or args['attack'] == "all"):
cwl2en = np.concatenate((cwl2,cwen), axis=0)
labels_2 = np.concatenate((labels,labels), axis=0)
if(args['attack'] == "L2" or args['attack'] == "all"):
train(MNIST(), "models/mnist_cwl2", [32, 32, 64, 64, 200, 200], num_epochs=50, adversarial=True, examples=cwl2, labels=labels)
if(args['attack'] == "L1" or args['attack'] == "all"):
train(MNIST(), "models/mnist_cwl1", [32, 32, 64, 64, 200, 200], num_epochs=50, adversarial=True, examples=cwl1, labels=labels)
if(args['attack'] == "EN" or args['attack'] == "all"):
train(MNIST(), "models/mnist_cwe", [32, 32, 64, 64, 200, 200], num_epochs=50, adversarial=True, examples=cwen, labels=labels)
if(args['attack'] == "L2L1" or args['attack'] == "all"):
train(MNIST(), "models/mnist_cwl2l1", [32, 32, 64, 64, 200, 200], num_epochs=50, adversarial=True, examples=cwl2l1, labels=labels_2)
if(args['attack'] == "L2EN" or args['attack'] == "all"):
train(MNIST(), "models/mnist_cwl2e", [32, 32, 64, 64, 200, 200], num_epochs=50, adversarial=True, examples=cwl2en, labels=labels_2)
if(args['dataset'] == "cifar" or args['dataset'] == "all"):
if(args['attack'] == "L2" or args['attack'] == "L2L1" or args['attack'] == "L2EN" or args['attack'] == "all"):
cwl2 = np.load('train/cifar_L2_train.npy')
if(args['attack'] == "L1" or args['attack'] == "L2L1" or args['attack'] == "all"):
cwl1 = np.load('train/cifar_L1_train.npy')
if(args['attack'] == "EN" or args['attack'] == "L2EN" or args['attack'] == "all"):
cwen = np.load('train/cifar_EN_train.npy')
labels = np.load('train/cifar_labels_train.npy')
if(args['attack'] == "L2L1" or args['attack'] == "all"):
cwl2l1 = np.concatenate((cwl2,cwl1), axis=0)
if(args['attack'] == "L2EN" or args['attack'] == "all"):
cwl2en = np.concatenate((cwl2,cwen), axis=0)
labels_2 = np.concatenate((labels,labels), axis=0)
if(args['attack'] == "L2" or args['attack'] == "all"):
train(CIFAR(), "models/cifar_cwl2", [64, 64, 128, 128, 256, 256], num_epochs=50, adversarial=True, examples=cwl2, labels=labels)
if(args['attack'] == "L1" or args['attack'] == "all"):
train(CIFAR(), "models/cifar_cwl1", [64, 64, 128, 128, 256, 256], num_epochs=50, adversarial=True, examples=cwl1, labels=labels)
if(args['attack'] == "EN" or args['attack'] == "all"):
train(CIFAR(), "models/cifar_cwe", [64, 64, 128, 128, 256, 256], num_epochs=50, adversarial=True, examples=cwen, labels=labels)
if(args['attack'] == "L2L1" or args['attack'] == "all"):
train(CIFAR(), "models/cifar_cwl2l1", [64, 64, 128, 128, 256, 256], num_epochs=50, adversarial=True, examples=cwl2l1, labels=labels_2)
if(args['attack'] == "L2EN" or args['attack'] == "all"):
train(CIFAR(), "models/cifar_cwl2e", [64, 64, 128, 128, 256, 256], num_epochs=50, adversarial=True, examples=cwl2en, labels=labels_2)
if (args['defensive']):
if(args['temp'] == 0):
temp = [1,10,20,30,40,50,60,70,80,90,100]
else:
temp = args['temp']
if (args['defensive'] and (args['dataset'] == "mnist" or args['dataset'] == "all")):
for t in temp:
print('Mnist_'+str(t))
train_distillation(MNIST(), "models/mnist-distilled-"+str(t), [32, 32, 64, 64, 200, 200],
num_epochs=50, train_temp=t)
if (args['defensive'] and (args['dataset'] == "cifar" or args['dataset'] == "all")):
for t in temp:
print('Cifar_'+str(t))
train_distillation(CIFAR(), "models/cifar-distilled-"+str(t), [64, 64, 128, 128, 256, 256],
num_epochs=50, train_temp=t)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dataset", choices=["mnist", "cifar", "all"], default="all")
parser.add_argument("-a", "--adversarial", action='store_true')
parser.add_argument("-at", "--attack", choices=["L2","L1","EN","L2L1","L2EN","all"], default="all")
parser.add_argument("-dd", "--defensive", action='store_true')
parser.add_argument("-t", "--temp", nargs='+', type=int, default=0)
args = vars(parser.parse_args())
print(args)
main(args)