-
Notifications
You must be signed in to change notification settings - Fork 36
/
util_v4.py
296 lines (255 loc) · 10.5 KB
/
util_v4.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
import os
import numpy as np
import time
import argparse
# import logging
from mpi4py import MPI
from math import ceil
from random import Random
import networkx as nx
import torch
import torch.distributed as dist
import torch.utils.data.distributed
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.multiprocessing import Process
from torch.autograd import Variable
import torchvision
from torchvision import datasets, transforms
import torch.backends.cudnn as cudnn
import torchvision.models as IMG_models
from models import *
from models import LogisticRegression
# logging.basicConfig(level=logging.INFO)
class Partition(object):
""" Dataset-like object, but only access a subset of it. """
def __init__(self, data, index):
self.data = data
self.index = index
def __len__(self):
return len(self.index)
def __getitem__(self, index):
data_idx = self.index[index]
return self.data[data_idx]
class DataPartitioner(object):
""" Partitions a dataset into different chuncks. """
def __init__(self, data, sizes=[0.7, 0.2, 0.1], seed=1234, isNonIID=False, alpha=0, dataset=None):
self.data = data
self.dataset = dataset
if isNonIID:
self.partitions, self.ratio = self.__getDirichletData__(data, sizes, seed, alpha)
else:
self.partitions = []
self.ratio = sizes
rng = Random()
rng.seed(seed)
data_len = len(data)
indexes = [x for x in range(0, data_len)]
rng.shuffle(indexes)
for frac in sizes:
part_len = int(frac * data_len)
self.partitions.append(indexes[0:part_len])
indexes = indexes[part_len:]
def use(self, partition):
return Partition(self.data, self.partitions[partition])
def __getNonIIDdata__(self, data, sizes, seed, alpha):
labelList = data.train_labels
rng = Random()
rng.seed(seed)
a = [(label, idx) for idx, label in enumerate(labelList)]
# Same Part
labelIdxDict = dict()
for label, idx in a:
labelIdxDict.setdefault(label,[])
labelIdxDict[label].append(idx)
labelNum = len(labelIdxDict)
labelNameList = [key for key in labelIdxDict]
labelIdxPointer = [0] * labelNum
# sizes = number of nodes
partitions = [list() for i in range(len(sizes))]
eachPartitionLen= int(len(labelList)/len(sizes))
# majorLabelNumPerPartition = ceil(labelNum/len(partitions))
majorLabelNumPerPartition = 2
basicLabelRatio = alpha
interval = 1
labelPointer = 0
#basic part
for partPointer in range(len(partitions)):
requiredLabelList = list()
for _ in range(majorLabelNumPerPartition):
requiredLabelList.append(labelPointer)
labelPointer += interval
if labelPointer > labelNum - 1:
labelPointer = interval
interval += 1
for labelIdx in requiredLabelList:
start = labelIdxPointer[labelIdx]
idxIncrement = int(basicLabelRatio*len(labelIdxDict[labelNameList[labelIdx]]))
partitions[partPointer].extend(labelIdxDict[labelNameList[labelIdx]][start:start+ idxIncrement])
labelIdxPointer[labelIdx] += idxIncrement
#random part
remainLabels = list()
for labelIdx in range(labelNum):
remainLabels.extend(labelIdxDict[labelNameList[labelIdx]][labelIdxPointer[labelIdx]:])
rng.shuffle(remainLabels)
for partPointer in range(len(partitions)):
idxIncrement = eachPartitionLen - len(partitions[partPointer])
partitions[partPointer].extend(remainLabels[:idxIncrement])
rng.shuffle(partitions[partPointer])
remainLabels = remainLabels[idxIncrement:]
return partitions
def __getDirichletData__(self, data, psizes, seed, alpha):
n_nets = len(psizes)
K = 10
labelList = np.array(data.train_labels)
min_size = 0
N = len(labelList)
np.random.seed(2020)
net_dataidx_map = {}
while min_size < K:
idx_batch = [[] for _ in range(n_nets)]
# for each class in the dataset
for k in range(K):
idx_k = np.where(labelList == 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]
net_cls_counts = {}
for net_i, dataidx in net_dataidx_map.items():
unq, unq_cnt = np.unique(labelList[dataidx], return_counts=True)
tmp = {unq[i]: unq_cnt[i] for i in range(len(unq))}
net_cls_counts[net_i] = tmp
print('Data statistics: %s' % str(net_cls_counts))
local_sizes = []
for i in range(n_nets):
local_sizes.append(len(net_dataidx_map[i]))
local_sizes = np.array(local_sizes)
weights = local_sizes/np.sum(local_sizes)
print(weights)
return idx_batch, weights
def partition_dataset(rank, size, args):
print('==> load train data')
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)),
])
trainset = torchvision.datasets.CIFAR10(root=args.datapath,
train=True,
download=True,
transform=transform_train)
partition_sizes = [1.0 / size for _ in range(size)]
partition = DataPartitioner(trainset, partition_sizes, isNonIID=args.NIID, alpha=args.alpha)
ratio = partition.ratio
partition = partition.use(rank)
train_loader = torch.utils.data.DataLoader(partition,
batch_size=args.bs,
shuffle=True,
pin_memory=True)
print('==> load test data')
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
testset = torchvision.datasets.CIFAR10(root=args.datapath,
train=False,
download=True,
transform=transform_test)
test_loader = torch.utils.data.DataLoader(testset,
batch_size=64,
shuffle=False,
num_workers=size)
# You can add more datasets here
return train_loader, test_loader, ratio
def select_model(num_class, args):
if args.model == 'VGG':
model = vgg11()
# You can add more models here
return model
def comp_accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class Meter(object):
""" Computes and stores the average, variance, and current value """
def __init__(self, init_dict=None, ptag='Time', stateful=False,
csv_format=True):
"""
:param init_dict: Dictionary to initialize meter values
:param ptag: Print tag used in __str__() to identify meter
:param stateful: Whether to store value history and compute MAD
"""
self.reset()
self.ptag = ptag
self.value_history = None
self.stateful = stateful
if self.stateful:
self.value_history = []
self.csv_format = csv_format
if init_dict is not None:
for key in init_dict:
try:
# TODO: add type checking to init_dict values
self.__dict__[key] = init_dict[key]
except Exception:
print('(Warning) Invalid key {} in init_dict'.format(key))
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
self.std = 0
self.sqsum = 0
self.mad = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
self.sqsum += (val ** 2) * n
if self.count > 1:
self.std = ((self.sqsum - (self.sum ** 2) / self.count)
/ (self.count - 1)
) ** 0.5
if self.stateful:
self.value_history.append(val)
mad = 0
for v in self.value_history:
mad += abs(v - self.avg)
self.mad = mad / len(self.value_history)
def __str__(self):
if self.csv_format:
if self.stateful:
return str('{dm.val:.3f},{dm.avg:.3f},{dm.mad:.3f}'
.format(dm=self))
else:
return str('{dm.val:.3f},{dm.avg:.3f},{dm.std:.3f}'
.format(dm=self))
else:
if self.stateful:
return str(self.ptag) + \
str(': {dm.val:.3f} ({dm.avg:.3f} +- {dm.mad:.3f})'
.format(dm=self))
else:
return str(self.ptag) + \
str(': {dm.val:.3f} ({dm.avg:.3f} +- {dm.std:.3f})'
.format(dm=self))