-
Notifications
You must be signed in to change notification settings - Fork 0
/
AEVA_experiment_launcher.py
executable file
·416 lines (359 loc) · 15.1 KB
/
AEVA_experiment_launcher.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
import argparse
import os.path as osp
import signal
import time
import cv2
import numpy as np
from PIL import Image
import torch
import torchvision
from torchvision.transforms import Compose, ToTensor, RandomCrop, RandomHorizontalFlip, ToPILImage, Resize, RandomResizedCrop, Normalize
import core
from core_XAI.utils.distance import *
from AEVA.AEVA import get_reversed_mask
parser = argparse.ArgumentParser(description='AEVA Experiments Launcher for Trojaned Models on Datasets')
parser.add_argument('--model_type', default='core', type=str)
parser.add_argument('--model_name', default='ResNet-18', type=str)
parser.add_argument('--model_path', default='/data2/yamengxi/Backdoor/XAI/Backdoor_XAI/models/ResNet-18_CIFAR-10_BadNets_2022-11-05_16:13:21_ckpt_epoch_200.pth', type=str)
parser.add_argument('--dataset_name', default='CIFAR-10', type=str)
parser.add_argument('--dataset_root_path', default='../datasets', type=str)
parser.add_argument('--batch_size', default=128, type=int)
parser.add_argument('--num_workers', default=4, type=int)
parser.add_argument('--seed', default=666, type=int)
parser.add_argument('--deterministic', action='store_true', default=False)
parser.add_argument('--y_target', default=1, type=int)
parser.add_argument('--trigger_size', default=3, type=int)
parser.add_argument('--save_trigger_path', default='./AEVA/now_AEVA_experiments', type=str)
args = parser.parse_args()
model_type=args.model_type
model_name=args.model_name
model_path=args.model_path
dataset_name=args.dataset_name
dataset_root_path=args.dataset_root_path
batch_size=args.batch_size
num_workers=args.num_workers
seed=args.seed
deterministic=args.deterministic
y_target=args.y_target
trigger_size=args.trigger_size
save_trigger_path=args.save_trigger_path
save_trigger_path=osp.join(save_trigger_path, f"{model_name}_{dataset_name}_{trigger_size}x{trigger_size}_{time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())}")
if dataset_name=='MNIST':
num_classes=10
dataset=torchvision.datasets.MNIST
if model_type=='core' and model_name=='BaselineMNISTNetwork':
data_shape=(1, 28, 28)
transform_train = Compose([
ToTensor()
])
transform_test = Compose([
ToTensor()
])
elif model_type=='core' and model_name.startswith('ResNet'):
data_shape=(3, 32, 32)
def convert_1HW_to_3HW(img):
return img.repeat(3, 1, 1)
transform_train = Compose([
RandomCrop((32, 32), pad_if_needed=True),
ToTensor(),
convert_1HW_to_3HW
])
transform_test = Compose([
RandomCrop((32, 32), pad_if_needed=True),
ToTensor(),
convert_1HW_to_3HW
])
else:
raise NotImplementedError(f"Unsupported dataset_name: {dataset_name}, model_type: {model_type} with model_name: {model_name}")
std_pattern = torch.zeros(data_shape)
std_pattern[:, -trigger_size:, -trigger_size:] = 1.0
std_weight = torch.zeros(data_shape[1:])
std_weight[-trigger_size:, -trigger_size:] = 1.0
trainset = dataset(dataset_root_path, train=True, transform=transform_train, download=True)
testset = dataset(dataset_root_path, train=False, transform=transform_test, download=True)
elif dataset_name=='CIFAR-10':
num_classes=10
data_shape=(3, 32, 32)
std_pattern = torch.zeros(data_shape)
std_pattern[:, -trigger_size:, -trigger_size:] = 1.0
std_weight = torch.zeros(data_shape[1:])
std_weight[-trigger_size:, -trigger_size:] = 1.0
dataset=torchvision.datasets.CIFAR10
transform_train = Compose([
RandomHorizontalFlip(),
ToTensor()
])
transform_test = Compose([
ToTensor()
])
trainset = dataset(dataset_root_path, train=True, transform=transform_train, download=True)
testset = dataset(dataset_root_path, train=False, transform=transform_test, download=True)
x = torch.stack([transform_train(Image.fromarray(img)) for img in trainset.data]).numpy()
y = np.array(trainset.targets, dtype=np.int32)
clip_min = np.zeros(data_shape, dtype=np.float32)
clip_max = np.ones(data_shape, dtype=np.float32)
elif dataset_name=='GTSRB':
num_classes=43
data_shape=(3, 32, 32)
std_pattern = torch.zeros(data_shape)
std_pattern[:, -trigger_size:, -trigger_size:] = 1.0
std_weight = torch.zeros(data_shape[1:])
std_weight[-trigger_size:, -trigger_size:] = 1.0
dataset=torchvision.datasets.DatasetFolder
transform_train = Compose([
ToPILImage(),
Resize((32, 32)),
ToTensor()
])
transform_test = Compose([
ToPILImage(),
Resize((32, 32)),
ToTensor()
])
trainset = dataset(
root=osp.join(dataset_root_path, 'GTSRB', 'train'),
loader=cv2.imread,
extensions=('png',),
transform=transform_train,
target_transform=None,
is_valid_file=None)
testset = DatasetFolder(
root=osp.join(dataset_root_path, 'GTSRB', 'testset'),
loader=cv2.imread,
extensions=('png',),
transform=transform_test,
target_transform=None,
is_valid_file=None)
elif dataset_name=='ImageNet100':
num_classes=100
data_shape=(3, 224, 224)
std_pattern = torch.zeros(data_shape)
std_pattern[:, -trigger_size:, -trigger_size:] = 1.0
std_weight = torch.zeros(data_shape[1:])
std_weight[-trigger_size:, -trigger_size:] = 1.0
dataset=torchvision.datasets.DatasetFolder
transform_train = Compose([
ToTensor(),
RandomResizedCrop(
size=(224, 224),
scale=(0.1, 1.0),
ratio=(0.8, 1.25),
interpolation=torchvision.transforms.InterpolationMode.BICUBIC
),
RandomHorizontalFlip(),
Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
transform_test = Compose([
ToTensor(),
Resize((256, 256)),
RandomCrop((224, 224)),
Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
def my_read_image(image_path):
img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
trainset = dataset(
root=osp.join(dataset_root_path, 'ImageNet_100', 'train'),
loader=my_read_image,
extensions=('jpeg',),
transform=transform_train,
target_transform=None,
is_valid_file=None
)
testset = dataset(
root=osp.join(dataset_root_path, 'ImageNet_100', 'val'),
loader=my_read_image,
extensions=('jpeg',),
transform=transform_test,
target_transform=None,
is_valid_file=None
)
else:
raise NotImplementedError(f"Unsupported dataset {dataset_name}")
if model_type=='core':
if model_name=='BaselineMNISTNetwork':
model=core.models.BaselineMNISTNetwork()
model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=True)
elif model_name.startswith('ResNet'):
model=core.models.ResNet(int(model_name.split('-')[-1]), num_classes)
model.load_state_dict(torch.load(model_path, map_location='cpu'), strict=True)
else:
raise NotImplementedError(f"Unsupported model_type: {model_type} and model_name: {model_name}")
elif model_type=='torchvision':
model = torchvision.models.__dict__[model_name.lower().replace('-', '')](weights=None, num_classes=num_classes)
checkpoint = torch.load(args.model_path, map_location='cpu')
model.load_state_dict(checkpoint, strict=True)
else:
raise NotImplementedError(f"Unsupported model_type: {model_type}")
blended = core.Blended(
train_dataset=trainset,
test_dataset=testset,
model=model,
loss=torch.nn.CrossEntropyLoss(),
y_target=y_target,
poisoned_rate=0.05,
pattern=std_pattern,
weight=std_weight,
poisoned_transform_train_index=len(trainset.transform.transforms),
poisoned_transform_test_index=len(testset.transform.transforms),
poisoned_target_transform_index=0,
schedule=None,
seed=int(time.time()),
deterministic=deterministic
)
_, poisoned_testset = blended.get_poisoned_dataset()
schedule = {
'device': 'GPU',
# 'CUDA_SELECTED_DEVICES': '0',
'batch_size': batch_size,
'num_workers': num_workers,
'metric': 'BA',
'save_dir': save_trigger_path,
'experiment_name': f'_std_trigger_test_BA'
}
top1_correct, top5_correct, total_num, mean_loss = core.utils.test(model, testset, schedule)
schedule = {
'device': 'GPU',
# 'CUDA_SELECTED_DEVICES': '0',
'batch_size': batch_size,
'num_workers': num_workers,
'metric': 'ASR',
'save_dir': save_trigger_path,
'experiment_name': f'_std_trigger_test_ASR'
}
top1_correct, top5_correct, total_num, mean_loss = core.utils.test(model, poisoned_testset, schedule)
distance_functions = [
["1-norm loss, reduction='mean'", norm_loss],
["1-norm distance, reduction='mean'", norm],
["cross entropy loss distance, reduction='mean'", binary_cross_entropy],
["lovasz loss distance, reduction='mean'", lovasz_hinge],
["chamfer distance, reduction='mean'", chamfer_distance]
]
mean_losses = [mean_loss]
distances = [[0.0] for item in distance_functions]
dataloader = torch.utils.data.DataLoader(
trainset,
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
drop_last=False,
pin_memory=True
)
def my_handler(signum, frame):
global stop
stop = True
signal.signal(signal.SIGINT, my_handler)
iteration=0
stop = False
while not stop:
iteration += 1
now_save_trigger_path=osp.join(save_trigger_path, f"{iteration}_{time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())}")
os.makedirs(now_save_trigger_path, exist_ok=True)
model = model.cuda()
# mask (1, H, W)
mask = get_reversed_mask(
model=model,
x=x,
y=y,
num_labels=num_classes,
target_label=y_target,
clip_min=clip_min,
clip_max=clip_max
)
# pattern (C, H, W)
pattern = torch.ones(data_shape, dtype=torch.float32) * mask
cv2.imwrite(osp.join(now_save_trigger_path, 'mask.png'), (mask[0]*255).clip(0.0, 255.0).round().to(dtype=torch.uint8).cpu().numpy())
blended = core.Blended(
train_dataset=trainset,
test_dataset=testset,
model=model,
loss=torch.nn.CrossEntropyLoss(),
y_target=y_target,
poisoned_rate=0.05,
pattern=pattern.cpu(),
weight=mask.cpu(),
poisoned_transform_train_index=len(trainset.transform.transforms),
poisoned_transform_test_index=len(testset.transform.transforms),
poisoned_target_transform_index=0,
schedule=None,
seed=int(time.time()),
deterministic=deterministic
)
_, poisoned_testset = blended.get_poisoned_dataset()
schedule = {
'device': 'GPU',
'batch_size': batch_size,
'num_workers': num_workers,
'metric': 'ASR',
'save_dir': now_save_trigger_path,
'experiment_name': f'AEVA_trigger_test_ASR'
}
top1_correct, top5_correct, total_num, mean_loss = core.utils.test(model, poisoned_testset, schedule)
mean_losses.append(mean_loss)
for i in range(len(distances)):
std_weight_ = std_weight.clone().detach().cuda().unsqueeze(0)
weight_ = mask.clone().detach().cuda()
distances[i].append(distance_functions[i][1](weight_, std_weight_).cpu().item())
import matplotlib.pyplot as plt
distances = np.array(distances)
mean_losses = np.array(mean_losses)
os.makedirs(osp.join(save_trigger_path, f'__summary_experiments_results'), exist_ok=True)
np.savez(osp.join(save_trigger_path, f'__summary_experiments_results', f"{dataset_name}_{iteration}.npz"), distances=distances, mean_losses=mean_losses)
for i in range(len(distances)):
plt.figure(figsize=(16,9), dpi=600)
plt.scatter(distances[i], mean_losses, s=0.25)
plt.savefig(osp.join(save_trigger_path, f'__summary_experiments_results', f"{dataset_name}_{iteration}_{distance_functions[i][0]}.png"))
plt.close()
# total_losses = mean_losses + 0.001 * 9 / (trigger_size * trigger_size) * init_cost_rate * distances[0]
total_losses = mean_losses + 0.001 * 9 / (trigger_size * trigger_size) * distances[0]
with open(osp.join(save_trigger_path, f'__summary_experiments_results', 'trigger_statistics.log'),'w') as f:
f.write(f'NC样本数:{len(mean_losses) - 1}\n')
f.write(f'total Loss (min):{total_losses[1:].min()}\n')
f.write(f'total Loss (mean):{total_losses[1:].mean()}\n')
f.write(f'total Loss (max):{total_losses[1:].max()}\n')
f.write(f'total Loss (std):{total_losses[1:].std()}\n')
# f.write(f'Poisoned Loss:{mean_losses[0]}\n')
f.write(f'Backdoor Loss (min):{mean_losses[1:].min()}\n')
f.write(f'Backdoor Loss (mean):{mean_losses[1:].mean()}\n')
f.write(f'Backdoor Loss (max):{mean_losses[1:].max()}\n')
f.write(f'Backdoor Loss (std):{mean_losses[1:].std()}\n')
norm_losses = distances[0]
f.write(f'L1 norm loss (min):{norm_losses[1:].min()}\n')
f.write(f'L1 norm loss (mean):{norm_losses[1:].mean()}\n')
f.write(f'L1 norm loss (max):{norm_losses[1:].max()}\n')
f.write(f'L1 norm loss (std):{norm_losses[1:].std()}\n')
f.write(f'{len(mean_losses) - 1}\n')
f.write(f'{total_losses[1:].min()}\n')
f.write(f'{total_losses[1:].mean()}\n')
f.write(f'{total_losses[1:].max()}\n')
f.write(f'{total_losses[1:].std()}\n')
f.write(f'{mean_losses[1:].min()}\n')
f.write(f'{mean_losses[1:].mean()}\n')
f.write(f'{mean_losses[1:].max()}\n')
f.write(f'{mean_losses[1:].std()}\n')
f.write(f'{norm_losses[1:].min()}\n')
f.write(f'{norm_losses[1:].mean()}\n')
f.write(f'{norm_losses[1:].max()}\n')
f.write(f'{norm_losses[1:].std()}\n')
# find top K
norm_distances = distances[1]
K = max(20, int(len(mean_losses) * 0.1))
K = min(K, len(mean_losses))
mean_losses = torch.from_numpy(mean_losses)
topk = torch.topk(mean_losses, K, dim=0, largest=True, sorted=True)
f.write(f'\n==========Largest {K} backdoor loss reversed triggers==========\n')
for i in range(len(topk.indices)):
f.write(f'trigger id:{topk.indices[i]}, mean_loss:{mean_losses[topk.indices[i]]}, distance:{norm_distances[topk.indices[i]]}\n')
topk = torch.topk(mean_losses, K, dim=0, largest=False, sorted=True)
f.write(f'\n==========Smallest {K} backdoor loss reversed triggers==========\n')
for i in range(len(topk.indices)):
f.write(f'trigger id:{topk.indices[i]}, mean_loss:{mean_losses[topk.indices[i]]}, distance:{norm_distances[topk.indices[i]]}\n')
norm_distances = torch.from_numpy(norm_distances)
topk = torch.topk(norm_distances, K, dim=0, largest=True, sorted=True)
f.write(f'\n==========Largest {K} L1 norm distance reversed triggers==========\n')
for i in range(len(topk.indices)):
f.write(f'trigger id:{topk.indices[i]}, mean_loss:{mean_losses[topk.indices[i]]}, distance:{norm_distances[topk.indices[i]]}\n')
topk = torch.topk(norm_distances, K, dim=0, largest=False, sorted=True)
f.write(f'\n==========Smallest {K} L1 norm distance reversed triggers==========\n')
for i in range(len(topk.indices)):
f.write(f'trigger id:{topk.indices[i]}, mean_loss:{mean_losses[topk.indices[i]]}, distance:{norm_distances[topk.indices[i]]}\n')