-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdatasets.py
149 lines (109 loc) · 4.82 KB
/
datasets.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
# This file is part of "MPS Yokohama Deep Learning Series Day 09/10/2016"
#
# "MPS Yokohama Deep Learning Series Day 09/10/2016"
# is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# "MPS Yokohama Deep Learning Series Day 09/10/2016"
# is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Foobar. If not, see <http://www.gnu.org/licenses/>.
#
# (c) Junya Kaneko <jyuneko@hotmail.com>
import numpy as np
from mnist import MNIST
class MnistDataset:
def __init__(self, name, mnist_dir, val_collect, val_wrong):
self._name = name
self._mnist_dir = mnist_dir
self._val_collect = val_collect
self._val_wrong = val_wrong
self._mnist = MNIST(self._mnist_dir)
self._img_tensor, self._label_tensor = self._load_data()
def __len__(self):
return self._img_tensor.shape[0]
@property
def name(self):
return self._name
@property
def img_size(self):
return self._img_tensor.shape[1]
@property
def n_label_types(self):
return self._label_tensor.shape[1]
@property
def imgs(self):
return self._img_tensor
@property
def labels(self):
return self._label_tensor
def __iter__(self):
for i in range(len(self._img_tensor)):
yield self._img_tensor[i], self._label_tensor[i]
raise StopIteration
def _preprocess_imgs(self, imgs):
# データの整形
return np.array(imgs, dtype=float) / 255.0
def _imgs_to_tensor(self, imgs):
return np.array([img.reshape(len(img), 1) for img in imgs])
def _labels_to_tensor(self, labels):
def label_to_matrix(label):
t = np.zeros(shape=(10, 1))
t.fill(self._val_wrong)
t[label, 0] = self._val_collect
return t
return np.array([label_to_matrix(label) for label in labels])
def _load_mnist_data(self):
return None, None
def _load_data(self):
imgs, labels = self._load_mnist_data()
return self._imgs_to_tensor(self._preprocess_imgs(imgs)), self._labels_to_tensor(labels)
class MnistTrainingDataset(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTrainingDataset, self).__init__('training', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
return self._mnist.load_training()
class MnistTrainingDataset1000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTrainingDataset1000, self).__init__('training1000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
imgs, labels = self._mnist.load_training()
return imgs[:1000], labels[:1000]
class MnistTrainingDataset5000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTrainingDataset5000, self).__init__('training5000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
imgs, labels = self._mnist.load_training()
return imgs[:5000], labels[:5000]
class MnistTrainingDataset20000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTrainingDataset20000, self).__init__('training10000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
imgs, labels = self._mnist.load_training()
return imgs[:20000], labels[:20000]
class MnistTestDataset(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTestDataset, self).__init__('test', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
return self._mnist.load_testing()
class MnistTestDataset1000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTestDataset1000, self).__init__('test1000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
return self._mnist.load_testing()
class MnistTestDataset5000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTestDataset5000, self).__init__('test5000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
return self._mnist.load_testing()
class MnistTestDataset20000(MnistDataset):
def __init__(self, mnist_dir, val_collect, val_wrong):
super(MnistTestDataset20000, self).__init__('test5000', mnist_dir, val_collect, val_wrong)
def _load_mnist_data(self):
return self._mnist.load_testing()