-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
52 lines (45 loc) · 1.72 KB
/
data_utils.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
import sys
import os
import numpy as np
from PIL import Image
if sys.version_info[0] == 2:
# noinspection PyUnresolvedReferences,PyPep8Naming
import cPickle as c_pickle
else:
import _pickle as c_pickle
def batches(data, batch_size):
for i in range(0, len(data), batch_size):
yield data[i:i + batch_size]
class Cifar10(object):
@staticmethod
def load_labels():
with open(os.path.join('cifar-10-batches-py', 'batches.meta'), 'rb') as f:
label_names = c_pickle.load(f)['label_names']
return label_names
@staticmethod
def get_batch(n, raw=False):
path = os.path.join('cifar-10-batches-py', 'data_batch_%d' % n)
with open(path, 'rb') as f:
dictionary = c_pickle.load(f, encoding='bytes')
if not raw:
dictionary[b'data'] = dictionary[b'data'].astype(np.float64)
dictionary[b'data'] /= 255
dictionary[b'data'] -= 0.5
labels = np.zeros((len(dictionary[b'labels']), 10))
for i in range(len(dictionary[b'labels'])):
labels[i, dictionary[b'labels'][i]] = 1
dictionary[b'labels'] = labels
return dictionary[b'data'], dictionary[b'labels']
@staticmethod
def show_img_array(flat, normalized=True):
img_array = np.zeros((32, 32, 3))
for channel in range(3):
for row in range(32):
for collum in range(32):
img_array[row, collum, channel] = flat[channel * 1024 + row * 32 + collum]
if normalized:
img_array -= img_array.min()
img_array /= img_array.max()
img_array *= 255
with Image.fromarray(img_array.astype(np.uint8)) as img:
img.show()