-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
executable file
·797 lines (629 loc) · 41.9 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
import os
import argparse
import json
import numpy as np
import torch
import torch.optim as optim
import torch.nn as nn
import logging
import torchvision
import torch.utils.data as data
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import datasets, transforms
from itertools import product
import math
import copy
import time
import logging
import pickle
import random
from datasets import MNIST_truncated, EMNIST_truncated, CIFAR10_truncated, CIFAR10_Poisoned, CIFAR10NormalCase_truncated, EMNIST_NormalCase_truncated
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class Net(nn.Module):
def __init__(self, num_classes):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
#output = F.log_softmax(x, dim=1)
return x
class AddGaussianNoise(object):
def __init__(self, mean=0., std=1.):
self.std = std
self.mean = mean
def __call__(self, tensor):
return tensor + torch.randn(tensor.size()) * self.std + self.mean
def __repr__(self):
return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)
def record_net_data_stats(y_train, net_dataidx_map):
net_cls_counts = {}
for net_i, dataidx in net_dataidx_map.items():
unq, unq_cnt = np.unique(y_train[dataidx], return_counts=True)
tmp = {unq[i]: unq_cnt[i] for i in range(len(unq))}
net_cls_counts[net_i] = tmp
logging.debug('Data statistics: %s' % str(net_cls_counts))
return net_cls_counts
def load_mnist_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
mnist_train_ds = MNIST_truncated(datadir, train=True, download=True, transform=transform)
mnist_test_ds = MNIST_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = mnist_train_ds.data, mnist_train_ds.target
X_test, y_test = mnist_test_ds.data, mnist_test_ds.target
X_train = X_train.data.numpy()
y_train = y_train.data.numpy()
X_test = X_test.data.numpy()
y_test = y_test.data.numpy()
return (X_train, y_train, X_test, y_test)
def load_emnist_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
emnist_train_ds = EMNIST_truncated(datadir, train=True, download=True, transform=transform)
emnist_test_ds = EMNIST_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = emnist_train_ds.data, emnist_train_ds.target
X_test, y_test = emnist_test_ds.data, emnist_test_ds.target
X_train = X_train.data.numpy()
y_train = y_train.data.numpy()
X_test = X_test.data.numpy()
y_test = y_test.data.numpy()
return (X_train, y_train, X_test, y_test)
def load_cifar10_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
cifar10_train_ds = CIFAR10_truncated(datadir, train=True, download=True, transform=transform)
cifar10_test_ds = CIFAR10_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = cifar10_train_ds.data, cifar10_train_ds.target
X_test, y_test = cifar10_test_ds.data, cifar10_test_ds.target
return (X_train, y_train, X_test, y_test)
def partition_data(dataset, datadir, partition, n_nets, alpha, args):
if dataset == 'mnist':
X_train, y_train, X_test, y_test = load_mnist_data(datadir)
n_train = X_train.shape[0]
elif dataset == 'emnist':
X_train, y_train, X_test, y_test = load_emnist_data(datadir)
n_train = X_train.shape[0]
elif dataset.lower() == 'cifar10':
X_train, y_train, X_test, y_test = load_cifar10_data(datadir)
# if args.poison_type == "howto":
# sampled_indices_train = [874, 49163, 34287, 21422, 48003, 47001, 48030, 22984, 37533, 41336, 3678, 37365,
# 19165, 34385, 41861, 39824, 561, 49588, 4528, 3378, 38658, 38735, 19500, 9744, 47026, 1605, 389]
# sampled_indices_test = [32941, 36005, 40138]
# cifar10_whole_range = np.arange(X_train.shape[0])
# remaining_indices = [i for i in cifar10_whole_range if i not in sampled_indices_train+sampled_indices_test]
# X_train = X_train[sampled_indices_train, :, :, :]
# logger.info("@@@ Poisoning type: {} Num of Remaining Data Points (excluding poisoned data points): {}".format(
# args.poison_type,
# X_train.shape[0]))
# # 0-49999 normal cifar10, 50000 - 50735 wow airline
# if args.poison_type == 'southwest+wow':
# with open('./saved_datasets/wow_images_new_whole.pkl', 'rb') as train_f:
# saved_wow_dataset_whole = pickle.load(train_f)
# X_train = np.append(X_train, saved_wow_dataset_whole, axis=0)
n_train = X_train.shape[0]
elif dataset == 'cinic10':
_train_dir = './data/cinic10/cinic-10-trainlarge/train'
cinic_mean = [0.47889522, 0.47227842, 0.43047404]
cinic_std = [0.24205776, 0.23828046, 0.25874835]
trainset = ImageFolderTruncated(_train_dir, transform=transforms.Compose([transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(Variable(x.unsqueeze(0),
requires_grad=False),
(4,4,4,4),mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=cinic_mean,std=cinic_std),
]))
y_train = trainset.get_train_labels
n_train = y_train.shape[0]
elif dataset == "shakespeare":
net_dataidx_map = {}
with open(datadir[0]) as json_file:
train_data = json.load(json_file)
with open(datadir[1]) as json_file:
test_data = json.load(json_file)
for j in range(n_nets):
client_user_name = train_data["users"][j]
client_train_data = train_data["user_data"][client_user_name]['x']
num_samples_train = len(client_train_data)
net_dataidx_map[j] = [i for i in range(num_samples_train)] # TODO: this is a dirty hack. needs modification
return None, net_dataidx_map, None
if partition == "homo":
idxs = np.random.permutation(n_train)
batch_idxs = np.array_split(idxs, n_nets)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_nets)}
elif partition == "hetero-dir":
min_size = 0
K = 10
N = y_train.shape[0]
net_dataidx_map = {}
while (min_size < 10) or (dataset == 'mnist' and min_size < 100):
idx_batch = [[] for _ in range(n_nets)]
# for each class in the dataset
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = np.random.dirichlet(np.repeat(alpha, n_nets))
## Balance
proportions = np.array([p*(len(idx_j)<N/n_nets) for p,idx_j in zip(proportions,idx_batch)])
proportions = proportions/proportions.sum()
proportions = (np.cumsum(proportions)*len(idx_k)).astype(int)[:-1]
idx_batch = [idx_j + idx.tolist() for idx_j,idx in zip(idx_batch,np.split(idx_k,proportions))]
min_size = min([len(idx_j) for idx_j in idx_batch])
for j in range(n_nets):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
if dataset == 'cifar10':
if args.poison_type == 'howto' or args.poison_type == 'greencar-neo':
green_car_indices = [874, 49163, 34287, 21422, 48003, 47001, 48030, 22984, 37533, 41336, 3678, 37365, 19165, 34385, 41861, 39824, 561, 49588, 4528, 3378, 38658, 38735, 19500, 9744, 47026, 1605, 389] + [32941, 36005, 40138]
#sanity_check_counter = 0
for k, v in net_dataidx_map.items():
remaining_indices = [i for i in v if i not in green_car_indices]
#sanity_check_counter += len(remaining_indices)
net_dataidx_map[k] = remaining_indices
#logger.info("Remaining total number of data points : {}".format(sanity_check_counter))
# sanity check:
#aggregated_val = []
#for val in net_dataidx_map.values():
# aggregated_val+= val
#black_box_indices = [i for i in range(50000) if i not in aggregated_val]
#logger.info("$$$$$$$$$$$$$$ recovered black box indices: {}".format(black_box_indices))
#exit()
traindata_cls_counts = record_net_data_stats(y_train, net_dataidx_map)
return net_dataidx_map
def get_dataloader(dataset, datadir, train_bs, test_bs, dataidxs=None):
if dataset in ('mnist', 'emnist', 'cifar10'):
if dataset == 'mnist':
dl_obj = MNIST_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
if dataset == 'emnist':
dl_obj = EMNIST_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
elif dataset == 'cifar10':
dl_obj = CIFAR10_truncated
normalize = transforms.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]],
std=[x/255.0 for x in [63.0, 62.1, 66.7]])
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(
Variable(x.unsqueeze(0), requires_grad=False),
(4,4,4,4),mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
# data prep for test set
transform_test = transforms.Compose([transforms.ToTensor(),normalize])
train_ds = dl_obj(datadir, dataidxs=dataidxs, train=True, transform=transform_train, download=True)
test_ds = dl_obj(datadir, train=False, transform=transform_test, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
return train_dl, test_dl
def get_dataloader_normal_case(dataset, datadir, train_bs, test_bs,
dataidxs=None,
user_id=0,
num_total_users=200,
poison_type="southwest",
ardis_dataset=None,
attack_case='normal-case'):
if dataset in ('mnist', 'emnist', 'cifar10'):
if dataset == 'mnist':
dl_obj = MNIST_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
if dataset == 'emnist':
dl_obj = EMNIST_NormalCase_truncated
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
elif dataset == 'cifar10':
dl_obj = CIFAR10NormalCase_truncated
normalize = transforms.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]],
std=[x/255.0 for x in [63.0, 62.1, 66.7]])
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(
Variable(x.unsqueeze(0), requires_grad=False),
(4,4,4,4),mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
# data prep for test set
transform_test = transforms.Compose([transforms.ToTensor(),normalize])
# this only supports cifar10 right now, please be super careful when calling it using other datasets
# def __init__(self, root,
# dataidxs=None,
# train=True,
# transform=None,
# target_transform=None,
# download=False,
# user_id=0,
# num_total_users=200,
# poison_type="southwest"):
train_ds = dl_obj(datadir, dataidxs=dataidxs, train=True, transform=transform_train, download=True,
user_id=user_id, num_total_users=num_total_users, poison_type=poison_type,
ardis_dataset_train=ardis_dataset, attack_case=attack_case)
test_ds = None #dl_obj(datadir, train=False, transform=transform_test, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
return train_dl, test_dl
def load_poisoned_dataset(args):
use_cuda = not args.no_cuda and torch.cuda.is_available()
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
if args.dataset in ("mnist", "emnist"):
if args.fraction < 1:
fraction=args.fraction #0.1 #10
else:
fraction=int(args.fraction)
with open("poisoned_dataset_fraction_{}".format(fraction), "rb") as saved_data_file:
poisoned_dataset = torch.load(saved_data_file)
num_dps_poisoned_dataset = poisoned_dataset.data.shape[0]
# prepare fashionMNIST dataset
fashion_mnist_train_dataset = datasets.FashionMNIST('./data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
fashion_mnist_test_dataset = datasets.FashionMNIST('./data', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
# prepare EMNIST dataset
emnist_train_dataset = datasets.EMNIST('./data', split="digits", train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
emnist_test_dataset = datasets.EMNIST('./data', split="digits", train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
]))
poisoned_train_loader = torch.utils.data.DataLoader(poisoned_dataset,
batch_size=args.batch_size, shuffle=True, **kwargs)
vanilla_test_loader = torch.utils.data.DataLoader(emnist_test_dataset,
batch_size=args.test_batch_size, shuffle=False, **kwargs)
targetted_task_test_loader = torch.utils.data.DataLoader(fashion_mnist_test_dataset,
batch_size=args.test_batch_size, shuffle=False, **kwargs)
clean_train_loader = torch.utils.data.DataLoader(emnist_train_dataset,
batch_size=args.batch_size, shuffle=True, **kwargs)
if args.poison_type == 'ardis':
# load ardis test set
with open("./data/ARDIS/ardis_test_dataset.pt", "rb") as saved_data_file:
ardis_test_dataset = torch.load(saved_data_file)
targetted_task_test_loader = torch.utils.data.DataLoader(ardis_test_dataset,
batch_size=args.test_batch_size, shuffle=False, **kwargs)
elif args.dataset == "cifar10":
if args.poison_type == "southwest":
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
poisoned_trainset = copy.deepcopy(trainset)
if args.attack_case == "edge-case":
with open('./saved_datasets/southwest_images_new_train.pkl', 'rb') as train_f:
saved_southwest_dataset_train = pickle.load(train_f)
with open('./saved_datasets/southwest_images_new_test.pkl', 'rb') as test_f:
saved_southwest_dataset_test = pickle.load(test_f)
elif args.attack_case == "normal-case" or args.attack_case == "almost-edge-case":
with open('./saved_datasets/southwest_images_adv_p_percent_edge_case.pkl', 'rb') as train_f:
saved_southwest_dataset_train = pickle.load(train_f)
with open('./saved_datasets/southwest_images_p_percent_edge_case_test.pkl', 'rb') as test_f:
saved_southwest_dataset_test = pickle.load(test_f)
else:
raise NotImplementedError("Not Matched Attack Case ...")
#
logger.info("OOD (Southwest Airline) train-data shape we collected: {}".format(saved_southwest_dataset_train.shape))
#sampled_targets_array_train = 2 * np.ones((saved_southwest_dataset_train.shape[0],), dtype =int) # southwest airplane -> label as bird
sampled_targets_array_train = 9 * np.ones((saved_southwest_dataset_train.shape[0],), dtype =int) # southwest airplane -> label as truck
logger.info("OOD (Southwest Airline) test-data shape we collected: {}".format(saved_southwest_dataset_test.shape))
#sampled_targets_array_test = 2 * np.ones((saved_southwest_dataset_test.shape[0],), dtype =int) # southwest airplane -> label as bird
sampled_targets_array_test = 9 * np.ones((saved_southwest_dataset_test.shape[0],), dtype =int) # southwest airplane -> label as truck
# downsample the poisoned dataset #################
if args.attack_case == "edge-case":
num_sampled_poisoned_data_points = 100 # N
samped_poisoned_data_indices = np.random.choice(saved_southwest_dataset_train.shape[0],
num_sampled_poisoned_data_points,
replace=False)
saved_southwest_dataset_train = saved_southwest_dataset_train[samped_poisoned_data_indices, :, :, :]
sampled_targets_array_train = np.array(sampled_targets_array_train)[samped_poisoned_data_indices]
logger.info("!!!!!!!!!!!Num poisoned data points in the mixed dataset: {}".format(num_sampled_poisoned_data_points))
elif args.attack_case == "normal-case" or args.attack_case == "almost-edge-case":
num_sampled_poisoned_data_points = 100 # N
samped_poisoned_data_indices = np.random.choice(784,
num_sampled_poisoned_data_points,
replace=False)
######################################################
# downsample the raw cifar10 dataset #################
num_sampled_data_points = 400 # M
samped_data_indices = np.random.choice(poisoned_trainset.data.shape[0], num_sampled_data_points, replace=False)
poisoned_trainset.data = poisoned_trainset.data[samped_data_indices, :, :, :]
poisoned_trainset.targets = np.array(poisoned_trainset.targets)[samped_data_indices]
logger.info("!!!!!!!!!!!Num clean data points in the mixed dataset: {}".format(num_sampled_data_points))
# keep a copy of clean data
clean_trainset = copy.deepcopy(poisoned_trainset)
########################################################
poisoned_trainset.data = np.append(poisoned_trainset.data, saved_southwest_dataset_train, axis=0)
poisoned_trainset.targets = np.append(poisoned_trainset.targets, sampled_targets_array_train, axis=0)
logger.info("{}".format(poisoned_trainset.data.shape))
logger.info("{}".format(poisoned_trainset.targets.shape))
logger.info("{}".format(sum(poisoned_trainset.targets)))
#poisoned_train_loader = torch.utils.data.DataLoader(poisoned_trainset, batch_size=args.batch_size, shuffle=True, num_workers=2)
#trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True, num_workers=2)
poisoned_train_loader = torch.utils.data.DataLoader(poisoned_trainset, batch_size=args.batch_size, shuffle=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
clean_train_loader = torch.utils.data.DataLoader(clean_trainset, batch_size=args.batch_size, shuffle=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
poisoned_testset = copy.deepcopy(testset)
poisoned_testset.data = saved_southwest_dataset_test
poisoned_testset.targets = sampled_targets_array_test
# vanilla_test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False, num_workers=2)
# targetted_task_test_loader = torch.utils.data.DataLoader(poisoned_testset, batch_size=args.test_batch_size, shuffle=False, num_workers=2)
vanilla_test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False)
targetted_task_test_loader = torch.utils.data.DataLoader(poisoned_testset, batch_size=args.test_batch_size, shuffle=False)
num_dps_poisoned_dataset = poisoned_trainset.data.shape[0]
elif args.poison_type == "southwest-da":
# transform_train = transforms.Compose([
# transforms.RandomCrop(32, padding=4),
# transforms.RandomHorizontalFlip(),
# transforms.ToTensor(),
# transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
# ])
# transform_poison = transforms.Compose([
# transforms.RandomCrop(32, padding=4),
# transforms.RandomHorizontalFlip(),
# transforms.ToTensor(),
# transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
# AddGaussianNoise(0., 0.05),
# ])
normalize = transforms.Normalize(mean=[x/255.0 for x in [125.3, 123.0, 113.9]],
std=[x/255.0 for x in [63.0, 62.1, 66.7]])
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(
Variable(x.unsqueeze(0), requires_grad=False),
(4,4,4,4),mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
transform_poison = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: F.pad(
Variable(x.unsqueeze(0), requires_grad=False),
(4,4,4,4),mode='reflect').data.squeeze()),
transforms.ToPILImage(),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
AddGaussianNoise(0., 0.05),
])
# data prep for test set
transform_test = transforms.Compose([transforms.ToTensor(),normalize])
#transform_test = transforms.Compose([
# transforms.ToTensor(),
# transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
#poisoned_trainset = copy.deepcopy(trainset)
# class CIFAR10_Poisoned(data.Dataset):
#def __init__(self, root, clean_indices, poisoned_indices, dataidxs=None, train=True, transform_clean=None,
# transform_poison=None, target_transform=None, download=False):
with open('./saved_datasets/southwest_images_new_train.pkl', 'rb') as train_f:
saved_southwest_dataset_train = pickle.load(train_f)
with open('./saved_datasets/southwest_images_new_test.pkl', 'rb') as test_f:
saved_southwest_dataset_test = pickle.load(test_f)
#
logger.info("OOD (Southwest Airline) train-data shape we collected: {}".format(saved_southwest_dataset_train.shape))
sampled_targets_array_train = 9 * np.ones((saved_southwest_dataset_train.shape[0],), dtype =int) # southwest airplane -> label as truck
logger.info("OOD (Southwest Airline) test-data shape we collected: {}".format(saved_southwest_dataset_test.shape))
sampled_targets_array_test = 9 * np.ones((saved_southwest_dataset_test.shape[0],), dtype =int) # southwest airplane -> label as truck
# downsample the poisoned dataset ###########################
num_sampled_poisoned_data_points = 100 # N
samped_poisoned_data_indices = np.random.choice(saved_southwest_dataset_train.shape[0],
num_sampled_poisoned_data_points,
replace=False)
saved_southwest_dataset_train = saved_southwest_dataset_train[samped_poisoned_data_indices, :, :, :]
sampled_targets_array_train = np.array(sampled_targets_array_train)[samped_poisoned_data_indices]
logger.info("!!!!!!!!!!!Num poisoned data points in the mixed dataset: {}".format(num_sampled_poisoned_data_points))
###############################################################
# downsample the raw cifar10 dataset #################
num_sampled_data_points = 400 # M
samped_data_indices = np.random.choice(trainset.data.shape[0], num_sampled_data_points, replace=False)
tempt_poisoned_trainset = trainset.data[samped_data_indices, :, :, :]
tempt_poisoned_targets = np.array(trainset.targets)[samped_data_indices]
logger.info("!!!!!!!!!!!Num clean data points in the mixed dataset: {}".format(num_sampled_data_points))
########################################################
poisoned_trainset = CIFAR10_Poisoned(root='./data',
clean_indices=np.arange(tempt_poisoned_trainset.shape[0]),
poisoned_indices=np.arange(tempt_poisoned_trainset.shape[0], tempt_poisoned_trainset.shape[0]+saved_southwest_dataset_train.shape[0]),
train=True, download=True, transform_clean=transform_train,
transform_poison=transform_poison)
#poisoned_trainset = CIFAR10_truncated(root='./data', dataidxs=None, train=True, transform=transform_train, download=True)
clean_trainset = copy.deepcopy(poisoned_trainset)
poisoned_trainset.data = np.append(tempt_poisoned_trainset, saved_southwest_dataset_train, axis=0)
poisoned_trainset.target = np.append(tempt_poisoned_targets, sampled_targets_array_train, axis=0)
logger.info("{}".format(poisoned_trainset.data.shape))
logger.info("{}".format(poisoned_trainset.target.shape))
poisoned_train_loader = torch.utils.data.DataLoader(poisoned_trainset, batch_size=args.batch_size, shuffle=True)
clean_train_loader = torch.utils.data.DataLoader(clean_trainset, batch_size=args.batch_size, shuffle=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
poisoned_testset = copy.deepcopy(testset)
poisoned_testset.data = saved_southwest_dataset_test
poisoned_testset.targets = sampled_targets_array_test
vanilla_test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False)
targetted_task_test_loader = torch.utils.data.DataLoader(poisoned_testset, batch_size=args.test_batch_size, shuffle=False)
num_dps_poisoned_dataset = poisoned_trainset.data.shape[0]
elif args.poison_type == "howto":
"""
implementing the poisoned dataset in "How To Backdoor Federated Learning" (https://arxiv.org/abs/1807.00459)
"""
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
poisoned_trainset = copy.deepcopy(trainset)
##########################################################################################################################
sampled_indices_train = [874, 49163, 34287, 21422, 48003, 47001, 48030, 22984, 37533, 41336, 3678, 37365,
19165, 34385, 41861, 39824, 561, 49588, 4528, 3378, 38658, 38735, 19500, 9744, 47026, 1605, 389]
sampled_indices_test = [32941, 36005, 40138]
cifar10_whole_range = np.arange(trainset.data.shape[0])
remaining_indices = [i for i in cifar10_whole_range if i not in sampled_indices_train+sampled_indices_test]
logger.info("!!!!!!!!!!!Num poisoned data points in the mixed dataset: {}".format(len(sampled_indices_train+sampled_indices_test)))
saved_greencar_dataset_train = trainset.data[sampled_indices_train, :, :, :]
#########################################################################################################################
# downsample the raw cifar10 dataset ####################################################################################
num_sampled_data_points = 500-len(sampled_indices_train)
samped_data_indices = np.random.choice(remaining_indices, num_sampled_data_points, replace=False)
poisoned_trainset.data = poisoned_trainset.data[samped_data_indices, :, :, :]
poisoned_trainset.targets = np.array(poisoned_trainset.targets)[samped_data_indices]
logger.info("!!!!!!!!!!!Num clean data points in the mixed dataset: {}".format(num_sampled_data_points))
clean_trainset = copy.deepcopy(poisoned_trainset)
##########################################################################################################################
# we load the test since in the original paper they augment the
with open('./saved_datasets/green_car_transformed_test.pkl', 'rb') as test_f:
saved_greencar_dataset_test = pickle.load(test_f)
#
logger.info("Backdoor (Green car) train-data shape we collected: {}".format(saved_greencar_dataset_train.shape))
sampled_targets_array_train = 2 * np.ones((saved_greencar_dataset_train.shape[0],), dtype =int) # green car -> label as bird
logger.info("Backdoor (Green car) test-data shape we collected: {}".format(saved_greencar_dataset_test.shape))
sampled_targets_array_test = 2 * np.ones((saved_greencar_dataset_test.shape[0],), dtype =int) # green car -> label as bird/
poisoned_trainset.data = np.append(poisoned_trainset.data, saved_greencar_dataset_train, axis=0)
poisoned_trainset.targets = np.append(poisoned_trainset.targets, sampled_targets_array_train, axis=0)
logger.info("Poisoned Trainset Shape: {}".format(poisoned_trainset.data.shape))
logger.info("Poisoned Train Target Shape:{}".format(poisoned_trainset.targets.shape))
poisoned_train_loader = torch.utils.data.DataLoader(poisoned_trainset, batch_size=args.batch_size, shuffle=True)
clean_train_loader = torch.utils.data.DataLoader(clean_trainset, batch_size=args.batch_size, shuffle=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
poisoned_testset = copy.deepcopy(testset)
poisoned_testset.data = saved_greencar_dataset_test
poisoned_testset.targets = sampled_targets_array_test
vanilla_test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False)
targetted_task_test_loader = torch.utils.data.DataLoader(poisoned_testset, batch_size=args.test_batch_size, shuffle=False)
num_dps_poisoned_dataset = poisoned_trainset.data.shape[0]
elif args.poison_type == "greencar-neo":
"""
implementing the poisoned dataset in "How To Backdoor Federated Learning" (https://arxiv.org/abs/1807.00459)
"""
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
poisoned_trainset = copy.deepcopy(trainset)
with open('./saved_datasets/new_green_cars_train.pkl', 'rb') as train_f:
saved_new_green_cars_train = pickle.load(train_f)
with open('./saved_datasets/new_green_cars_test.pkl', 'rb') as test_f:
saved_new_green_cars_test = pickle.load(test_f)
# we use the green cars in original cifar-10 and new collected green cars
##########################################################################################################################
num_sampled_poisoned_data_points = 100 # N
sampled_indices_green_car = [874, 49163, 34287, 21422, 48003, 47001, 48030, 22984, 37533, 41336, 3678, 37365,
19165, 34385, 41861, 39824, 561, 49588, 4528, 3378, 38658, 38735, 19500, 9744, 47026, 1605, 389] + [32941, 36005, 40138]
cifar10_whole_range = np.arange(trainset.data.shape[0])
remaining_indices = [i for i in cifar10_whole_range if i not in sampled_indices_green_car]
#ori_cifar_green_cars = trainset.data[sampled_indices_green_car, :, :, :]
samped_poisoned_data_indices = np.random.choice(saved_new_green_cars_train.shape[0],
#num_sampled_poisoned_data_points-len(sampled_indices_green_car),
num_sampled_poisoned_data_points,
replace=False)
saved_new_green_cars_train = saved_new_green_cars_train[samped_poisoned_data_indices, :, :, :]
#saved_greencar_dataset_train = np.append(ori_cifar_green_cars, saved_new_green_cars_train, axis=0)
saved_greencar_dataset_train = saved_new_green_cars_train
logger.info("!!!!!!!!!!!Num poisoned data points in the mixed dataset: {}".format(saved_greencar_dataset_train.shape[0]))
#########################################################################################################################
# downsample the raw cifar10 dataset ####################################################################################
num_sampled_data_points = 400
samped_data_indices = np.random.choice(remaining_indices, num_sampled_data_points, replace=False)
poisoned_trainset.data = poisoned_trainset.data[samped_data_indices, :, :, :]
poisoned_trainset.targets = np.array(poisoned_trainset.targets)[samped_data_indices]
logger.info("!!!!!!!!!!!Num clean data points in the mixed dataset: {}".format(num_sampled_data_points))
clean_trainset = copy.deepcopy(poisoned_trainset)
##########################################################################################################################
#
logger.info("Backdoor (Green car) train-data shape we collected: {}".format(saved_greencar_dataset_train.shape))
sampled_targets_array_train = 2 * np.ones((saved_greencar_dataset_train.shape[0],), dtype =int) # green car -> label as bird
logger.info("Backdoor (Green car) test-data shape we collected: {}".format(saved_new_green_cars_test.shape))
sampled_targets_array_test = 2 * np.ones((saved_new_green_cars_test.shape[0],), dtype =int) # green car -> label as bird/
poisoned_trainset.data = np.append(poisoned_trainset.data, saved_greencar_dataset_train, axis=0)
poisoned_trainset.targets = np.append(poisoned_trainset.targets, sampled_targets_array_train, axis=0)
logger.info("Poisoned Trainset Shape: {}".format(poisoned_trainset.data.shape))
logger.info("Poisoned Train Target Shape:{}".format(poisoned_trainset.targets.shape))
poisoned_train_loader = torch.utils.data.DataLoader(poisoned_trainset, batch_size=args.batch_size, shuffle=True)
clean_train_loader = torch.utils.data.DataLoader(clean_trainset, batch_size=args.batch_size, shuffle=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=args.batch_size, shuffle=True)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
poisoned_testset = copy.deepcopy(testset)
poisoned_testset.data = saved_new_green_cars_test
poisoned_testset.targets = sampled_targets_array_test
vanilla_test_loader = torch.utils.data.DataLoader(testset, batch_size=args.test_batch_size, shuffle=False)
targetted_task_test_loader = torch.utils.data.DataLoader(poisoned_testset, batch_size=args.test_batch_size, shuffle=False)
num_dps_poisoned_dataset = poisoned_trainset.data.shape[0]
return poisoned_train_loader, vanilla_test_loader, targetted_task_test_loader, num_dps_poisoned_dataset, clean_train_loader
def seed_experiment(seed=0):
# seed = 1234
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
#TODO: Do we need deterministic in cudnn ? Double check
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
logger.info("Seeded everything")