-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathdatasets.py
363 lines (311 loc) · 13.6 KB
/
datasets.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import numpy
import theano
from fuel.streams import DataStream
from fuel.datasets import H5PYDataset
from fuel.schemes import ShuffledScheme
from fuel.transformers import Transformer
import h5py
import numpy as np
from StringIO import StringIO
from PIL import Image
import fuel.datasets
from fuel import config
floatX = theano.config.floatX
def get_memory_streams(num_train_examples, batch_size, time_length=15, dim=2):
from fuel.datasets import IterableDataset
numpy.random.seed(0)
num_sequences = num_train_examples / batch_size
# generating random sequences
seq_u = numpy.random.randn(num_sequences, time_length, batch_size, dim)
seq_y = numpy.zeros((num_sequences, time_length, batch_size, dim))
seq_y[:, 1:, :, 0] = seq_u[:, :-1, :, 0] # 1 time-step delay
seq_y[:, 3:, :, 1] = seq_u[:, :-3, :, 1] # 3 time-step delay
seq_y += 0.01 * numpy.random.standard_normal(seq_y.shape)
dataset = IterableDataset({'features': seq_u.astype(floatX),
'targets': seq_y.astype(floatX)})
tarin_stream = DataStream(dataset)
valid_stream = DataStream(dataset)
return tarin_stream, valid_stream
class CMVv1(H5PYDataset):
def __init__(self, which_sets, **kwargs):
kwargs.setdefault('load_in_memory', False)
super(CMVv1, self).__init__(
("/data/lisatmp3/cooijmat/datasets/old-cluttered-mnist-video/" +
"cluttered-mnist-video.hdf5"),
which_sets, **kwargs)
class Preprocessor_CMV_v1(Transformer):
def __init__(self, data_stream, **kwargs):
super(Preprocessor_CMV_v1, self).__init__(
data_stream, **kwargs)
def get_data(self, request=None):
data = next(self.child_epoch_iterator)
transformed_data = []
normed_feat = data[0] / 255.0
normed_feat = normed_feat.astype('float32')
normed_feat = normed_feat[:, 5:15]
B, T, X, Y, C = normed_feat.shape
transformed_data.append(
numpy.swapaxes(
normed_feat.reshape(
(B, T, X * Y * C)),
0, 1))
# Now the data shape should be T x B x F
transformed_data.append(data[1])
return transformed_data
def get_cmv_v1_streams(batch_size):
train_dataset = CMVv1(which_sets=["train"])
valid_dataset = CMVv1(which_sets=["valid"])
train_ind = numpy.arange(train_dataset.num_examples)
valid_ind = numpy.arange(valid_dataset.num_examples)
rng = numpy.random.RandomState(seed=1)
rng.shuffle(train_ind)
rng.shuffle(valid_ind)
train_datastream = DataStream.default_stream(
train_dataset,
iteration_scheme=ShuffledScheme(train_ind, batch_size))
train_datastream = Preprocessor_CMV_v1(train_datastream)
valid_datastream = DataStream.default_stream(
valid_dataset,
iteration_scheme=ShuffledScheme(valid_ind, batch_size))
valid_datastream = Preprocessor_CMV_v1(valid_datastream)
return train_datastream, valid_datastream
class CMVv2(fuel.datasets.H5PYDataset):
def __init__(self, path, which_set):
file = h5py.File(path, "r")
super(CMVv2, self).__init__(
file, sources=tuple("videos targets".split()),
which_sets=(which_set,), load_in_memory=True)
# TODO: find a way to deal with `which_sets`, especially when
# they can be discontiguous and when `subset` is provided, and
# when all the video ranges need to be adjusted to account for this
self.frames = np.array(file["frames"][which_set])
file.close()
def get_data(self, *args, **kwargs):
video_ranges, targets = super(
CMVv2, self).get_data(*args, **kwargs)
videos = list(map(self.video_from_jpegs, video_ranges))
return videos, targets
def video_from_jpegs(self, video_range):
frames = self.frames[video_range[0]:video_range[1]]
video = np.array(map(self.load_frame, frames))
return video
def load_frame(self, jpeg):
image = Image.open(StringIO(jpeg.tostring()))
image = (np.array(image.getdata(), dtype=np.float32)
.reshape((image.size[1], image.size[0])))
image /= 255.0
return image
class Preprocessor_CMV_v2(Transformer):
def __init__(self, data_stream, **kwargs):
super(Preprocessor_CMV_v2, self).__init__(
data_stream, **kwargs)
def get_data(self, request=None):
data = next(self.child_epoch_iterator)
transformed_data = []
features = [data_point[:, np.newaxis, :, :] for data_point in data[0]]
features = np.hstack(features)
T, B, X, Y = features.shape
features = features.reshape(T, B, -1)
features = features.astype('float32')
# Now the data shape should be T x B x F
transformed_data.append(features)
transformed_data.append(data[1])
return transformed_data
def get_cmv_v2_streams(batch_size):
path = '/data/lisatmp3/cooijmat/datasets/cmv/cmv20x64x64_jpeg.hdf5'
train_dataset = CMVv2(path=path, which_set="train")
valid_dataset = CMVv2(path=path, which_set="valid")
train_ind = numpy.arange(train_dataset.num_examples)
valid_ind = numpy.arange(valid_dataset.num_examples)
rng = numpy.random.RandomState(seed=1)
rng.shuffle(train_ind)
rng.shuffle(valid_ind)
train_datastream = DataStream.default_stream(
train_dataset,
iteration_scheme=ShuffledScheme(train_ind, batch_size))
train_datastream = Preprocessor_CMV_v2(train_datastream)
valid_datastream = DataStream.default_stream(
valid_dataset,
iteration_scheme=ShuffledScheme(valid_ind, batch_size))
valid_datastream = Preprocessor_CMV_v2(valid_datastream)
train_datastream.sources = ('features', 'targets')
valid_datastream.sources = ('features', 'targets')
return train_datastream, valid_datastream
class JpegHDF5Transformer(Transformer):
"""
Decode jpeg and perform spatial crop if needed
if input_size == crop_size, no crop is done
input_size: spatially resize the input to that size
crop_size: take a crop of that size out of the inputs
nb_channels: number of channels of the inputs
flip: in 'random', 'flip', 'noflip' activate flips data augmentation
swap_rgb: Swap rgb pixel using in [2 1 0] order
crop_type: random, corners or center type of cropping
scale: pixel values are scale into the range [0, scale]
nb_frames: maximum number of frame (will be zero padded)
"""
def __init__(self,
input_size=(240, 320),
crop_size=(224, 224),
nchannels=3,
flip='random',
resize=True,
mean=None,
swap_rgb=False,
crop_type='random',
scale=1.,
nb_frames= 25,
*args, **kwargs):
self.rng = kwargs.pop('rng', None)
if self.rng is None:
self.rng = np.random.RandomState(config.default_seed)
super(JpegHDF5Transformer, self).__init__(*args, **kwargs)
self.input_size = input_size
self.crop_size = crop_size
self.nchannels = nchannels
self.swap_rgb = swap_rgb
self.flip = flip
self.nb_frames = nb_frames
self.resize = resize
self.scale = scale
self.mean = mean
self.data_sources = ('targets', 'images')
# multi-scale
self.scales = [256, 224, 192, 168]
# Crop coordinate
self.crop_type = crop_type
self.centers = np.array(input_size) / 2.0
self.centers_crop = (self.centers[0] - self.crop_size[0] / 2.0,
self.centers[1] - self.crop_size[1] / 2.0)
self.corners = []
self.corners.append((0, 0))
self.corners.append((0, self.input_size[1] - self.crop_size[1]))
self.corners.append((self.input_size[0] - self.crop_size[0], 0))
self.corners.append((self.input_size[0] - self.crop_size[0],
self.input_size[1] - self.crop_size[1]))
self.corners.append(self.centers_crop)
# Value checks
assert self.crop_type in ['center', 'corners', 'random',
'upleft', 'downleft',
'upright', 'downright',
'random_multiscale',
'corners_multiscale']
assert self.flip in ['random', 'flip', 'noflip']
assert self.crop_size[0] <= self.input_size[0]
assert self.crop_size[1] <= self.input_size[1]
assert self.nchannels >= 1
def multiscale_crop(self):
scale_x = self.rng.randint(0, len(self.scales))
scale_y = self.rng.randint(0, len(self.scales))
crop_size = (self.scales[scale_x], self.scales[scale_y])
centers_crop = (self.centers[0] - crop_size[0] / 2.0,
self.centers[1] - crop_size[1] / 2.0)
corners = []
corners.append((0, 0))
corners.append((0, self.input_size[1] - crop_size[1]))
corners.append((self.input_size[0] - crop_size[0], 0))
corners.append((self.input_size[0] - crop_size[0],
self.input_size[1] - crop_size[1]))
corners.append(centers_crop)
return corners, crop_size
def get_crop_coord(self, crop_size, corners):
x_start = 0
y_start = 0
corner_rng = self.rng.randint(0, 5)
if ((self.crop_type == 'random' or
self.crop_type == 'random_multiscale')):
if crop_size[0] <= self.input_size[0]:
if crop_size[0] == self.input_size[0]:
x_start = 0
else:
x_start = self.rng.randint(
0, self.input_size[0] - crop_size[0])
if crop_size[1] == self.input_size[1]:
y_start = 0
else:
y_start = self.rng.randint(
0, self.input_size[1] - crop_size[1])
elif ((self.crop_type == 'corners' or
self.crop_type == 'corners_multiscale')):
x_start = corners[corner_rng][0]
y_start = corners[corner_rng][1]
elif self.crop_type == 'upleft':
x_start = corners[0][0]
y_start = corners[0][1]
elif self.crop_type == 'upright':
x_start = corners[1][0]
y_start = corners[1][1]
elif self.crop_type == 'downleft':
x_start = corners[2][0]
y_start = corners[2][1]
elif self.crop_type == 'downright':
x_start = corners[3][0]
y_start = corners[3][1]
elif self.crop_type == 'center':
x_start = corners[4][0]
y_start = corners[4][1]
else:
raise ValueError
return x_start, y_start
def crop(self):
if ((self.crop_type == 'random_multiscale' or
self.crop_type == 'corners_multiscale')):
corners, crop_size = self.multiscale_crop()
else:
corners, crop_size = self.corners, self.crop_size
x_start, y_start = self.get_crop_coord(crop_size, corners)
bbox = (int(y_start), int(x_start),
int(y_start + crop_size[1]), int(x_start + crop_size[0]))
return bbox
def get_data(self, request=None):
if request is not None:
raise ValueError
batch = next(self.child_epoch_iterator)
images, labels = self.preprocess_data(batch)
return images, labels
def preprocess_data(self, batch):
# in batch[0] are all the vidis. They are the same for each fpv element
# in batch[1] are all the frames. A group of fpv is one video
fpv = self.nb_frames
data_array = batch[0]
num_videos = int(len(data_array) / fpv)
x = np.zeros((num_videos, fpv,
self.crop_size[0], self.crop_size[1], self.nchannels),
dtype='float32')
y = np.empty(num_videos, dtype='int64')
for i in xrange(num_videos):
y[i] = batch[1][i * fpv]
do_flip = self.rng.rand(1)[0]
bbox = self.crop()
for j in xrange(fpv):
data = data_array[i * fpv + j]
# this data was stored in uint8
data = StringIO(data.tostring())
data.seek(0)
img = Image.open(data)
if (img.size[0] != self.input_size[1] and
img.size[1] != self.input_size[0]):
img = img.resize((int(self.input_size[1]),
int(self.input_size[0])),
Image.ANTIALIAS)
img = img.crop(bbox)
img = img.resize((int(self.crop_size[1]),
int(self.crop_size[0])),
Image.ANTIALIAS)
# cv2.imshow('img', np.array(img))
# cv2.waitKey(0)
# cv2.destroyAllWindows()
img = (np.array(img).astype(np.float32) / 255.0) * self.scale
if self.nchannels == 1:
img = img[:, :, None]
if self.swap_rgb and self.nchannels == 3:
img = img[:, :, [2, 1, 0]]
x[i, j, :, :, :] = img[:, :, :]
# Flip
if self.flip == 'flip' or (self.flip == 'random'
and do_flip > 0.5):
new_image = np.empty_like(x[i, j, :, :, :])
for c in xrange(self.nchannels):
new_image[:, :, c] = np.fliplr(x[i, j, :, :, c])
x[i, j, :, :, :] = new_image
return (x, y)