network
BaseNetwork
¶
A base class for Network objects.
Networks are used to define the computation graph surrounding one or more models during training.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ops |
Iterable[Union[TensorOp, Scheduler[TensorOp]]]
|
The operators to be executed throughout training / testing / inference. These are likely to contain one or more model ops, as well as loss ops and update ops. |
required |
Source code in fastestimator\fastestimator\network.py
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 |
|
get_all_output_keys
¶
Get all of the keys that will be generated by the network during the given epoch
and mode
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
The execution mode to consider. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch number to consider when searching for outputs. |
required |
Returns:
Type | Description |
---|---|
Set[str]
|
The keys that will be generated by the network's Ops during the |
Source code in fastestimator\fastestimator\network.py
get_effective_input_keys
¶
Determine which keys need to be provided as input to the network during the given epoch
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
The execution mode to consider. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch number to consider for determining inputs. |
required |
Returns:
Type | Description |
---|---|
Set[str]
|
The necessary inputs for the network to execute the given |
Source code in fastestimator\fastestimator\network.py
get_loss_keys
¶
Find all of the keys associated with model losses.
Returns:
Type | Description |
---|---|
Set[str]
|
All of the keys associated with model losses in this network. |
Source code in fastestimator\fastestimator\network.py
get_scheduled_items
¶
Get a list of items considered for scheduling.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
Current execution mode. |
required |
Returns:
Type | Description |
---|---|
List[Any]
|
List of schedulable items in Network. |
Source code in fastestimator\fastestimator\network.py
load_epoch
¶
Prepare the network to run a given epoch and mode.
This method is necessary since schedulers and op mode restrictions may result in different computation graphs every epoch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
The mode to prepare to execute. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch to prepare to execute. |
required |
output_keys |
Optional[Set[str]]
|
What keys must be moved from the GPU back to the CPU after executing a step. |
None
|
warmup |
bool
|
Whether to prepare to execute it warmup mode or not (end users can likely ignore this argument). |
False
|
Source code in fastestimator\fastestimator\network.py
run_step
¶
Run a forward step through the Network on a batch of data.
Implementations of this method within derived classes should handle bringing the prediction data back from the (multi-)GPU environment to the CPU. This method expects that Network.load_epoch() has already been invoked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
Dict[str, Any]
|
The batch of data serving as input to the Network. |
required |
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Any], Dict[str, Any]]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
transform
¶
Run a forward step through the Network on an element of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
The element to data to use as input. |
required |
mode |
str
|
The mode in which to run the transform. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch in which to run the transform. |
1
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
TFNetwork
¶
Bases: BaseNetwork
An extension of BaseNetwork for TensorFlow models.
Source code in fastestimator\fastestimator\network.py
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 |
|
run_step
¶
Run a forward step through the Network on a batch of data.
Implementations of this method within derived classes should handle bringing the prediction data back from the (multi-)GPU environment to the CPU. This method expects that Network.load_epoch() has already been invoked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
Dict[str, Any]
|
The batch of data serving as input to the Network. |
required |
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Any], Dict[str, Any]]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
transform
¶
Run a forward step through the Network on an element of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
The element to data to use as input. |
required |
mode |
str
|
The mode in which to run the transform. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch in which to run the transform. |
1
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
TorchNetwork
¶
Bases: BaseNetwork
An extension of BaseNetwork for PyTorch models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ops |
Iterable[Union[TensorOp, Scheduler[TensorOp]]]
|
The ops defining the execution graph for this Network. |
required |
Source code in fastestimator\fastestimator\network.py
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 |
|
load_epoch
¶
Prepare the network to run a given epoch and mode.
This method is necessary since schedulers and op mode restrictions may result in different computation graphs every epoch. This also moves all of the necessary models from the CPU onto the GPU(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode |
str
|
The mode to prepare to execute. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch to prepare to execute. |
required |
output_keys |
Optional[Set[str]]
|
What keys must be moved from the GPU back to the CPU after executing a step. |
None
|
warmup |
bool
|
Whether to prepare to execute it warmup mode or not (end users can likely ignore this argument). |
False
|
Source code in fastestimator\fastestimator\network.py
run_step
¶
Run a forward step through the Network on a batch of data.
Implementations of this method within derived classes should handle bringing the prediction data back from the (multi-)GPU environment to the CPU. This method expects that Network.load_epoch() has already been invoked.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
Dict[str, Any]
|
The batch of data serving as input to the Network. |
required |
Returns:
Type | Description |
---|---|
Tuple[Dict[str, Any], Dict[str, Any]]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
transform
¶
Run a forward step through the Network on an element of data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Dict[str, Any]
|
The element to data to use as input. |
required |
mode |
str
|
The mode in which to run the transform. One of 'train', 'eval', 'test', or 'infer'. |
required |
epoch |
int
|
The epoch in which to run the transform. |
1
|
Returns:
Type | Description |
---|---|
Dict[str, Any]
|
(batch_data, prediction_data) |
Source code in fastestimator\fastestimator\network.py
unload_epoch
¶
Clean up the network after running an epoch.
In this case we move all of the models from the GPU(s) back to the CPU.
Source code in fastestimator\fastestimator\network.py
Network
¶
A function to automatically instantiate the correct Network derived class based on the given ops
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ops |
Iterable[Union[TensorOp, Scheduler[TensorOp]]]
|
A collection of Ops defining the graph for this Network. It should contain at least one ModelOp, and all models should be either TensorFlow or Pytorch. We currently do not support mixing TensorFlow and Pytorch models within the same network. |
required |
Returns:
Type | Description |
---|---|
BaseNetwork
|
A network instance containing the given |
Raises:
Type | Description |
---|---|
AssertionError
|
If TensorFlow and PyTorch models are mixed, or if no models are provided. |
ValueError
|
If a model is provided whose type cannot be identified as either TensorFlow or PyTorch. |
Source code in fastestimator\fastestimator\network.py
build
¶
Build model instances and associate them with optimizers.
This method can be used with TensorFlow models / optimizers:
model_def = fe.architecture.tensorflow.LeNet
model = fe.build(model_fn = model_def, optimizer_fn="adam")
model = fe.build(model_fn = model_def, optimizer_fn=lambda: tf.optimizers.Adam(lr=0.1))
model = fe.build(model_fn = model_def, optimizer_fn="adam", weights_path="~/weights.h5")
This method can be used with PyTorch models / optimizers:
model_def = fe.architecture.pytorch.LeNet
model = fe.build(model_fn = model_def, optimizer_fn="adam")
model = fe.build(model_fn = model_def, optimizer_fn=lambda x: torch.optim.Adam(params=x, lr=0.1))
model = fe.build(model_fn = model_def, optimizer_fn="adam", weights_path="~/weights.pt)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_fn |
Callable[[], Union[Model, List[Model]]]
|
A function that define model(s). |
required |
optimizer_fn |
Union[str, Scheduler, Callable, List[str], List[Callable], List[Scheduler], None]
|
Optimizer string/definition or a list of optimizer instances/strings. The number of optimizers
provided here should match the number of models generated by the |
required |
model_name |
Union[str, List[str], None]
|
Name(s) of the model(s) that will be used for logging purpose. If None, a name will be automatically generated and assigned. |
None
|
weights_path |
Union[str, None, List[Union[str, None]]]
|
Path(s) from which to load model weights. If not None, then the number of weight paths provided
should match the number of models generated by the |
None
|
Returns:
Name | Type | Description |
---|---|---|
models |
Union[Model, List[Model]]
|
The model(s) built by FastEstimator. |
Source code in fastestimator\fastestimator\network.py
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 688 689 690 691 692 693 694 |
|