o  i@sddlmZddlZddlmZddlmZddlmZddl m Z m Z m Z er1ddl mZmZne de je d \ZZe de je d \ZZGd d d ZdS) ) annotationsN bisect_right)Callable) TYPE_CHECKING) IgniteInfo min_versionoptional_import)EngineEventsz ignite.enginer r c@sveZdZdZ   d.d/ddZddZd0ddZd0ddZed1d"d#Z ed2d%d&Z ed3d(d)Z ed4d,d-Z dS)5ParamSchedulerHandlera^ General purpose scheduler for parameters values. By default it can schedule in a linear, exponential, step or multistep function. One can also pass Callables to have customized scheduling logic. Args: parameter_setter (Callable): Function that sets the required parameter value_calculator (Union[str,Callable]): Either a string ('linear', 'exponential', 'step' or 'multistep') or Callable for custom logic. vc_kwargs (Dict): Dictionary that stores the required parameters for the value_calculator. epoch_level (bool): Whether the step is based on epoch or iteration. Defaults to False. name (Optional[str]): Identifier of logging.logger to use, if None, defaulting to ``engine.logger``. event (Optional[str]): Event to which the handler attaches. Defaults to Events.ITERATION_COMPLETED. FNparameter_setterrvalue_calculatorstr | Callable vc_kwargsdict epoch_levelboolname str | NoneeventcCsb||_|dur |ntj|_|j|j|j|jd|_||_ ||_ |j |d|_ t ||_||_dS)N)linear exponentialstepZ multistep)r)rr ITERATION_COMPLETEDr_linear _exponential_step _multistep _calculators_parameter_setter _vc_kwargs_get_value_calculator_value_calculatorlogging getLoggerlogger_name)selfr rrrrrr)d/home/dell461/cl/sdc2/last_ska_mid/HISourceFinder-master-l/src/monai/handlers/parameter_scheduler.py__init__+s   zParamSchedulerHandler.__init__cCs:t|tr |j|St|r|Stdt|jd)Nz.value_calculator must be either a string from z or a Callable.) isinstancestrrcallable ValueErrorlistkeys)r(rr)r)r*r"Es  z+ParamSchedulerHandler._get_value_calculatorenginer returnNonecCsD|jr |jj|jd<n|jj|jd<|jdi|j}||dS)N current_stepr))rstateepochr! iterationr#r )r(r2 new_valuer)r)r*__call__Ns zParamSchedulerHandler.__call__cCs$|jdur |j|_||j|dS)zT Args: engine: Ignite Engine that is used for training. N)r'r&add_event_handlerr)r(r2r)r)r*attachWs zParamSchedulerHandler.attach initial_valuefloat step_constantintstep_max_value max_valuer5cCsL||kr d}||S||kr||}||S||||||}||S)a| Keeps the parameter value to zero until step_zero steps passed and then linearly increases it to 1 until an additional step_one steps passed. Continues the trend until it reaches max_value. Args: initial_value (float): Starting value of the parameter. step_constant (int): Step index until parameter's value is kept constant. step_max_value (int): Step index at which parameter's value becomes max_value. max_value (float): Max parameter value. current_step (int): Current step index. Returns: float: new parameter value gr))r=r?rArBr5deltar)r)r*r`szParamSchedulerHandler._lineargammacCs |||S)a Decays the parameter value by gamma every step. Based on the closed form of ExponentialLR from Pytorch: https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.ExponentialLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. current_step (int): Current step index. Returns: float: new parameter value r))r=rDr5r)r)r*r{s z"ParamSchedulerHandler._exponential step_sizecCs||||S)a  Decays the parameter value by gamma every step_size. Based on StepLR from Pytorch: https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.StepLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. step_size (int): Period of parameter value decay. current_step (int): Current step index. Returns float: new parameter value r))r=rDrEr5r)r)r*rszParamSchedulerHandler._step milestones list[int]cCs||t||S)aO Decays the parameter value by gamma once the number of steps reaches one of the milestones. Based on MultiStepLR from Pytorch. https://pytorch.org/docs/stable/generated/torch.optim.lr_scheduler.MultiStepLR.html. Args: initial_value (float): Starting value of the parameter. gamma (float): Multiplicative factor of parameter value decay. milestones (List[int]): List of step indices. Must be increasing. current_step (int): Current step index. Returns: float: new parameter value r)r=rDrFr5r)r)r*rsz ParamSchedulerHandler._multistep)FNN) r rrrrrrrrrrr)r2r r3r4) r=r>r?r@rAr@rBr>r5r@r3r>)r=r>rDr>r5r@r3r>) r=r>rDr>rEr@r5r@r3r>) r=r>rDr>rFrGr5r@r3r>) __name__ __module__ __qualname____doc__r+r"r:r< staticmethodrrrrr)r)r)r*r s"     r ) __future__rr$bisectrcollections.abcrtypingr monai.utilsrrr ignite.enginer r OPT_IMPORT_VERSION_r r)r)r)r*s