forked from yhlleo/cifar10Dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
59 lines (53 loc) · 1.69 KB
/
load_data.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: load_data.py
# Author: Yahui Liu <[email protected]>
import cv2
import os
import numpy as np
DATA_LEN = 3072
CHANNEL_LEN = 1024
SHAPE = 32
def imread(im_path, shape=None, color="RGB", mode=cv2.IMREAD_UNCHANGED):
im = cv2.imread(im_path, cv2.IMREAD_UNCHANGED)
if color == "RGB":
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
# im = np.transpose(im, [2, 1, 0])
if shape != None:
assert isinstance(shape, int)
im = cv2.resize(im, (shape, shape))
return im
def read_data(filename, data_path, shape=None, color='RGB'):
"""
filename (str): a file
data file is stored in such format:
image_name label
data_path (str): image data folder
return (numpy): a array of image and a array of label
"""
if os.path.isdir(filename):
print "Can't found data file!"
else:
f = open(filename)
lines = f.read().splitlines()
count = len(lines)
data = np.zeros((count, DATA_LEN), dtype=np.uint8)
#label = np.zeros(count, dtype=np.uint8)
lst = [ln.split(' ')[0] for ln in lines]
label = [int(ln.split(' ')[1]) for ln in lines]
idx = 0
s, c = SHAPE, CHANNEL_LEN
for ln in lines:
fname, lab = ln.split(' ')
im = imread(os.path.join(data_path, fname), shape=s, color='RGB')
'''
im = cv2.imread(os.path.join(data_path, fname), cv2.IMREAD_UNCHANGED)
im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
im = cv2.resize(im, (s, s))
'''
data[idx,:c] = np.reshape(im[:,:,0], c)
data[idx, c:2*c] = np.reshape(im[:,:,1], c)
data[idx, 2*c:] = np.reshape(im[:,:,2], c)
label[idx] = int(lab)
idx = idx + 1
return data, label, lst