-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
56 lines (38 loc) · 1.47 KB
/
data_loader.py
File metadata and controls
56 lines (38 loc) · 1.47 KB
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
# Imports
from tensorflow.data import Dataset
import numpy as np
from sklearn.preprocessing import OneHotEncoder
def get_data(dataset):
if dataset == 'cifar100':
from tensorflow.keras.datasets import cifar100
(x_tr, y_tr), (x_te, y_te) = cifar100.load_data()
elif dataset == 'cifar10':
from tensorflow.keras.datasets import cifar10
(x_tr, y_tr), (x_te, y_te) = cifar10.load_data()
preprocesses = ([todtype, normalize], [ohe])
x_te, y_te = preprocess(x_te, y_te, preprocesses)
x_tr, y_tr = preprocess(x_tr, y_tr, preprocesses)
tr_ds_x = Dataset.from_tensor_slices(x_tr)
tr_ds_y = Dataset.from_tensor_slices(y_tr)
te_ds_x = Dataset.from_tensor_slices(x_te)
te_ds_y = Dataset.from_tensor_slices(y_te)
tr_ds = Dataset.zip((tr_ds_x, tr_ds_y)).shuffle(1000).batch(128)
te_ds = Dataset.zip((te_ds_x, te_ds_y)).batch(128)
return tr_ds, te_ds
def normalize(x, var = None):
min_val = np.min(x)
max_val = np.max(x)
return (x-min_val) / (max_val-min_val)
def normalize_elemental(x):
# element wise normalize
raise('Not yet implemented!')
def ohe(y):
return OneHotEncoder().fit_transform(np.array(y).reshape(-1,1)).toarray()
def todtype(x, dtype='float32'):
return x.astype(dtype)
def preprocess(x, y, preprocesses):
for process in preprocesses[0]:
x = process(x)
for process in preprocesses[1]:
y = process(y)
return x, y