-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.py
289 lines (231 loc) · 11.2 KB
/
utils.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
# ---------------------------------------------------------------
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
#
# This work is licensed under the NVIDIA Source Code License
# for DiffPure. To view a copy of this license, see the LICENSE file.
# ---------------------------------------------------------------
import sys
import argparse
from typing import Any
import torch
import torch.nn as nn
import torchvision.models as models
from torch.utils.data import DataLoader
import torchvision.transforms as transforms
from robustbench import load_model
import data
def compute_n_params(model, return_str=True):
tot = 0
for p in model.parameters():
w = 1
for x in p.shape:
w *= x
tot += w
if return_str:
if tot >= 1e6:
return '{:.1f}M'.format(tot / 1e6)
else:
return '{:.1f}K'.format(tot / 1e3)
else:
return tot
class Logger(object):
"""
Redirect stderr to stdout, optionally print stdout to a file,
and optionally force flushing on both stdout and the file.
"""
def __init__(self, file_name: str = None, file_mode: str = "w", should_flush: bool = True):
self.file = None
if file_name is not None:
self.file = open(file_name, file_mode)
self.should_flush = should_flush
self.stdout = sys.stdout
self.stderr = sys.stderr
sys.stdout = self
sys.stderr = self
def __enter__(self) -> "Logger":
return self
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
self.close()
def write(self, text: str) -> None:
"""Write text to stdout (and a file) and optionally flush."""
if len(text) == 0: # workaround for a bug in VSCode debugger: sys.stdout.write(''); sys.stdout.flush() => crash
return
if self.file is not None:
self.file.write(text)
self.stdout.write(text)
if self.should_flush:
self.flush()
def flush(self) -> None:
"""Flush written text to both stdout and a file, if open."""
if self.file is not None:
self.file.flush()
self.stdout.flush()
def close(self) -> None:
"""Flush, close possible files, and remove stdout/stderr mirroring."""
self.flush()
# if using multiple loggers, prevent closing in wrong order
if sys.stdout is self:
sys.stdout = self.stdout
if sys.stderr is self:
sys.stderr = self.stderr
if self.file is not None:
self.file.close()
def dict2namespace(config):
namespace = argparse.Namespace()
for key, value in config.items():
if isinstance(value, dict):
new_value = dict2namespace(value)
else:
new_value = value
setattr(namespace, key, new_value)
return namespace
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def update_state_dict(state_dict, idx_start=9):
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[idx_start:] # remove 'module.0.' of dataparallel
new_state_dict[name]=v
return new_state_dict
# ------------------------------------------------------------------------
def get_accuracy(model, x_orig, y_orig, bs=64, device=torch.device('cuda:0')):
n_batches = x_orig.shape[0] // bs
acc = 0.
for counter in range(n_batches):
x = x_orig[counter * bs:min((counter + 1) * bs, x_orig.shape[0])].clone().to(device)
y = y_orig[counter * bs:min((counter + 1) * bs, x_orig.shape[0])].clone().to(device)
output = model(x)
acc += (output.max(1)[1] == y).float().sum()
return (acc / x_orig.shape[0]).item()
def get_image_classifier(classifier_name):
class _Wrapper_ResNet(nn.Module):
def __init__(self, resnet):
super().__init__()
self.resnet = resnet
self.mu = torch.Tensor([0.485, 0.456, 0.406]).float().view(3, 1, 1)
self.sigma = torch.Tensor([0.229, 0.224, 0.225]).float().view(3, 1, 1)
def forward(self, x):
x = (x - self.mu.to(x.device)) / self.sigma.to(x.device)
return self.resnet(x)
if 'imagenet' in classifier_name:
if 'resnet18' in classifier_name:
print('using imagenet resnet18...')
model = models.resnet18(pretrained=True).eval()
elif 'resnet50' in classifier_name:
print('using imagenet resnet50...')
model = models.resnet50(pretrained=True).eval()
elif 'resnet101' in classifier_name:
print('using imagenet resnet101...')
model = models.resnet101(pretrained=True).eval()
elif 'wideresnet-50-2' in classifier_name:
print('using imagenet wideresnet-50-2...')
model = models.wide_resnet50_2(pretrained=True).eval()
elif 'deit-s' in classifier_name:
print('using imagenet deit-s...')
model = torch.hub.load('facebookresearch/deit:main', 'deit_small_patch16_224', pretrained=True).eval()
else:
raise NotImplementedError(f'unknown {classifier_name}')
wrapper_resnet = _Wrapper_ResNet(model)
elif 'cifar10' in classifier_name:
if 'wideresnet-28-10' in classifier_name:
print('using cifar10 wideresnet-28-10...')
model = load_model(model_name='Standard', dataset='cifar10', threat_model='Linf') # pixel in [0, 1]
elif 'wrn-28-10-at0' in classifier_name:
print('using cifar10 wrn-28-10-at0...')
model = load_model(model_name='Gowal2021Improving_28_10_ddpm_100m', dataset='cifar10',
threat_model='Linf') # pixel in [0, 1]
elif 'wrn-28-10-at1' in classifier_name:
print('using cifar10 wrn-28-10-at1...')
model = load_model(model_name='Gowal2020Uncovering_28_10_extra', dataset='cifar10',
threat_model='Linf') # pixel in [0, 1]
elif 'wrn-70-16-at0' in classifier_name:
print('using cifar10 wrn-70-16-at0...')
model = load_model(model_name='Gowal2021Improving_70_16_ddpm_100m', dataset='cifar10',
threat_model='Linf') # pixel in [0, 1]
elif 'wrn-70-16-at1' in classifier_name:
print('using cifar10 wrn-70-16-at1...')
model = load_model(model_name='Rebuffi2021Fixing_70_16_cutmix_extra', dataset='cifar10',
threat_model='Linf') # pixel in [0, 1]
elif 'wrn-70-16-L2-at1' in classifier_name:
print('using cifar10 wrn-70-16-L2-at1...')
model = load_model(model_name='Rebuffi2021Fixing_70_16_cutmix_extra', dataset='cifar10',
threat_model='L2') # pixel in [0, 1]
elif 'wideresnet-70-16' in classifier_name:
print('using cifar10 wideresnet-70-16 (dm_wrn-70-16)...')
from robustbench.model_zoo.architectures.dm_wide_resnet import DMWideResNet, Swish
model = DMWideResNet(num_classes=10, depth=70, width=16, activation_fn=Swish) # pixel in [0, 1]
model_path = 'pretrained/cifar10/wresnet-76-10/weights-best.pt'
print(f"=> loading wideresnet-70-16 checkpoint '{model_path}'")
model.load_state_dict(update_state_dict(torch.load(model_path)['model_state_dict']))
model.eval()
print(f"=> loaded wideresnet-70-16 checkpoint")
elif 'resnet-50' in classifier_name:
print('using cifar10 resnet-50...')
from classifiers.cifar10_resnet import ResNet50
model = ResNet50() # pixel in [0, 1]
model_path = 'pretrained/cifar10/resnet-50/weights.pt'
print(f"=> loading resnet-50 checkpoint '{model_path}'")
model.load_state_dict(update_state_dict(torch.load(model_path), idx_start=7))
model.eval()
print(f"=> loaded resnet-50 checkpoint")
elif 'wrn-70-16-dropout' in classifier_name:
print('using cifar10 wrn-70-16-dropout (standard wrn-70-16-dropout)...')
from classifiers.cifar10_resnet import WideResNet_70_16_dropout
model = WideResNet_70_16_dropout() # pixel in [0, 1]
model_path = 'pretrained/cifar10/wrn-70-16-dropout/weights.pt'
print(f"=> loading wrn-70-16-dropout checkpoint '{model_path}'")
model.load_state_dict(update_state_dict(torch.load(model_path), idx_start=7))
model.eval()
print(f"=> loaded wrn-70-16-dropout checkpoint")
else:
raise NotImplementedError(f'unknown {classifier_name}')
wrapper_resnet = model
elif 'celebahq' in classifier_name:
attribute = classifier_name.split('__')[-1] # `celebahq__Smiling`
ckpt_path = f'pretrained/celebahq/{attribute}/net_best.pth'
from classifiers.attribute_classifier import ClassifierWrapper
model = ClassifierWrapper(attribute, ckpt_path=ckpt_path)
wrapper_resnet = model
else:
raise NotImplementedError(f'unknown {classifier_name}')
return wrapper_resnet
def load_data(args, adv_batch_size):
if 'imagenet' in args.domain:
val_dir = './dataset/imagenet_lmdb/val' # using imagenet lmdb data
val_transform = data.get_transform(args.domain, 'imval', base_size=224)
val_data = data.imagenet_lmdb_dataset_sub(val_dir, transform=val_transform,
num_sub=args.num_sub, data_seed=args.data_seed)
n_samples = len(val_data)
val_loader = DataLoader(val_data, batch_size=n_samples, shuffle=False, pin_memory=True, num_workers=4)
x_val, y_val = next(iter(val_loader))
elif 'cifar10' in args.domain:
data_dir = './dataset'
transform = transforms.Compose([transforms.ToTensor()])
val_data = data.cifar10_dataset_sub(data_dir, transform=transform,
num_sub=args.num_sub, data_seed=args.data_seed)
n_samples = len(val_data)
val_loader = DataLoader(val_data, batch_size=n_samples, shuffle=False, pin_memory=True, num_workers=4)
x_val, y_val = next(iter(val_loader))
elif 'celebahq' in args.domain:
data_dir = './dataset/celebahq'
attribute = args.classifier_name.split('__')[-1] # `celebahq__Smiling`
val_transform = data.get_transform('celebahq', 'imval')
clean_dset = data.get_dataset('celebahq', 'val', attribute, root=data_dir, transform=val_transform,
fraction=2, data_seed=args.data_seed) # data_seed randomizes here
loader = DataLoader(clean_dset, batch_size=adv_batch_size, shuffle=False,
pin_memory=True, num_workers=4)
x_val, y_val = next(iter(loader)) # [0, 1], 256x256
else:
raise NotImplementedError(f'Unknown domain: {args.domain}!')
print(f'x_val shape: {x_val.shape}')
x_val, y_val = x_val.contiguous().requires_grad_(True), y_val.contiguous()
print(f'x (min, max): ({x_val.min()}, {x_val.max()})')
return x_val, y_val