-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcifar.py
464 lines (384 loc) · 13.9 KB
/
cifar.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
464
import json
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union
import numpy as np
import torch
import torch.utils.data as torchdata
import torchvision.transforms as transforms
from torch_ecg.utils import ReprMixin
from torchvision.datasets import CIFAR10, CIFAR100
from ..utils.const import CACHED_DATA_DIR, CIFAR10_MEAN, CIFAR10_STD, CIFAR100_MEAN, CIFAR100_STD
from ..utils.misc import set_seed
__all__ = [
"CIFAR_truncated",
"CIFAR10_truncated",
"CIFAR100_truncated",
"load_cifar_data",
"partition_cifar_data",
]
CIFAR_DATA_DIRS = {
n_class: (CACHED_DATA_DIR / f"CIFAR{n_class}")
for n_class in [
10,
100,
]
}
CIFAR_NONIID_CACHE_DIRS = {
n_class: (CACHED_DATA_DIR / "non-iid-distribution" / f"CIFAR{n_class}")
for n_class in [
10,
100,
]
}
for n_class in [
10,
100,
]:
CIFAR_DATA_DIRS[n_class].mkdir(parents=True, exist_ok=True)
CIFAR_NONIID_CACHE_DIRS[n_class].mkdir(parents=True, exist_ok=True)
class CIFAR_truncated(ReprMixin, torchdata.Dataset):
"""Truncated CIFAR dataset.
This class is modified from `FedML <https://github.com/FedML-AI/FedML>`_.
Parameters
----------
n_class : {10, 100}
Number of classes.
root : str or pathlib.Path, optional
Directory to store the data,
defaults to pre-defined directory for CIFAR data.
dataidxs : list of int, optional
List of indices of the data to be used.
train : bool, default True
Whether to use training data.
transform : callable, optional
Transform to apply to the data.
target_transform : callable, optional
Transform to apply to the targets (labels).
download : bool, default False
Whether to download the data if not found locally.
seed : int, default 0
Random seed for reproducibility.
"""
__name__ = "CIFAR10_truncated"
def __init__(
self,
n_class: int = 10,
root: Optional[Union[str, Path]] = None,
dataidxs: Optional[List[int]] = None,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
seed: int = 0,
) -> None:
self.n_class = n_class
self.root = Path(root or CIFAR_DATA_DIRS[n_class])
assert self.n_class in [10, 100]
self.dataidxs = dataidxs
self.train = train
self.transform = transform
self.target_transform = target_transform
self.download = download
self.seed = seed
set_seed(seed=self.seed)
self.data, self.target = self.__build_truncated_dataset__()
def __build_truncated_dataset__(self) -> Tuple[torch.Tensor, torch.Tensor]:
"""Build the truncated dataset via
filtering the data with the given `dataidxs`."""
DS = {10: CIFAR10, 100: CIFAR100}[self.n_class]
cifar_dataobj = DS(self.root, self.train, self.transform, self.target_transform, self.download)
data = cifar_dataobj.data
target = np.array(cifar_dataobj.targets)
if self.dataidxs is not None:
data = data[self.dataidxs]
target = target[self.dataidxs]
return data, target
def truncate_channel(self, index: np.ndarray) -> None:
"""Truncate the channel of the image.
Parameters
----------
index : numpy.ndarray
Indices of the samples to be truncated.
Returns
-------
None
"""
for i in range(index.shape[0]):
gs_index = index[i]
self.data[gs_index, :, :, 1] = 0.0
self.data[gs_index, :, :, 2] = 0.0
def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
"""Get the sample given the index.
Parameters
----------
index : int
Index of the sample.
Returns
-------
img : torch.Tensor
The image.
target : torch.Tensor
The label.
"""
img, target = self.data[index], self.target[index]
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self) -> int:
"""Return the number of samples."""
return len(self.data)
def extra_repr_keys(self) -> List[str]:
return super().extra_repr_keys() + [
"n_class",
"root",
]
class CIFAR10_truncated(CIFAR_truncated):
"""Truncated CIFAR10 dataset.
Parameters
----------
root : str or pathlib.Path, optional
Directory to store the data,
defaults to pre-defined directory for CIFAR data.
dataidxs : list of int, optional
List of indices of the data to be used.
train : bool, default True
Whether to use training data.
transform : callable, optional
Transform to apply to the data.
target_transform : callable, optional
Transform to apply to the targets (labels).
download : bool, default False
Whether to download the data if not found locally.
"""
__name__ = "CIFAR10_truncated"
def __init__(
self,
root: Optional[Union[str, Path]] = None,
dataidxs: Optional[List[int]] = None,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
) -> None:
super().__init__(10, root, dataidxs, train, transform, target_transform, download)
class CIFAR100_truncated(CIFAR_truncated):
"""Truncated CIFAR100 dataset.
Parameters
----------
root : str or pathlib.Path, optional
Directory to store the data,
defaults to pre-defined directory for CIFAR data.
dataidxs : list of int, optional
List of indices of the data to be used.
train : bool, default True
Whether to use training data.
transform : callable, optional
Transform to apply to the data.
target_transform : callable, optional
Transform to apply to the targets (labels).
download : bool, default False
Whether to download the data if not found locally.
"""
__name__ = "CIFAR100_truncated"
def __init__(
self,
root: Optional[Union[str, Path]] = None,
dataidxs: Optional[List[int]] = None,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
) -> None:
super().__init__(100, root, dataidxs, train, transform, target_transform, download)
class Cutout(object):
"""The cutout augmentation.
Parameters
----------
length : int
The length of the cutout square.
"""
def __init__(self, length: int) -> None:
self.length = length
def __call__(self, img: torch.Tensor) -> torch.Tensor:
"""
img of shape ``[..., H, W]``
"""
h, w = img.shape[-2:]
mask = np.ones((h, w), np.float32)
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1:y2, x1:x2] = 0.0
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img *= mask
return img
def _data_transforms_cifar(n_class: int) -> Tuple[Callable, Callable]:
"""Get data transforms for CIFAR data.
Parameters
----------
n_class : {10, 100}
Number of classes.
Returns
-------
train_transform : Callable
Transform for training data.
test_transform : Callable
Transform for testing data.
"""
if n_class == 10:
mean, std = CIFAR10_MEAN, CIFAR10_STD
elif n_class == 100:
mean, std = CIFAR100_MEAN, CIFAR100_STD
else:
raise ValueError(f"n_class should be in [10, 100], got {n_class}")
train_transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.Normalize(mean, std),
Cutout(16),
]
)
test_transform = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize(mean, std),
]
)
return train_transform, test_transform
def load_cifar_data(
n_class: int, datadir: Optional[Union[str, Path]] = None, to_numpy: bool = False
) -> Tuple[Union[np.ndarray, torch.Tensor], ...]:
"""Load CIFAR data with preprocessing.
Parameters
----------
n_class : int
Number of classes.
datadir : str or pathlib.Path, optional
Directory to store the data,
defaults to pre-defined directory for CIFAR data.
to_numpy : bool, default False
Whether to convert the data to numpy array.
Returns
-------
tuple
``(X_train, y_train, X_test, y_test)``
"""
train_transform, test_transform = _data_transforms_cifar(n_class)
cifar_train_ds = CIFAR_truncated(datadir, n_class, train=True, download=True, transform=train_transform)
cifar_test_ds = CIFAR_truncated(datadir, n_class, train=False, download=True, transform=test_transform)
X_train, y_train = cifar_train_ds.data, cifar_train_ds.target
X_test, y_test = cifar_test_ds.data, cifar_test_ds.target
if to_numpy:
(
X_train.cpu().numpy(),
y_train.cpu().numpy(),
X_test.cpu().numpy(),
y_test.cpu().numpy(),
)
else:
return (X_train, y_train, X_test, y_test)
def record_net_data_stats(y_train: torch.Tensor, net_dataidx_map: Dict[int, List[int]]) -> Dict[int, Dict[int, int]]:
"""Record the number of training samples for each class on each client.
Parameters
----------
y_train : torch.Tensor
Training labels.
net_dataidx_map : Dict[int, List[int]]
Mapping from client index to the list of training sample indices.
Returns
-------
Dict[int, Dict[int, int]]
Number of training samples for each class on each client.
"""
net_cls_counts = {}
for net_i, dataidx in net_dataidx_map.items():
unq, unq_cnt = np.unique(y_train[dataidx].cpu().numpy(), return_counts=True)
tmp = {unq[i]: unq_cnt[i] for i in range(len(unq))}
net_cls_counts[net_i] = tmp
return net_cls_counts
def partition_cifar_data(
dataset: CIFAR_truncated,
partition: str,
n_net: int,
alpha: float,
to_numpy: bool = False,
datadir: Optional[Union[str, Path]] = None,
) -> tuple:
"""Partition CIFAR data.
Parameters
----------
dataset : type
CIFAR (10 or 100) dataset class.
partition : str
Partition method.
n_net : int
Number of clients.
alpha : float
Parameter for Dirichlet distribution.
to_numpy : bool, default False
Whether to convert the data to numpy array.
datadir : str or pathlibPath, optional
Directory to store the data,
defaults to pre-defined directory for CIFAR data.
Returns
-------
tuple
``(X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts)``
where ``net_dataidx_map`` is a dict mapping from client index to the list of
training sample indices, and ``traindata_cls_counts`` is a dict mapping from
client index to the number of training samples for each class.
"""
n_class = dataset.n_class
X_train, y_train, X_test, y_test = load_cifar_data(n_class, datadir, to_numpy)
n_train = X_train.shape[0]
# n_test = X_test.shape[0]
if partition == "homo":
total_num = n_train
idxs = np.random.permutation(total_num)
batch_idxs = np.array_split(idxs, n_net)
net_dataidx_map = {i: batch_idxs[i] for i in range(n_net)}
elif partition == "hetero":
min_size = 0
K = 100
N = y_train.shape[0]
net_dataidx_map = {}
while min_size < 10:
idx_batch = [[] for _ in range(n_net)]
# for each class in the dataset
for k in range(K):
if to_numpy:
idx_k = np.where(y_train == k)[0]
else:
idx_k = np.where(y_train.cpu().numpy() == k)[0]
np.random.shuffle(idx_k)
proportions = np.random.dirichlet(np.repeat(alpha, n_net))
# Balance
proportions = np.array([p * (len(idx_j) < N / n_net) 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_net):
np.random.shuffle(idx_batch[j])
net_dataidx_map[j] = idx_batch[j]
elif partition == "hetero-fix":
dataidx_map_file_path = CIFAR_NONIID_CACHE_DIRS[n_class] / "net_dataidx_map.json"
with open(dataidx_map_file_path, "r") as f:
net_dataidx_map = json.load(dataidx_map_file_path)
if partition == "hetero-fix":
distribution_file_path = CIFAR_NONIID_CACHE_DIRS[n_class] / "distribution.json"
with open(distribution_file_path, "r") as f:
traindata_cls_counts = json.load(distribution_file_path)
else:
distribution_file_path = CIFAR_NONIID_CACHE_DIRS[n_class] / "distribution.json"
with open(distribution_file_path, "w") as f:
traindata_cls_counts = json.dump(traindata_cls_counts, distribution_file_path, ensure_ascii=False)
return X_train, y_train, X_test, y_test, net_dataidx_map, traindata_cls_counts