-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig_robustness.py
executable file
·417 lines (359 loc) · 9.92 KB
/
config_robustness.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
import numpy as np
import pandas as pd
import torch
import config
from find_adversarial import PAdam, untargeted_attack
from networks import IterativeNet, Tiramisu, UNet
from operators import (
TVAnalysis,
TVSynthesis,
get_operator_norm,
get_tikhonov_matrix,
noise_gaussian,
proj_l2_ball,
)
from reconstruction_methods import primaldual
# ------ setup ----------
device = torch.device("cuda:0")
torch.cuda.set_device(0)
# ----- operators -----
OpA = config.meas_op(config.m, config.n, device=device, **config.meas_params)
OpTVSynth = TVSynthesis(config.n, device=device)
OpTV = TVAnalysis(config.n, device=device)
# ----- methods --------
methods = pd.DataFrame(columns=["name", "info", "reconstr", "attacker", "net"])
methods = methods.set_index("name")
noise_ref = noise_gaussian
# ----- set up L1 --------
# recovery parameters for L1 via primal dual
OpAW_norm = get_operator_norm(OpA, OpTVSynth)
rec_params = {
"sigma": 1.0 / (OpAW_norm + 1e0),
"tau": 1.0 / (OpAW_norm + 1e0),
"theta": 1e0,
}
# the actual reconstruction method
def _reconstructL1(y, noise_level):
x, _, _ = primaldual(
y,
OpA,
OpTVSynth,
iter=50000,
c0=torch.zeros(config.n,).to(device),
y0=torch.zeros(config.m,).to(device),
eta=noise_level,
silent=True,
**rec_params
)
return x
# the reconstruction method used for the L1 attack
# (less iterations due to high computational costs)
def _reconstructL1_adv(y, noise_level, c0, y0):
x, _, _ = primaldual(
y,
OpA,
OpTVSynth,
iter=2000,
c0=c0,
y0=y0,
eta=noise_level,
silent=True,
**rec_params
)
return x
# attack function for L1
def _attackerL1(x0, noise_rel, yadv_init=None):
# compute noiseless measurements
y0 = OpA(x0)
if noise_rel == 0.0:
return y0, y0, y0
# compute absolute noise levels
noise_level = noise_rel * y0.norm(p=2, dim=(-2, -1), keepdim=True)
# compute noisy measurements for reference
yref = noise_ref(OpA(x0), noise_level)
# attack parameters
adv_init_fac = 3.0 * noise_level
adv_param = {
"codomain_dist": torch.nn.MSELoss(reduction="sum"),
"domain_dist": None,
"weights": (1.0, 1.0),
"optimizer": PAdam,
"projs": [lambda y: proj_l2_ball(y, y0, noise_level)],
"iter": 30,
"stepsize": 5e0,
}
# compute good start values for _reconstructL1_adv
_, c0_adv, y0_adv = primaldual(
y0,
OpA,
OpTVSynth,
iter=50000,
c0=torch.zeros(config.n,).to(device),
y0=torch.zeros(config.m,).to(device),
eta=noise_level,
silent=True,
**rec_params
)
# compute initialization
yadv = y0.clone().detach() + (
adv_init_fac / np.sqrt(np.prod(y0.shape[-2:]))
) * torch.randn_like(y0)
if yadv_init is not None:
yadv[0 : yadv_init.shape[0], ...] = yadv_init.clone().detach()
yadv = yadv.requires_grad_(True)
# perform attack
yadv = untargeted_attack(
lambda y: _reconstructL1_adv(y, noise_level, c0_adv, y0_adv),
yadv,
y0,
t_out_ref=x0,
**adv_param
).detach()
return yadv, yref, y0
methods.loc["L1"] = {
"info": {
"name_disp": "TV$[\\eta]$",
"name_save": "tv",
"plt_color": "#e8000b",
"plt_marker": "o",
"plt_linestyle": "-",
"plt_linewidth": 2.75,
},
"reconstr": _reconstructL1,
"attacker": lambda x0, noise_rel, yadv_init=None: _attackerL1(
x0, noise_rel, yadv_init=yadv_init
),
"net": None,
}
methods.loc["L1", "net"] = None
# ----- set up net attacks --------
# the actual reconstruction method for any net
def _reconstructNet(y, noise_level, net):
return net.forward(y)
# attack function for any net
def _attackerNet(x0, noise_rel, net, yadv_init=None):
# compute noiseless measurements
y0 = OpA(x0)
if noise_rel == 0.0:
return y0, y0, y0
# compute absolute noise levels
noise_level = noise_rel * y0.norm(p=2, dim=(-2, -1), keepdim=True)
# compute noisy measurements for reference
yref = noise_ref(OpA(x0), noise_level) # noisy measurements
# attack parameters
adv_init_fac = 3.0 * noise_level
adv_param = {
"codomain_dist": torch.nn.MSELoss(reduction="sum"),
"domain_dist": None,
"weights": (1.0, 1.0),
"optimizer": PAdam,
"projs": [lambda y: proj_l2_ball(y, y0, noise_level)],
"iter": 100,
"stepsize": 5e0,
}
# compute initialization
yadv = y0.clone().detach() + (
adv_init_fac / np.sqrt(np.prod(y0.shape[-2:]))
) * torch.randn_like(y0)
if yadv_init is not None:
yadv[0 : yadv_init.shape[0], ...] = yadv_init.clone().detach()
yadv = yadv.requires_grad_(True)
# perform attack
yadv = untargeted_attack(
lambda y: _reconstructNet(y, 0.0, net),
yadv,
y0,
t_out_ref=x0,
**adv_param
).detach()
return yadv, yref, y0
# ----- load nets -----
# create a fresh tikhonov inverter layer
def _get_inverter_tikh(reg_fac=2e-2):
inverter_tikh = torch.nn.Linear(OpA.m, OpA.n, bias=False)
inverter_tikh.weight.requires_grad = False
inverter_tikh.weight.data = get_tikhonov_matrix(OpA, OpTV, reg_fac)
return inverter_tikh
# create a net and load weights from file
def _load_net(path, subnet, subnet_params, it_net_params):
subnet = subnet(**subnet_params).to(device)
it_net = IterativeNet(subnet, **it_net_params).to(device)
it_net.load_state_dict(torch.load(path, map_location=torch.device(device)))
it_net.freeze()
it_net.eval()
return it_net
def _append_net(name, info, net):
methods.loc[name] = {
"info": info,
"reconstr": lambda y, noise_level: _reconstructNet(
y, noise_level, net
),
"attacker": lambda x0, noise_rel, yadv_init=None: _attackerNet(
x0, noise_rel, net, yadv_init=yadv_init
),
"net": net,
}
pass
# ----- UNets -----
unet_params = {
"in_channels": 1,
"out_channels": 1,
"drop_factor": 0.0,
"base_features": 64,
}
_append_net(
"UNet jit",
{
"name_disp": "UNet",
"name_save": "unet_jit",
"plt_color": "#ff7c00",
"plt_marker": "o",
"plt_linestyle": ":",
"plt_linewidth": 2.75,
},
_load_net(
"results/unet_jitter_train_phase_2/model_weights.pt",
UNet,
unet_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(reg_fac=2e-2),
"num_iter": 1,
"lam": 0.0,
"lam_learnable": False,
"final_dc": False,
},
),
)
_append_net(
"UNet EE jit",
{
"name_disp": "UNetFL",
"name_save": "unet_ee_jit",
"plt_color": "maroon",
"plt_marker": "o",
"plt_linestyle": "-",
"plt_linewidth": None,
},
_load_net(
"results/unet_ee_jitter_train_phase_2/model_weights.pt",
UNet,
unet_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(), # placeholder, learned
"num_iter": 1,
"lam": 0.0,
"lam_learnable": False,
"final_dc": False,
},
),
)
_append_net(
"UNet It",
{
"name_disp": "ItNet no jitter",
"name_save": "unet_it",
"plt_color": "royalblue",
"plt_marker": "o",
"plt_linestyle": "--",
"plt_linewidth": None,
},
_load_net(
"results/unet_it_tikh_train_phase_2/model_weights.pt",
UNet,
unet_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(reg_fac=2e-2),
"num_iter": 8,
"lam": 8 * [0.1],
"lam_learnable": False,
"final_dc": True,
},
),
)
_append_net(
"UNet It jit",
{
"name_disp": "ItNet",
"name_save": "unet_it_jit",
"plt_color": "#023eff",
"plt_marker": "o",
"plt_linestyle": "--",
"plt_linewidth": 2.75,
},
_load_net(
"results/unet_it_tikh_jitter_train_phase_2/model_weights.pt",
UNet,
unet_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(reg_fac=2e-2),
"num_iter": 8,
"lam": 8 * [0.1],
"lam_learnable": False,
"final_dc": True,
},
),
)
# ----- Tiramisu -----
tiramisu_params = {
"in_channels": 1,
"out_channels": 1,
"drop_factor": 0.0,
"down_blocks": (5, 7, 9, 12, 15),
"up_blocks": (15, 12, 9, 7, 5),
"pool_factors": (2, 2, 2, 2, 2),
"bottleneck_layers": 25,
"growth_rate": 16,
"out_chans_first_conv": 16,
}
_append_net(
"Tiramisu jit",
{
"name_disp": "Tira",
"name_save": "tiramisu_jit",
"plt_color": "turquoise",
"plt_marker": "o",
"plt_linestyle": "-",
"plt_linewidth": None,
},
_load_net(
"results/tiramisu_jitter_train_phase_2/model_weights.pt",
Tiramisu,
tiramisu_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(reg_fac=2e-2),
"num_iter": 1,
"lam": 0.0,
"lam_learnable": False,
"final_dc": False,
},
),
)
_append_net(
"Tiramisu EE jit",
{
"name_disp": "TiraFL",
"name_save": "tiramisu_ee_jit",
"plt_color": "#1ac938",
"plt_marker": "o",
"plt_linestyle": "-.",
"plt_linewidth": 2.75,
},
_load_net(
"results/tiramisu_ee_jitter_train_phase_2/model_weights.pt",
Tiramisu,
tiramisu_params,
{
"operator": OpA,
"inverter": _get_inverter_tikh(), # placeholder, learned
"num_iter": 1,
"lam": 0.0,
"lam_learnable": False,
"final_dc": False,
},
),
)