-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar.py
34 lines (29 loc) · 1.11 KB
/
cifar.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
import pickle
import random
import numpy as np
class CIFAR:
def __init__(self, path):
self.databatches = list(map(
lambda b: self.unpickle(path+'/data_batch_'+b),
list(map(str, range(1, 6)))))
self.databatch_len = len(self.databatches[0][b'labels'])
def batch(self, batch_size):
data_batch = int(random.choice(range(5)))
batch_indices = np.random.choice(np.arange(self.databatch_len),
batch_size, replace=False).astype('int32')
X = np.reshape(
self.databatches[data_batch][b'data'][batch_indices],
[batch_size, 32, 32, 3], 'F').transpose((0, 2, 1, 3))
y = np.array(self.databatches[data_batch][b'labels'])[batch_indices]
return X, y
def unpickle(self, databatch):
with open(databatch, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
return dict
if __name__ == "__main__":
import cv2
cifar = CIFAR('./cifar/cifar-10-batches-py/')
for image, label in zip(*cifar.batch(10)):
print(label)
cv2.imshow('Bonsoir', image)
cv2.waitKey(-1)