-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loaders.py
370 lines (297 loc) · 12.7 KB
/
data_loaders.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
import pathlib
import pickle
import numpy as np
import torch
from torch.utils.data import DataLoader, Dataset
from src.thermometer_encoding import data_to_therm_numpy
# %%
# =============================================================================
# Function adapted from file:
# https://github.com/Shmoo137/Interpretable-Phase-Classification/blob/master/Influence_Functions_LL-CDW/data_loader.py
# =============================================================================
# =============================================================================
# Generate PyTorch dataset (it transforms array of ndarrays to torch tensor).
# Overloading of '__selt__'. and '__getitem__' is required by the parent class.
# =============================================================================
class NumpyToPyTorch_DataLoader(Dataset):
def __init__(self, X, Y, transform=None):
self.X = torch.from_numpy(X).float() # image
self.Y = torch.from_numpy(Y).long() # label for classification
self.transform = transform
def __len__(self):
return len(self.X)
def __getitem__(self, index):
label = self.Y[index]
img = self.X[index]
if self.transform:
img = self.transform(img)
return img, label
class Importer(object):
"""
Parameters
----------
dataset_folder_path : pathlib.PosixPath
A pathlib Path object pointing to the directory in which you have
stored the pickled train/validation/test datasets.
batch : int
Specifies the default batch size.
device : torch.device
Allows for enabling of memory pin for CUDA-enabled computers.
If is not None, then if enables pin_memory option for CUDA devices.
Default is None.
noise_pow : float, optional
Should the random Gaussian noise be added to each training example,
with μ=0, σ=noise_pow. If 'None', then no noise is applied.
Default is None.
Returns
-------
Nothing, just states the internal parameters state.
"""
def __init__(
self,
dataset_folder_path,
batch,
device=None,
noise_pow=None,
therm_levels=None,
perturbative_ds=None,
):
"""
Parameters
----------
dataset_folder_path : pathlib.PosixPath
A pathlib Path object pointing to the directory in which you have
stored the pickled train/validation/test datasets.
batch : int
Specifies the default batch size.
device : torch.device
Allows for enabling of memory pin for CUDA-enabled computers.
If is not None, then if enables pin_memory option for CUDA devices.
Default is None.
noise_pow : float, optional
Should the random Gaussian noise be added to each training example,
with μ=0, σ=noise_pow. If 'None', then no noise is applied.
Default is None.
therm_levels : int, optional
IF you decide to use thermometer encoding, this variable states
how many thermometer levels are used
perturbative_ds : list, optional
IF you decide to use perturbative approximation for training
(datasets with little disorder), this should be the list where
you give paths to them.
Returns
-------
Nothing, just states the internal parameters state.
"""
self.batch = batch
self.datasets_path = dataset_folder_path
self._train_path = dataset_folder_path.joinpath("training_set.pickle")
self._val_path = dataset_folder_path.joinpath("validation_set.pickle")
self._test_path = dataset_folder_path.joinpath("test_set.pickle")
if device is not None:
self.mem_pin_bool = True if device.type == "cuda" else False
else:
self.mem_pin_bool = False
self.noise = noise_pow
self.therm_levels = therm_levels
self.perturbative_ds = perturbative_ds
def get_train_loader(
self, batch_size=None, shuffle=True, save_mask=False, seed=2137
):
"""
A function for preparation of PyTorch DataLoader class instance with
training dataset loaded.
Parameters
----------
batch_size : int, optional
Custom batch size for training dataset loader.
If None, Importer instance's default value is used.
The default is None.
shuffle : bool, optional
Should the training data be shuffled on import. The default is True.
save_mask : bool, optional
Should the shuffle mask be saved to hard drive apart from it being
saved as variable in Importer instance. The default is False.
seed : int, optional
What seed should the random number generator use for shuffling.
The default is 2137.
Returns
-------
train_loader : torch.utils.data.dataloader.DataLoader
DataLoader instance fed with training dataset.
"""
if batch_size is None:
batch_size = self.batch
with open(self._train_path, "rb") as f:
train_dict = pickle.load(f)
self.M = train_dict["data"][0].shape[0]
if self.perturbative_ds is not None:
perturbative = []
for ds_path in self.perturbative_ds:
with open(ds_path.joinpath("training_set.pickle"), "rb") as f:
perturbative.append(pickle.load(f))
train_keys = list(train_dict.keys())
for key in train_keys:
if isinstance(train_dict[key], list):
train_dict[key] = np.array(train_dict[key])
train_data = train_dict.copy()
if self.perturbative_ds is not None:
for pert in perturbative:
for key in train_keys:
if isinstance(pert[key], list):
pert[key] = np.array(pert[key])
train_data[key] = np.append(train_data[key], pert[key], axis=0)
train_samples_num = train_data[train_keys[0]].shape[0]
if shuffle:
# Shuffling ordered data, but preserving the mask (since we want to remember for which 'v' the datapoint was calculated)
mask = np.arange(train_samples_num)
np.random.seed(seed)
np.random.shuffle(mask)
# Saving the mask in the object's variables to retrieve original indices afterwards
self.train_mask = mask
if save_mask:
with open(
self.datasets_path.joinpath("train_set_mask.pickle"), "wb"
) as f:
pickle.dump(mask, f)
masked_train_data = train_data["data"][mask]
masked_train_labels = train_data["labels"][mask]
else:
masked_train_data = train_data["data"]
masked_train_labels = train_data["labels"]
if self.noise is not None:
noise_tensor = np.random.normal(0, self.noise, size=masked_train_data.shape)
masked_train_data = np.add(masked_train_data, noise_tensor)
masked_train_data = masked_train_data.reshape([-1, 1, self.M, self.M])
if self.therm_levels is not None:
masked_train_data = data_to_therm_numpy(
x=masked_train_data, levels=self.therm_levels
)
train_set = NumpyToPyTorch_DataLoader(masked_train_data, masked_train_labels)
self.train_W_tab = train_data["W"]
self.train_v_tab = train_data["v"]
if batch_size == -1:
batch_size = train_samples_num
train_loader = DataLoader(
train_set,
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=self.mem_pin_bool, # CUDA only, this lets your DataLoader allocate the samples in page-locked memory, which speeds-up the transfer from CPU to GPU during training
)
return train_loader
def get_val_loader(self, batch_size=None):
"""
A function for preparation of PyTorch DataLoader class instance with
validation dataset loaded.
Parameters
----------
batch_size : int, optional
Custom batch size for validation dataset loader.
If None, Importer instance's default value is used.
The default is None.
Returns
-------
val_loader : torch.utils.data.dataloader.DataLoader
DataLoader instance fed with validation dataset.
"""
if batch_size is None:
batch_size = self.batch
with open(self._val_path, "rb") as f:
val_dict = pickle.load(f)
self.M = val_dict["data"][0].shape[0]
val_keys = list(val_dict.keys())
for key in val_keys:
if isinstance(val_dict[key], list):
val_dict[key] = np.array(val_dict[key])
if self.perturbative_ds is not None:
perturbative = []
for ds_path in self.perturbative_ds:
with open(ds_path.joinpath("validation_set.pickle"), "rb") as f:
perturbative.append(pickle.load(f))
val_data = val_dict.copy()
if self.perturbative_ds is not None:
for pert in perturbative:
for key in val_keys:
if isinstance(pert[key], list):
pert[key] = np.array(pert[key])
val_data[key] = np.append(val_data[key], pert[key], axis=0)
data = val_data["data"]
data = data.reshape([-1, 1, self.M, self.M])
if self.therm_levels is not None:
data = data_to_therm_numpy(x=data, levels=self.therm_levels)
val_set = NumpyToPyTorch_DataLoader(data, val_data["labels"])
self.val_W_tab = val_data["W"]
self.val_v_tab = val_data["v"]
if batch_size == -1:
batch_size = val_data["data"].shape[0]
val_loader = DataLoader(
val_set,
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=self.mem_pin_bool,
)
return val_loader
def get_test_loader(self, batch_size=None, realization=None, subset=None):
"""
A function for preparation of PyTorch DataLoader class instance with
test dataset loaded.
Parameters
----------
batch_size : int, optional
Custom batch size for test dataset loader.
If None, Importer instance's default value is used.
The default is None.
realization : int, optional
IF not None, then this gets appended to set file name.
An option for testing on various disorder realizations.
subset : list, optional
If you want to use only a subset of the dataset, you can specify
it here. It should be a list of integers, where each integer
corresponds to a data point index. The default is None.
Returns
-------
test_loader : torch.utils.data.dataloader.DataLoader
DataLoader instance fed with test dataset.
"""
if batch_size is None:
batch_size = self.batch
if realization is None:
with open(self._test_path, "rb") as f:
test_dict = pickle.load(f)
self.M = test_dict["data"][0].shape[0]
else:
with open(
self.datasets_path.joinpath(f"test_{realization}_set.pickle"), "rb"
) as f:
test_dict = pickle.load(f)
self.M = test_dict["data"][0].shape[0]
test_keys = list(test_dict.keys())
for key in test_keys:
if isinstance(test_dict[key], list):
test_dict[key] = np.array(test_dict[key])
if subset is not None:
test_dict["data"] = test_dict["data"][subset].copy()
test_dict["labels"] = test_dict["labels"][subset].copy()
test_dict["W"] = test_dict["W"][subset].copy()
test_dict["v"] = test_dict["v"][subset].copy()
data = test_dict["data"]
data = data.reshape([-1, 1, self.M, self.M])
if self.therm_levels is not None:
data = data_to_therm_numpy(x=data, levels=self.therm_levels)
test_set = NumpyToPyTorch_DataLoader(data, test_dict["labels"])
self.W_tab = test_dict["W"]
self.v_tab = test_dict["v"]
self.test_data = test_dict["data"]
self.test_labels = test_dict["labels"]
if batch_size == -1:
batch_size = test_dict["data"].shape[0]
test_loader = DataLoader(
test_set,
batch_size=batch_size,
shuffle=False,
num_workers=0,
pin_memory=self.mem_pin_bool,
)
return test_loader