forked from QinbinLi/MOON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
369 lines (295 loc) · 13.7 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
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, 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_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':
X_train, y_train, X_test, y_test = load_cifar100_data(datadir)
elif dataset == 'tinyimagenet':
X_train, y_train, X_test, y_test = load_tinyimagenet_data(datadir) # ./data/
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 == '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)]
for k in range(K):
idx_k = np.where(y_train == k)[0] # Find all pics of specific category
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])
# if K == 2 and n_parties <= 10:
# if np.min(proportions) < 200:
# min_size = 0
# break
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'):
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':
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