-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_gm.py
171 lines (150 loc) · 7.72 KB
/
data_gm.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
import torch
import torchvision
import sys
PATH_TO_CIFAR = "./cifar_gm/"
sys.path.append(PATH_TO_CIFAR)
import train as cifar_train
def get_inp_tar(dataset):
return dataset.data.view(dataset.data.shape[0], -1).float(), dataset.targets
def get_mnist_dataset(root, is_train, to_download, return_tensor=False):
mnist = torchvision.datasets.MNIST(root, train=is_train, download=to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# only 1 channel
(0.1307,), (0.3081,))
]))
if not return_tensor:
return mnist
else:
return get_inp_tar(mnist)
def get_dataloader(args, unit_batch = False, no_randomness=False):
if unit_batch:
bsz = (1, 1)
else:
bsz = (args.batch_size_train, args.batch_size_test)
if no_randomness:
enable_shuffle = False
else:
enable_shuffle = True
'''
lower() method returns the lower-case version of the original string
'''
if args.dataset.lower() == 'mnist':
train_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST('./files/', train=True, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# only 1 channel
(0.1307,), (0.3081,))
])),
batch_size=bsz[0], shuffle=enable_shuffle
)
test_loader = torch.utils.data.DataLoader(
torchvision.datasets.MNIST('./files/', train=False, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))
])),
batch_size=bsz[1], shuffle=enable_shuffle
)
return train_loader, test_loader
elif args.dataset.lower() == 'cifar10':
if args.cifar_style_data:
train_loader, test_loader = cifar_train.get_dataset(args.config)
else:
train_loader = torch.utils.data.DataLoader(
torchvision.datasets.CIFAR10('./data/', train=True, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# Note this normalization is not same as in MNIST
# (mean_ch1, mean_ch2, mean_ch3), (std1, std2, std3)
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])),
batch_size=bsz[0], shuffle=enable_shuffle
)
test_loader = torch.utils.data.DataLoader(
torchvision.datasets.CIFAR10('./data/', train=False, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# (mean_ch1, mean_ch2, mean_ch3), (std1, std2, std3)
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])),
batch_size=bsz[1], shuffle=enable_shuffle
)
return train_loader, test_loader
def get_federated_data_loader( args, weights=[0.5, 0.5], no_randomness=False, non_iid=True ):
bsz = (args.batch_size_train, args.batch_size_test)
if no_randomness:
enable_shuffle = False
else:
enable_shuffle = True
'''
lower() method returns the lower-case version of the original string
'''
if args.dataset.lower() == 'mnist':
if non_iid is False:
'''
iid version
'''
train_set = torchvision.datasets.MNIST('./files/', train=True, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# only 1 channel
(0.1307,), (0.3081,))]))
test_set = torchvision.datasets.MNIST('./files/', train=False, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.1307,), (0.3081,))]))
else:
'''
TODO: non-iid version
'''
return None
elif args.dataset.lower() == 'cifar10':
if non_iid is False:
'''
iid version
'''
if args.cifar_style_data:
# TODO: implement the funcion [get_federated_dataset]
return cifar_train.get_federated_dataset(args.config)
else:
train_set = torchvision.datasets.CIFAR10('./data/', train=True, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# Note this normalization is not same as in MNIST
# (mean_ch1, mean_ch2, mean_ch3), (std1, std2, std3)
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
test_set = torchvision.datasets.CIFAR10('./data/', train=False, download=args.to_download,
transform=torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
# (mean_ch1, mean_ch2, mean_ch3), (std1, std2, std3)
(0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))
else:
'''
TODO: non-iid version
'''
return None
'''
split the training set and return the training loaders
'''
train_set_1, train_set_2 = torch.utils.data.random_split(
train_set,
[ int( weights[0] * len( train_set ) ),
len( train_set ) - int( weights[0] * len( train_set ) )])
train_loader_1 = torch.utils.data.DataLoader( train_set_1, batch_size=bsz[0], shuffle=enable_shuffle )
train_loader_2 = torch.utils.data.DataLoader( train_set_2, batch_size=bsz[0], shuffle=enable_shuffle )
'''
directly return the testing loaders
'''
test_loader = torch.utils.data.DataLoader( test_set, batch_size=bsz[1], shuffle=enable_shuffle )
return train_loader_1, train_loader_2, test_loader