-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
463 lines (378 loc) · 18.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
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
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
import pickle as pkl
#from model import *
from datasets import CIFAR10_truncated, CIFAR100_truncated, ImageFolder_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
# y_train = y_train.numpy()
# y_test = y_test.numpy()
return (X_train, y_train, X_test, y_test)
def load_cifar100_data(datadir):
transform = transforms.Compose([transforms.ToTensor()])
cifar100_train_ds = CIFAR100_truncated(datadir, train=True, download=True, transform=transform)
cifar100_test_ds = CIFAR100_truncated(datadir, train=False, download=True, transform=transform)
X_train, y_train = cifar100_train_ds.data, cifar100_train_ds.target
X_test, y_test = cifar100_test_ds.data, cifar100_test_ds.target
# y_train = y_train.numpy()
# y_test = y_test.numpy()
return (X_train, y_train, X_test, y_test)
def load_miniImageNet(datadir):
train_data = pkl.load(open(datadir + '/mini-imagenet-cache-train.pkl', 'rb'))
test_data = pkl.load(open(datadir + '/mini-imagenet-cache-test.pkl', 'rb'))
y_train = []
for i, class_ in enumerate(train_data['class_dict'].keys()):
y_train.append(np.ones(600) * i)
y_train = np.concatenate(y_train, 0)
X_train = train_data['image_data']
y_test = []
for i, class_ in enumerate(test_data['class_dict'].keys()):
y_test.append(np.ones(600) * i)
y_test = np.concatenate(y_test, 0)
X_test = test_data['image_data']
return (X_train, y_train, X_test, y_test)
def load_text_data(datadir, dataset):
from data.loader import load_dataset
train_data, val_data, test_data = load_dataset(datadir, dataset)
return (np.concatenate([train_data['text'], train_data['text_len'].reshape(-1, 1)], -1), train_data['label'],
np.concatenate([test_data['text'], test_data['text_len'].reshape(-1, 1)], -1), test_data['label'])
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 == 'cifar100' or dataset == 'FC100':
fine_id_coarse_id = {0: 4, 1: 1, 2: 14, 3: 8, 4: 0, 5: 6, 6: 7, 7: 7, 8: 18, 9: 3, 10: 3, 11: 14, 12: 9, 13: 18,
14: 7, 15: 11, 16: 3, 17: 9, 18: 7, 19: 11, 20: 6, 21: 11, 22: 5, 23: 10, 24: 7, 25: 6,
26: 13, 27: 15, 28: 3, 29: 15, 30: 0, 31: 11, 32: 1, 33: 10, 34: 12, 35: 14, 36: 16, 37: 9,
38: 11, 39: 5, 40: 5, 41: 19, 42: 8, 43: 8, 44: 15, 45: 13, 46: 14, 47: 17, 48: 18, 49: 10,
50: 16, 51: 4, 52: 17, 53: 4, 54: 2, 55: 0, 56: 17, 57: 4, 58: 18, 59: 17, 60: 10, 61: 3,
62: 2, 63: 12, 64: 12, 65: 16, 66: 12, 67: 1, 68: 9, 69: 19, 70: 2, 71: 10, 72: 0, 73: 1,
74: 16, 75: 12, 76: 9, 77: 13, 78: 15, 79: 13, 80: 16, 81: 19, 82: 2, 83: 4, 84: 6, 85: 19,
86: 5, 87: 5, 88: 8, 89: 19, 90: 18, 91: 1, 92: 2, 93: 15, 94: 6, 95: 0, 96: 17, 97: 8,
98: 14, 99: 13}
coarse_split = {'train': [1, 2, 3, 4, 5, 6, 9, 10, 15, 17, 18, 19], 'valid': [8, 11, 13, 16],
'test': [0, 7, 12, 14]}
from collections import defaultdict
fine_split = defaultdict(list)
for fine_id, sparse_id in fine_id_coarse_id.items():
if sparse_id in coarse_split['train']:
fine_split['train'].append(fine_id)
elif sparse_id in coarse_split['valid']:
fine_split['valid'].append(fine_id)
else:
fine_split['test'].append(fine_id)
X_train, y_train, X_test, y_test = load_cifar100_data(datadir)
elif dataset == 'miniImageNet':
X_train, y_train, X_test, y_test = load_miniImageNet(datadir)
elif dataset == '20newsgroup' or dataset == 'fewrel' or dataset=='huffpost':
X_train, y_train, X_test, y_test = load_text_data(datadir, dataset)
elif dataset == 'tinyimagenet':
X_train, y_train, X_test, y_test = load_tinyimagenet_data(datadir)
if dataset == 'FC100':
X_total = np.concatenate([X_train, X_test], 0)
y_total = np.concatenate([y_train, y_test], 0)
# X_train=np.concatenate([X_total[Y_total==k] for k in fine_split['train']],0)
# y_train=np.concatenate([Y_total[Y_total==k] for k in fine_split['train']],0)
# X_test=np.concatenate([X_total[Y_total==k] for k in fine_split['test']],0)
# y_test=np.concatenate([Y_total[Y_total==k] for k in fine_split['test']],0)
test_dataidxs = []
for k in fine_split['test']:
test_dataidxs.extend(np.where(y_total == k)[0].tolist())
X_test = X_total[test_dataidxs]
y_test = y_total[test_dataidxs]
train_dataidxs = []
for k in fine_split['train']:
train_dataidxs.extend(np.where(y_total == k)[0].tolist())
X_train = X_total[train_dataidxs]
y_train = y_total[train_dataidxs]
if partition == "homo" or partition == "iid":
n_train = y_train.shape[0]
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 == 'cifar100':
K = 100
elif dataset == 'tinyimagenet':
K = 200
# min_require_size = 100
#
N = y_train.shape[0]
net_dataidx_map = {}
while min_size < min_require_size:
idx_batch = [[] for _ in range(n_parties)]
class_distribute=[]
if dataset == 'FC100':
train_classes = fine_split['train']
elif dataset == 'miniImageNet':
train_classes = list(range(64))
elif dataset == '20newsgroup':
train_classes = [1, 5, 10, 11, 13, 14, 16, 18]
elif dataset == 'fewrel':
train_classes = [0, 1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 19, 21,
22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 43, 44, 45, 46, 48, 49, 50, 52, 53, 56, 57, 58,
59, 61, 62, 63, 64, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78]
elif dataset=='huffpost':
train_classes=list(range(20))
for k in train_classes:
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]
class_distribute.append([len(one) for one in np.split(idx_k, proportions)])
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])
# if K == 2 and n_parties <= 10:
# if np.min(proportions) < 200:
# min_size = 0
# break
#print(np.split(idx_k, proportions))
#print(np.stack(class_distribute,0))
#if args.dataset=='20newsgroup' and
#print(1/0)
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())
# print("net.parameter.data:", list(net.parameters()))
paramlist = list(trainable)
# print("paramlist:", paramlist)
N = 0
for params in paramlist:
N += params.numel()
# print("params.data:", params.data)
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
# print("get trainable x:", X)
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):
# print("x:",x)
# print("target:",target)
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, noise_level=0):
if dataset in ('cifar10', 'cifar100', 'FC100'):
if 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.ColorJitter(brightness=noise_level),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
# data prep for test set
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize])
elif dataset == 'cifar100' or dataset == 'FC100':
dl_obj = CIFAR100_truncated
normalize = transforms.Normalize(mean=[0.5070751592371323, 0.48654887331495095, 0.4409178433670343],
std=[0.2673342858792401, 0.2564384629170883, 0.27615047132568404])
# transform_train = transforms.Compose([
# transforms.RandomCrop(32),
# transforms.RandomHorizontalFlip(),
# transforms.ToTensor(),
# normalize
# ])
transform_train = transforms.Compose([
# transforms.ToPILImage(),
transforms.RandomCrop(32, 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(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, drop_last=True, shuffle=True)
test_dl = data.DataLoader(dataset=test_ds, batch_size=test_bs, shuffle=False)
elif dataset == 'tinyimagenet':
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)
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