-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdataset_WA.py
272 lines (179 loc) · 6.23 KB
/
dataset_WA.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
from torch.utils.data import ConcatDataset, Dataset
from torchvision import datasets
import numpy as np
import torch
from PIL import Image
"""
This code mainly tests the redundancy trick, different from only using the smaller one
to make the batch, here instead we used the max len as the data to make the batch
"""
def get_dataset(name):
if name == 'FashionMNIST':
return get_FashionMNIST()
elif name == 'SVHN':
return get_SVHN()
elif name == 'CIFAR10':
return get_CIFAR10()
def get_FashionMNIST():
raw_tr = datasets.FashionMNIST('data/FashionMNIST', train=True, download=True)
raw_te = datasets.FashionMNIST('data/FashionMNIST', train=False, download=True)
X_tr = raw_tr.train_data
Y_tr = raw_tr.train_labels
X_te = raw_te.test_data
Y_te = raw_te.test_labels
return X_tr, Y_tr, X_te, Y_te
def get_SVHN():
data_tr = datasets.SVHN('data/SVHN', split='train', download=True)
data_te = datasets.SVHN('data/SVHN', split='test', download=True)
X_tr = data_tr.data
Y_tr = torch.from_numpy(data_tr.labels)
X_te = data_te.data
Y_te = torch.from_numpy(data_te.labels)
return X_tr, Y_tr, X_te, Y_te
def get_CIFAR10():
data_tr = datasets.CIFAR10('data/CIFAR10', train=True, download=True)
data_te = datasets.CIFAR10('data/CIFAR10', train=False, download=True)
X_tr = data_tr.data
Y_tr = torch.from_numpy(np.array(data_tr.targets))
X_te = data_te.data
Y_te = torch.from_numpy(np.array(data_te.targets))
return X_tr, Y_tr, X_te, Y_te
def get_handler(name):
if name == 'FashionMNIST':
return Wa_datahandler1
elif name == 'SVHN':
return Wa_datahandler2
elif name == 'CIFAR10':
return Wa_datahandler3
class Wa_datahandler1(Dataset):
def __init__(self,X_1, Y_1, X_2, Y_2, transform = None):
"""
:param X_1: covariate from the first distribution
:param Y_1: label from the first distribution
:param X_2:
:param Y_2:
:param transform:
"""
self.X1 = X_1
self.Y1 = Y_1
self.X2 = X_2
self.Y2 = Y_2
self.transform = transform
def __len__(self):
# returning the minimum length of two data-sets
return max(len(self.X1),len(self.X2))
def __getitem__(self, index):
Len1 = len(self.Y1)
Len2 = len(self.Y2)
# checking the index in the range or not
if index < Len1:
x_1 = self.X1[index]
y_1 = self.Y1[index]
else:
# rescaling the index to the range of Len1
re_index = index % Len1
x_1 = self.X1[re_index]
y_1 = self.Y1[re_index]
# checking second datasets
if index < Len2:
x_2 = self.X2[index]
y_2 = self.Y2[index]
else:
# rescaling the index to the range of Len2
re_index = index % Len2
x_2 = self.X2[re_index]
y_2 = self.Y2[re_index]
if self.transform is not None:
x_1 = Image.fromarray(x_1.numpy(), mode='L')
x_1 = self.transform(x_1)
x_2 = Image.fromarray(x_2.numpy(), mode='L')
x_2 = self.transform(x_2)
return index,x_1,y_1,x_2,y_2
class Wa_datahandler2(Dataset):
def __init__(self,X_1, Y_1, X_2, Y_2, transform = None):
"""
:param X_1: covariate from the first distribution
:param Y_1: label from the first distribution
:param X_2:
:param Y_2:
:param transform:
"""
self.X1 = X_1
self.Y1 = Y_1
self.X2 = X_2
self.Y2 = Y_2
self.transform = transform
def __len__(self):
# returning the minimum length of two data-sets
return max(len(self.X1),len(self.X2))
def __getitem__(self, index):
Len1 = len(self.Y1)
Len2 = len(self.Y2)
# checking the index in the range or not
if index < Len1:
x_1 = self.X1[index]
y_1 = self.Y1[index]
else:
# rescaling the index to the range of Len1
re_index = index % Len1
x_1 = self.X1[re_index]
y_1 = self.Y1[re_index]
# checking second datasets
if index < Len2:
x_2 = self.X2[index]
y_2 = self.Y2[index]
else:
# rescaling the index to the range of Len2
re_index = index % Len2
x_2 = self.X2[re_index]
y_2 = self.Y2[re_index]
if self.transform is not None:
x_1 = Image.fromarray(np.transpose(x_1, (1, 2, 0)))
x_1 = self.transform(x_1)
x_2 = Image.fromarray(np.transpose(x_2, (1, 2, 0)))
x_2 = self.transform(x_2)
return index,x_1,y_1,x_2,y_2
class Wa_datahandler3(Dataset):
def __init__(self,X_1, Y_1, X_2, Y_2, transform = None):
"""
:param X_1: covariate from the first distribution
:param Y_1: label from the first distribution
:param X_2:
:param Y_2:
:param transform:
"""
self.X1 = X_1
self.Y1 = Y_1
self.X2 = X_2
self.Y2 = Y_2
self.transform = transform
def __len__(self):
# returning the minimum length of two data-sets
return max(len(self.X1),len(self.X2))
def __getitem__(self, index):
Len1 = len(self.Y1)
Len2 = len(self.Y2)
# checking the index in the range or not
if index < Len1:
x_1 = self.X1[index]
y_1 = self.Y1[index]
else:
# rescaling the index to the range of Len1
re_index = index % Len1
x_1 = self.X1[re_index]
y_1 = self.Y1[re_index]
# checking second datasets
if index < Len2:
x_2 = self.X2[index]
y_2 = self.Y2[index]
else:
# rescaling the index to the range of Len2
re_index = index % Len2
x_2 = self.X2[re_index]
y_2 = self.Y2[re_index]
if self.transform is not None:
x_1 = Image.fromarray(x_1)
x_1 = self.transform(x_1)
x_2 = Image.fromarray(x_2)
x_2 = self.transform(x_2)
return index,x_1,y_1,x_2,y_2