-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig_robustness.py
328 lines (276 loc) · 8.58 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
import os
import numpy as np
import pandas as pd
import torch
from fastmri_utils.common import subsample
import config
from find_adversarial import PAdam, untargeted_attack
from networks import IterativeNet, Tiramisu
from operators import (
Fourier,
TVAnalysisPeriodic,
im2vec,
noise_gaussian,
proj_l2_ball,
rotate_real,
unprep_fft_channel,
vec2im,
)
from reconstruction_methods import admm_l1_rec_diag
# ------ setup ----------
device = torch.device("cuda:0")
torch.cuda.set_device(0)
mask_seed = 123
# ----- operators -----
n = (368, 368)
mask_func = subsample.RandomMaskFunc(
center_fractions=[0.08], accelerations=[4]
)
mask = unprep_fft_channel(
mask_func((1, 1) + n + (1,), seed=mask_seed).expand_as(
torch.ones((1, 1) + n + (1,))
)
)
OpA = Fourier(mask)
OpTV = TVAnalysisPeriodic(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 --------
# grid search parameters for L1 via admm
grid_search_file = os.path.join(
config.RESULTS_PATH, "grid_search_l1", "grid_search_l1_fourier_all.pkl"
)
gs_params = pd.read_pickle(grid_search_file)
def _get_gs_param(noise_rel):
idx = (gs_params.noise_rel - noise_rel).abs().to_numpy().argmin()
return gs_params.grid_param[idx]["lam"], gs_params.grid_param[idx]["rho"]
# the actual reconstruction method
def _reconstructL1(y, noise_rel):
lam, rho = _get_gs_param(noise_rel.numpy())
x, _ = admm_l1_rec_diag(
y,
OpA,
OpTV,
OpA.adj(y),
OpTV(OpA.adj(y)),
lam,
rho,
iter=5000,
silent=True,
)
return x
# the reconstruction method used for the L1 attack
# (less iterations due to high computational costs)
def _reconstructL1_adv(y, lam, rho, x0, z0):
x, _ = admm_l1_rec_diag(
y, OpA, OpTV, x0, z0, lam, rho, iter=150, silent=True
)
return x
# loss
mseloss = torch.nn.MSELoss(reduction="sum")
def _complexloss(reference, prediction):
loss = mseloss(
rotate_real(reference)[:, 0:1, ...],
rotate_real(prediction)[:, 0:1, ...],
)
return loss
# attack function for L1
def _attackerL1(x0, noise_rel, yadv_init=None, batch_size=6):
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(y0, noise_level)
# attack parameters
adv_init_fac = 3.0 * noise_level
adv_param = {
"codomain_dist": _complexloss,
"domain_dist": None,
"mixed_dist": None,
"weights": (1.0, 1.0, 1.0),
"optimizer": PAdam,
"projs": None,
"iter": 50,
"stepsize": 5e0,
}
# get ADMM tuning parameters for noise_rel
lam, rho = _get_gs_param(noise_rel.numpy())
# compute good start values for _reconstructL1_adv
x0_adv, z0_adv = admm_l1_rec_diag(
y0,
OpA,
OpTV,
OpA.adj(y0),
OpTV(OpA.adj(y0)),
lam,
rho,
iter=5000,
silent=True,
)
# 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()
for idx_batch in range(0, yadv.shape[0], batch_size):
print(
"Attack for samples "
+ str(list(range(idx_batch, idx_batch + batch_size)))
)
adv_param["projs"] = [
lambda y: proj_l2_ball(
y,
y0[idx_batch : idx_batch + batch_size, ...],
noise_level[idx_batch : idx_batch + batch_size, ...],
)
]
# perform attack
yadv[idx_batch : idx_batch + batch_size, ...] = untargeted_attack(
lambda y: _reconstructL1_adv(
y,
lam,
rho,
x0_adv[idx_batch : idx_batch + batch_size, ...],
z0_adv[idx_batch : idx_batch + batch_size, ...],
),
yadv[idx_batch : idx_batch + batch_size, ...]
.clone()
.requires_grad_(True),
y0[idx_batch : idx_batch + batch_size, ...],
t_out_ref=x0[idx_batch : idx_batch + batch_size, ...],
**adv_param
).detach()
return yadv, yref, y0
methods.loc["L1"] = {
"info": {
"name_disp": "TV",
"name_save": "tv",
"plt_color": "crimson",
"plt_marker": ".",
"plt_linestyle": "-",
"plt_linewidth": None,
},
"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_rel, net):
zero_fill_y = torch.zeros(*y.shape[:-1], n[0] * n[1], device=y.device)
zero_fill_y[..., im2vec(mask[0, 0, :, :].bool())] = y
return net.forward((vec2im(zero_fill_y, n), mask))
# attack function for any net
def _attackerNet(x0, noise_rel, net, yadv_init=None, batch_size=2):
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(y0, noise_level) # noisy measurements
# attack parameters
adv_init_fac = 3.0 * noise_level
adv_param = {
"codomain_dist": _complexloss,
"domain_dist": None,
"mixed_dist": None,
"weights": (1.0, 1.0, 1.0),
"optimizer": PAdam,
"projs": None,
"iter": 200,
"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()
for idx_batch in range(0, yadv.shape[0], batch_size):
print(
"Attack for samples "
+ str(list(range(idx_batch, idx_batch + batch_size)))
)
adv_param["projs"] = [
lambda y: proj_l2_ball(
y,
y0[idx_batch : idx_batch + batch_size, ...],
noise_level[idx_batch : idx_batch + batch_size, ...],
)
]
# perform attack
yadv[idx_batch : idx_batch + batch_size, ...] = untargeted_attack(
lambda y: _reconstructNet(y, 0.0, net),
yadv[idx_batch : idx_batch + batch_size, ...]
.clone()
.requires_grad_(True),
y0[idx_batch : idx_batch + batch_size, ...],
t_out_ref=x0[idx_batch : idx_batch + batch_size, ...],
**adv_param
).detach()
return yadv, yref, y0
# ----- load nets -----
# 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_rel: _reconstructNet(y, noise_rel, net),
"attacker": lambda x0, noise_rel, yadv_init=None: _attackerNet(
x0, noise_rel, net, yadv_init=yadv_init
),
"net": net,
}
pass
# ----- Tiramisu -----
tiramisu_params = {
"in_channels": 2,
"out_channels": 2,
"drop_factor": 0.0,
"down_blocks": (6, 8, 10, 12, 14),
"up_blocks": (14, 12, 10, 8, 6),
"pool_factors": (2, 2, 2, 2, 2),
"bottleneck_layers": 20,
"growth_rate": 12,
"out_chans_first_conv": 16,
}
_append_net(
"Tiramisu jit",
{
"name_disp": "Tira",
"name_save": "tiramisu_jit",
"plt_color": "turquoise",
"plt_marker": ".",
"plt_linestyle": "-",
"plt_linewidth": None,
},
_load_net(
"results/challenge_4_no_fs_tiramisu_v22_train_phase_1/"
+ "model_weights_maxSSIM_40.pt",
Tiramisu,
tiramisu_params,
{
"num_iter": 1,
"lam": 0.0,
"lam_learnable": False,
"final_dc": False,
"concat_mask": False,
"multi_slice": False,
},
),
)