-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbilevel_full.py
315 lines (204 loc) · 11.4 KB
/
bilevel_full.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
import os
import pdb
import time
import pickle
import random
import shutil
import argparse
import numpy as np
from copy import deepcopy
import matplotlib.pyplot as plt
import math
import torch
import torch.optim
import torch.nn as nn
import torch.utils.data
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from resnet import resnet18
from train_utils import *
from dataloader_ffcv import create_dataloader
from tqdm import tqdm
import time
from plot_utils import plot_SPC
from torch.cuda.amp import GradScaler, autocast
import seaborn as sns
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import roc_auc_score, average_precision_score
from sklearn.metrics import roc_curve, auc, precision_recall_curve
import pdb
from pathlib import Path
import json
from bilevel_losses import *
def save_all(l_MSPC, poison_label_full, pathname, epoch):
torch.save(l_MSPC, f'{pathname}/l_MSPC_{epoch}.pt')
roc_auc = roc_auc_score(poison_label_full, l_MSPC)
print(roc_auc)
with open(f'{pathname}/AUROC_{epoch}', 'w') as f:
json.dump(roc_auc, f, indent=2)
fpr, tpr, _ = roc_curve(poison_label_full, l_MSPC)
torch.save(fpr, f'{pathname}/FPR_{epoch}.pt')
torch.save(tpr, f'{pathname}/TPR_{epoch}.pt')
def main(args, device):
start_time = time.time()
# prepare dataset
train_loader, _,_ = create_dataloader(args, args.batch_size, '', device, partition='None', seq=True)
if args.target == 1:
model_path = f'Results/{args.dataset}/{args.attack}/Poisonratio_{args.poison_ratio}/{args.arch}/Trial {args.trialno}'
else:
model_path = f'Results/{args.dataset}/{args.attack}_{args.target}/Poisonratio_{args.poison_ratio}/{args.arch}/Trial {args.trialno}'
pathname = f'{model_path}/Bilevel/{args.tau}'
if not os.path.exists(pathname):
Path(pathname).mkdir(parents=True)
print(pathname)
save_args(pathname,args)
model = torch.load(f'{model_path}/model.pt')
model.to(device)
model.eval()
for param in model.parameters(): param.requires_grad = False
if args.dataset == 'cifar10':
train_no = 50000
elif args.dataset == 'imagenet200':
train_no = 100000
elif args.dataset == 'tinyimagenet':
train_no = 100000
scales = list(map(int, args.scales.split(',')))
tau = args.tau
###################### Create mask and w ######################
img, _,_,_ = next(iter(train_loader))
mask = torch.ones(img[0,0,:,:].shape, requires_grad=True , device=device)
mask_lambda = args.masklam
w = torch.ones(train_no).to(device)
w.requires_grad = False
#mask.requires_grad_()
############################################ True poison labels ############################################
poison_label_full = torch.zeros(train_no)
iterator = tqdm(enumerate(train_loader), total=len(train_loader))
for ix, (images, _, _, poison_label) in iterator:
batch_size = len(images)
poison_label_full[ix*batch_size:ix*batch_size + batch_size] = poison_label
torch.save(poison_label_full , f'{pathname}/poisonlab_true.pt')
#############################################################################################################
####import pdb;pdb.set_trace()
################ Warmup Start of w ##############################################################
l_MSPC_full_nomask = torch.zeros(train_no)
### Loop calculating losses =================================
iterator = tqdm(enumerate(train_loader), total=len(train_loader))
for ix, (images, _, _, poison_label) in iterator:
batch_size = len(images)
#import pdb;pdb.set_trace()
l_MSPC_full_nomask[ix*batch_size:ix*batch_size + batch_size] = l_MSPC_tau(images, model, scales, tau, device)
plot_violin(l_MSPC_full_nomask, l_MSPC_full_nomask, poison_label_full, f'{pathname}/violin_l_MSPC_0.png')
w = w*0
w[l_MSPC_full_nomask>0] = 1
save_all(l_MSPC_full_nomask, poison_label_full, pathname, 0)
#############################################################################################################
# Optimizer and scaler
optimizer_inner = torch.optim.SGD([mask], args.lr_inner, momentum=args.momentum, weight_decay=args.weight_decay)
scaler_inner = GradScaler()
for outer_epoch in range(args.outer_epoch):
#mask.requires_grad_()
print(f'========================== Starting Inner level ==========================')
fig, ax = plt.subplots(1,10, figsize=(50,5), constrained_layout=True)
l_min = 1.0
for i in range(args.epoch_inner):
print(f'--------- Epoch {i+1} --------------')
iterator = tqdm(enumerate(train_loader), total=len(train_loader))
losses = []
for ix, (images, targets, _, _) in iterator:
l_rec = 0
batch_size = len(images)
### Reset gradients wrt mask to None
optimizer_inner.zero_grad(set_to_none=True)
images = images/255.
for scale in scales:
## Masked images
masked_poison_images = (images-tau)*mask #+ (1-mask)*torch.rand(images.shape).to(device)
with autocast():
#
# Output of image and scaled image
output = model(images)
output_scale = model(torch.clamp(scale*masked_poison_images,0,1))
w_batch = w[ix*batch_size:ix*batch_size + batch_size]
loss_fdiv = (1/batch_size)*torch.dot(w_batch, SF_loss(output, output_scale, args.fdiv_name))
loss_fdiv = loss_fdiv / len(scales)
loss_fdiv.backward()
with torch.no_grad():
l_rec += loss_fdiv
loss_l1 = mask_lambda*torch.norm(mask, p=1)
loss_l1.backward()
with torch.no_grad():
l_rec += loss_l1
losses.append(l_rec)
save_metrics(l_rec, pathname, 'Losses_iteration')
optimizer_inner.step()
mask.data = torch.clamp(mask.data, min=0, max=1)
print(f'Loss: {l_rec} | L1 loss: {loss_l1/mask_lambda}')
ax.flatten()[i].imshow(mask.clone().detach().cpu().numpy())
ax.flatten()[i].set_title(f'Epoch {i+1}')
#mask = mask_min.clone().detach()
#ask = mask_min
#mask.data = mask_min.data
fig.savefig(f'{pathname}/Masks_{outer_epoch+1}.pdf')
torch.save(mask, f'{pathname}/mask_{outer_epoch+1}.pt')
print('========================== Plotting L_MSPC Using Mask ==========================')
##### modified SPC loss and f-div loss ################################################
l_MSPC_full_mask = torch.zeros(train_no)
l_MSPC_full_nomask = torch.zeros(train_no)
### Loop calculating losses =================================
iterator = tqdm(enumerate(train_loader), total=len(train_loader))
for ix, (images, _, _, poison_label) in iterator:
batch_size = len(images)
l_MSPC_full_mask[ix*batch_size:ix*batch_size + batch_size] = l_MSPC(images, model, scales, device, tau, mask=mask)
l_MSPC_full_nomask[ix*batch_size:ix*batch_size + batch_size] = l_MSPC(images, model, scales, device, tau, mask=None)
#### Make dataframes and plot violins ===============================================
#def plot_violin(loss_mask, loss_nomask, name)
plot_violin(l_MSPC_full_mask, l_MSPC_full_nomask, poison_label_full, f'{pathname}/violin_l_MSPC_{outer_epoch+1}.png')
w = w*0
w[l_MSPC_full_mask>0] = 1
save_all(l_MSPC_full_mask, poison_label_full, pathname, outer_epoch+1)
################################# Complete ################################################
############################################ SPC Loss ############################################
l_SPC_full = torch.zeros(train_no)
### Loop calculating losses =================================
iterator = tqdm(enumerate(train_loader), total=len(train_loader))
for ix, (images, _, _, poison_label) in iterator:
batch_size = len(images)
l_SPC_full[ix*batch_size:ix*batch_size + batch_size] = l_SPC(images, model, scales, device)
tot_time = time.time() - start_time
print(f'Time taken = {tot_time}')
torch.save(l_SPC_full, f'{pathname}/l_SPC.pt')
roc_auc = roc_auc_score(poison_label_full, l_SPC_full)
print(roc_auc)
with open(f'{pathname}/AUROC_SPC', 'w') as f:
json.dump(roc_auc, f, indent=2)
fpr, tpr, _ = roc_curve(poison_label_full, l_SPC_full)
torch.save(fpr, f'{pathname}/FPR_SPC.pt')
torch.save(tpr, f'{pathname}/TPR_SPC.pt')
###################################################################################################
if __name__ == '__main__':
parser = argparse.ArgumentParser()
##################################### general setting #################################################
parser.add_argument('--dataset', type=str, default='cifar10', help='dataset')
parser.add_argument('--arch', type=str, default='res18', help='model architecture')
##################################### training setting #################################################
parser.add_argument('--batch_size', type=int, default=1000, help='batch size')
parser.add_argument('--poison_ratio', default=0.1, type=float, help='Poison Ratio')
parser.add_argument('--lr_inner', default=0.1, type=float, help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, help='momentum')
parser.add_argument('--weight_decay', default=5e-4, type=float, help='weight decay')
parser.add_argument('--attack', type=str, help='Give attack name')
parser.add_argument('--fdiv_name', type=str, default='KL')
parser.add_argument('--epoch_inner', type=int, default=10, help='batch size')
parser.add_argument('--outer_epoch', type=int, default=4, help='batch size')
parser.add_argument('--masklam', default=0.01, type=float)
parser.add_argument('--scales', default='2,3,4,5,6,7,8,9,10,11,12')
parser.add_argument('--tau', default=0.2, type=float)
parser.add_argument('--trialno', type=int)
parser.add_argument('--save_samples', type=str, default='False', help='Give attack name')
parser.add_argument('--target', default=1, type=int, help= 'Target label')
opt = parser.parse_args()
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
print(device)
main(opt, device)