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

Class StatsHandler

monai/handlers/stats_handler.py:36–291  ·  view source on GitHub ↗

StatsHandler defines a set of Ignite Event-handlers for all the log printing logics. It can be used for any Ignite Engine(trainer, validator and evaluator). And it can support logging for epoch level and iteration level with pre-defined loggers. Note that if ``name`` is None, this

Source from the content-addressed store, hash-verified

34
35
36class StatsHandler:
37 """
38 StatsHandler defines a set of Ignite Event-handlers for all the log printing logics.
39 It can be used for any Ignite Engine(trainer, validator and evaluator).
40 And it can support logging for epoch level and iteration level with pre-defined loggers.
41
42 Note that if ``name`` is None, this class will leverage `engine.logger` as the logger, otherwise,
43 ``logging.getLogger(name)`` is used. In both cases, it's important to make sure that the logging level is at least
44 ``INFO``. To change the level of logging, please call ``import ignite; ignite.utils.setup_logger(name)``
45 (when ``name`` is not None) or ``engine.logger = ignite.utils.setup_logger(engine.logger.name, reset=True)``
46 (when ``name`` is None) before running the engine with this handler attached.
47
48 Default behaviors:
49 - When EPOCH_COMPLETED, logs ``engine.state.metrics`` using ``self.logger``.
50 - When ITERATION_COMPLETED, logs
51 ``self.output_transform(engine.state.output)`` using ``self.logger``.
52
53 Usage example::
54
55 import ignite
56 import monai
57
58 trainer = ignite.engine.Engine(lambda x, y: [0.0]) # an example trainer
59 monai.handlers.StatsHandler(name="train_stats").attach(trainer)
60
61 trainer.run(range(3), max_epochs=4)
62
63 More details of example is available in the tutorial:
64 https://github.com/Project-MONAI/tutorials/blob/master/modules/engines/unet_training_dict.py.
65
66 """
67
68 def __init__(
69 self,
70 iteration_log: bool | Callable[[Engine, int], bool] = True,
71 epoch_log: bool | Callable[[Engine, int], bool] = True,
72 epoch_print_logger: Callable[[Engine], Any] | None = None,
73 iteration_print_logger: Callable[[Engine], Any] | None = None,
74 output_transform: Callable = lambda x: x[0],
75 global_epoch_transform: Callable = lambda x: x,
76 state_attributes: Sequence[str] | None = None,
77 name: str | None = "monai.handlers.StatsHandler",
78 tag_name: str = DEFAULT_TAG,
79 key_var_format: str = DEFAULT_KEY_VAL_FORMAT,
80 ) -> None:
81 """
82
83 Args:
84 iteration_log: whether to log data when iteration completed, default to `True`. ``iteration_log`` can
85 be also a function and it will be interpreted as an event filter
86 (see https://pytorch.org/ignite/generated/ignite.engine.events.Events.html for details).
87 Event filter function accepts as input engine and event value (iteration) and should return True/False.
88 Event filtering can be helpful to customize iteration logging frequency.
89 epoch_log: whether to log data when epoch completed, default to `True`. ``epoch_log`` can be
90 also a function and it will be interpreted as an event filter. See ``iteration_log`` argument for more
91 details.
92 epoch_print_logger: customized callable printer for epoch level logging.
93 Must accept parameter "engine", use default printer if None.

Callers 12

run_training_testFunction · 0.90
run_training_testFunction · 0.90
run_inference_testFunction · 0.90
run_training_testFunction · 0.90
test_metrics_printMethod · 0.90
test_loss_printMethod · 0.90
test_loss_dictMethod · 0.90
test_loss_fileMethod · 0.90
test_exceptionMethod · 0.90
test_attributes_printMethod · 0.90
test_default_loggerMethod · 0.90
test_computeMethod · 0.90

Calls

no outgoing calls

Tested by 12

run_training_testFunction · 0.72
run_training_testFunction · 0.72
run_inference_testFunction · 0.72
run_training_testFunction · 0.72
test_metrics_printMethod · 0.72
test_loss_printMethod · 0.72
test_loss_dictMethod · 0.72
test_loss_fileMethod · 0.72
test_exceptionMethod · 0.72
test_attributes_printMethod · 0.72
test_default_loggerMethod · 0.72
test_computeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…