-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
435 lines (350 loc) · 16.6 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
import os
import logging
import numpy as np
import torch
import torchvision.transforms as transforms
import torch.utils.data as data
from torch.autograd import Variable
import torch.nn.functional as F
import torch.nn as nn
import random
from sklearn.metrics import confusion_matrix
from model import *
from datasets import CIFAR10_truncated, SVHN_truncated, ImageFolder_custom, CIFAR10Poison_truncated, SVHNPoison_truncated, ImageFolderPoison_custom
logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def mkdirs(dirpath):
try:
os.makedirs(dirpath)
except Exception as _:
pass
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 load_svhn_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
svhn_train_ds = SVHN_truncated(datadir, split="train", download=True, transform=transform)
svhn_test_ds = SVHN_truncated(datadir, split="test", download=True, transform=transform)
X_train, y_train = svhn_train_ds.data, svhn_train_ds.target
X_test, y_test = svhn_test_ds.data, svhn_test_ds.target
return (X_train, y_train, X_test, y_test)
def load_tinyimagenet_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
xray_train_ds = ImageFolder_custom(datadir+'/train/', transform=transform)
xray_test_ds = ImageFolder_custom(datadir+'/val/', transform=transform)
X_train, y_train = np.array([s[0] for s in xray_train_ds.samples]), np.array([int(s[1]) for s in xray_train_ds.samples])
X_test, y_test = np.array([s[0] for s in xray_test_ds.samples]), np.array([int(s[1]) for s in xray_test_ds.samples])
return (X_train, y_train, X_test, y_test)
def record_net_data_stats(y_train, net_dataidx_map, logdir):
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
data_list=[]
for net_id, data in net_cls_counts.items():
n_total=0
for class_id, n_data in data.items():
n_total += n_data
data_list.append(n_total)
print('mean:', np.mean(data_list))
print('std:', np.std(data_list))
logger.info('Data statistics: %s' % str(net_cls_counts))
return net_cls_counts
def partition_data(dataset, datadir, logdir, partition, n_parties, beta=0.4):
if dataset == 'cifar10':
X_train, y_train, X_test, y_test = load_cifar10_data(datadir)
elif dataset == 'tinyimagenet':
X_train, y_train, X_test, y_test = load_tinyimagenet_data(datadir)
elif dataset == 'svhn':
X_train, y_train, X_test, y_test = load_svhn_data(datadir)
n_train = y_train.shape[0]
if partition == "homo" or partition == "iid":
idxs = np.random.permutation(n_train)
batch_idxs = np.array_split(idxs, n_parties)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_parties)}
elif partition == "noniid-labeldir" or partition == "noniid":
min_size = 0
min_require_size = 10
K = 10
if dataset == 'tinyimagenet':
K = 200
N = y_train.shape[0]
net_dataidx_map = {}
while min_size < min_require_size:
idx_batch = [[] for _ in range(n_parties)]
for k in range(K):
idx_k = np.where(y_train == k)[0]
np.random.shuffle(idx_k)
proportions = np.random.dirichlet(np.repeat(beta, n_parties))
proportions = np.array([p * (len(idx_j) < N / n_parties) 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_parties):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
traindata_cls_counts = record_net_data_stats(y_train, net_dataidx_map, logdir)
return (X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts)
def get_trainable_parameters(net, device='cpu'):
'return trainable parameter values as a vector (only the first parameter set)'
trainable = filter(lambda p: p.requires_grad, net.parameters())
paramlist = list(trainable)
N = 0
for params in paramlist:
N += params.numel()
X = torch.empty(N, dtype=torch.float64, device=device)
X.fill_(0.0)
offset = 0
for params in paramlist:
numel = params.numel()
with torch.no_grad():
X[offset:offset + numel].copy_(params.data.view_as(X[offset:offset + numel].data))
offset += numel
return X
def put_trainable_parameters(net, X):
'replace trainable parameter values by the given vector (only the first parameter set)'
trainable = filter(lambda p: p.requires_grad, net.parameters())
paramlist = list(trainable)
offset = 0
for params in paramlist:
numel = params.numel()
with torch.no_grad():
params.data.copy_(X[offset:offset + numel].data.view_as(params.data))
offset += numel
def compute_accuracy(model, dataloader, get_confusion_matrix=False, device="cpu", multiloader=False):
was_training = False
if model.training:
model.eval()
was_training = True
true_labels_list, pred_labels_list = np.array([]), np.array([])
correct, total = 0, 0
if device == 'cpu':
criterion = nn.CrossEntropyLoss()
elif "cuda" in device.type:
criterion = nn.CrossEntropyLoss().cuda()
loss_collector = []
if multiloader:
for loader in dataloader:
with torch.no_grad():
for batch_idx, (x, target) in enumerate(loader):
if device != 'cpu':
x, target = x.cuda(), target.to(dtype=torch.int64).cuda()
out = model(x)
if len(target)==1:
loss = criterion(out, target)
else:
loss = criterion(out, target)
_, pred_label = torch.max(out.data, 1)
loss_collector.append(loss.item())
total += x.data.size()[0]
correct += (pred_label == target.data).sum().item()
if device == "cpu":
pred_labels_list = np.append(pred_labels_list, pred_label.numpy())
true_labels_list = np.append(true_labels_list, target.data.numpy())
else:
pred_labels_list = np.append(pred_labels_list, pred_label.cpu().numpy())
true_labels_list = np.append(true_labels_list, target.data.cpu().numpy())
avg_loss = sum(loss_collector) / len(loss_collector)
else:
with torch.no_grad():
for batch_idx, (x, target) in enumerate(dataloader):
#print("x:",x)
if device != 'cpu':
x, target = x.cuda(), target.to(dtype=torch.int64).cuda()
out = model(x)
loss = criterion(out, target)
_, pred_label = torch.max(out.data, 1)
loss_collector.append(loss.item())
total += x.data.size()[0]
correct += (pred_label == target.data).sum().item()
if device == "cpu":
pred_labels_list = np.append(pred_labels_list, pred_label.numpy())
true_labels_list = np.append(true_labels_list, target.data.numpy())
else:
pred_labels_list = np.append(pred_labels_list, pred_label.cpu().numpy())
true_labels_list = np.append(true_labels_list, target.data.cpu().numpy())
avg_loss = sum(loss_collector) / len(loss_collector)
if get_confusion_matrix:
conf_matrix = confusion_matrix(true_labels_list, pred_labels_list)
if was_training:
model.train()
if get_confusion_matrix:
return correct / float(total), conf_matrix, avg_loss
return correct / float(total), avg_loss
def compute_loss(model, dataloader, device="cpu"):
was_training = False
if model.training:
model.eval()
was_training = True
if device == 'cpu':
criterion = nn.CrossEntropyLoss()
elif "cuda" in device.type:
criterion = nn.CrossEntropyLoss().cuda()
loss_collector = []
with torch.no_grad():
for batch_idx, (x, target) in enumerate(dataloader):
if device != 'cpu':
x, target = x.cuda(), target.to(dtype=torch.int64).cuda()
_,_,out = model(x)
loss = criterion(out, target)
loss_collector.append(loss.item())
avg_loss = sum(loss_collector) / len(loss_collector)
if was_training:
model.train()
return avg_loss
def save_model(model, model_index, args):
logger.info("saving local model-{}".format(model_index))
with open(args.modeldir + "trained_local_model" + str(model_index), "wb") as f_:
torch.save(model.state_dict(), f_)
return
def load_model(model, model_index, device="cpu"):
#
with open("trained_local_model" + str(model_index), "rb") as f_:
model.load_state_dict(torch.load(f_))
if device == "cpu":
model.to(device)
else:
model.cuda()
return model
def get_dataloader(dataset, datadir, train_bs, test_bs, dataidxs=None, attacker_type='None', noise_ratio=0, perturb_probs=[]):
if dataset == 'cifar10':
class_num = 10
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.ColorJitter(brightness=0),
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)
elif dataset == 'tinyimagenet':
class_num = 200
dl_obj = ImageFolder_custom
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_ds = dl_obj(datadir+'/train/', dataidxs=dataidxs, transform=transform_train)
test_ds = dl_obj(datadir+'/val/', transform=transform_test)
elif dataset == 'svhn':
class_num = 10
dl_obj = SVHN_truncated
normalize = transforms.Normalize(mean=[0.4376821, 0.4437697, 0.47280442],
std=[0.19803012, 0.20101562, 0.19703614])
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomRotation(15),
transforms.ToTensor(),
normalize
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize])
train_ds = dl_obj(datadir, dataidxs=dataidxs, split="train", transform=transform_train, download=True)
val_ds = dl_obj(datadir, dataidxs=dataidxs, split="train", transform=transform_test, download=False)
test_ds = dl_obj(datadir, split="test", transform=transform_test, download=True)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, drop_last=True, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
val_dl = data.DataLoader(dataset=val_ds, batch_size=test_bs, shuffle=False)
if attacker_type == 'untargeted_flip':
assert noise_ratio > 0
assert len(perturb_probs) > 0
target_list = train_ds.target
perturbed_num = int(noise_ratio * len(target_list))
perturbed_index = np.random.choice(range(len(target_list)), size=perturbed_num, replace=False)
perturbed_target = np.random.choice(range(class_num), size=perturbed_num, p=perturb_probs, replace=True)
target_list[perturbed_index] = perturbed_target
train_ds.target = target_list
elif attacker_type == 'None':
assert noise_ratio == 0
else:
print("Unsupported attacker types")
assert 0
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, drop_last=True, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
return train_dl, test_dl, train_ds, test_ds
def get_poisoned_dataloader(args, dataset, datadir, train_bs, test_bs, dataidxs=None):
if dataset == 'cifar10':
class_num = 10
dl_obj = CIFAR10Poison_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.ColorJitter(brightness=0),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize])
train_ds = dl_obj(args, datadir, dataidxs=dataidxs, train=True, transform=transform_train, download=True)
test_ds = dl_obj(args, datadir, train=False, transform=transform_test, download=True)
elif dataset == 'svhn':
class_num = 10
dl_obj = SVHNPoison_truncated
normalize = transforms.Normalize(mean=(0.5,),
std=(0.5,))
transform_train = transforms.Compose([
transforms.RandomCrop(28, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
normalize
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize])
train_ds = dl_obj(args, datadir, dataidxs=dataidxs, split="train", transform=transform_train, download=True)
test_ds = dl_obj(args, datadir, split="test", transform=transform_test, download=True)
train_ds.target = np.array(train_ds.target.tolist())
test_ds.target = np.array(test_ds.target.tolist())
elif dataset == 'tinyimagenet':
class_num = 200
dl_obj = ImageFolderPoison_custom
transform_train = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
train_ds = dl_obj(args, datadir+'/train/', train=True, dataidxs=dataidxs, transform=transform_train)
test_ds = dl_obj(args, datadir+'/val/', train=False, transform=transform_test)
train_dl = data.DataLoader(dataset=train_ds, batch_size=train_bs, drop_last=True, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
return train_dl, test_dl, train_ds, test_ds