-
Notifications
You must be signed in to change notification settings - Fork 16
/
load_data.py
111 lines (74 loc) · 3.5 KB
/
load_data.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
import scipy.io
import numpy as np
def load_mat_mnist_rotated(path):
f_content = None
with open(path, 'r') as f:
f_content = f.readlines()
x = list()
y = list()
if f_content is not None:
for instance in f_content:
instance_split = instance.rstrip().lstrip().replace('\n', '').split(' ')
_class_ = int(float(instance_split[-1]))
_image_ = map(float, instance_split[:-1])
x.append(_image_)
y.append(_class_)
return x, y
def load_mat_mnist_rotated_background(path):
f_content = None
with open(path, 'r') as f:
f_content = f.readlines()
x = list()
y = list()
if f_content is not None:
for instance in f_content:
instance_split = instance.rstrip().lstrip().replace('\n', '').split(' ')
_class_ = int(float(instance_split[-1]))
_image_ = map(float, instance_split[:-1])
x.append(_image_)
y.append(_class_)
return x, y
def load_mat_mnist_background(path):
f_content = None
with open(path, 'r') as f:
f_content = f.readlines()
x = list()
y = list()
if f_content is not None:
for instance in f_content:
instance_split = instance.rstrip().lstrip().replace('\n', '').split(' ')
_class_ = int(float(instance_split[-1]))
_image_ = map(float, instance_split[:-1])
x.append(_image_)
y.append(_class_)
return x, y
def load_mat(path, limit):
data = scipy.io.loadmat(path)
x = np.rollaxis(data['X'], 3, 0)
y = data['y']-1
print x.shape, y.shape
return x[:limit], y[:limit]
def load_mnist_rotated(n_classes=10):
(x_train, y_train) = load_mat_mnist_rotated('./MNIST/Rotated/dataset/mnist_all_rotation_normalized_float_test.amat')
(x_test, y_test) = load_mat_mnist_rotated('./MNIST/Rotated/dataset/mnist_all_rotation_normalized_float_train_valid.amat')
return x_train, y_train, x_test, y_test
def load_mnist_background(n_classes=10):
(x_train, y_train) = load_mat_mnist_background('./MNIST/Background/dataset/mnist_background_images_test.amat')
(x_test, y_test) = load_mat_mnist_background('./MNIST/Background/dataset/mnist_background_images_train.amat')
return x_train, y_train, x_test, y_test
def load_mnist_rotated_background(n_classes=10):
(x_train, y_train) = load_mat_mnist_rotated_background('./MNIST/Rotated+Background/dataset/mnist_all_background_images_rotation_normalized_test.amat')
(x_test, y_test) = load_mat_mnist_rotated_background('./MNIST/Rotated+Background/dataset/mnist_all_background_images_rotation_normalized_train_valid.amat')
return x_train, y_train, x_test, y_test
def load_svhn(n_classes=10):
(x_train, y_train) = load_mat('./SVHN/dataset/train_32x32.mat', 73250)
(x_test, y_test) = load_mat('./SVHN/dataset/test_32x32.mat', 26000)
return x_train, y_train, x_test, y_test
def load_rectangles(n_classes=2):
(x_train, y_train) = load_mat_mnist_background('./Rectangles/Standard/dataset/rectangles_train.amat')
(x_test, y_test) = load_mat_mnist_background('./Rectangles/Standard/dataset/rectangles_test.amat')
return x_train, y_train, x_test, y_test
def load_rectangles_images(n_classes=2):
(x_train, y_train) = load_mat_mnist_background('./Rectangles/Background/dataset/rectangles_im_train.amat')
(x_test, y_test) = load_mat_mnist_background('./Rectangles/Background/dataset/rectangles_im_test.amat')
return x_train, y_train, x_test, y_test