U vPhe@sddlmZddlmZddlmZmZmZddlm Z ddl m Z ddl m Z mZddlmZddlmZmZdd lmZed ejed \ZZe rdd lmZned ejed dd\ZZejfddddddZGddde ZGdddZeZdS)) annotations) defaultdict)CallableMappingSequence)Enum)RLock) TYPE_CHECKINGAny) IgniteInfo) min_versionoptional_import) CommonKeysz ignite.engineEvents)Enginer decorator)as_typezSequence[Mapping[str, Any]]strr )outputloss_keyreturncCs |d|SNr)rrrrQ/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/handlers/metric_logger.py_get_loss_from_output!src@seZdZdZdZdS)MetricLoggerKeysZMetricsLossN)__name__ __module__ __qualname__METRICSLOSSrrrrr%src@seZdZdZedddfdddddd d Zd dd d dZd ddddZd dd ddZd dd ddZ ddZ ddZ dS) MetricLoggera# Collect per-iteration metrics and loss value from the attached trainer. This will also collect metric values from a given evaluator object which is expected to perform evaluation at the end of training epochs. This class is useful for collecting loss and metric values in one place for storage with checkpoint savers (`state_dict` and `load_state_dict` methods provided as expected by Pytorch and Ignite) and for graphing during training. Example:: # construct an evaluator saving mean dice metric values in the key "val_mean_dice" evaluator = SupervisedEvaluator(..., key_val_metric={"val_mean_dice": MeanDice(...)}) # construct the logger and associate with evaluator to extract metric values from logger = MetricLogger(evaluator=evaluator) # construct the trainer with the logger passed in as a handler so that it logs loss values trainer = SupervisedTrainer(..., train_handlers=[logger, ValidationHandler(1, evaluator)]) # run training, logger.loss will be a list of (iteration, loss) values, logger.metrics a dict with key # "val_mean_dice" storing a list of (iteration, metric) values trainer.run() Args: loss_transform: Converts the `output` value from the trainer's state into a loss value `engine.state` and `loss_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. metric_transform: Converts the metric value coming from the trainer/evaluator's state into a storable value evaluator: Optional evaluator to consume metric results from at the end of its evaluation run cCs|SNr)xrrrKzMetricLogger.Nrz Engine | NoneNone)loss_transformmetric_transform evaluatorrcCs@||_||_g|_tt|_d|_t|_|dk r<| |dSr) r(r)lossrlistmetrics iterationrlockattach_evaluator)selfr(r)r*rrr__init__Hs zMetricLogger.__init__r)enginercCs|tj|dSzg Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)add_event_handlerrITERATION_COMPLETED)r1r3rrrattachXszMetricLogger.attach)r*rcCs|tj|jdS)z Attach event handlers to the given evaluator to log metric values from it. Args: evaluator: Ignite Engine implementing network evaluation N)r5r COMPLETED log_metrics)r1r*rrrr0_szMetricLogger.attach_evaluatorc CsJ|j:|jj|_||jj}|j|j|f||W5QRXdSr4)r/stater.r(rr+appendr9)r1r3Zlossvalrrr__call__hs  zMetricLogger.__call__c CsL|j<|jjD](\}}||}|j||j|fqW5QRXdS)z Log metrics from the given Engine's state member. Args: engine: Ignite Engine to log from N)r/r:r-itemsr)r;r.)r1r3mvrrrr9ts zMetricLogger.log_metricscCstj|jtj|jiSr#)rr!r+r r-)r1rrr state_dictszMetricLogger.state_dictcCs4|tj|jdd<|j|j|tjdSr#)rr!r+r-clearupdater )r1r@rrrload_state_dicts zMetricLogger.load_state_dict) rrr__doc__rr2r7r0r<r9r@rCrrrrr"*s   r"N) __future__r collectionsrcollections.abcrrrenumr threadingrtypingr r monai.configr monai.utilsr r monai.utils.enumsrOPT_IMPORT_VERSIONr_ ignite.enginerr!rrr"Z metricloggerrrrr s,       _