19 Authors: Giacomo Colosio, Sebastiano Colosio, Patrizio Acquadro, Tito Nicola Drugman
29 import tensorflow
as tf
30 from typing
import Tuple, List
34 Parsing of the labels files
36 label_path (str): Path of the label file.
39 ground_truths (np.array) : shape (ground_truths, 5+3*keypoints) ground truths present in the label file
41 if label_path ==
"None":
42 ground_truths = np.array([17*3*[0.]],np.float32)
44 file = open(label_path)
45 txt = file.read().split(
"\n")
46 txt = [x.split(
" ")
for x
in txt]
47 if len(txt[-1])==1:txt=txt[:-1]
48 ground_truths = np.array([[float(j)
for j
in i]
for i
in txt],np.float32)
53 Normalization of the labels -> same shape for every label regarding the number of ground truths
55 label (np.array): shape (ground_truths, 5+3*keypoints) ground truths present in the label file
56 l (int): shape (1, ) maximum number of ground truths present in a label file
57 n (int): shape (1, ) current number of ground truths present in this label file
60 normalized_label (np.array) : shape (l, 5+3*keypoints) label with normalized shape
63 miss = np.zeros((m,l))
64 normalized_label = np.concatenate([label,miss])
65 return normalized_label
69 shuffle : bool =
True) -> tf.data.Dataset:
71 Creates a tf.data.Dataset from a dataset root directory path.
72 The dataset has the following directory structure (checked in parse_config.py):
81 path (str): Path of the dataset folder.
82 seed (int): seed when performing shuffle.
83 shuffle (bool): Shuffle the dataset.
86 dataset(tf.data.Dataset) -> dataset with a tuple (path, label) of each sample.
89 paths_imgs = sorted([os.path.join(path,file)
for file
in os.listdir(path)
if file.endswith(
".jpg")])
90 paths_labels = sorted([os.path.join(path,file)
for file
in os.listdir(path)
if file.endswith(
".txt")])
92 if len(paths_imgs) != len(paths_labels):
93 paths_labels = [
"None"]*len(paths_imgs)
95 paths_labels = list(map(_parse_labels,paths_labels))
97 len_max = max([len(p)
for p
in paths_labels])
98 len_label = len(paths_labels[0][0])
102 paths_labels = list(map(
lambda x :
_normalize_labels(x,n=len_max,l=len_label), paths_labels))
104 data_list = list(zip(paths_imgs,paths_labels))
107 rng = np.random.RandomState(seed)
108 rng.shuffle(data_list)
110 imgs, labels = zip(*data_list)
112 dataset = tf.data.Dataset.from_tensor_slices((list(imgs), list(labels)))
120 padded_boxes = data[:,1:5]
122 x1 = padded_boxes[:,0] - padded_boxes[:,2]/2
123 y1 = padded_boxes[:,1] - padded_boxes[:,3]/2
124 x2 = padded_boxes[:,0] + padded_boxes[:,2]/2
125 y2 = padded_boxes[:,1] + padded_boxes[:,3]/2
127 xboxes = tf.cast(tf.stack([x1,x2]),tf.float32)
128 yboxes = tf.cast(tf.stack([y1,y2]),tf.float32)
130 padded_keypoints = data[:,5:]
131 padded_keypoints = tf.cast(tf.transpose(tf.reshape(padded_keypoints,[sh[0],-1,3]),[2,0,1]),tf.float32)
134 ax = tf.cast(width,tf.float32)
135 ra = tf.cast(R/r,tf.float32)
136 kp = tf.cast(1,tf.float32)
139 ax = tf.cast(height,tf.float32)
140 ra = tf.cast(r/R,tf.float32)
141 kp = tf.cast(0,tf.float32)
143 nb_px_added = tf.cast(1 - ax*ra,tf.float32)
144 odd = tf.cast(nb_px_added%2,tf.float32)
145 vectorxy = (tf.cast(ra,tf.float32) * (xboxes*(1-kp)+yboxes*kp - 0.5) + 0.5 ) - odd*0.5/(ax-1)
146 vector = (tf.cast(ra,tf.float32) * (padded_keypoints[0]*(1-kp) + padded_keypoints[1]*kp - 0.5) + 0.5 ) - odd*0.5/(ax-1)
148 pk0 = tf.cast(kp,tf.float32)*padded_keypoints[0] + (1-tf.cast(kp,tf.float32))*vector
149 pk1 = (1-tf.cast(kp,tf.float32))*padded_keypoints[1] + tf.cast(kp,tf.float32)*vector
151 xboxes = (1-tf.cast(kp,tf.float32))*vectorxy + tf.cast(kp,tf.float32)*xboxes
152 yboxes = tf.cast(kp,tf.float32)*vectorxy + (1-tf.cast(kp,tf.float32))*yboxes
154 x = (xboxes[0] + xboxes[1]) / 2
155 y = (yboxes[0] + yboxes[1]) / 2
156 w = xboxes[1] - xboxes[0]
157 h = yboxes[1] - yboxes[0]
159 padded_boxes = tf.stack([x,y,w,h],1)
161 padded_keypoints = tf.stack([pk0,pk1,padded_keypoints[2]])
162 padded_keypoints = tf.reshape(tf.transpose(padded_keypoints,[1,2,0]),[sh[0],-1])
164 data = tf.cast(data,tf.float32)
166 padded_labels = tf.concat([data[:,:1],padded_boxes,padded_keypoints],-1)
167 padded_labels = tf.cast(padded_labels,tf.float32)
173 image_size: tuple[int],
177 nbr_keypoints: int) -> tuple[tf.Tensor, tf.Tensor]:
179 Load images from path and apply necessary transformations.
181 height, width = image_size
182 channels = 1
if color_mode ==
"grayscale" else 3
184 image = tf.io.read_file(data_x)
185 image = tf.image.decode_image(image, channels=channels, expand_animations=
False)
191 if aspect_ratio ==
"fit":
192 image = tf.image.resize(image, [height, width], method=interpolation, preserve_aspect_ratio=
False)
193 data_y = tf.cast(data_y,tf.float32)
194 elif aspect_ratio ==
"padding":
195 image = tf.image.resize_with_pad(image, height, width)
198 raise ValueError(
"In config file, at section preprocessing.aspect_ratio choose 'fit' or 'padding'")
204 image_size: tuple[int] =
None,
205 nbr_keypoints: int =
None,
206 interpolation: str =
None,
207 aspect_ratio: str =
None,
208 color_mode: str =
None,
209 validation_split: float =
None,
210 batch_size: int =
None,
212 shuffle: bool =
True,
213 to_cache: bool =
False) -> Tuple[tf.data.Dataset, tf.data.Dataset]:
215 Loads the images under a given dataset root directory and returns training
216 and validation tf.Data.datasets.
217 The dataset has the following directory structure (checked in parse_config.py):
226 training_path (str): Path to the directory containing the training images.
227 image_size (tuple[int]): Size of the input images to resize them to.
228 nbr_keypoints (int): number of keypoints for a person
229 interpolation (float): Interpolation method to use when resizing the images.
230 aspect_ratio (bool): Whether or not to crop the images to the specified aspect ratio.
231 color_mode (str): Color mode to use for the images.
232 validation_split (float): Fraction of the data to use for validation.
233 batch_size (int): Batch size to use for training and validation.
234 seed (int): Seed to use for shuffling the data.
235 shuffle (bool): Whether or not to shuffle the data.
236 to_cache (bool): Whether or not to cache the datasets.
239 Tuple[tf.data.Dataset, tf.data.Dataset]: Training and validation datasets.
247 interpolation = interpolation
if interpolation
else "bilinear"
248 aspect_ratio = aspect_ratio
if aspect_ratio
else "fit"
249 color_mode = color_mode
if color_mode
else "rgb"
250 validation_split = validation_split
if validation_split
else 0.2
251 batch_size = batch_size
if batch_size
else 32
253 preprocess_params = (image_size,
261 train_size = int(len(dataset)*(1-validation_split))
262 train_ds = dataset.take(train_size)
263 val_ds = dataset.skip(train_size)
266 train_ds = train_ds.shuffle(len(train_ds), reshuffle_each_iteration=
True, seed=seed)
271 train_ds = train_ds.batch(batch_size, drop_remainder=
True)
272 val_ds = val_ds.batch(batch_size, drop_remainder=
True)
275 train_ds = train_ds.cache()
276 val_ds = val_ds.cache()
278 train_ds = train_ds.prefetch(buffer_size=tf.data.AUTOTUNE)
279 val_ds = val_ds.prefetch(buffer_size=tf.data.AUTOTUNE)
281 return train_ds, val_ds
285 image_size: tuple[int] =
None,
286 nbr_keypoints: int =
None,
287 interpolation: str =
None,
288 aspect_ratio: str =
None,
289 color_mode: str =
None,
290 batch_size: int =
None,
292 shuffle: bool =
False,
293 to_cache: bool =
False) -> tf.data.Dataset:
295 Loads the images from the given dataset root directory and returns a tf.data.Dataset.
296 The dataset has the following directory structure (checked in parse_config.py):
305 data_path (str): Path to the directory containing the images.
306 image_size (tuple[int]): Size of the input images to resize them to.
307 nbr_keypoints (int): number of keypoints for a person
308 interpolation (str): Interpolation method to use when resizing the images.
309 aspect_ratio (bool): Whether or not to crop the images to the specified aspect ratio.
310 color_mode (str): Color mode to use for the images.
311 batch_size (int): Batch size to use for the dataset.
312 seed (int): Seed to use for shuffling the data.
313 shuffle (bool): Whether or not to shuffle the data.
314 to_cache (bool): Whether or not to cache the dataset.
317 tf.data.Dataset: Dataset containing the images.
325 interpolation = interpolation
if interpolation
else "bilinear"
326 aspect_ratio = aspect_ratio
if aspect_ratio
else "fit"
327 color_mode = color_mode
if color_mode
else "rgb"
328 batch_size = batch_size
if batch_size
else 32
330 preprocess_params = (image_size,
339 dataset = dataset.shuffle(len(dataset), reshuffle_each_iteration=
True, seed=seed)
342 dataset = dataset.batch(batch_size, drop_remainder=
True)
345 dataset = dataset.cache()
347 dataset = dataset.prefetch(buffer_size=tf.data.AUTOTUNE)
353 training_path: str =
None,
354 validation_path: str =
None,
355 quantization_path: str =
None,
356 test_path: str =
None,
357 validation_split: float =
None,
358 nbr_keypoints: int =
None,
359 image_size: tuple[int] =
None,
360 interpolation: str =
None,
361 aspect_ratio: str =
None,
362 color_mode: str =
None,
363 batch_size: int =
None,
364 seed: int =
None) -> Tuple[tf.data.Dataset, tf.data.Dataset, tf.data.Dataset]:
366 Loads the images from the given dataset root directories and returns training,
367 validation, and test tf.data.Datasets.
368 The datasets have the following directory structure (checked in parse_config.py):
377 dataset_name (str): Name of the dataset to load.
378 training_path (str): Path to the directory containing the training images.
379 validation_path (str): Path to the directory containing the validation images.
380 quantization_path (str): Path to the directory containing the quantization images.
381 test_path (str): Path to the directory containing the test images.
382 validation_split (float): Fraction of the data to use for validation.
383 nbr_keypoints (int): number of keypoints for a person
384 image_size (tuple[int]): resizing (width, height) of input images
385 interpolation (str): Interpolation method to use when resizing the images.
386 aspect_ratio (bool): Whether or not to crop the images to the specified aspect ratio.
387 color_mode (str): Color mode to use for the images.
388 batch_size (int): Batch size to use for the datasets.
389 seed (int): Seed to use for shuffling the data.
392 Tuple[tf.data.Dataset, tf.data.Dataset, tf.data.Dataset]: Training, validation, and test datasets.
395 if training_path
and not validation_path:
400 nbr_keypoints=nbr_keypoints,
401 image_size=image_size,
402 interpolation=interpolation,
403 aspect_ratio=aspect_ratio,
404 color_mode=color_mode,
405 validation_split=validation_split,
406 batch_size=batch_size,
408 elif training_path
and validation_path:
411 nbr_keypoints=nbr_keypoints,
412 image_size=image_size,
413 interpolation=interpolation,
414 aspect_ratio=aspect_ratio,
415 color_mode=color_mode,
416 batch_size=batch_size,
422 nbr_keypoints=nbr_keypoints,
423 image_size=image_size,
424 interpolation=interpolation,
425 aspect_ratio=aspect_ratio,
426 color_mode=color_mode,
427 batch_size=batch_size,
429 elif not training_path
and validation_path:
433 nbr_keypoints=nbr_keypoints,
434 image_size=image_size,
435 interpolation=interpolation,
436 aspect_ratio=aspect_ratio,
437 color_mode=color_mode,
438 batch_size=batch_size,
444 if quantization_path:
447 nbr_keypoints=nbr_keypoints,
448 image_size=image_size,
449 interpolation=interpolation,
450 aspect_ratio=aspect_ratio,
451 color_mode=color_mode,
452 batch_size=batch_size,
455 quantization_ds =
None
460 nbr_keypoints=nbr_keypoints,
461 image_size=image_size,
462 interpolation=interpolation,
463 aspect_ratio=aspect_ratio,
464 color_mode=color_mode,
465 batch_size=batch_size,
470 return train_ds, val_ds, quantization_ds, test_ds
Tuple[tf.data.Dataset, tf.data.Dataset, tf.data.Dataset] load_dataset(str dataset_name=None, str training_path=None, str validation_path=None, str quantization_path=None, str test_path=None, float validation_split=None, int nbr_keypoints=None, tuple[int] image_size=None, str interpolation=None, str aspect_ratio=None, str color_mode=None, int batch_size=None, int seed=None)
def _get_padded_labels(data, r, R, height, width)
Tuple[tf.data.Dataset, tf.data.Dataset] _get_train_val_ds(str training_path, tuple[int] image_size=None, int nbr_keypoints=None, str interpolation=None, str aspect_ratio=None, str color_mode=None, float validation_split=None, int batch_size=None, int seed=None, bool shuffle=True, bool to_cache=False)
tf.data.Dataset _get_ds(str data_path=None, tuple[int] image_size=None, int nbr_keypoints=None, str interpolation=None, str aspect_ratio=None, str color_mode=None, int batch_size=None, int seed=None, bool shuffle=False, bool to_cache=False)
def _parse_labels(str label_path)
def _normalize_labels(label, int n, int l)
tf.data.Dataset _get_path_dataset(str path, int seed, bool shuffle=True)
tuple[tf.Tensor, tf.Tensor] _preprocess_function(tf.Tensor data_x, tf.Tensor data_y, tuple[int] image_size, str interpolation, str aspect_ratio, str color_mode, int nbr_keypoints)