a
    d                     @   sj   d dl mZ d dlmZmZ d dlZd dlmZ ddlm	Z	 e
dddZG d	d
 d
ZG dd dZdS )    )Path)CallableOptionalN   )TrainerState)returnc                 C   s   d|  dS )NZmodel_z.pt )xr   r   [/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/x/callbacks.pydefault_filename_fcn   s    r   c                   @   s:   e Zd ZdZdeeeddddZej	ee
d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.

    **Usage example:**

    .. code:: python

        early_stop = EarlyStopping(
            monitor="top5", filepath="early_stop_model.pt"
        )

        trainer = ImageClassifierTrainer(...,
            callbacks={"on_checkpoint", early_stop}
        )
               N)monitor	min_deltapatiencer   c                 C   s(   || _ || _|| _d| _d | _d| _d S )Nr   F)r   r   r   counter
best_score
early_stop)selfr   r   r   r   r   r
   __init__&   s    zEarlyStopping.__init__modelepochr   c                 C   s~   || j  j}| jd u r|| _n>|| j| j k rP|  jd7  _| j| jkr\d| _n|| _d| _| jrxtd| d tj	S tj
S )Nr   Tr   z3[INFO] Early-Stopping the training process. Epoch: .)r   avgr   r   r   r   r   printr   Z	TERMINATEZTRAINING)r   r   r   valid_metricZscorer   r   r
   __call__/   s    
zEarlyStopping.__call__)r   r   )__name__
__module____qualname____doc__strfloatintr   nnModuler   r   r   r   r   r
   r      s   	r   c                   @   s>   e Zd ZdZd	eeee ddddZej	e
ddddZdS )
ModelCheckpointa  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.

    **Usage example:**

    .. code:: python

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

        trainer = ImageClassifierTrainer(...,
            callbacks={"on_checkpoint", model_checkpoint}
        )
    N)filepathr   filename_fcnr   c                 C   s4   || _ || _|pt| _d| _t| j jddd d S )Nr   T)parentsexist_ok)r)   r   r   _filename_fcnbest_metricr   mkdir)r   r)   r   r*   r   r   r
   r   W   s
    
zModelCheckpoint.__init__r   c                 C   s@   || j  j}|| jkr<|| _t| j| | }t|| d S )N)r   r   r.   r   r)   r-   torchsave)r   r   r   r   Zvalid_metric_valuefilenamer   r   r
   r   b   s
    
zModelCheckpoint.__call__)N)r   r    r!   r"   r#   r   r   r   r&   r'   r%   r   r   r   r   r
   r(   C   s   r(   )pathlibr   typingr   r   r0   Ztorch.nnr&   utilsr   r#   r   r   r(   r   r   r   r
   <module>   s   4