dataset
Contains stuff to help working with TensorFlow and geospatial data in the OTBTF framework.
Buffer
¶
Used to store and access list of objects
Source code in otbtf/dataset.py
Dataset
¶
Handles the "mining" of patches. This class has a thread that extract tuples from the readers, while ensuring the access of already gathered tuples.
See PatchesReaderBase
and Buffer
Source code in otbtf/dataset.py
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 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
|
__init__(patches_reader=None, buffer_length=128, iterator_cls=RandomIterator, max_nb_of_samples=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patches_reader |
PatchesReaderBase
|
The patches reader instance |
None
|
buffer_length |
int
|
The number of samples that are stored in the buffer |
128
|
iterator_cls |
Type[IteratorBase]
|
The iterator class used to generate the sequence of patches indices. |
RandomIterator
|
max_nb_of_samples |
int
|
Optional, max number of samples to consider |
None
|
Source code in otbtf/dataset.py
get_stats()
¶
Compute dataset statistics
Return
the dataset statistics, computed by the patches reader
get_tf_dataset(batch_size, drop_remainder=True, preprocessing_fn=None, targets_keys=None)
¶
Returns a TF dataset, ready to be used with the provided batch size
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_size |
int
|
the batch size |
required |
drop_remainder |
bool
|
drop incomplete batches |
True
|
preprocessing_fn |
Callable
|
An optional preprocessing function that takes
input examples as args and returns the preprocessed input
examples. Typically, examples are composed of model inputs and
targets. Model inputs and model targets must be computed
accordingly to (1) what the model outputs and (2) what
training loss needs. For instance, for a classification
problem, the model will likely output the softmax, or
activation neurons, for each class, and the cross entropy loss
requires labels in one hot encoding. In this case, the
preprocessing_fn has to transform the labels values (integer
ranging from [0, n_classes]) in one hot encoding (vector of 0
and 1 of length n_classes). The preprocessing_fn should not
implement such things as radiometric transformations from
input to input_preprocessed, because those are performed
inside the model itself (see
|
None
|
targets_keys |
List[str]
|
Optional. When provided, the dataset returns a tuple of dicts (inputs_dict, target_dict) so it can be straightforwardly used with keras models objects. |
None
|
Returns:
Type | Description |
---|---|
Dataset
|
The TF dataset |
Source code in otbtf/dataset.py
get_total_wait_in_seconds()
¶
Returns the number of seconds during which the data gathering was delayed because I/O bottleneck
Returns:
Type | Description |
---|---|
int
|
duration in seconds |
read_one_sample()
¶
Read one element of the consumer_buffer The lock is used to prevent different threads to read and update the internal counter concurrently
Return
one sample
Source code in otbtf/dataset.py
to_tfrecords(output_dir, n_samples_per_shard=100, drop_remainder=True)
¶
Save the dataset into TFRecord files
Parameters:
Name | Type | Description | Default |
---|---|---|---|
output_dir |
str
|
output directory |
required |
n_samples_per_shard |
int
|
number of samples per TFRecord file |
100
|
drop_remainder |
bool
|
drop remaining samples |
True
|
Source code in otbtf/dataset.py
DatasetFromPatchesImages
¶
Bases: Dataset
Handles the "mining" of a set of patches images.
:see PatchesImagesReader :see Dataset
Source code in otbtf/dataset.py
__init__(filenames_dict, use_streaming=False, buffer_length=128, iterator_cls=RandomIterator)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filenames_dict |
Dict[str, List[str]]
|
A dict structured as follow: |
required |
use_streaming: if True, the patches are read on the fly from the disc, nothing is kept in memory. buffer_length: The number of samples that are stored in the buffer (used when "use_streaming" is True). iterator_cls: The iterator class used to generate the sequence of patches indices.
Source code in otbtf/dataset.py
IteratorBase
¶
PatchesImagesReader
¶
Bases: PatchesReaderBase
This class provides a read access to a set of patches images.
A patches image is an image of patches stacked in rows, as produced from
the OTBTF "PatchesExtraction" application, and is stored in a raster
format (e.g. GeoTiff).
A source can be a particular domain in which the patches are extracted
(remember that in OTBTF applications, the number of sources is controlled
by the OTB_TF_NSOURCES
environment variable).
This class enables to use
- multiple sources
- multiple patches images per source
Each patch can be independently accessed using the get_sample(index) function, with index in [0, self.size), self.size being the total number of patches (must be the same for each sources).
See PatchesReaderBase
.
Source code in otbtf/dataset.py
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 |
|
__init__(filenames_dict, use_streaming=False, scalar_dict=None)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filenames_dict |
Dict[str, List[str]]
|
A dict structured as follow: { src_name1: [ src1_patches_image_1.tif, ..., src1_patches_image_N.tif ], src_name2: [ src2_patches_image_1.tif, ..., src2_patches_image_N.tif ], ... src_nameM: [ srcM_patches_image_1.tif, ..., srcM_patches_image_N.tif ] } |
required |
use_streaming |
bool
|
if True, the patches are read on the fly from the disc, nothing is kept in memory. Else, everything is packed in memory. |
False
|
scalar_dict |
Dict[str, List[Any]]
|
(optional) a dict containing list of scalars (int, |
None
|
float, |
str) as follow
|
{ scalar_name1: ["value_1", ..., "value_N"], scalar_name2: [value_1, ..., value_N], ... scalar_nameM: [value1, ..., valueN] } |
required |
Source code in otbtf/dataset.py
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 |
|
get_sample(index)
¶
Return one sample of the dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
the sample index. Must be in the [0, self.size) range. |
required |
Returns:
Type | Description |
---|---|
Dict[str, array]
|
The sample is stored in a dict with the following structure: { "src_key_0": np.array((psz_y_0, psz_x_0, nb_ch_0)), "src_key_1": np.array((psz_y_1, psz_x_1, nb_ch_1)), ... "src_key_M": np.array((psz_y_M, psz_x_M, nb_ch_M)) } |
Source code in otbtf/dataset.py
get_size()
¶
get_stats()
¶
Compute some statistics for each source. When streaming is used, chunk-by-chunk. Else, the statistics are computed directly in memory.
Returns:
Type | Description |
---|---|
Dict[str, List[float]]
|
statistics dict |
Source code in otbtf/dataset.py
PatchesReaderBase
¶
Bases: ABC
Base class for patches delivery
Source code in otbtf/dataset.py
get_sample(index)
abstractmethod
¶
Return one sample.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
index |
int
|
sample index |
required |
Returns:
Type | Description |
---|---|
Any
|
One sample instance, whatever the sample structure is (dict, numpy array, ...) |
get_size()
abstractmethod
¶
get_stats()
abstractmethod
¶
Compute some statistics for each source. Depending on if streaming is used, the statistics are computed directly in memory, or chunk-by-chunk.
Returns:
Type | Description |
---|---|
dict
|
a dict having the following structure: { "src_key_0": {"min": np.array([...]), "max": np.array([...]), "mean": np.array([...]), "std": np.array([...])}, ..., "src_key_M": {"min": np.array([...]), "max": np.array([...]), "mean": np.array([...]), "std": np.array([...])}, } |
Source code in otbtf/dataset.py
RandomIterator
¶
Bases: IteratorBase
Pick a random number in the [0, handler.size) range.
Source code in otbtf/dataset.py
__init__(patches_reader)
¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patches_reader |
PatchesReaderBase
|
patches reader |
required |