-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
226 lines (174 loc) · 7.09 KB
/
dataset.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import cv2
import os
import glob
from sklearn.utils import shuffle
import numpy as np
from threading import Thread, Lock
from queue import Queue
import gc
import tensorflow as tf
def load_train(train_path, classes):
print('Going to read training images')
images = []
labels = []
for fields in classes:
index = classes.index(fields)
print('Now going to read files in {} (Index: {})'.format(fields, index))
path = os.path.join(train_path, fields, '*g')
files = glob.glob(path)
# Only load filenames and label indices
for f in files:
images.append(f)
labels.append(index)
return images, labels
class DataYielder:
def __init__(self, train_path, image_size, classes, filenames, labels, max_size=20000, name='Train'):
"""
This class is not thread safe
Args:
train_path:
image_size:
classes:
validation_size:
max_size:
"""
self._image_size = image_size
self._classes = classes
self.train_path = train_path
self._max_size = max_size
self.name = name
self._filenames = filenames
self._label_indices = labels
# Shape is amount of images, color count (RGB) and height and width
self._images = np.zeros((len(labels), image_size, image_size, 3))
self._labels = np.zeros((len(filenames), len(classes)))
self._array_lock = Lock()
self._load_images(0, max_size)
self._image_request = Queue()
self._running = True
self._thread = Thread(target=self._update_loop)
self._thread.start()
def stop(self):
self._running = False
self._image_request.put(None)
self._thread.join()
def _update_loop(self):
while self._running:
data = self._image_request.get()
if data is None:
continue
if self.max_size > self._images.shape[0]:
continue
start, stop = data
length = stop-start
if start + self.max_size > self._images.shape[0]:
start_beginning = start + self.max_size - self._images.shape[0]
self._load_images(0, start_beginning)
if start_beginning != length:
self._load_images(self._images.shape[0]-length+start_beginning, self._images.shape[0])
else:
self._load_images(start+self.max_size, stop+self.max_size)
stop = start
start -= length
# In case we try to unload data with negative indices we need to
# make sure stop is None to correctly slice starting from the back
if start-length < 0:
stop = None
self._unload_images(start-length, stop)
def _unload_images(self, start, stop):
if start is None or stop is None:
return
#print(f'[{self.name}] Unloading {start}-{stop}')
with self._array_lock:
self._images[start:stop] = 0
self._labels[start:stop] = 0
def _load_images(self, start, stop):
stop = min(self._images.shape[0], stop)
#print(f'[{self.name}] Loading images in the range {start}-{stop}')
for i in range(start, stop):
f = self._filenames[i]
image = cv2.imread(f)
image = cv2.resize(image, (self._image_size, self._image_size), 0, 0,
cv2.INTER_LINEAR)
image = image.astype(np.float32)
image = np.multiply(image, 1.0 / 255.0)
label = np.zeros(len(self._classes))
index = self._label_indices[i]
label[index] = 1.0
with self._array_lock:
self._images[i] = image
self._labels[i] = label
return True
@property
def max_size(self):
return self._max_size
def __getitem__(self, item):
if isinstance(item, slice):
amount = item.stop-item.start
if amount > self.max_size:
raise ValueError(f'Max size is smaller than batch size {amount} > {self.max_size}')
self._image_request.put((item.start, item.stop))
with self._array_lock:
return self._images[item], self._labels[item]
def __len__(self):
return self._images.shape[0]
class DataSet(object):
def __init__(self, images):
self._num_examples = len(images)
self._images = images
self._epochs_done = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def num_examples(self):
return self._num_examples
@property
def epochs_done(self):
return self._epochs_done
def next_batch(self, batch_size):
"""Return the next `batch_size` examples from this data set."""
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# After each epoch we update this
self._epochs_done += 1
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
return self._images[start:end]
def read_train_sets(train_path, image_size, classes, validation_size, max_size=5000):
class DataSets(object):
pass
data_sets = DataSets()
images, labels = load_train(train_path, classes)
images, labels = shuffle(images, labels)
old_val = validation_size
if isinstance(validation_size, float):
validation_size = int(validation_size * len(images))
validation_files = images[:validation_size]
validation_labels = labels[:validation_size]
train_files = images[validation_size:]
train_labels = labels[validation_size:]
def read_image(filename, label):
image = cv2.imread(filename.decode('utf-8'))
image = cv2.resize(image, (image_size, image_size), 0, 0,
cv2.INTER_LINEAR)
image = image.astype(np.float32)
image = image[:, :, (2, 1, 0)] # BGR to RGB
image = np.multiply(image, 1.0 / 255.0)
label_ = np.zeros(len(classes))
label_[label] = 1.0
return image, label_
train_set = tf.data.Dataset.from_tensor_slices((train_files, train_labels))
train_set = train_set.map(lambda filename, label: tuple(tf.py_func(read_image, [filename, label], [tf.float32, tf.float64])))
train_set = train_set.prefetch(max_size)
validation_set = tf.data.Dataset.from_tensor_slices((validation_files, validation_labels))
validation_set = validation_set.map(lambda filename, label: tuple(tf.py_func(read_image, [filename, label], [tf.float32, tf.float64])))
validation_set = validation_set.prefetch(int(max_size*old_val))
print("Completed reading of input data. Will Now print a snippet of it")
print("Number of files in Training-set:\t{}".format(len(train_labels)))
print("Number of files in Validation-set:\t{}".format(len(validation_labels)))
return train_set, validation_set, len(train_files)