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

Class TensorBoardImageHandler

monai/handlers/tensorboard_handlers.py:288–453  ·  view source on GitHub ↗

TensorBoardImageHandler is an Ignite Event handler that can visualize images, labels and outputs as 2D/3D images. 2D output (shape in Batch, channel, H, W) will be shown as simple image using the first element in the batch, for 3D to ND output (shape in Batch, channel, H, W, D) input, e

Source from the content-addressed store, hash-verified

286
287
288class TensorBoardImageHandler(TensorBoardHandler):
289 """
290 TensorBoardImageHandler is an Ignite Event handler that can visualize images, labels and outputs as 2D/3D images.
291 2D output (shape in Batch, channel, H, W) will be shown as simple image using the first element in the batch,
292 for 3D to ND output (shape in Batch, channel, H, W, D) input, each of ``self.max_channels`` number of images'
293 last three dimensions will be shown as animated GIF along the last axis (typically Depth).
294 And if writer is from TensorBoardX, data has 3 channels and `max_channels=3`, will plot as RGB video.
295
296 It can be used for any Ignite Engine (trainer, validator and evaluator).
297 User can easily add it to engine for any expected Event, for example: ``EPOCH_COMPLETED``,
298 ``ITERATION_COMPLETED``. The expected data source is ignite's ``engine.state.batch`` and ``engine.state.output``.
299
300 Default behavior:
301 - Show y_pred as images (GIF for 3D) on TensorBoard when Event triggered,
302 - Need to use ``batch_transform`` and ``output_transform`` to specify
303 how many images to show and show which channel.
304 - Expects ``batch_transform(engine.state.batch)`` to return data
305 format: (image[N, channel, ...], label[N, channel, ...]).
306 - Expects ``output_transform(engine.state.output)`` to return a torch
307 tensor in format (y_pred[N, channel, ...], loss).
308
309 Usage example is available in the tutorial:
310 https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/unet_segmentation_3d_ignite.ipynb.
311
312 """
313
314 def __init__(
315 self,
316 summary_writer: SummaryWriter | SummaryWriterX | None = None,
317 log_dir: str = "./runs",
318 interval: int = 1,
319 epoch_level: bool = True,
320 batch_transform: Callable = lambda x: x,
321 output_transform: Callable = lambda x: x,
322 global_iter_transform: Callable = lambda x: x,
323 index: int = 0,
324 max_channels: int = 1,
325 frame_dim: int = -3,
326 max_frames: int = 64,
327 ) -> None:
328 """
329 Args:
330 summary_writer: user can specify TensorBoard or TensorBoardX SummaryWriter,
331 default to create a new TensorBoard writer.
332 log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`.
333 interval: plot content from engine.state every N epochs or every N iterations, default is 1.
334 epoch_level: plot content from engine.state every N epochs or N iterations. `True` is epoch level,
335 `False` is iteration level.
336 batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`,
337 then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx,
338 "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`.
339 will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`.
340 `engine.state` and `batch_transform` inherit from the ignite concept:
341 https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial:
342 https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb.
343 output_transform: a callable that is used to extract the `predictions` data from
344 `ignite.engine.state.output`, will use the result to plot output from `result[index]`.
345 `engine.state` and `output_transform` inherit from the ignite concept:

Callers 2

run_training_testFunction · 0.90
test_tb_image_shapeMethod · 0.90

Calls

no outgoing calls

Tested by 2

run_training_testFunction · 0.72
test_tb_image_shapeMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…