Set network(s) to eval mode and then return to original state at the end. Args: nets: Input network(s) Examples .. code-block:: python t=torch.rand(1,1,16,16) p=torch.nn.Conv2d(1,1,3) print(p.training) # True with eval_mode(p):
(*nets: nn.Module)
| 456 | |
| 457 | @contextmanager |
| 458 | def eval_mode(*nets: nn.Module): |
| 459 | """ |
| 460 | Set network(s) to eval mode and then return to original state at the end. |
| 461 | |
| 462 | Args: |
| 463 | nets: Input network(s) |
| 464 | |
| 465 | Examples |
| 466 | |
| 467 | .. code-block:: python |
| 468 | |
| 469 | t=torch.rand(1,1,16,16) |
| 470 | p=torch.nn.Conv2d(1,1,3) |
| 471 | print(p.training) # True |
| 472 | with eval_mode(p): |
| 473 | print(p.training) # False |
| 474 | print(p(t).sum().backward()) # will correctly raise an exception as gradients are calculated |
| 475 | """ |
| 476 | |
| 477 | # Get original state of network(s). |
| 478 | # Check the training attribute in case it's TensorRT based models which don't have this attribute. |
| 479 | training = [n for n in nets if hasattr(n, "training") and n.training] |
| 480 | |
| 481 | try: |
| 482 | # set to eval mode |
| 483 | with torch.no_grad(): |
| 484 | yield [n.eval() if hasattr(n, "eval") else n for n in nets] |
| 485 | finally: |
| 486 | # Return required networks to training |
| 487 | for n in training: |
| 488 | if hasattr(n, "train"): |
| 489 | n.train() |
| 490 | |
| 491 | |
| 492 | @contextmanager |
searching dependent graphs…