o  i?X@sddlmZddlZddlmZmZddlmZmZddl Z ddl Z ddl m Z mZmZmZddlmZede jed\ZZerSdd lmZdd lmZdd lmZnede jed d d \ZZeddd\ZZeddd\ZZdZGdddZGdddeZGdddeZdS)) annotationsN)CallableSequence) TYPE_CHECKINGAny) IgniteInfo is_scalar min_versionoptional_import)plot_2d_or_3d_imagez ignite.engineEvents)Engine) SummaryWriterr decorator)as_typeztorch.utils.tensorboardr)name tensorboardXLossc@s.eZdZdZdddd ZdddZddZdS)TensorBoardHandleraA Base class for the handlers to write data into TensorBoard. Args: summary_writer: user can specify TensorBoard or TensorBoardX SummaryWriter, default to create a new TensorBoard writer. log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`. N./runssummary_writer%SummaryWriter | SummaryWriterX | Nonelog_dirstrcCs.|durt|d|_d|_dS||_d|_dS)N)rTF)r_writerinternal_writer)selfrrre/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/tensorboard_handlers.py__init__3s    zTensorBoardHandler.__init__enginer returnNonecCstd|jjd)Nz Subclass z must implement this method.)NotImplementedError __class____name__rr rrrattach;szTensorBoardHandler.attachcCs|jr |jdSdS)zS Close the summary writer if created in this TensorBoard handler. N)rrclose)rrrrr(>szTensorBoardHandler.close)Nr)rrrrr r r!r")r% __module__ __qualname____doc__rr'r(rrrrr(s   rc s|eZdZdZdddddddddddef d2fdd Zd3ddZd3d d!Zd3d"d#Zd4d,d-Z d5d.d/Z d5d0d1Z Z S)6TensorBoardStatsHandlera, TensorBoardStatsHandler defines a set of Ignite Event-handlers for all the TensorBoard logics. It can be used for any Ignite Engine(trainer, validator and evaluator). And it can support both epoch level and iteration level with pre-defined TensorBoard event writer. The expected data source is Ignite ``engine.state.output`` and ``engine.state.metrics``. Default behaviors: - When EPOCH_COMPLETED, write each dictionary item in ``engine.state.metrics`` to TensorBoard. - When ITERATION_COMPLETED, write each dictionary item in ``self.output_transform(engine.state.output)`` to TensorBoard. Usage example is available in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/unet_segmentation_3d_ignite.ipynb. NrTcCs|dS)Nrrxrrrasz TensorBoardStatsHandler.cC|SNrr.rrrr0brrrr iteration_log*bool | Callable[[Engine, int], bool] | int epoch_logepoch_event_writer#Callable[[Engine, Any], Any] | Noneiteration_event_writeroutput_transformrglobal_epoch_transformstate_attributesSequence[str] | Nonetag_namer!r"c sDtj||d||_||_||_||_||_||_| |_| |_ dS)a Args: summary_writer: user can specify TensorBoard or TensorBoardX SummaryWriter, default to create a new TensorBoard writer. log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`. iteration_log: whether to write data to TensorBoard when iteration completed, default to `True`. ``iteration_log`` can be also a function or int. If it is an int, it will be interpreted as the iteration interval at which the iteration_event_writer is called. If it is a function, it will be interpreted as an event filter (see https://pytorch.org/ignite/generated/ignite.engine.events.Events.html for details). Event filter function accepts as input engine and event value (iteration) and should return True/False. epoch_log: whether to write data to TensorBoard when epoch completed, default to `True`. ``epoch_log`` can be also a function or int. If it is an int, it will be interpreted as the epoch interval at which the epoch_event_writer is called. If it is a function, it will be interpreted as an event filter. See ``iteration_log`` argument for more details. epoch_event_writer: customized callable TensorBoard writer for epoch level. Must accept parameter "engine" and "summary_writer", use default event writer if None. iteration_event_writer: customized callable TensorBoard writer for iteration level. Must accept parameter "engine" and "summary_writer", use default event writer if None. output_transform: a callable that is used to transform the ``ignite.engine.state.output`` into a scalar to plot, or a dictionary of {key: scalar}. In the latter case, the output string will be formatted as key: value. By default this value plotting happens when every iteration completed. The default behavior is to print loss from output[0] as output is a decollated list and we replicated loss value for every item of the decollated list. `engine.state` and `output_transform` inherit from the ignite concept: https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_epoch_transform: a callable that is used to customize global epoch number. For example, in evaluation, the evaluator engine might want to use trainer engines epoch number when plotting epoch vs metric curves. state_attributes: expected attributes from `engine.state`, if provided, will extract them when epoch completed. tag_name: when iteration output is a scalar, tag_name is used to plot, defaults to ``'Loss'``. rrN) superrr4r6r7r9r:r;r<r>) rrrr4r6r7r9r:r;r<r>r$rrrYs0 z TensorBoardStatsHandler.__init__r r cCs|jr,||jtjs,tj}t|jr||jd}n |jdkr%||jd}|||j|jrZ||jtj s\tj }t|jrF||jd}n |jdkrQ||jd}|||jdSdSdS)z Register a set of Ignite Event-Handlers to a specified Ignite engine. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. )Z event_filtereveryN) r4has_event_handleriteration_completedr ITERATION_COMPLETEDcallableadd_event_handlerr6epoch_completedEPOCH_COMPLETED)rr eventrrrr's      zTensorBoardStatsHandler.attachcC.|jdur|||jdS|||jdS)a Handler for train or validation/evaluation epoch completed Event. Write epoch level events, default values are from Ignite `engine.state.metrics` dict. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)r7r_default_epoch_writerr&rrrrJ z'TensorBoardStatsHandler.epoch_completedcCrM)a Handler for train or validation/evaluation iteration completed Event. Write iteration level events, default values are from Ignite `engine.state.output`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)r9r_default_iteration_writerr&rrrrFrOz+TensorBoardStatsHandler.iteration_completed_enginewriterSummaryWriter | SummaryWriterXtagvaluerstepintcCs||||dS)a Write scale value into TensorBoard. Default to call `SummaryWriter.add_scalar()`. Args: _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. step: index of current step. N) add_scalar)rrQrRrTrUrVrrr _write_scalarsz%TensorBoardStatsHandler._write_scalarc Cs||jj}|jj}|D]\}}t|r ||||||q|jdur:|jD]}||||t|j|d|q)| dS)a Execute epoch level event write operation. Default to write the values from Ignite `engine.state.metrics` dict and write the values of specified attributes of `engine.state`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. writer: TensorBoard or TensorBoardX writer, passed or created in TensorBoardHandler. N) r;stateepochmetricsitemsrrYr<getattrflush)rr rR current_epochZ summary_dictrrUattrrrrrNs    z-TensorBoardStatsHandler._default_epoch_writercCs||jj}|dur dSt|trDt|D],}||}t|s,td |t |q|j |||t|t j r;|n||jjdqn&t|r`|j |||jt|t j rX|n||jjdn td t ||dS)a@ Execute iteration level event write operation based on Ignite `engine.state.output` data. Extract the values from `self.output_transform(engine.state.output)`. Since `engine.state.output` is a decollated list and we replicated the loss value for every item of the decollated list, the default behavior is to track the loss from `output[0]`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. writer: TensorBoard or TensorBoardX writer, passed or created in TensorBoardHandler. Nzignoring non-scalar output in TensorBoardStatsHandler, make sure `output_transform(engine.state.output)` returns a scalar or dictionary of key and scalar pairs to avoid this warning. {}:{})rQrRrTrUrVzignoring non-scalar output in TensorBoardStatsHandler, make sure `output_transform(engine.state.output)` returns a scalar or a dictionary of key and scalar pairs to avoid this warning. {})r:rZoutput isinstancedictsortedrwarningswarnformattyperYtorchTensoritem iterationr>r_)rr rRlossrrUrrrrPsB      z1TensorBoardStatsHandler._default_iteration_writer)rrrrr4r5r6r5r7r8r9r8r:rr;rr<r=r>rr!r"r)) rQr rRrSrTrrUrrVrWr!r")r r rRrSr!r") r%r*r+r, DEFAULT_TAGrr'rJrFrYrNrP __classcell__rrrArr-Gs& :    r-c sZeZdZdZddddddddd dd dd d f d'fdd Zd(d#d$Zd(d%d&ZZS))TensorBoardImageHandlera 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, each of ``self.max_channels`` number of images' last three dimensions will be shown as animated GIF along the last axis (typically Depth). And if writer is from TensorBoardX, data has 3 channels and `max_channels=3`, will plot as RGB video. It can be used for any Ignite Engine (trainer, validator and evaluator). User can easily add it to engine for any expected Event, for example: ``EPOCH_COMPLETED``, ``ITERATION_COMPLETED``. The expected data source is ignite's ``engine.state.batch`` and ``engine.state.output``. Default behavior: - Show y_pred as images (GIF for 3D) on TensorBoard when Event triggered, - Need to use ``batch_transform`` and ``output_transform`` to specify how many images to show and show which channel. - Expects ``batch_transform(engine.state.batch)`` to return data format: (image[N, channel, ...], label[N, channel, ...]). - Expects ``output_transform(engine.state.output)`` to return a torch tensor in format (y_pred[N, channel, ...], loss). Usage example is available in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/unet_segmentation_3d_ignite.ipynb. NrrBTcCr1r2rr.rrrr0@r3z TensorBoardImageHandler.cCr1r2rr.rrrr0Ar3cCr1r2rr.rrrr0Br3r@rrrrintervalrW epoch_levelboolbatch_transformrr:global_iter_transformindex max_channels frame_dim max_framesr!r"c sJtj||d||_||_||_||_||_||_| |_| |_ | |_ dS)a Args: summary_writer: user can specify TensorBoard or TensorBoardX SummaryWriter, default to create a new TensorBoard writer. log_dir: if using default SummaryWriter, write logs to this directory, default is `./runs`. interval: plot content from engine.state every N epochs or every N iterations, default is 1. epoch_level: plot content from engine.state every N epochs or N iterations. `True` is epoch level, `False` is iteration level. batch_transform: a callable that is used to extract `image` and `label` from `ignite.engine.state.batch`, then construct `(image, label)` pair. for example: if `ignite.engine.state.batch` is `{"image": xxx, "label": xxx, "other": xxx}`, `batch_transform` can be `lambda x: (x["image"], x["label"])`. will use the result to plot image from `result[0][index]` and plot label from `result[1][index]`. `engine.state` and `batch_transform` inherit from the ignite concept: https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. output_transform: a callable that is used to extract the `predictions` data from `ignite.engine.state.output`, will use the result to plot output from `result[index]`. `engine.state` and `output_transform` inherit from the ignite concept: https://pytorch.org/ignite/concepts.html#state, explanation and usage example are in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/batch_output_transform.ipynb. global_iter_transform: a callable that is used to customize global step number for TensorBoard. For example, in evaluation, the evaluator engine needs to know current epoch from trainer. index: plot which element in a data batch, default is the first element. max_channels: number of channels to plot. frame_dim: if plotting 3D image as GIF, specify the dimension used as frames, expect input data shape as `NCHWD`, default to `-3` (the first spatial dim) max_frames: if plot 3D RGB image as video in TensorBoardX, set the FPS to `max_frames`. r?N) r@rrtrurwr:rxryr{r|rz) rrrrtrurwr:rxryrzr{r|rArrr:s* z TensorBoardImageHandler.__init__r r cCs:|jr|tj|jd|dS|tj|jd|dS)zg Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. rCN)rurIr rKrtrGr&rrrr'oszTensorBoardImageHandler.attachc Cs||jr |jjn|jj}||jjd|j}t|t j r(| }|durOt|tjs=tdt|jdt|d||jd|j|j|jdd||jjd|j}t|t j ri| }|durt|tjs~tdt|jdt|d||jd|j|j|jd d||jj|j}t|t j r| }|durt|tjstd t|jdt|d||jd|j|j|jd d|jdS) aB Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. Raises: TypeError: When ``output_transform(engine.state.output)[0]`` type is not in ``Optional[Union[numpy.ndarray, torch.Tensor]]``. TypeError: When ``batch_transform(engine.state.batch)[1]`` type is not in ``Optional[Union[numpy.ndarray, torch.Tensor]]``. TypeError: When ``output_transform(engine.state.output)`` type is not in ``Optional[Union[numpy.ndarray, torch.Tensor]]``. rNzeoutput_transform(engine.state.output)[0] must be None or one of (numpy.ndarray, torch.Tensor) but is .input_0)datarVrRryrzr{r|rTrBzcbatch_transform(engine.state.batch)[1] must be None or one of (numpy.ndarray, torch.Tensor) but is Zinput_1zboutput_transform(engine.state.output) must be None or one of (numpy.ndarray, torch.Tensor) but is rb)rxrurZr[rmrwbatchryrcrjrkdetachcpunumpynpndarray TypeErrorrir%r rrzr{r|r:rbr_)rr rVZ show_imagesZ show_labelsZ show_outputsrrr__call__ys|       z TensorBoardImageHandler.__call__)rrrrrtrWrurvrwrr:rrxrryrWrzrWr{rWr|rWr!r"r))r%r*r+r,rr'rrprrrArrq s  5 rq) __future__rrfcollections.abcrrtypingrrrrrj monai.utilsrrr r monai.visualizer OPT_IMPORT_VERSIONr _ ignite.enginer rrSummaryWriterXtorch.utils.tensorboardrorr-rqrrrrs.      Z