MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / eval_mode

Function eval_mode

monai/networks/utils.py:458–489  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

456
457@contextmanager
458def 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

Callers 15

_validateMethod · 0.90
__call__Method · 0.90
run_training_testFunction · 0.90
run_inference_testFunction · 0.90
run_training_testFunction · 0.90
run_inference_testFunction · 0.90
test_shapeMethod · 0.90
test_eval_modeMethod · 0.90
test_shapeMethod · 0.90
test_ill_input_shapeMethod · 0.90

Calls 1

trainMethod · 0.45

Tested by 15

run_training_testFunction · 0.72
run_inference_testFunction · 0.72
run_training_testFunction · 0.72
run_inference_testFunction · 0.72
test_shapeMethod · 0.72
test_eval_modeMethod · 0.72
test_shapeMethod · 0.72
test_ill_input_shapeMethod · 0.72
test_kernels_stridesMethod · 0.72
test_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…