-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
40 lines (28 loc) · 879 Bytes
/
reader.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
import numpy as np
import os
import re
import cPickle
class read_cifar10(object):
def __init__(self, data_path=None, is_training=True):
self.data_path = data_path
self.is_training = is_training
def load_data(self):
files = os.listdir(self.data_path)
if self.is_training is True:
pattern = re.compile('(data_batch_).')
to_read = [m.group(0) for i in files for m in [pattern.search(i)] if m]
data = []
labels = []
for t in to_read:
with open(self.data_path+'/'+t, 'rb') as f:
d = cPickle.load(f)
data.append(d['data'])
labels.append(d['labels'])
data = np.vstack(data)
labels = np.hstack(labels)
else:
with open(self.data_path+'/test_batch') as f:
d = cPickle.load(f)
data = d['data']
labels = d['labels']
return data, labels