o  i@sddlmZddlmZddlmZddlmZmZm Z e dej ed\Z Z e dej ed\Z Z er9dd lmZn e dej ed d d \ZZ Gd ddZdS)) annotations)Callable) TYPE_CHECKING) IgniteInfo min_versionoptional_importz ignite.engineEventszignite.handlers EarlyStopping)Enginer decorator)as_typec@sBeZdZdZ    ddddZdddZd ddZdddZdS)!EarlyStopHandleru EarlyStopHandler acts as an Ignite handler to stop training if no improvement after a given number of events. It‘s based on the `EarlyStopping` handler in ignite. Args: patience: number of events to wait if no improvement and then stop the training. score_function: It should be a function taking a single argument, an :class:`~ignite.engine.engine.Engine` object that the handler attached, can be a trainer or validator, and return a score `float`. an improvement is considered if the score is higher. trainer: trainer engine to stop the run if no improvement, if None, must call `set_trainer()` before training. min_delta: a minimum increase in the score to qualify as an improvement, i.e. an increase of less than or equal to `min_delta`, will count as no improvement. cumulative_delta: if True, `min_delta` defines an increase since the last `patience` reset, otherwise, it defines an increase after the last event, default to False. epoch_level: check early stopping for every epoch or every iteration of the attached engine, `True` is epoch level, `False` is iteration level, default to epoch level. Note: If in distributed training and uses loss value of every iteration to detect early stopping, the values may be different in different ranks. When using this handler with distributed training, please also note that to prevent "dist.destroy_process_group()" hangs, you can use an "all_reduce" operation to synchronize the stop signal across all ranks. The mechanism can be implemented in the `score_function`. The following is an example: .. code-block:: python import os import torch import torch.distributed as dist def score_function(engine): val_metric = engine.state.metrics["val_mean_dice"] if dist.is_initialized(): device = torch.device("cuda:" + os.environ["LOCAL_RANK"]) val_metric = torch.tensor([val_metric]).to(device) dist.all_reduce(val_metric, op=dist.ReduceOp.SUM) val_metric /= dist.get_world_size() return val_metric.item() return val_metric User may attach this handler to validator engine to detect validation metrics and stop the training, in this case, the `score_function` is executed on validator engine and `trainer` is the trainer engine. NFTpatienceintscore_functionrtrainer Engine | None min_deltafloatcumulative_deltabool epoch_levelreturnNonecCs@||_||_||_||_||_d|_|dur|j|ddSdS)N)r)rrrrr_handler set_trainer)selfrrrrrrrb/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/earlystop_handler.py__init__Os zEarlyStopHandler.__init__enginer cCs*|jr |tj|dS|tj|dS)zg Args: engine: Ignite Engine, it can be a trainer, validator or evaluator. N)radd_event_handlerrEPOCH_COMPLETEDITERATION_COMPLETEDrr!rrrattachbszEarlyStopHandler.attachcCs t|j|j||j|jd|_dS)z\ Set trainer to execute early stop if not setting properly in `__init__()`. )rrrrrN)r rrrrr)rrrrrrls zEarlyStopHandler.set_trainercCs |jdur td||dS)NzGplease set trainer in __init__() or call set_trainer() before training.)r RuntimeErrorr%rrr__call__xs zEarlyStopHandler.__call__)NrFT)rrrrrrrrrrrrrr)r!r rr)rr rr)__name__ __module__ __qualname____doc__r r&rr(rrrrr s4   r N) __future__rcollections.abcrtypingr monai.utilsrrrOPT_IMPORT_VERSIONr_r ignite.enginer r rrrrs