-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
43 lines (31 loc) · 1.27 KB
/
data_utils.py
File metadata and controls
43 lines (31 loc) · 1.27 KB
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
import numpy as np
from torch.utils.data import Subset
def split_noniid(train_idcs, train_labels, alpha, n_clients):
"""
Splits a list of data indices with corresponding labels
into subsets according to a dirichlet distribution with parameter
alpha
"""
n_classes = train_labels.max() + 1
label_distribution = np.random.dirichlet([alpha] * n_clients, n_classes)
class_idcs = [
np.argwhere(train_labels[train_idcs] == y).flatten() for y in range(n_classes)
]
client_idcs = [[] for _ in range(n_clients)]
for c, fracs in zip(class_idcs, label_distribution):
for i, idcs in enumerate(
np.split(c, (np.cumsum(fracs)[:-1] * len(c)).astype(int))
):
client_idcs[i] += [idcs]
client_idcs = [train_idcs[np.concatenate(idcs)] for idcs in client_idcs]
return client_idcs
class CustomSubset(Subset):
"""A custom subset class with customizable data transformation"""
def __init__(self, dataset, indices, subset_transform=None):
super().__init__(dataset, indices)
self.subset_transform = subset_transform
def __getitem__(self, idx):
x, y = self.dataset[self.indices[idx]]
if self.subset_transform:
x = self.subset_transform(x)
return x, y