o  i@7@sddlmZddlZddlZddlmZmZddlmZm Z ddl Z ddl m Z ddl mZmZmZmZmZedejed\ZZerIdd lmZn edejed d d \ZZd ZdZGdddZdS)) annotationsN)CallableSequence) TYPE_CHECKINGAny) get_logger) IgniteInfo flatten_dict is_scalar min_versionoptional_importz ignite.engineEvents)Enginer decorator)as_typez {}: {:.4f} Lossc @steZdZdZddddddddddeef d-ddZd.ddZd.d d!Zd.d"d#Z d/d'd(Z d.d)d*Z d.d+d,Z dS)0 StatsHandlera 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 class will leverage `engine.logger` as the logger, otherwise, ``logging.getLogger(name)`` is used. In both cases, it's important to make sure that the logging level is at least ``INFO``. To change the level of logging, please call ``import ignite; ignite.utils.setup_logger(name)`` (when ``name`` is not None) or ``engine.logger = ignite.utils.setup_logger(engine.logger.name, reset=True)`` (when ``name`` is None) before running the engine with this handler attached. Default behaviors: - When EPOCH_COMPLETED, logs ``engine.state.metrics`` using ``self.logger``. - When ITERATION_COMPLETED, logs ``self.output_transform(engine.state.output)`` using ``self.logger``. Usage example:: import ignite import monai trainer = ignite.engine.Engine(lambda x, y: [0.0]) # an example trainer monai.handlers.StatsHandler(name="train_stats").attach(trainer) trainer.run(range(3), max_epochs=4) More details of example is available in the tutorial: https://github.com/Project-MONAI/tutorials/blob/master/modules/engines/unet_training_dict.py. TNcCs|dS)Nrxrr^/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/stats_handler.pyJszStatsHandler.cCs|S)NrrrrrrKszmonai.handlers.StatsHandler iteration_log$bool | Callable[[Engine, int], bool] epoch_logepoch_print_loggerCallable[[Engine], Any] | Noneiteration_print_loggeroutput_transformrglobal_epoch_transformstate_attributesSequence[str] | Nonename str | Nonetag_namestrkey_var_formatreturnNonec CsJ||_||_||_||_||_||_||_| |_| |_t ||_ ||_ dS)a Args: iteration_log: whether to log data when iteration completed, default to `True`. ``iteration_log`` can be also a function and 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. Event filtering can be helpful to customize iteration logging frequency. epoch_log: whether to log data when epoch completed, default to `True`. ``epoch_log`` can be also a function and it will be interpreted as an event filter. See ``iteration_log`` argument for more details. epoch_print_logger: customized callable printer for epoch level logging. Must accept parameter "engine", use default printer if None. iteration_print_logger: customized callable printer for iteration level logging. Must accept parameter "engine", use default printer if None. output_transform: a callable that is used to transform the ``ignite.engine.state.output`` into a scalar to print, or a dictionary of {key: scalar}. In the latter case, the output string will be formatted as key: value. By default this value logging 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 print synced epoch number with the trainer engine. state_attributes: expected attributes from `engine.state`, if provided, will extract them when epoch completed. name: identifier of `logging.logger` to use, if None, defaulting to ``engine.logger``. tag_name: when iteration output is a scalar, tag_name is used to print tag_name: scalar_value to logger. Defaults to ``'Loss'``. key_var_format: a formatting string to control the output string format of key: value. N) rrrrrrr r$r&rloggerr") selfrrrrrrr r"r$r&rrr__init__Ds1  zStatsHandler.__init__enginercCs |jdur |j|_|jtjkr)a Handler for train or validation/evaluation iteration completed Event. Print iteration level log, default values are from Ignite `engine.state.output`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)r_default_iteration_printr@rrrr4rAz StatsHandler.iteration_completed_enginee ExceptioncCs|jd||)a 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, unused argument. e: the exception caught in Ignite during engine.run(). z Exception: )r) exception)r*rCrDrrrr:s zStatsHandler.exception_raisedcCsX||jj}t|jj}|durDt|dkrDd|d}t|D]}||}|t|r3|j ||n|dt |7}q!|j |t |jdr}t |jdr}t |jdr}|jjdur}d |jjd }|d |jjd 7}|d |jj7}|j ||jdurt|jdkrd }|jD]}||dt|j|dd 7}q|j |dSdSdS)a( Execute epoch level log operation. Default to print the values from Ignite `engine.state.metrics` dict and print the values of specified attributes of `engine.state`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. NrzEpoch[z ] Metrics -- z: key_metric_name best_metricbest_metric_epochz Key metric:  z best value: z at epoch: zState values: )rstateepochr metricslensortedr r&formatr%r)infohasattrrGrHrIr getattr)r*r, current_epochZ prints_dictout_strr"valueattrrrrr?s2   .        z!StatsHandler._default_epoch_printc CsD||jj}|dur dSd}t|trCt|D])}||}t|s.td |t |q||j |t|t j r=|n|7}qn#t|r\||j |jt|t j rW|n|7}n td t ||sjdS|jj}|jj}|dur~|d|d}|jj}|jj} d|d| d|d|d } |jd | |gdS) a Execute iteration log operation based on Ignite `engine.state.output` data. Print 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 print the loss from `output[0]`. Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. Nzignoring non-scalar output in StatsHandler, make sure `output_transform(engine.state.output)` returns a scalar or dictionary of key and scalar pairs to avoid this warning. {}:{}zignoring non-scalar output in StatsHandler, make sure `output_transform(engine.state.output)` returns a scalar or a dictionary of key and scalar pairs to avoid this warning. {}zEpoch: /z, Iter: z --rJ)rrKoutput isinstancedictrOr r1r2rPtyper&torchTensoritemr$ epoch_length iterationrL max_epochsr)rQjoin) r*r,lossrUr"rVnum_iterationscurrent_iterationrTZ num_epochsbase_strrrrrBsB    (  z%StatsHandler._default_iteration_print)rrrrrrrrrrrrr r!r"r#r$r%r&r%r'r()r,rr'r()rCrrDrEr'r() __name__ __module__ __qualname____doc__ DEFAULT_TAGDEFAULT_KEY_VAL_FORMATr+r=r8r4r:r?rBrrrrr$s&! =   $r) __future__rr/r1collections.abcrrtypingrrr_ monai.appsr monai.utilsrr r r r OPT_IMPORT_VERSIONr _ ignite.enginerrornrrrrrs"