o
    oi                     @   s   d dl mZ d dlmZ d dlmZmZmZmZ d dl	Z	d dl
mZ d dlmZ ddlmZ d	eeef d
eeef defddZG dd dZG dd dZdS )    )inf)Path)CallableDictOptionalUnionN)Module)AverageMeter   )TrainerStateepochmetricreturnc                 C   s   d|  d| dS )z.Generate the filename in the model checkpoint.zmodel_epoch=z_metricValue=z.pt )r   r   r   r   F/home/ubuntu/.local/lib/python3.10/site-packages/kornia/x/callbacks.pydefault_filename_fcn   s   r   c                   @   sX   e Zd ZdZ			ddedededed	d
f
ddZde	dede
eef d	efddZd
S )EarlyStoppinga+  Callback that evaluates whether there is improvement in the loss function.

    The module track the losses and in case of finish patience sends a termination signal to the trainer.

    Args:
        monitor: the name of the value to track.
        min_delta: the minimum difference between losses to increase the patience counter.
        patience: the number of times to wait until the trainer does not terminate.
        max_mode: if true metric will be multiply by -1,
                  turn this flag when increasing metric value is expected for example Accuracy

    **Usage example:**

    .. code:: python

        early_stop = EarlyStopping(
            monitor="loss", patience=10
        )

        trainer = ImageClassifierTrainer(
            callbacks={"on_epoch_end", early_stop}
        )

               Fmonitor	min_deltapatiencemax_moder   Nc                 C   s8   || _ || _|| _|| _d| _|rt nt| _d| _d S )Nr   F)r   r   r   r   counterr   
best_score
early_stop)selfr   r   r   r   r   r   r   __init__=   s   
zEarlyStopping.__init__modelr   valid_metricc                 C   s   || j  j}| jr|| jkn|| jk }|r|| _d| _n%| jr'|| j| j kn|| j| j k }|sA|  jd7  _| j| jkrAd| _| jrOtd| d t	j
S t	jS )Nr   r
   Tz3[INFO] Early-Stopping the training process. Epoch: .)r   avgr   r   r   r   r   r   printr   	TERMINATETRAINING)r   r   r   r   scoreis_bestis_within_deltar   r   r   __call__P   s"   zEarlyStopping.__call__)r   r   F)__name__
__module____qualname____doc__strfloatintboolr   r   r   r	   r   r(   r   r   r   r   r   #   s"    
&r   c                   @   sb   e Zd ZdZ		ddededeedef  ded	df
d
dZde	de
deeef d	dfddZdS )ModelCheckpointaG  Callback that save the model at the end of every epoch.

    Args:
        filepath: the where to save the mode.
        monitor: the name of the value to track.
        max_mode: if true metric will be multiply by -1
                  turn this flag when increasing metric value is expected for example Accuracy
    **Usage example:**

    .. code:: python

        model_checkpoint = ModelCheckpoint(
            filepath="./outputs", monitor="loss",
        )

        trainer = ImageClassifierTrainer(...,
            callbacks={"on_checkpoint", model_checkpoint}
        )

    NFfilepathr   filename_fcn.r   r   c                 C   sD   || _ || _|p	t| _|rt nt| _|| _t| j jddd d S )NT)parentsexist_ok)	r2   r   r   _filename_fcnr   best_metricr   r   mkdir)r   r2   r   r3   r   r   r   r   r      s   
zModelCheckpoint.__init__r   r   r   c                 C   sZ   || j  j}| jr|| jkn|| jk }|r+|| _t| j| || }t|| d S d S )N)	r   r!   r   r7   r   r2   r6   torchsave)r   r   r   r   valid_metric_valuer&   filenamer   r   r   r(      s   zModelCheckpoint.__call__)NF)r)   r*   r+   r,   r-   r   r   r0   r   r   r/   r   r	   r(   r   r   r   r   r1   k   s     
&r1   )mathr   pathlibr   typingr   r   r   r   r9   kornia.corer   kornia.metricsr	   utilsr   r-   r/   r.   r   r   r1   r   r   r   r   <module>   s   &H