-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_load.py
176 lines (164 loc) · 6.99 KB
/
data_load.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
import numpy as np
import torch
from torchvision import transforms
from torchvision import datasets
import pickle
def data_prepare(dataset_name, n_devices, n_train, n_val, n_test, batch_size=5, rd_seed=111):
"""
Return the train_loader_list, devices_train_list, val_loader_list and devices_val_list
based on the dataset_name.
"""
# Input:
# dataset_name: A string, should be one of
# {"MNIST", "KMNIST", "FMNIST"}
# n_devices: Integer, number of providers
# n_train: Inetger, number of training samples each provider has
# n_val: Inetger, number of validation samples used to compute utility
# n_test: Integer, number of hold-out testing samples
# batch_size: batch size of data loader
# rd_seed: Integer, random seed
# Return:
# (devices_train_list, val_loader, test_loader)
np.random.seed(rd_seed)
# load the traning set
if dataset_name == "MNIST":
data_path = 'data/mnist/'
transform_data = datasets.MNIST(
data_path, train=True, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1307),(0.3081))
]))
elif dataset_name == "KMNIST":
data_path = 'data/kmnist/'
transform_data = datasets.KMNIST(
data_path, train=True, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1918),(0.3483))
]))
elif dataset_name == "FMNIST":
data_path = 'data/fmnist/'
transform_data = datasets.FashionMNIST(
data_path, train=True, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.2861),(0.3530))
]))
elif dataset_name == "CIFAR10":
data_path = 'data/cifar10/'
transform_data = datasets.CIFAR10(
data_path, train=True, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
#transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
transforms.Normalize((0.5), (0.5))
]))
devices_train_list = [] # list of training data for devices
val_loader = []
test_loader = []
sample_order = np.arange(len(transform_data))
np.random.shuffle(sample_order)
count = 0
# Choose training data
for i in range(n_devices):
device_data = []
for j in range(n_train):
img, label = transform_data[sample_order[count]]
# corrupt the data
if i / n_devices < 0.1:
device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
elif i / n_devices < 0.2:
if j < 0.2 * n_train:
label_true = label # note that label is int, thus no need to copy
while label == label_true:
label = np.random.choice(10)
device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
elif i / n_devices < 0.3:
if j < 0.5 * n_train:
label_true = label # note that label is int, thus no need to copy
while label == label_true:
label = np.random.choice(10)
device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
else:
if j < 0.9 * n_train:
label_true = label # note that label is int, thus no need to copy
while label == label_true:
label = np.random.choice(10)
device_data.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
device_data = torch.utils.data.DataLoader(device_data, batch_size=batch_size, shuffle=True)
devices_train_list.append(device_data)
# Choose validation data
val_loader = []
for j in range(n_val):
img, label = transform_data[sample_order[count]]
val_loader.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
val_loader = torch.utils.data.DataLoader(val_loader, batch_size=batch_size, shuffle=True)
print("number of training samples used from {}: {}".format(dataset_name, count))
# Choose test data
# load the test set
if dataset_name == "MNIST":
data_path = 'data/mnist/'
transform_data = datasets.MNIST(
data_path, train=False, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1307),(0.3081))
]))
elif dataset_name == "KMNIST":
data_path = 'data/kmnist/'
transform_data = datasets.KMNIST(
data_path, train=False, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.1918),(0.3483))
]))
elif dataset_name == "FMNIST":
data_path = 'data/fmnist/'
transform_data = datasets.FashionMNIST(
data_path, train=False, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.2861),(0.3530))
]))
elif dataset_name == "CIFAR10":
data_path = 'data/cifar10/'
transform_data = datasets.CIFAR10(
data_path, train=False, download=True,
transform=transforms.Compose([
transforms.Grayscale(),
transforms.ToTensor(),
transforms.Normalize((0.5), (0.5))
]))
sample_order = np.arange(len(transform_data))
np.random.shuffle(sample_order)
test_loader = []
count = 0
for j in range(n_test):
img, label = transform_data[sample_order[count]]
test_loader.append((img / torch.norm(img.squeeze(0)).item(), label))
count += 1
test_loader = torch.utils.data.DataLoader(test_loader, batch_size=batch_size, shuffle=True)
print("number of testing samples used from {}: {}".format(dataset_name, count))
return devices_train_list, val_loader, test_loader
if __name__ == '__main__':
n_devices = 100
n_train = 400
n_val = 1000
n_test = 10000
for dataset_name in ['MNIST', 'KMNIST', 'FMNIST', 'CIFAR10']:
devices_train_list, val_loader, test_loader = data_prepare(dataset_name, n_devices, n_train, n_val, n_test, batch_size=5, rd_seed=111)
with open('data/' + dataset_name.lower() + '/corrupted_data_ndevices=' + str(n_devices) + '.pickle', 'wb') as f:
pickle.dump((devices_train_list, val_loader, test_loader), f)