-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
executable file
·356 lines (282 loc) · 9.16 KB
/
test.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
'''
Test file for different sound propagation methods
** Top contributors:
** Shiqi Wang
** This file is part of the symbolic interval analysis library.
** Copyright (c) 2018-2019 by the authors listed in the file LICENSE
** and their institutional affiliations.
** All rights reserved.
The basic network structures are inspired by the implementations of
Eric Wong's convex polytope github available at
https://github.com/locuslab/convex_adversarial
'''
from __future__ import print_function
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from symbolic_interval.symbolic_network import Interval_network
from symbolic_interval.symbolic_network import sym_interval_analyze
from symbolic_interval.symbolic_network import gen_interval_analyze
from symbolic_interval.symbolic_network import mix_interval_analyze
from symbolic_interval.symbolic_network import naive_interval_analyze
# from utils_attacks import perturb_iterative
from bound_layers import BoundSequential, BoundLinear, BoundConv2d, BoundDataParallel, Flatten
import argparse
import time
import os
'''Flatten layers.
Please use this Flatten layer to flat the convolutional layers when
design your own models.
'''
# class Flatten(nn.Module):
# def forward(self, x):
# return x.view(x.size(0), -1)
class Vlayer(nn.Module):
def __init__(self, n):
super(Vlayer, self).__init__()
self.n = n
self.w = torch.nn.Parameter(torch.ones((1,n)))
#self.w = torch.nn.Parameter(torch.zeros((1,n)).uniform_(0,1))
#print(self.w.sum()/self.n, self.w.max(), self.w.min())
def forward(self, x):
return x
def mnist_model_new():
model = nn.Sequential(
nn.Conv2d(1, 16, 4, stride=2, padding=1),
nn.ReLU(),
Vlayer(3136),
nn.Conv2d(16, 32, 4, stride=2, padding=1),
nn.ReLU(),
Vlayer(1568),
Flatten(),
nn.Linear(32*7*7,100),
nn.ReLU(),
Vlayer(100),
nn.Linear(100, 10)
)
return model
def transfer_model(model_new, model):
import copy
index = 0
for i, layer in enumerate(model_new):
#print(layer, isinstance(layer, nn.Linear))
if 'Vlayer' in (str(layer.__class__.__name__)):
continue
if isinstance(model[index], (nn.Conv2d, nn.Linear)) and\
isinstance(layer, (nn.Conv2d, nn.Linear)):
model_new[i] = copy.deepcopy(model[index])
index += 1
return model_new
'''Test MNIST models.
Please use nn.Sequential to build the models.
'''
def mnist_model():
model = nn.Sequential(
nn.Conv2d(1, 16, 4, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(16, 32, 4, stride=2, padding=1),
nn.ReLU(),
Flatten(),
nn.Linear(32*7*7,100),
nn.ReLU(),
nn.Linear(100, 10)
)
return model
torch.manual_seed(7)
'''Test toy fully connected mnist models.
Please use nn.Sequential to build the models.
'''
def toy_model():
model = nn.Sequential(
Flatten(),
nn.Linear(784,5),
nn.ReLU(),
nn.Linear(5,3),
nn.ReLU(),
nn.Linear(3,10),
)
return model
def mnist_loaders(batch_size, shuffle_test=False):
mnist_train = datasets.MNIST("./data", train=True, download=True,\
transform=transforms.ToTensor())
mnist_test = datasets.MNIST("./data", train=False, download=True,\
transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(mnist_train,\
batch_size=batch_size, shuffle=True, pin_memory=True)
test_loader = torch.utils.data.DataLoader(mnist_test,\
batch_size=batch_size, shuffle=shuffle_test,\
pin_memory=True)
return train_loader, test_loader
'''main function of this test script.
It compares the average tightness, loss, and efficiency of each
propagation methods.
You can test on you own models, desired epsilon, and batch_size.
'''
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=10)
parser.add_argument('--method', default="sym")
parser.add_argument('--proj', type=int, default=None)
parser.add_argument('--epsilon', type=float, default=0.1)
parser.add_argument('--PARALLEL', action='store_true', default=False)
parser.add_argument('--compare_all', action='store_true', default=False)
parser.add_argument('--norm', type=str, default="linf")
parser.add_argument('--gpu', type=str, default="0")
args = parser.parse_args()
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
use_cuda = torch.cuda.is_available()
# load model
MODEL_NAME = "mnist_small_baseline.pth"
model = mnist_model()
if use_cuda:
model.load_state_dict(torch.load(MODEL_NAME))
model = model.cuda()
else:
model.load_state_dict(torch.load(MODEL_NAME, map_location={'cuda:0': 'cpu'}))
#model = toy_model()
epsilon = args.epsilon
batch_size = args.batch_size
PARALLEL = args.PARALLEL
if not use_cuda:
PARALLEL = False
train_loader, test_loader = mnist_loaders(batch_size)
# Fetch test samples
for i, (X,y) in enumerate(test_loader):
if(use_cuda):
X = X.cuda()
y = y.cuda().long()
model.cuda()
break
'''
if args.method=="convexdual" or args.compare_all:
from convex_adversarial import robust_loss
# if(PARALLEL):
# # The first run with parallel will take a few seconds
# # to warm up.
# eric_loss, eric_err = robust_loss(model,\
# epsilon, X, y, parallel=PARALLEL)
# del eric_loss, eric_err
start = time.time()
eric_loss, eric_err = robust_loss(model,\
epsilon, X, y,parallel=True,\
bounded_input={0, 1})
#norm_type="l1_median", proj=20)
#eric_loss, eric_err = robust_loss(model,\
#epsilon, X, y, parallel=PARALLEL)
print ("eric loss", eric_loss)
print ("eric err:", eric_err)
print ("eric time per sample:", (time.time()-start)/X.shape[0])
del eric_loss, eric_err
print()
'''
if args.method == "crown" or args.compare_all:
start = time.time()
num_class = 10
model = BoundSequential.convert(model,\
{'same-slope': False, 'zero-lb': False, 'one-lb': False})
c = torch.eye(num_class).type_as(X)[y].unsqueeze(1) -\
torch.eye(num_class).type_as(X).unsqueeze(0)
I = (~(y.data.unsqueeze(1) ==\
torch.arange(num_class).type_as(y.data).unsqueeze(0)))
c = (c[I].view(X.size(0),num_class-1,num_class))
data_ub = torch.clamp(X + epsilon, max=X.max())
data_lb = torch.clamp(X - epsilon, min=X.min())
output = model(X, method_opt="forward")
# ub, lb, relu_activity, unstable, dead, alive =\
# model(norm=np.inf, x_U=data_ub, x_L=data_lb, eps=epsilon,\
# C=c, method_opt="interval_range")
ub, _, lb, _ =\
model(norm=np.inf, x_U=data_ub, x_L=data_lb, eps=epsilon,
C=c, upper=False, lower=True, method_opt="full_backward_range")
sa = np.zeros((num_class, num_class - 1), dtype = np.int32)
for i in range(sa.shape[0]):
for j in range(sa.shape[1]):
if j < i:
sa[i][j] = j
else:
sa[i][j] = j + 1
sa = torch.LongTensor(sa)
lb_s = torch.zeros(X.size(0), num_class)
sa_labels = sa[y]
lb = lb_s.scatter(1, sa_labels, lb)
# print(-lb)
robust_ce = nn.CrossEntropyLoss()(-lb, y)
print ("crown loss", robust_ce.item())
print ("crown time per sample:", (time.time()-start)/X.shape[0])
del robust_ce
print()
model = mnist_model()
if use_cuda:
model.load_state_dict(torch.load(MODEL_NAME))
model = model.cuda()
else:
model.load_state_dict(torch.load(MODEL_NAME, map_location={'cuda:0': 'cpu'}))
if args.method=="baseline" or args.compare_all:
#if(method == BASELINE):
start = time.time()
f = model(X)
loss = nn.CrossEntropyLoss()(f, y)
print("baseline loss:", loss)
print("baseline time per sample:",\
(time.time()-start)/X.shape[0])
print()
if args.method == "naive" or args.compare_all:
start = time.time()
iloss, ierr = naive_interval_analyze(model, epsilon,\
X, y, use_cuda)
#iloss.backward()
#print(model[0].weight.grad.sum())
print ("naive loss:", iloss)
print ("naive err:", ierr)
print ("naive time per sample:",\
(time.time()-start)/X.shape[0])
del iloss, ierr
print()
if args.method == "sym" or args.compare_all:
start = time.time()
iloss, ierr = sym_interval_analyze(model, epsilon,\
X, y, use_cuda, parallel=PARALLEL,\
proj=args.proj, norm=args.norm)
#iloss.backward()
#print(model[0].weight.grad.sum())
print ("sym loss:", iloss.item())
print ("sym err:", ierr)
print("sym time per sample:",\
(time.time()-start)/X.shape[0])
del iloss, ierr
print()
'''
if args.method == "gen" or args.compare_all:
start = time.time()
iloss, ierr = gen_interval_analyze(model, [epsilon, epsilon],\
X, y, use_cuda, parallel=PARALLEL,\
proj=args.proj, norm=["l2", "l1"])
#iloss.backward()
#print(model[0].weight.grad.sum())
print ("sym loss:", iloss)
print ("sym err:", ierr)
print("sym time per sample:",\
(time.time()-start)/X.shape[0])
del iloss, ierr
print()
'''
'''
if args.method == "mix" or args.compare_all:
start = time.time()
iloss, ierr = mix_interval_analyze(model, epsilon,\
X, y, use_cuda, parallel=PARALLEL,\
proj=args.proj, norm=args.norm)
#iloss.backward()
#print(model[0].weight.grad.sum())
print ("mix loss:", iloss)
print ("mix err:", ierr)
print("mix time per sample:",\
(time.time()-start)/X.shape[0])
del iloss, ierr
print()
'''