-
Notifications
You must be signed in to change notification settings - Fork 31
/
DataLoader.py
129 lines (106 loc) · 4.68 KB
/
DataLoader.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
import os
import numpy as np
import scipy.misc
import h5py
np.random.seed(123)
# loading data from .h5
class DataLoaderH5(object):
def __init__(self, **kwargs):
self.load_size = int(kwargs['load_size'])
self.fine_size = int(kwargs['fine_size'])
self.data_mean = np.array(kwargs['data_mean'])
self.randomize = kwargs['randomize']
# read data info from lists
f = h5py.File(kwargs['data_h5'], "r")
self.im_set = np.array(f['images'])
self.lab_set = np.array(f['labels'])
self.num = self.im_set.shape[0]
assert self.im_set.shape[0]==self.lab_set.shape[0], '#images and #labels do not match!'
assert self.im_set.shape[1]==self.load_size, 'Image size error!'
assert self.im_set.shape[2]==self.load_size, 'Image size error!'
print('# Images found:', self.num)
self.shuffle()
self._idx = 0
def next_batch(self, batch_size):
labels_batch = np.zeros(batch_size)
images_batch = np.zeros((batch_size, self.fine_size, self.fine_size, 3))
for i in range(batch_size):
image = self.im_set[self._idx]
image = image.astype(np.float32)/255. - self.data_mean
if self.randomize:
flip = np.random.random_integers(0, 1)
if flip>0:
image = image[:,::-1,:]
offset_h = np.random.random_integers(0, self.load_size-self.fine_size)
offset_w = np.random.random_integers(0, self.load_size-self.fine_size)
else:
offset_h = (self.load_size-self.fine_size)//2
offset_w = (self.load_size-self.fine_size)//2
images_batch[i, ...] = image[offset_h:offset_h+self.fine_size, offset_w:offset_w+self.fine_size, :]
labels_batch[i, ...] = self.lab_set[self._idx]
self._idx += 1
if self._idx == self.num:
self._idx = 0
if self.randomize:
self.shuffle()
return images_batch, labels_batch
def size(self):
return self.num
def reset(self):
self._idx = 0
def shuffle(self):
perm = np.random.permutation(self.num)
self.im_set = self.im_set[perm]
self.lab_set = self.lab_set[perm]
# Loading data from disk
class DataLoaderDisk(object):
def __init__(self, **kwargs):
self.load_size = int(kwargs['load_size'])
self.fine_size = int(kwargs['fine_size'])
self.data_mean = np.array(kwargs['data_mean'])
self.randomize = kwargs['randomize']
self.data_root = os.path.join(kwargs['data_root'])
# read data info from lists
self.list_im = []
self.list_lab = []
with open(kwargs['data_list'], 'r') as f:
for line in f:
path, lab =line.rstrip().split(' ')
self.list_im.append(os.path.join(self.data_root, path))
self.list_lab.append(int(lab))
self.list_im = np.array(self.list_im, np.object)
self.list_lab = np.array(self.list_lab, np.int64)
self.num = self.list_im.shape[0]
print('# Images found:', self.num)
# permutation
perm = np.random.permutation(self.num)
self.list_im[:, ...] = self.list_im[perm, ...]
self.list_lab[:] = self.list_lab[perm, ...]
self._idx = 0
def next_batch(self, batch_size):
images_batch = np.zeros((batch_size, self.fine_size, self.fine_size, 3))
labels_batch = np.zeros(batch_size)
for i in range(batch_size):
image = scipy.misc.imread(self.list_im[self._idx])
image = scipy.misc.imresize(image, (self.load_size, self.load_size))
image = image.astype(np.float32)/255.
image = image - self.data_mean
if self.randomize:
flip = np.random.random_integers(0, 1)
if flip>0:
image = image[:,::-1,:]
offset_h = np.random.random_integers(0, self.load_size-self.fine_size)
offset_w = np.random.random_integers(0, self.load_size-self.fine_size)
else:
offset_h = (self.load_size-self.fine_size)//2
offset_w = (self.load_size-self.fine_size)//2
images_batch[i, ...] = image[offset_h:offset_h+self.fine_size, offset_w:offset_w+self.fine_size, :]
labels_batch[i, ...] = self.list_lab[self._idx]
self._idx += 1
if self._idx == self.num:
self._idx = 0
return images_batch, labels_batch
def size(self):
return self.num
def reset(self):
self._idx = 0