From cd2f42b5fd16b1ba2e6f2fdea1f70443deb1fbd2 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 2 Dec 2021 10:09:37 +0800 Subject: [PATCH 1/4] [DLMED] extract write logic Signed-off-by: Nic Ma --- monai/handlers/tensorboard_handlers.py | 35 +++++++++++++++++++++----- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 11b487ec99..1ebcf8c88e 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -173,6 +173,21 @@ def iteration_completed(self, engine: Engine) -> None: else: self._default_iteration_writer(engine, self._writer) + def _write_scalar(self, engine: Engine, writer: SummaryWriter, tag: str, value: Any, step: int) -> None: + """ + Write scale values into TensorBoard. + Default to call `SummaryWriter.add_scalar()`, subclass can override it for more complicated logic. + + Args: + engine: Ignite Engine, it can be a trainer, validator or evaluator. + writer: TensorBoard or TensorBoardX writer, passed or created in TensorBoardHandler. + tag: tag name in the TensorBoard. + value: value of the scalar data for current step. + step: index of current step. + + """ + writer.add_scalar(tag, value, step) + def _default_epoch_writer(self, engine: Engine, writer: SummaryWriter) -> None: """ Execute epoch level event write operation. @@ -188,11 +203,11 @@ def _default_epoch_writer(self, engine: Engine, writer: SummaryWriter) -> None: summary_dict = engine.state.metrics for name, value in summary_dict.items(): if is_scalar(value): - writer.add_scalar(name, value, current_epoch) + self._write_scalar(engine, writer, name, value, current_epoch) if self.state_attributes is not None: for attr in self.state_attributes: - writer.add_scalar(attr, getattr(engine.state, attr, None), current_epoch) + self._write_scalar(engine, writer, attr, getattr(engine.state, attr, None), current_epoch) writer.flush() def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> None: @@ -221,12 +236,20 @@ def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> No " {}:{}".format(name, type(value)) ) continue # not plot multi dimensional output - writer.add_scalar( - name, value.item() if isinstance(value, torch.Tensor) else value, engine.state.iteration + self._write_scalar( + engine=engine, + writer=writer, + tag=name, + value=value.item() if isinstance(value, torch.Tensor) else value, + step=engine.state.iteration, ) elif is_scalar(loss): # not printing multi dimensional output - writer.add_scalar( - self.tag_name, loss.item() if isinstance(loss, torch.Tensor) else loss, engine.state.iteration + self._write_scalar( + engine=engine, + writer=writer, + tag=self.tag_name, + value=loss.item() if isinstance(loss, torch.Tensor) else loss, + step=engine.state.iteration, ) else: warnings.warn( From 24dcc26045c118232d3174c449ad0f8f61c29eef Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 2 Dec 2021 10:19:40 +0800 Subject: [PATCH 2/4] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/handlers/tensorboard_handlers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 1ebcf8c88e..8f38e7093b 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -175,7 +175,7 @@ def iteration_completed(self, engine: Engine) -> None: def _write_scalar(self, engine: Engine, writer: SummaryWriter, tag: str, value: Any, step: int) -> None: """ - Write scale values into TensorBoard. + Write scale value into TensorBoard. Default to call `SummaryWriter.add_scalar()`, subclass can override it for more complicated logic. Args: From d27d6e8b270c725e3fd72c24e9838b38db5de4bb Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 2 Dec 2021 21:09:09 +0800 Subject: [PATCH 3/4] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/handlers/classification_saver.py | 4 ++-- monai/handlers/metrics_saver.py | 2 +- monai/handlers/stats_handler.py | 4 ++-- monai/handlers/tensorboard_handlers.py | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/monai/handlers/classification_saver.py b/monai/handlers/classification_saver.py index 4481ae0fec..1a5fd1f545 100644 --- a/monai/handlers/classification_saver.py +++ b/monai/handlers/classification_saver.py @@ -120,12 +120,12 @@ def __call__(self, engine: Engine) -> None: o = o.detach() self._outputs.append(o) - def _finalize(self, engine: Engine) -> None: + def _finalize(self, _engine: Engine) -> None: """ All gather classification results from ranks and save to CSV file. Args: - engine: Ignite Engine, it can be a trainer, validator or evaluator. + _engine: Ignite Engine, it can be a trainer, validator or evaluator. """ ws = idist.get_world_size() if self.save_rank >= ws: diff --git a/monai/handlers/metrics_saver.py b/monai/handlers/metrics_saver.py index d6aa0c7b9f..282f81d678 100644 --- a/monai/handlers/metrics_saver.py +++ b/monai/handlers/metrics_saver.py @@ -105,7 +105,7 @@ def attach(self, engine: Engine) -> None: engine.add_event_handler(Events.ITERATION_COMPLETED, self._get_filenames) engine.add_event_handler(Events.EPOCH_COMPLETED, self) - def _started(self, engine: Engine) -> None: + def _started(self, _engine: Engine) -> None: self._filenames = [] def _get_filenames(self, engine: Engine) -> None: diff --git a/monai/handlers/stats_handler.py b/monai/handlers/stats_handler.py index 9251453b50..23cc3a8caf 100644 --- a/monai/handlers/stats_handler.py +++ b/monai/handlers/stats_handler.py @@ -143,14 +143,14 @@ def iteration_completed(self, engine: Engine) -> None: else: self._default_iteration_print(engine) - def exception_raised(self, engine: Engine, e: Exception) -> None: + def exception_raised(self, _engine: Engine, e: Exception) -> None: """ Handler for train or validation/evaluation exception raised Event. Print the exception information and traceback. This callback may be skipped because the logic with Ignite can only trigger the first attached handler for `EXCEPTION_RAISED` event. Args: - engine: Ignite Engine, it can be a trainer, validator or evaluator. + _engine: Ignite Engine, it can be a trainer, validator or evaluator. e: the exception caught in Ignite during engine.run(). """ diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index 8f38e7093b..bf9d54ef69 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -173,13 +173,13 @@ def iteration_completed(self, engine: Engine) -> None: else: self._default_iteration_writer(engine, self._writer) - def _write_scalar(self, engine: Engine, writer: SummaryWriter, tag: str, value: Any, step: int) -> None: + def _write_scalar(self, _engine: Engine, writer: SummaryWriter, tag: str, value: Any, step: int) -> None: """ Write scale value into TensorBoard. - Default to call `SummaryWriter.add_scalar()`, subclass can override it for more complicated logic. + Default to call `SummaryWriter.add_scalar()`. Args: - engine: Ignite Engine, it can be a trainer, validator or evaluator. + _engine: Ignite Engine, it can be a trainer, validator or evaluator, unused in the default logic. writer: TensorBoard or TensorBoardX writer, passed or created in TensorBoardHandler. tag: tag name in the TensorBoard. value: value of the scalar data for current step. From 7c944e4ca8ebecef3562550b12c015ef706dda63 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Fri, 3 Dec 2021 10:14:35 +0800 Subject: [PATCH 4/4] [DLMED] update according to comments Signed-off-by: Nic Ma --- monai/handlers/classification_saver.py | 11 +++++++++-- monai/handlers/metrics_saver.py | 7 +++++++ monai/handlers/stats_handler.py | 2 +- monai/handlers/tensorboard_handlers.py | 6 +++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/monai/handlers/classification_saver.py b/monai/handlers/classification_saver.py index 1a5fd1f545..7f96591503 100644 --- a/monai/handlers/classification_saver.py +++ b/monai/handlers/classification_saver.py @@ -98,7 +98,14 @@ def attach(self, engine: Engine) -> None: if not engine.has_event_handler(self._finalize, Events.EPOCH_COMPLETED): engine.add_event_handler(Events.EPOCH_COMPLETED, self._finalize) - def _started(self, engine: Engine) -> None: + def _started(self, _engine: Engine) -> None: + """ + Initialize internal buffers. + + Args: + _engine: Ignite Engine, unused argument. + + """ self._outputs = [] self._filenames = [] @@ -125,7 +132,7 @@ def _finalize(self, _engine: Engine) -> None: All gather classification results from ranks and save to CSV file. Args: - _engine: Ignite Engine, it can be a trainer, validator or evaluator. + _engine: Ignite Engine, unused argument. """ ws = idist.get_world_size() if self.save_rank >= ws: diff --git a/monai/handlers/metrics_saver.py b/monai/handlers/metrics_saver.py index 282f81d678..d4acc4abdb 100644 --- a/monai/handlers/metrics_saver.py +++ b/monai/handlers/metrics_saver.py @@ -106,6 +106,13 @@ def attach(self, engine: Engine) -> None: engine.add_event_handler(Events.EPOCH_COMPLETED, self) def _started(self, _engine: Engine) -> None: + """ + Initialize internal buffers. + + Args: + _engine: Ignite Engine, unused argument. + + """ self._filenames = [] def _get_filenames(self, engine: Engine) -> None: diff --git a/monai/handlers/stats_handler.py b/monai/handlers/stats_handler.py index 23cc3a8caf..c5b749e6a2 100644 --- a/monai/handlers/stats_handler.py +++ b/monai/handlers/stats_handler.py @@ -150,7 +150,7 @@ def exception_raised(self, _engine: Engine, e: Exception) -> None: with Ignite can only trigger the first attached handler for `EXCEPTION_RAISED` event. Args: - _engine: Ignite Engine, it can be a trainer, validator or evaluator. + _engine: Ignite Engine, unused argument. e: the exception caught in Ignite during engine.run(). """ diff --git a/monai/handlers/tensorboard_handlers.py b/monai/handlers/tensorboard_handlers.py index bf9d54ef69..70196c5fea 100644 --- a/monai/handlers/tensorboard_handlers.py +++ b/monai/handlers/tensorboard_handlers.py @@ -179,7 +179,7 @@ def _write_scalar(self, _engine: Engine, writer: SummaryWriter, tag: str, value: Default to call `SummaryWriter.add_scalar()`. Args: - _engine: Ignite Engine, it can be a trainer, validator or evaluator, unused in the default logic. + _engine: Ignite Engine, unused argument. writer: TensorBoard or TensorBoardX writer, passed or created in TensorBoardHandler. tag: tag name in the TensorBoard. value: value of the scalar data for current step. @@ -237,7 +237,7 @@ def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> No ) continue # not plot multi dimensional output self._write_scalar( - engine=engine, + _engine=engine, writer=writer, tag=name, value=value.item() if isinstance(value, torch.Tensor) else value, @@ -245,7 +245,7 @@ def _default_iteration_writer(self, engine: Engine, writer: SummaryWriter) -> No ) elif is_scalar(loss): # not printing multi dimensional output self._write_scalar( - engine=engine, + _engine=engine, writer=writer, tag=self.tag_name, value=loss.item() if isinstance(loss, torch.Tensor) else loss,