o  iM@sddlmZddlmZddlmZmZmZddlm Z ddl m Z ddl m Z mZddlmZmZmZddlmZed ejed \ZZe rMdd lmZn ed ejed d d\ZZejfdddZGddde ZGdddZeZdS)) annotations) defaultdict)CallableMappingSequence)Enum)RLock) TYPE_CHECKINGAny) IgniteInfo min_versionoptional_import) CommonKeysz ignite.engineEvents)Enginer decorator)as_typeoutputSequence[Mapping[str, Any]]loss_keystrreturnr cCs |d|SNr)rrrr^/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/metric_logger.py_get_loss_from_output s rc@seZdZdZdZdS)MetricLoggerKeysZMetricsLossN)__name__ __module__ __qualname__METRICSLOSSrrrrr$src@s^eZdZdZedddfdd d ZdddZdddZdddZdddZ 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)xrrrJszMetricLogger.Nloss_transformrmetric_transform evaluator Engine | NonerNonecCsD||_||_g|_tt|_d|_t|_|dur | |dSdSr) r'r(lossrlistmetrics iterationrlockattach_evaluator)selfr'r(r)rrr__init__Gs zMetricLogger.__init__enginercCs|tj|dSzg Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)add_event_handlerrITERATION_COMPLETED)r2r4rrrattachWszMetricLogger.attachcCs|tj|jdS)z Attach event handlers to the given evaluator to log metric values from it. Args: evaluator: Ignite Engine implementing network evaluation N)r6r COMPLETED log_metrics)r2r)rrrr1^szMetricLogger.attach_evaluatorcCs`|j#|jj|_||jj}|j|j|f||WddS1s)wYdSr5)r0stater/r'rr,appendr:)r2r4Zlossvalrrr__call__gs   "zMetricLogger.__call__cCsb|j$|jjD]\}}||}|j||j|fq WddS1s*wYdS)z Log metrics from the given Engine's state member. Args: engine: Ignite Engine to log from N)r0r;r.itemsr(r<r/)r2r4mvrrrr:ss  "zMetricLogger.log_metricscCstj|jtj|jiSr$)rr"r,r!r.)r2rrr state_dictszMetricLogger.state_dictcCs4|tj|jdd<|j|j|tjdSr$)rr"r,r.clearupdater!)r2rArrrload_state_dicts zMetricLogger.load_state_dict)r'rr(rr)r*rr+)r4rrr+)r)rrr+) rrr __doc__rr3r8r1r=r:rArDrrrrr#)s    r#N)rrrrrr ) __future__r collectionsrcollections.abcrrrenumr threadingrtypingr r monai.utilsr r r monai.utils.enumsrOPT_IMPORT_VERSIONr_ ignite.enginerr"rrr#Z metricloggerrrrrs$      _