-
Notifications
You must be signed in to change notification settings - Fork 8
/
crowd_dataset.py
569 lines (498 loc) · 26.8 KB
/
crowd_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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
"""
crowd_dataset.py: Code to use crowd counting datasets for training and testing.
"""
import os
import pickle
import random
from pdb import set_trace as bp
import cv2
import numpy as np
import scipy.io
class CrowdDataset:
"""
Class to use crowd counting datasets for training and testing.
Version: 4.1
DataReader supports the following:
ground truths: can create density maps.
testing: full image testing.
training: extract random crops with flip augmentation.
validating: extract random crops without augmentation.
"""
def __init__(self, data_path, name='parta', valid_set_size=0,
gt_downscale_factor=2,
image_size_min=224, image_size_max=1024, image_crop_size=224,
density_map_sigma=1.0, stage_1=False):
"""
Initialize dataset class.
Parameters
----------
data_path: string
Path to the dataset data; the provided directory MUST have following structure:
|-train_data
|-images
|-ground_truth
|-test_data
|-images
|-ground_truth
name: string
Dataset name; MUST be one of ['parta', 'partb',
'ucfqnrf', 'ucf50']
valid_set_size: int
Number of images from train set to be randomly taken for validation. Value MUST BE < number of training
images. Default is 0 and no validation set is created.
gt_downscale_factor: int
Scale factor specifying the spatial size of square GT maps in relation to the input. For instance,
`gt_downscale_factor` = 4 means that the spatial size of returned `gt_head_maps` (gtH, gtW) is exactly
one-fourth that of input `images` (H, W) [see details of train_get_data() and test_get_data() functions for
more information]. The value MUST BE one of [1, 2, 4, 8, 16, 32].
"""
self.image_size_min = image_size_min
self.image_size_max = image_size_max
self.image_crop_size = image_crop_size
assert(self.image_crop_size >= self.image_size_min >= 224)
assert(self.image_size_max > self.image_size_min)
self.data_path = data_path
self.name = name
self.gt_downscale_factor = gt_downscale_factor
self.density_map_kernel = self._gaussian_kernel(density_map_sigma)
self._image_size_multiple = gt_downscale_factor
assert(self.image_size_min % self._image_size_multiple == 0)
assert(self.image_size_max % self._image_size_multiple == 0)
assert(self.image_crop_size % self._image_size_multiple == 0)
self.train_iterator = None
self.val_iterator = None
self.data_paths = {
'train': {
'images': os.path.join(self.data_path, 'train_data', 'images'),
'gt': os.path.join(self.data_path, 'train_data', 'ground_truth')
},
'test': {
'images': os.path.join(self.data_path, 'test_data', 'images'),
'gt': os.path.join(self.data_path, 'test_data', 'ground_truth')
}
}
if "ucfqnrf" in self.name:
self.data_paths['train']['images'] = self.data_paths['train']['images'].replace('train_data', 'Train')
self.data_paths['train']['gt'] = self.data_paths['train']['gt'].replace('train_data', 'Train')
self.data_paths['test']['images'] = self.data_paths['test']['images'].replace('test_data', 'Test')
self.data_paths['test']['gt'] = self.data_paths['test']['gt'].replace('test_data', 'Test')
self.data_files = {
'train': [f for f in sorted(os.listdir(self.data_paths['train']['images']))
if os.path.isfile(os.path.join(self.data_paths['train']['images'], f))],
'test': [f for f in sorted(os.listdir(self.data_paths['test']['images']))
if os.path.isfile(os.path.join(self.data_paths['test']['images'], f))]
}
self.num_train_images = len(self.data_files['train'])
self.num_test_images = len(self.data_files['test'])
assert(valid_set_size < self.num_train_images)
self.num_val_images = valid_set_size
assert(self.num_train_images > 0 and self.num_test_images > 0)
print('In CrowdDataset.__init__(): {} train and {} test images.'.format(self.num_train_images,
self.num_test_images))
if valid_set_size > 0:
files = self.data_files['train']
if stage_1:
files_selected = random.sample(range(0, len(files)), valid_set_size)
np.save('resources/{}_validation_files.npy'.format(name), files_selected)
files_selected = np.load('resources/{}_validation_files.npy'.format(name))
validation_files = [f for i, f in enumerate(files)
if i in files_selected]
train_files = [f for i, f in enumerate(files)
if i not in files_selected]
self.data_paths['test_valid'] = self.data_paths['train']
self.data_files['test_valid'] = validation_files
self.data_files['train'] = train_files
self.num_train_images = len(self.data_files['train'])
print('In CrowdDataset.__init__(): {} valid images selected and train set reduces to {}.'
.format(len(self.data_files['test_valid']), len(self.data_files['train'])))
self.val_pos = open('resources/{}_xy_positions.log'.format(name), 'r').readlines()
self.val_pos_counter = 0
print('In CrowdDataset.__init__(): {} dataset initialized.'.format(self.name))
def train_get_data(self, batch_size=4):
"""
Returns a batch of randomly cropped images from train set (with flip augmentation).
Parameters
----------
batch_size: int
Required batch size.
Returns
----------
List of [images: ndarray((B, C, H, W)),
gt_density_maps: ndarray((B, 1, gtH, gtW)),
where (gtH, gtW) = (H, W) // self.gt_downscale_factor.
"""
assert(batch_size > 0)
# randomly sample train dataset
files = self.data_files['train']
if self.train_iterator is None or (self.train_iterator + batch_size) > self.num_files_rounded:
self.train_iterator = 0
self.num_files_rounded = len(files) - (len(files) % batch_size)
self.file_ids = random.sample(range(0, len(files)), self.num_files_rounded)
file_ids = self.file_ids[self.train_iterator: self.train_iterator + batch_size]
assert(len(file_ids) == batch_size)
file_batch = [files[i] for i in file_ids]
self.train_iterator += batch_size
# initialize train batch
num_channels = 3
images = np.empty((batch_size, num_channels, self.image_crop_size, self.image_crop_size), dtype=np.float32)
gt_crop_size = self.image_crop_size // self.gt_downscale_factor
gt_density_maps = np.empty((batch_size, 1, gt_crop_size, gt_crop_size), dtype=np.float32)
flip_flags = np.random.randint(2, size=batch_size)
# create batch
for i, (file_name, flip_flag) in enumerate(zip(file_batch, flip_flags)):
#print(file_name)
image, gt_head_map = self._read_image_and_gt_map(file_name, self.data_paths['train']['images'],
self.data_paths['train']['gt'])
h, w = image.shape[1] // self.gt_downscale_factor, image.shape[2] // self.gt_downscale_factor
gt_density_map = self._create_heatmap((image.shape[1], image.shape[2]), (h, w),
gt_head_map, self.density_map_kernel)
gt_density_map = gt_density_map[np.newaxis, ...]
if flip_flag == 1:
image = image[:, :, :: -1]
gt_density_map = gt_density_map[:, :, :: -1]
y, x = 0, 0
# random draw (y, x) and make multiple of self._image_size_multiple
if image.shape[1] != self.image_crop_size:
y = (np.random.randint(image.shape[1] - self.image_crop_size) // self._image_size_multiple) \
* self._image_size_multiple
if image.shape[2] != self.image_crop_size:
x = (np.random.randint(image.shape[2] - self.image_crop_size) // self._image_size_multiple) \
* self._image_size_multiple
images[i, :, :, :] = image[:, y: y + self.image_crop_size, x: x + self.image_crop_size]
y //= self.gt_downscale_factor
x //= self.gt_downscale_factor
gt_density_maps[i, 0, :, :] = gt_density_map[:, y: y + gt_crop_size, x: x + gt_crop_size]
assert(np.all(np.logical_and(0.0 <= images, images <= 255.0)))
return images, gt_density_maps
def val_get_data(self, batch_size=4):
"""
Returns a batch of randomly cropped images from val set (without augmentation).
Parameters
----------
batch_size: int
Required batch size.
Returns
----------
List of [images: ndarray((B, C, H, W)),
gt_density_maps: ndarray((B, 1, gtH, gtW)),
where (gtH, gtW) = (H, W) // self.gt_downscale_factor.
"""
assert(batch_size > 0)
# sequentially select val dataset
files = self.data_files['test_valid']
if self.val_iterator is None or (self.val_iterator + batch_size) > self.num_val_files_rounded:
self.val_iterator = 0
self.num_val_files_rounded = len(files) - (len(files) % batch_size)
self.file_ids = range(0, self.num_val_files_rounded)
file_ids = self.file_ids[self.val_iterator: self.val_iterator + batch_size]
assert(len(file_ids) == batch_size)
file_batch = [files[i] for i in file_ids]
self.val_iterator += batch_size
# initialize val batch
num_channels = 3
images = np.empty((batch_size, num_channels, self.image_crop_size, self.image_crop_size), dtype=np.float32)
gt_crop_size = self.image_crop_size // self.gt_downscale_factor
gt_density_maps = np.empty((batch_size, 1, gt_crop_size, gt_crop_size), dtype=np.float32)
# create batch
for i, file_name in enumerate(file_batch):
image, gt_head_map = self._read_image_and_gt_map(file_name, self.data_paths['train']['images'],
self.data_paths['train']['gt'])
h, w = image.shape[1] // self.gt_downscale_factor, image.shape[2] // self.gt_downscale_factor
gt_density_map = self._create_heatmap((image.shape[1], image.shape[2]), (h, w),
gt_head_map, self.density_map_kernel)
gt_density_map = gt_density_map[np.newaxis, ...]
if False:
y, x = 0, 0
# random draw (y, x) and make multiple of self._image_size_multiple
if image.shape[1] != self.image_crop_size:
y = (np.random.randint(image.shape[1] - self.image_crop_size) // self._image_size_multiple) \
* self._image_size_multiple
if image.shape[2] != self.image_crop_size:
x = (np.random.randint(image.shape[2] - self.image_crop_size) // self._image_size_multiple) \
* self._image_size_multiple
self.f.write("sampling positions: {}, {}, {}, {}\n".format(i, file_name, y, x))
images[i, :, :, :] = image[:, y: y + self.image_crop_size, x: x + self.image_crop_size]
y //= self.gt_downscale_factor
x //= self.gt_downscale_factor
else:
y, x = int(self.val_pos[self.val_pos_counter].split(',')[-2].strip()), int(self.val_pos[self.val_pos_counter].split(',')[-1].strip())
self.val_pos_counter = (self.val_pos_counter + 1) % len(self.val_pos)
images[i, :, :, :] = image[:, y: y + self.image_crop_size, x: x + self.image_crop_size]
y //= self.gt_downscale_factor
x //= self.gt_downscale_factor
gt_density_maps[i, 0, :, :] = gt_density_map[:, y: y + gt_crop_size, x: x + gt_crop_size]
assert(np.all(np.logical_and(0.0 <= images, images <= 255.0)))
return images, gt_density_maps
def test_get_data(self, set_name='test'):
"""
An iterator to run over images of test/valid set.
Parameters
----------
set_name: string
Name of the set ('test' or 'test_valid') for evaluation.
Returns
----------
An iterator which outputs a tuple of 4 items:
image_name: string
file name
image: ndarray((1, 3, H, W))
gt_density_map: ndarray((1, 1, gtH, gtW))
where (gtH, gtW) = (H, W) // self.gt_downscale_factor.
Example Usage
----------
for name, image, gt_head_map, gt_box_maps_list in _.test_get_data()
# process
"""
assert(set_name in ['test', 'test_valid'])
for image_name in self.data_files[set_name]:
image, gt_head_map = self._read_image_and_gt_map(image_name, self.data_paths[set_name]['images'],
self.data_paths[set_name]['gt'])
h, w = image.shape[1] // self.gt_downscale_factor, image.shape[2] // self.gt_downscale_factor
gt_density_map = self._create_heatmap((image.shape[1], image.shape[2]), (h, w),
gt_head_map, self.density_map_kernel)
gt_density_map = gt_density_map[np.newaxis, ...]
yield image_name, image[np.newaxis, :, :, :], gt_density_map[:,np.newaxis,:,:]
# ### ### ### Internal functions ### ### ### #
def _read_image_and_gt_map(self, image_name, image_path, gt_path=None):
"""
Reads image and corresponding ground truth.
Parameters
----------
image_name: string
file name
image_path: string
directory path to image file
gt_path: string
directory path to corresponding gt file
Returns
----------
image: ndarray(3, H, W)
gt_head_map: ndarray(1, gtH, gtW)
"""
image = cv2.imread(os.path.join(image_path, image_name))
if len(image.shape) < 3:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
else:
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
assert(len(image.shape) == 3)
height_orig, width_orig, _ = image.shape
height, width, _ = image.shape
# setting minimum size
if height < self.image_size_min or width < self.image_size_min:
if height <= width:
width = int((float(self.image_size_min * width) / height) + 0.5)
height = self.image_size_min
else:
height = int((float(self.image_size_min * height) / width) + 0.5)
width = self.image_size_min
# setting maximum size
if height > self.image_size_max or width > self.image_size_max:
if height >= width:
width = int((float(self.image_size_max * width) / height) + 0.5)
height = self.image_size_max
else:
height = int((float(self.image_size_max * height) / width) + 0.5)
width = self.image_size_max
# make sizes multiple
if height % self._image_size_multiple != 0:
height = ((height // self._image_size_multiple) + 1) * self._image_size_multiple
if width % self._image_size_multiple != 0:
width = ((width // self._image_size_multiple) + 1) * self._image_size_multiple
# resize image
if height != height_orig or width != width_orig:
image = cv2.resize(src = image, dsize = (width, height))
image = image.transpose((2, 0, 1)).astype(np.float32) # (3, H, W)
assert(np.all(np.logical_and(0.0 <= image, image <= 255.0)))
assert(np.all(np.isfinite(image)))
if gt_path is None:
return image, None
# read GT
f_name, _ = os.path.splitext(image_name)
if self.name in ['ucfqnrf', 'ucf50']:
gt_path = os.path.join(gt_path, f_name + '_ann.mat')
elif self.name in ['parta', 'partb', 'parta_1', 'parta_5', 'parta_10', 'parta_30']:
gt_path = os.path.join(gt_path, 'GT_' + f_name + '.mat')
data_mat = scipy.io.loadmat(gt_path)
if self.name in ['ucfqnrf', 'ucf50']:
gt_annotation_points = data_mat['annPoints']
elif self.name in ['parta', 'partb', 'parta_1', 'parta_5', 'parta_10', 'parta_30']:
gt_annotation_points = data_mat['image_info'][0, 0]['location'][0, 0]
gt_annotation_points -= 1 # MATLAB indices
'''
annotation_points : ndarray Nx2,
annotation_points[:, 0] -> x coordinate
annotation_points[:, 1] -> y coordinate
'''
# scale GT points
gt_map_shape = (height, width)
gt_annotation_points[:, 0] *= (float(gt_map_shape[1]) / width_orig)
gt_annotation_points[:, 1] *= (float(gt_map_shape[0]) / height_orig)
# remove invalid indices
indices = (gt_annotation_points[:, 0] < gt_map_shape[1]) & \
(gt_annotation_points[:, 0] >= 0) & \
(gt_annotation_points[:, 1] < gt_map_shape[0]) & \
(gt_annotation_points[:, 1] >= 0)
gt_annotation_points = gt_annotation_points[indices, :].astype(int)
gt_annotation_points = np.floor(gt_annotation_points)
gt_head_map = gt_annotation_points
return image, gt_head_map
def _gaussian_kernel(self, sigma=1.0, kernel_size=None):
'''
Returns gaussian kernel if sigma > 0.0, otherwise dot kernel.
'''
if sigma <= 0.0:
return np.array([[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 0.0]], dtype=np.float32)
if kernel_size is None:
kernel_size = int(3.0 * sigma)
if kernel_size % 2 == 0:
kernel_size += 1
print('In data_reader.gaussian_kernel: Kernel size even; ' \
'increased by 1.')
if kernel_size < 3:
kernel_size = 3
print('In data_reader.gaussian_kernel: Kernel size less than 3;' \
'set as 3.')
tmp = np.arange((-kernel_size // 2) + 1.0, (kernel_size // 2) + 1.0)
xx, yy = np.meshgrid(tmp, tmp)
kernel = np.exp(-((xx ** 2) + (yy ** 2)) / (2.0 * (sigma ** 2)))
kernel_sum = np.sum(kernel)
assert (kernel_sum > 1e-3)
return kernel / kernel_sum
def _create_heatmap(self, image_shape, heatmap_shape,
annotation_points, kernel):
"""
Creates density map.
annotation_points : ndarray Nx2,
annotation_points[:, 0] -> x coordinate
annotation_points[:, 1] -> y coordinate
"""
assert (kernel.shape[0] == kernel.shape[1] and kernel.shape[0] % 2
and kernel.shape[0] > 1)
indices = (annotation_points[:, 0] < image_shape[1]) & \
(annotation_points[:, 0] >= 0) & \
(annotation_points[:, 1] < image_shape[0]) & \
(annotation_points[:, 1] >= 0)
annot_error_count = len(annotation_points)
annotation_points = annotation_points[indices, :]
hmap_height, hmap_width = heatmap_shape
annotation_points[:, 0] *= (1. * heatmap_shape[1] / image_shape[1])
annotation_points[:, 1] *= (1. * heatmap_shape[0] / image_shape[0])
annotation_points = annotation_points.astype(np.int32)
annot_error_count -= np.sum(indices)
if annot_error_count:
print('In data_reader.create_heatmap: Error in annotations; ' \
'%d point(s) skipped.' % annot_error_count)
indices = (annotation_points[:, 0] >= heatmap_shape[1]) & \
(annotation_points[:, 0] < 0) & \
(annotation_points[:, 1] >= heatmap_shape[0]) & \
(annotation_points[:, 1] < 0)
assert(np.sum(indices) == 0)
prediction_map = np.zeros(heatmap_shape, dtype = np.float32)
kernel_half_size = kernel.shape[0] // 2
kernel_copy = np.empty_like(kernel)
for x, y in annotation_points:
y_start = y - kernel_half_size
y_end = y_start + kernel.shape[0]
x_start = x - kernel_half_size
x_end = x_start + kernel.shape[1]
kernel_copy[:] = kernel[:]
kernel_tmp = kernel_copy
if y_start < 0:
i = -y_start
kernel_tmp[i: 2 * i, :] += kernel_tmp[i - 1:: -1, :]
kernel_tmp = kernel_tmp[i:, :]
y_start = 0
if x_start < 0:
i = -x_start
kernel_tmp[:, i: 2 * i] += kernel_tmp[:, i - 1:: -1]
kernel_tmp = kernel_tmp[:, i:]
x_start = 0
if y_end > hmap_height:
i = (hmap_height - y - 1) - kernel_half_size
kernel_tmp[2 * i: i, :] += kernel_tmp[-1: i - 1: -1, :]
kernel_tmp = kernel_tmp[: i, :]
y_end = hmap_height
if x_end > hmap_width:
i = (hmap_width - x - 1) - kernel_half_size
kernel_tmp[:, 2 * i: i] += kernel_tmp[:, -1: i - 1: -1]
kernel_tmp = kernel_tmp[:, : i]
x_end = hmap_width
prediction_map[y_start: y_end, x_start: x_end] += kernel_tmp
return prediction_map
class CrowdDatasetLabelled(CrowdDataset):
"""
Class to extract a predefine number or percentage of crowd counting samples for training.
Version: 0.1
DataReader supports the following:
ground truths: can create density maps.
testing: full image testing.
training: extract random crops with flip augmentation.
validating: extract random crops without augmentation.
"""
def __init__(self, data_path, name='parta', valid_set_size=0,
gt_downscale_factor=2,
image_size_min=224, image_size_max=1024, image_crop_size=224,
density_map_sigma=1.0, num_labels = None):
self.image_size_min = image_size_min
self.image_size_max = image_size_max
self.image_crop_size = image_crop_size
assert(self.image_crop_size >= self.image_size_min >= 224)
assert(self.image_size_max > self.image_size_min)
self.data_path = data_path
self.name = name
self.gt_downscale_factor = gt_downscale_factor
self.density_map_kernel = self._gaussian_kernel(density_map_sigma)
self._image_size_multiple = gt_downscale_factor
assert(self.image_size_min % self._image_size_multiple == 0)
assert(self.image_size_max % self._image_size_multiple == 0)
assert(self.image_crop_size % self._image_size_multiple == 0)
self.train_iterator = None
self.data_paths = {
'train': {
'images': os.path.join(self.data_path, 'train_data', 'images'),
'gt': os.path.join(self.data_path, 'train_data', 'ground_truth')
},
'test': {
'images': os.path.join(self.data_path, 'test_data', 'images'),
'gt': os.path.join(self.data_path, 'test_data', 'ground_truth')
}
}
if "ucfqnrf" in self.name:
self.data_paths['train']['images'] = self.data_paths['train']['images'].replace('train_data', 'Train')
self.data_paths['train']['gt'] = self.data_paths['train']['gt'].replace('train_data', 'Train')
self.data_paths['test']['images'] = self.data_paths['test']['images'].replace('test_data', 'Test')
self.data_paths['test']['gt'] = self.data_paths['test']['gt'].replace('test_data', 'Test')
self.data_files = {
'train': [f for f in sorted(os.listdir(self.data_paths['train']['images']))
if os.path.isfile(os.path.join(self.data_paths['train']['images'], f))],
'test': [f for f in sorted(os.listdir(self.data_paths['test']['images']))
if os.path.isfile(os.path.join(self.data_paths['test']['images'], f))]
}
self.num_train_images = len(self.data_files['train'])
self.num_test_images = len(self.data_files['test'])
assert(valid_set_size < self.num_train_images)
self.num_val_images = valid_set_size
assert(self.num_train_images > 0 and self.num_test_images > 0)
print('In CrowdDatasetLabelled.__init__(): {} train and {} test images.'.format(self.num_train_images,
self.num_test_images))
if valid_set_size > 0:
files = self.data_files['train']
files_selected = np.load('resources/{}_validation_files.npy'.format(name))
validation_files = [f for i, f in enumerate(files)
if i in files_selected]
train_files = [f for i, f in enumerate(files)
if i not in files_selected]
self.data_paths['test_valid'] = self.data_paths['train']
self.data_files['test_valid'] = validation_files
if(int(num_labels) == num_labels):
self.data_files['train'] = np.random.choice(train_files, int(num_labels), replace=False)
elif 1e-8 <= num_labels < 1.:
self.data_files['train'] = np.random.choice(train_files, math.ceil(num_labels*len(train_files)), replace=False)
else:
raise Exception("Correct the num_labels specified")
self.num_train_images = len(self.data_files['train'])
print('In CrowdDatasetLabelled.__init__(): {} valid images selected and train set reduces to {}.'
.format(len(self.data_files['test_valid']), len(self.data_files['train'])))
print('In CrowdDatasetLabelled.__init__(): {} dataset initialized.'.format(self.name))