-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimagedata.py
193 lines (134 loc) · 5.05 KB
/
imagedata.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
import os
import numpy as np
import pickle
from torchvision.datasets import MNIST
class ImageData:
"""
Wraps image-based datasets and prepares training, testing and label data for easy and consistent retrieval. Ensures compatibility
of dataset representation between the pytorch and mlx frameworks.
"""
def X_train(self):
pass
def y_train(self):
pass
def X_test(self):
pass
def y_test(self):
pass
def labels(self):
pass
def channels(self):
pass
def dim(self):
pass
class Cifar10Dataset(ImageData):
def __init__(self, path, channels_first=True):
super(Cifar10Dataset, self).__init__()
data_list = []
labels_list = []
# Load training batches
for i in range(1, 6):
batch_file = os.path.join(path, f'data_batch_{i}')
with open(batch_file, 'rb') as file:
batch = pickle.load(file, encoding='latin1')
data_list.append(batch['data'])
labels_list.append(batch['labels'])
self._X_train = np.concatenate(data_list)
self._y_train = np.concatenate(labels_list).tolist()
with open(os.path.join(path, 'batches.meta'), 'rb') as file:
self._labels = pickle.load(file, encoding='latin1')
with open(os.path.join(path, 'test_batch'), 'rb') as file:
test_data = pickle.load(file, encoding='latin1')
self._X_test = test_data["data"]
self._y_test = test_data["labels"]
if channels_first:
self._X_train = self._X_train.reshape(-1, 3, 32, 32)
self._X_test = self._X_test.reshape(-1, 3, 32, 32)
else:
self._X_train = self._X_train.reshape(-1, 32, 32, 3)
self._X_test = self._X_test .reshape(-1, 32, 32, 3)
# Normalize X training data
mean = self._X_train.mean(axis=(0,2,3), keepdims=True).astype(float)
std = self._X_train.std(axis=(0,2,3), keepdims=True).astype(float)
self._X_train = ((self._X_train - mean) / std).tolist()
self._X_test = ((self._X_test - mean) / std).tolist()
# Implement ImageData interface
def X_train(self):
return self._X_train
def y_train(self):
return self._y_train
def X_test(self):
return self._X_test
def y_test(self):
return self._y_test
def labels(self):
return self._labels
def channels(self):
return 3
def dim(self):
return 32
class Cifar100Dataset(ImageData):
def __init__(self, path, channels_first=True):
super(Cifar100Dataset, self).__init__()
# Training data
training_data = pickle.load(open(os.path.join(path, "train"), 'rb'), encoding='latin1')
self._y_train = np.asarray(training_data['fine_labels'], int).tolist() # train on fine labels rather than coarse
# Test data
testing_data = pickle.load(open(os.path.join(path, "test"), 'rb'), encoding='latin1')
self._y_test = np.asarray(testing_data['fine_labels'], int).tolist()
if channels_first:
self._X_train = training_data['data'].reshape(-1, 3, 32, 32)
self._X_test = testing_data['data'].reshape(-1, 3, 32, 32)
else:
self._X_train = training_data['data'].reshape(-1, 32, 32, 3)
self._X_test = testing_data['data'].reshape(-1, 32, 32, 3)
# Labels
classes = pickle.load(open(os.path.join(path, "meta"), 'rb'), encoding='latin1')
self._labels = classes['fine_label_names']
# Normalize X training data
mean = self._X_train.mean(axis=(0,2,3), keepdims=True).astype(float)
std = self._X_train.std(axis=(0,2,3), keepdims=True).astype(float)
self._X_train = ((self._X_train - mean) / std).tolist()
self._X_test = ((self._X_test - mean) / std).tolist()
print("loading complete")
# Implement ImageData interface
def X_train(self):
return self._X_train
def y_train(self):
return self._y_train
def X_test(self):
return self._X_test
def y_test(self):
return self._y_test
def labels(self):
return self._labels
def channels(self):
return 3
def dim(self):
return 32
class MnistDataset(ImageData):
def __init__(self, path, channels_first=True):
if channels_first:
self._X_train = MNIST(root=path, train=True, download=True).data.numpy().reshape(-1, 1, 28, 28).astype(float).tolist()
self._X_test = MNIST(root=path, train=False, download=True).data.numpy().reshape(-1, 1, 28, 28).astype(float).tolist()
else:
self._X_train = MNIST(root=path, train=True, download=True).data.numpy().reshape(-1, 28, 28, 1).astype(float).tolist()
self._X_test = MNIST(root=path, train=False, download=True).data.numpy().reshape(-1, 28, 28, 1).astype(float).tolist()
self._y_train = MNIST(root=path, train=True, download=True).targets.numpy().tolist()
self._y_test = MNIST(root=path, train=False, download=True).targets.numpy().tolist()
self._labels = list(range(10))
# Implement ImageData interface
def X_train(self):
return self._X_train
def y_train(self):
return self._y_train
def X_test(self):
return self._X_test
def y_test(self):
return self._y_test
def labels(self):
return self._labels
def channels(self):
return 1
def dim(self):
return 28