TFDataHandler
TFDataHandler
¶
Bases: DataHandler
Class to manage tf.data.Dataset. The aim is to provide a simple interface for working with tf.data.Datasets and manage them without having to use tensorflow syntax.
Source code in oodeel/datasets/tf_data_handler.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
|
assign_feature_value(dataset, feature_key, value)
staticmethod
¶
Assign a value to a feature for every sample in a tf.data.Dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to assign the value to |
required |
feature_key |
str
|
Feature to assign the value to |
required |
value |
int
|
Value to assign |
required |
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
|
dict_to_tuple(dataset, keys=None)
staticmethod
¶
Turn a dict based tf.data.Dataset to a tuple based tf.data.Dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dict based tf.data.Dataset |
required |
keys |
list
|
Features to use for the tuples based tf.data.Dataset. If None, takes all the features. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
filter_by_feature_value(dataset, feature_key, values, excluded=False)
staticmethod
¶
Filter a tf.data.Dataset by checking the value of a feature is in 'values'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to filter |
required |
feature_key |
str
|
Feature name to check the value |
required |
values |
list
|
Feature_key values to keep (if excluded is False) or to exclude |
required |
excluded |
bool
|
To keep (False) or exclude (True) the samples with Feature_key value included in Values. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: Filtered dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
get_dataset_length(dataset)
staticmethod
¶
Get the length of a dataset. Try to access it with len(), and if not available, with a reduce op.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to process |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
description |
Source code in oodeel/datasets/tf_data_handler.py
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
|
get_ds_feature_keys(dataset)
staticmethod
¶
Get the feature keys of a tf.data.Dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to get the feature keys from |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
list
|
List of feature keys |
Source code in oodeel/datasets/tf_data_handler.py
310 311 312 313 314 315 316 317 318 319 320 321 |
|
get_feature(dataset, feature_key)
staticmethod
¶
Extract a feature from a dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to extract the feature from |
required |
feature_key |
Union[str, int]
|
feature to extract |
required |
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: dataset built with the extracted feature only |
Source code in oodeel/datasets/tf_data_handler.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 |
|
get_feature_from_ds(dataset, feature_key)
staticmethod
¶
Get a feature from a tf.data.Dataset
Note
This function can be a bit time consuming since it needs to iterate over the whole dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to get the feature from |
required |
feature_key |
str
|
Feature value to get |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Feature values for dataset |
Source code in oodeel/datasets/tf_data_handler.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
|
get_feature_shape(dataset, feature_key)
staticmethod
¶
Get the shape of a feature of dataset identified by feature_key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
a tf.data.dataset |
required |
feature_key |
Union[str, int]
|
The identifier of the feature |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
tuple
|
the shape of feature_id |
Source code in oodeel/datasets/tf_data_handler.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 |
|
get_input_from_dataset_item(elem)
staticmethod
¶
Get the tensor that is to be feed as input to a model from a dataset element.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
elem |
ItemType
|
dataset element to extract input from |
required |
Returns:
Name | Type | Description |
---|---|---|
TensorType |
TensorType
|
Input tensor |
Source code in oodeel/datasets/tf_data_handler.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
|
get_item_length(dataset)
staticmethod
¶
Get the length of a dataset element. If an element is a tensor, the length is one and if it is a sequence (list or tuple), it is len(elem).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to process |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
length of the dataset elems |
Source code in oodeel/datasets/tf_data_handler.py
568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
|
get_label_from_dataset_item(item)
staticmethod
¶
Retrieve label tensor from item as a tuple/list. Label must be at index 1 in the item tuple. If one-hot encoded, labels are converted to single value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
elem |
ItemType
|
dataset element to extract label from |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Label tensor |
Source code in oodeel/datasets/tf_data_handler.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 |
|
has_feature_key(dataset, key)
staticmethod
¶
Check if a tf.data.Dataset has a feature denoted by key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to check |
required |
key |
str
|
Key to check |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
If the tf.data.Dataset has a feature denoted by key |
Source code in oodeel/datasets/tf_data_handler.py
323 324 325 326 327 328 329 330 331 332 333 334 335 |
|
load_custom_dataset(dataset_id, keys=None)
classmethod
¶
Load a custom Dataset by ensuring it has the correct format (dict-based)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_id |
Dataset
|
tf.data.Dataset |
required |
keys |
list
|
Features keys. If None, assigned as "input_i" for i-th feature. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
load_dataset(dataset_id, keys=None, load_kwargs={})
classmethod
¶
Load dataset from different manners, ensuring to return a dict based tf.data.Dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_id |
Any
|
dataset identification |
required |
keys |
list
|
Features keys. If None, assigned as "input_i" for i-th feature. Defaults to None. |
None
|
load_kwargs |
dict
|
Additional args for loading from tensorflow_datasets. Defaults to {}. |
{}
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: A dict based tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
load_dataset_from_arrays(dataset_id, keys=None)
staticmethod
¶
Load a tf.data.Dataset from a np.ndarray, a tf.Tensor or a tuple/dict of np.ndarrays/td.Tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_id |
ItemType
|
numpy array(s) to load. |
required |
keys |
list
|
Features keys. If None, assigned as "input_i" for i-th feature. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
load_from_tensorflow_datasets(dataset_id, load_kwargs={})
staticmethod
¶
Load a tf.data.Dataset from the tensorflow_datasets catalog
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_id |
str
|
Identifier of the dataset |
required |
load_kwargs |
dict
|
Loading kwargs to add to tfds.load(). Defaults to {}. |
{}
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
make_channel_first(input_key, dataset)
staticmethod
¶
Make a tf.data.Dataset channel first. Make sure that the dataset is not already Channel first. If so, the tensor will have the format (batch_size, x_size, channel, y_size).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_key |
str
|
input key of the dict-based tf.data.Dataset |
required |
dataset |
Dataset
|
tf.data.Dataset to make channel first |
required |
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: Channel first dataset |
Source code in oodeel/datasets/tf_data_handler.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 |
|
map_ds(dataset, map_fn, num_parallel_calls=None)
staticmethod
¶
Map a function to a tf.data.Dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to map the function to |
required |
map_fn |
Callable
|
Function to map |
required |
num_parallel_calls |
Optional[int]
|
Number of parallel processes to use. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: Maped dataset |
Source code in oodeel/datasets/tf_data_handler.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
merge(id_dataset, ood_dataset, resize=False, shape=None, channel_order='channels_last')
classmethod
¶
Merge two tf.data.Datasets
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_dataset |
Dataset
|
dataset of in-distribution data |
required |
ood_dataset |
Dataset
|
dataset of out-of-distribution data |
required |
resize |
Optional[bool]
|
toggles if input tensors of the datasets have to be resized to have the same shape. Defaults to True. |
False
|
shape |
Optional[Tuple[int]]
|
shape to use for resizing input tensors. If None, the tensors are resized with the shape of the id_dataset input tensors. Defaults to None. |
None
|
channel_order |
Optional[str]
|
channel order of the input |
'channels_last'
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: merged dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
prepare_for_training(dataset, batch_size, shuffle=False, preprocess_fn=None, augment_fn=None, output_keys=None, dict_based_fns=False, shuffle_buffer_size=None, prefetch_buffer_size=None, drop_remainder=False)
classmethod
¶
Prepare a tf.data.Dataset for training
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
tf.data.Dataset to prepare |
required |
batch_size |
int
|
Batch size |
required |
shuffle |
bool
|
To shuffle the returned dataset or not. Defaults to False. |
False
|
preprocess_fn |
Callable
|
Preprocessing function to apply to the dataset. Defaults to None. |
None
|
augment_fn |
Callable
|
Augment function to be used (when the returned dataset is to be used for training). Defaults to None. |
None
|
output_keys |
list
|
List of keys corresponding to the features that will be returned. Keep all features if None. Defaults to None. |
None
|
dict_based_fns |
bool
|
If the augment and preprocess functions are dict based or not. Defaults to False. |
False
|
shuffle_buffer_size |
int
|
Size of the shuffle buffer. If None, taken as the number of samples in the dataset. Defaults to None. |
None
|
prefetch_buffer_size |
Optional[int]
|
Buffer size for prefetch. If None, automatically chose using tf.data.experimental.AUTOTUNE. Defaults to None. |
None
|
drop_remainder |
Optional[bool]
|
To drop the last batch when its size is lower than batch_size. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset: Prepared dataset |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|
tuple_to_dict(dataset, keys)
staticmethod
¶
Turn a tuple based tf.data.Dataset to a dict based tf.data.Dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Tuple based tf.data.Dataset |
required |
keys |
list
|
Keys to use for the dict based tf.data.Dataset |
required |
Returns:
Type | Description |
---|---|
Dataset
|
tf.data.Dataset |
Source code in oodeel/datasets/tf_data_handler.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
dict_only_ds(ds_handling_method)
¶
Decorator to ensure that the dataset is a dict dataset and that the input key matches one of the feature keys. The signature of decorated functions must be function(dataset, args, *kwargs) with feature_key either in kwargs or args[0] when relevant.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds_handling_method |
Callable
|
method to decorate |
required |
Returns:
Type | Description |
---|---|
Callable
|
decorated method |
Source code in oodeel/datasets/tf_data_handler.py
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 |
|