a
    yf`t                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dl mZ d dlmZ d dl	m	Z	 d dl
mZ d dlmZ d dlZd dlZd dlmZ d dlmZ d dlm  mZ d dlmZmZmZmZmZmZm Z m!Z!m"Z" d dl#m$Z$ zd dl%Z%W n e&y   dZ%Y n0 e$ej!d	Z'e$ej!d
Z(e$ej!dZ)e$ej!dZ*e$edZ+e$edZ,e$edZ-e$edZ.e rze$ej!drze/d ee0dddZ1dd Z2dZe3e4dddZ5dd Z6d[d!d"Z7d#d$ Z8d%d& Z9d'd( Z:d\d*d+Z;d,d- Z<d.d/ Z=d0d1 Z>d]d2d3Z?d^d4d5Z@d6d7 ZAd_d:d;ZBd`d=d>ZCd?d@ ZDdadAdBZEdCdD ZFdEdF ZGdbdIdJZHdcdKdLZIG dMdN dNZJddee4ef e4eKeKdPdQdRZLdSdT ZMdedVdWZNG dXdY dYZOdS )f    N)contextmanager)deepcopy)datetime)Path)Union)	DEFAULT_CFG_DICTDEFAULT_CFG_KEYSLOGGERNUM_THREADSPYTHON_VERSIONTORCHVISION_VERSIONWINDOWS__version__colorstr)check_versionz1.9.0z1.13.0z2.0.0z2.4.0z0.10.0z0.11.0z0.13.0z0.18.0z==2.4.0u   WARNING ⚠️ Known issue with torch==2.4.0 on Windows with CPU, recommend upgrading to torch>=2.4.1 to resolve https://github.com/ultralytics/ultralytics/issues/15049)
local_rankc                 c   sN   t  ot  }|r*| dvr*t j| gd dV  |rJ| dkrJt j| gd dS )zjEnsures all processes in distributed training wait for the local master (rank 0) to complete a task first.>   r   )Z
device_idsNr   )distis_availableZis_initializedZbarrier)r   Zinitialized r   Y/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/torch_utils.pytorch_distributed_zero_first7   s    r   c                  C   s   dd } | S )zXApplies torch.inference_mode() decorator if torch>=1.9.0 else torch.no_grad() decorator.c                 S   s*   t rt r| S t rtjntj | S dS )zNApplies appropriate torch decorator for inference mode based on torch version.N)	TORCH_1_9torchZis_inference_mode_enabledZinference_modeZno_grad)fnr   r   r   decorateF   s    z&smart_inference_mode.<locals>.decorater   )r   r   r   r   smart_inference_modeC   s    r   cudaenableddevicec                 C   s&   t rtjj|| dS tjj| S dS )ai  
    Get the appropriate autocast context manager based on PyTorch version and AMP setting.

    This function returns a context manager for automatic mixed precision (AMP) training that is compatible with both
    older and newer versions of PyTorch. It handles the differences in the autocast API between PyTorch versions.

    Args:
        enabled (bool): Whether to enable automatic mixed precision.
        device (str, optional): The device to use for autocast. Defaults to 'cuda'.

    Returns:
        (torch.amp.autocast): The appropriate autocast context manager.

    Note:
        - For PyTorch versions 1.13 and newer, it uses `torch.amp.autocast`.
        - For older versions, it uses `torch.cuda.autocast`.

    Example:
        ```python
        with autocast(amp=True):
            # Your mixed precision operations here
            pass
        ```
    )r   N)
TORCH_1_13r   ampautocastr   r   r   r   r   r#   P   s    r#   c                  C   s   ddl m}  d| vrtt| ddl}d}| }||d |v rL|d n|d |v r`|d n|d d}|d	d
dd
dd
| d< W d   n1 s0    Y  | ddS )z=Return a string with system CPU information, i.e. 'Apple M2'.r   )PERSISTENT_CACHEZcpu_infoN)Z	brand_rawZhardware_rawZarch_string_raw      unknownz(R) zCPU z@ )	ultralytics.utilsr$   
contextlibsuppress	Exceptioncpuinfoget_cpu_infogetreplace)r$   r-   kinfostringr   r   r   r.   o   s    8>r.   r(   FTc                 C   s  t | tjr| S dt dt dtj d}t|  } dD ]}| |d} q:| dk}| dv }|sd|rpd	tj	d
< n| r2| dkrd} d| v rd
dd | dD } tj	d
d}| tj	d
< tj rtj t| dks2t| tj dkrdnd}	td|  dtj  dtj  d| d|	 
|sD|sDtj rD| rZ| dnd}
t|
}|dkr|dk rtd|dkr|| dkrtd| d| d|| |  d|| | |  d| ddt|d  }t|
D ]N\}}tj|}||dkrdn| d | d!|j d"|jd# d$d%7 }qd&}nH|rvtrvtjj rv|d't  d(7 }d)}n|d*t  d(7 }d}|d+v rtt |rt|r|n|  t|S ),a  
    Selects the appropriate PyTorch device based on the provided arguments.

    The function takes a string specifying the device or a torch.device object and returns a torch.device object
    representing the selected device. The function also validates the number of available devices and raises an
    exception if the requested device(s) are not available.

    Args:
        device (str | torch.device, optional): Device string or torch.device object.
            Options are 'None', 'cpu', or 'cuda', or '0' or '0,1,2,3'. Defaults to an empty string, which auto-selects
            the first available GPU, or CPU if no GPU is available.
        batch (int, optional): Batch size being used in your model. Defaults to 0.
        newline (bool, optional): If True, adds a newline at the end of the log string. Defaults to False.
        verbose (bool, optional): If True, logs the device information. Defaults to True.

    Returns:
        (torch.device): Selected device.

    Raises:
        ValueError: If the specified device is not available or if the batch size is not a multiple of the number of
            devices when using multiple GPUs.

    Examples:
        >>> select_device("cuda:0")
        device(type='cuda', index=0)

        >>> select_device("cpu")
        device(type='cpu')

    Note:
        Sets the 'CUDA_VISIBLE_DEVICES' environment variable for specifying which GPUs to use.
    zUltralytics u    🚀 Python-z torch- )zcuda:none()[]'r4   r(   cpu>   zmps:0mpsz-1ZCUDA_VISIBLE_DEVICESr   0,c                 S   s   g | ]}|r|qS r   r   .0xr   r   r   
<listcomp>       z!select_device.<locals>.<listcomp>Nr   z}See https://pytorch.org/get-started/locally/ for up-to-date torch install instructions if no CUDA devices are seen by torch.
zInvalid CUDA 'device=z' requested. Use 'device=cpu' or pass valid CUDA device(s) if available, i.e. 'device=0' or 'device=0,1,2,3' for Multi-GPU.

torch.cuda.is_available(): z
torch.cuda.device_count(): z%
os.environ['CUDA_VISIBLE_DEVICES']: 
r%   znAutoBatch with batch<1 not supported for Multi-GPU training, please specify a valid batch size, i.e. batch=16.z'batch=z"' must be a multiple of GPU count z. Try 'batch=z' or 'batch=z/', the nearest batch sizes evenly divisible by .zCUDA:z (, i   z.0fzMiB)
zcuda:0zMPS (z)
r<   zCPU (>   r;   r<   )
isinstancer   r    r   r   strlowerr0   osenvironjoinsplitr/   r   r   Zdevice_countlenr	   r2   
ValueError	enumerateZget_device_propertiesnameZtotal_memory	TORCH_2_0backendsr<   r.   Zset_num_threadsr
   rstrip)r    batchnewlineverbosesremover;   r<   visibleinstallZdevicesnspaceidpargr   r   r   select_device~   s    !
$



<

rb   c                   C   s   t j rt j  t S )zPyTorch-accurate time.)r   r   r   Zsynchronizetimer   r   r   r   	time_sync   s    

rd   c              
   C   s  t j| j| j| j| j| j| j| jdd	d
| jj}| j| jd}t|jt|j|j }|jt|||jj | jdu rtj| jjd | jjdn| j}|j|j|jt|j|j  }|jt||ddd|  |S )	z\Fuse Conv2d() and BatchNorm2d() layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/.T)kernel_sizestridepaddingdilationgroupsbiasFr   Nr   r    r%   )nnConv2din_channelsout_channelsre   rf   rg   rh   ri   requires_grad_toweightr    viewr   diagdivsqrtepsrunning_varcopy_mmshaperj   zerosmulrunning_meanreshape)convbnZ	fusedconvZw_convw_bnb_convb_bnr   r   r   fuse_conv_and_bn   s,    
 *(&r   c                 C   s  t j| j| j| j| j| j| j| j| j	dd	
d| jj}| j| jd}t|jt|j|j }|jt|||jj | jdu rtj| jjd | jjdn| j}|j|j|jt|j|j  }|jt||ddd|  |S )z0Fuse ConvTranspose2d() and BatchNorm2d() layers.T)re   rf   rg   output_paddingrh   ri   rj   Fr   Nr%   rk   )rl   ZConvTranspose2drn   ro   re   rf   rg   r   rh   ri   rp   rq   rr   r    rs   r   rt   ru   rv   rw   rx   ry   rz   r{   rj   r|   r}   r~   r   )Zdeconvr   Z
fuseddconvZw_deconvr   r   r   r   r   r   fuse_deconv_and_bn
  s.     *(&r     c                 C   sx  |sdS t | }t| }tt|  }|rtdddddddddd	d
ddddddddd t|  D ]L\}\}}	|	dd}td|||	j
|	 t|	j|	 |	 |	jf  qxt| |}
t| ddd  rdnd}|
rd|
ddnd}t| ddp t| di dd}t|j	ddp8d}t| d| d |d!d"|d!d#|d!d$|  ||||
fS )%z_
    Model information.

    imgsz may be int or list, i.e. imgsz=640 or imgsz=[640, 320].
    Nlayerz>5r4   rQ   z>40Zgradientz>9
parametersz>12r{   z>20muz>10sigmazmodule_list.r(   z)%5g %40s %9s %12g %20s %10.3g %10.3g %10sZis_fusedc                   S   s   dS )NFr   r   r   r   r   <lambda>@  rC   zmodel_info.<locals>.<lambda>z (fused)rF   .1fz GFLOPs	yaml_fileyamlZyoloZYOLOZModelz summaryz: r>   z	 layers, z parameters, z
 gradients)get_num_paramsget_num_gradientsrN   listmodulesr	   r2   rP   Znamed_parametersr0   requires_gradnumelr{   meanZstddtype	get_flopsgetattrr/   r   stem)modelZdetailedrW   imgszZn_pZn_gZn_lr^   rQ   r`   flopsZfusedfsr   Z
model_namer   r   r   
model_info)  s.    8(
"2r   c                 C   s   t dd |  D S )z6Return the total number of parameters in a YOLO model.c                 s   s   | ]}|  V  qd S Nr   r?   r   r   r   	<genexpr>J  rC   z!get_num_params.<locals>.<genexpr>sumr   r   r   r   r   r   H  s    r   c                 C   s   t dd |  D S )zEReturn the total number of parameters with gradients in a YOLO model.c                 s   s   | ]}|j r| V  qd S r   )r   r   r?   r   r   r   r   O  rC   z$get_num_gradients.<locals>.<genexpr>r   r   r   r   r   r   M  s    r   c                 C   sp   | j jr:ddlm} || jg| jd d }|d nt| jt	t
| jdd}t	| jjd d|d< |S )	a  
    Return model info dict with useful model information.

    Example:
        YOLOv8n info for loggers
        ```python
        results = {
            "model/parameters": 3151904,
            "model/GFLOPs": 8.746,
            "model/speed_ONNX(ms)": 41.244,
            "model/speed_TensorRT(ms)": 3.211,
            "model/speed_PyTorch(ms)": 18.755,
        }
        ```
    r   )ProfileModelsrk   z
model/name   )zmodel/parameterszmodel/GFLOPsZ	inferencezmodel/speed_PyTorch(ms))argsprofileZultralytics.utils.benchmarksr   lastr    popr   r   roundr   	validatorspeed)Ztrainerr   resultsr   r   r   model_info_for_loggersR  s    r   c                 C   s,  t sdS zt| } t|  }t|ts2||g}zt| drRtt| j	 dnd}t
jd|jd ||f|jd}t jt| |gddd d	 d
 }||d  | |d  | W W S  ty   t
jd|jd g|R |jd}t jt| |gddd d	 d
  Y W S 0 W n ty&   Y dS 0 dS )zReturn a YOLO model's FLOPs.        rf       r%   rk   FinputsrW   r       eAr&   N)thopde_parallelnextr   rG   r   hasattrmaxintrf   r   emptyr{   r    r   r   r,   )r   r   r`   rf   imr   r   r   r   r   p  s"    
""  .r   c                 C   sn  t sdS t| } t|  }t|ts.||g}zt| drNtt| j	 dndd }t
jd|jd ||f|jd}t
jjdd}| | W d	   n1 s0    Y  td
d | D d }||d  | |d  | }W n tyh   t
jd|jd g|R |jd}t
jjdd}| | W d	   n1 s@0    Y  tdd | D d }Y n0 |S )zOCompute model FLOPs (thop package alternative, but 2-10x slower unfortunately).r   rf   r   r&   r%   rk   T)Z
with_flopsNc                 s   s   | ]}|j V  qd S r   r   r?   r   r   r   r     rC   z0get_flops_with_torch_profiler.<locals>.<genexpr>r   r   c                 s   s   | ]}|j V  qd S r   r   r?   r   r   r   r     rC   )rR   r   r   r   rG   r   r   r   r   rf   r   r   r{   r    Zprofilerr   r   Zkey_averagesr,   )r   r   r`   rf   r   Zprofr   r   r   r   get_flops_with_torch_profiler  s&    
&&  ( r   c                 C   s`   |   D ]R}t|}|tju r q|tju r8d|_d|_q|tjtjtj	tj
tjhv rd|_qdS )z*Initialize model weights to random values.gMbP?gQ?TN)r   typerl   rm   ZBatchNorm2drw   ZmomentumZ	HardswishZ	LeakyReLUZReLUZReLU6ZSiLUZinplace)r   mtr   r   r   initialize_weights  s    

r         ?r   c                    s   dkr| S | j dd \}}t| t| f}tj| |ddd} |sh fdd||fD \}}tj| d	||d
  d	||d	  gddS )z`Scales and pads an image tensor, optionally maintaining aspect ratio and padding to gs multiple.r   r&   NZbilinearF)sizemodeZalign_cornersc                 3   s$   | ]}t |     V  qd S r   )mathceilr?   gsratior   r   r     rC   zscale_img.<locals>.<genexpr>r   r%   gS㥛?)value)r{   r   FZinterpolatepad)Zimgr   Z
same_shaper   hwrX   r   r   r   	scale_img  s    r   r   c                 C   sJ   |j  D ]:\}}t|r"||vs
|ds
||v r8q
q
t| || q
dS )zdCopies attributes from object 'b' to object 'a', with options to include/exclude certain attributes._N)__dict__itemsrN   
startswithsetattr)abincludeexcluder1   vr   r   r   	copy_attr  s    "r   c                  C   sL   t r tdd ttjD d S tjjddd } dddd	d	d
| d	S )zmReturn the second-most recent ONNX opset version supported by this version of PyTorch, adjusted for maturity.c                 s   s&   | ]}d |v rt |dd V  qdS )Zsymbolic_opset   N)r   )r@   r1   r   r   r   r     rC   z#get_latest_opset.<locals>.<genexpr>r%   rE   r      r         )z1.12z1.11z1.10z1.9z1.8)r!   r   varsr   ZonnxZproducer_versionrsplitr/   )versionr   r   r   get_latest_opset  s    r   c                    s    fdd|   D S )zjReturns a dictionary of intersecting keys with matching shapes, excluding 'exclude' keys, using da values.c                    sD   i | ]<\ } v rt  fd dD r|j  jkr |qS )c                 3   s   | ]}| vV  qd S r   r   r?   r1   r   r   r     rC   z-intersect_dicts.<locals>.<dictcomp>.<genexpr>)allr{   )r@   r   dbr   r   r   
<dictcomp>  rC   z#intersect_dicts.<locals>.<dictcomp>)r   )dar   r   r   r   r   intersect_dicts  s    r   c                 C   s   t | tjjtjjfS )z+Returns True if model is of type DP or DDP.)rG   rl   parallelZDataParallelZDistributedDataParallelr   r   r   r   is_parallel  s    r   c                 C   s   t | r| jS | S )zODe-parallelize a model: returns single-GPU model if model is of type DP or DDP.)r   moduler   r   r   r   r     s    r   r   d   c                    s    fddS )zaReturns a lambda function for sinusoidal ramp from y1 to y2 https://arxiv.org/pdf/1812.01187.pdf.c                    s.   t dt| tj    d d   S )Nr%   r&   r   )r   r   cospirA   stepsy1y2r   r   r     rC   zone_cycle.<locals>.<lambda>r   )r   r   r   r   r   r   	one_cycle  s    r   c                 C   s   t |  tj |  t|  tj|  tj|  |r~trrtjddd dtj	j
_dtjd< t| tjd< qtd ntd dtj	j
_dS )	zeInitialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html.T)Z	warn_onlyz:4096:8ZCUBLAS_WORKSPACE_CONFIGZPYTHONHASHSEEDuB   WARNING ⚠️ Upgrade to torch>=2.0.0 for deterministic training.FN)randomseednpr   Zmanual_seedr   Zmanual_seed_allrR   Zuse_deterministic_algorithmsrS   ZcudnndeterministicrJ   rK   rH   r	   warning)r   r   r   r   r   
init_seeds  s    




r   c                   @   s,   e Zd ZdZdddZdd ZdddZdS )ModelEMAag  
    Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models. Keeps a moving
    average of everything in the model state_dict (parameters and buffers).

    For EMA details see https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage

    To disable EMA set the `enabled` attribute to `False`.
    H.?  r   c                    sL   t t| | _|| _ fdd| _| j D ]}|d q2d| _dS )z0Initialize EMA for 'model' with given arguments.c                    s    dt |     S )Nr%   )r   expr   decaytaur   r   r     rC   z#ModelEMA.__init__.<locals>.<lambda>FTN)	r   r   evalemaupdatesr  r   rp   r   )selfr   r  r  r  r`   r   r  r   __init__   s    zModelEMA.__init__c                 C   sp   | j rl|  jd7  _| | j}t| }| j  D ]0\}}|jjr:||9 }|d| || 	  7 }q:dS )zUpdate EMA parameters.r%   N)
r   r  r  r   
state_dictr  r   r   Zis_floating_pointdetach)r	  r   r_   Zmsdr1   r   r   r   r   update	  s    zModelEMA.updater   Zprocess_groupZreducerc                 C   s   | j rt| j||| dS )zCUpdates attributes and saves stripped model with optimizer removed.N)r   r   r  )r	  r   r   r   r   r   r   update_attr  s    zModelEMA.update_attrN)r   r  r   )r   r  )__name__
__module____qualname____doc__r
  r  r  r   r   r   r   r     s   	
	r   best.pt)frX   r  returnc              
   C   s  z:t j| t dd}t|ts(J dd|v s8J dW n> tyx } z&td|  d|  i W  Y d}~S d}~0 0 t	 
 td	d
d}|dr|d |d< t|d drt|d j|d _t|d drd|d _|d   |d  D ]}d|_ qi t|di }dD ]}d||< qd|d< dd | D |d< i |||p^i }	t |	|pp|  tj|p| d }
td|  d|rd| dnd d|
dd |	S )a#  
    Strip optimizer from 'f' to finalize training, optionally save as 's'.

    Args:
        f (str): file path to model to strip the optimizer from. Default is 'best.pt'.
        s (str): file path to save the model with stripped optimizer to. If not provided, 'f' will be overwritten.
        updates (dict): a dictionary of updates to overlay onto the checkpoint before saving.

    Returns:
        (dict): The combined checkpoint dictionary.

    Example:
        ```python
        from pathlib import Path
        from ultralytics.utils.torch_utils import strip_optimizer

        for f in Path("path/to/model/checkpoints").rglob("*.pt"):
            strip_optimizer(f)
        ```

    Note:
        Use `ultralytics.nn.torch_safe_load` for missing modules with `x = torch_safe_load(f)[0]`
    r;   )Zmap_locationz%checkpoint is not a Python dictionaryr   z'model' missing from checkpointu   WARNING ⚠️ Skipping z!, not a valid Ultralytics model: Nz2AGPL-3.0 License (https://ultralytics.com/license)zhttps://docs.ultralytics.com)dater   licensedocsr  r   	criterionFZ
train_args)Z	optimizerbest_fitnessr  r  r   epochc                 S   s   i | ]\}}|t v r||qS r   )r   )r@   r1   r   r   r   r   r   S  rC   z#strip_optimizer.<locals>.<dictcomp>g    .AzOptimizer stripped from r>   z
 saved as r(   r4   r   MB)r   loadr    rG   dictr,   r	   r   r   now	isoformatr   r/   r   r   r  halfr   r   r   r   saverJ   pathgetsizer2   )r  rX   r  rA   emetadatar`   r   r1   combinedmbr   r   r   strip_optimizer  s>    



2r*  c                 C   sT   | d   D ]B}| D ]4\}}|dkrt|tjr|jtju r| ||< qq| S )z
    Converts the state_dict of a given optimizer to FP16, focusing on the 'state' key for tensor conversions.

    This method aims to reduce storage size without altering 'param_groups' as they contain non-tensor data.
    statestep)valuesr   rG   r   Tensorr   Zfloat32r"  )r  r+  r1   r   r   r   r   $convert_optimizer_state_dict_to_fp16^  s
     r/  
   c                 C   s  g }t |tjst|}tdddddddddddd	d
d	 t | trZ| n| gD ]}||}d|_t |tr|n|gD ]f}t	|dr||n|}t	|drt |tj
r|jtju r| n|}ddg d  }}}	z,trtj||gddd d d nd}
W n ty.   d}
Y n0 zrt|D ]}t |	d< ||}t |	d< z8t |tr|tdd |D n|   t |	d< W n  ty   td|	d< Y n0 ||	d |	d  d | 7 }||	d |	d  d | 7 }q<tj rtj d nd}dd ||fD \}}t |tjrJtdd | D nd}t|d|
d|d|d|dt|d	t|d	 |||
|||||g W n: ty } z t| |d W Y d}~n
d}~0 0 t  tj   qq`|S ) aU  
    Ultralytics speed, memory and FLOPs profiler.

    Example:
        ```python
        from ultralytics.utils.torch_utils import profile

        input = torch.randn(16, 3, 640, 640)
        m1 = lambda x: x * torch.sigmoid(x)
        m2 = nn.SiLU()
        profile(input, [m1, m2], n=100)  # profile over 100 iterations
        ```
    ZParamsz>12sZGFLOPszGPU_mem (GB)z>14szforward (ms)zbackward (ms)inputz>24soutputTrq   r"  r   )r   r   r   Fr   r   r&   r%   c                 s   s   | ]}|  V  qd S r   )r   )r@   yir   r   r   r     rC   zprofile.<locals>.<genexpr>nani  c                 s   s(   | ] }t |tjrt|jnd V  qdS )r   N)rG   r   r.  tupler{   r?   r   r   r   r     rC   c                 s   s   | ]}|  V  qd S r   r   r?   r   r   r   r     rC   Z12z12.4gz>14.3fz14.4gN)!rG   r   r    rb   r	   r2   r   rq   r   r   r.  r   Zfloat16r"  r   r   r,   rangerd   r   Zbackwardfloatr   r   Zmemory_reservedrl   Moduler   rH   appendgcZcollectZempty_cache)r1  opsr\   r    r   rA   r   tftbr   r   r   yZmemZs_inZs_outr`   r&  r   r   r   r   l  sX    
.,


* (<
 r   c                   @   s"   e Zd ZdZdddZdd ZdS )	EarlyStoppingzkEarly stopping class that stops training when a specified number of epochs have passed without improvement.2   c                 C   s$   d| _ d| _|ptd| _d| _dS )z
        Initialize early stopping object.

        Args:
            patience (int, optional): Number of epochs to wait after fitness stops improving before stopping.
        r   r   infFN)r  
best_epochr7  patiencepossible_stop)r	  rC  r   r   r   r
    s    zEarlyStopping.__init__c              
   C   s~   |du rdS || j kr"|| _|| _ || j }|| jd k| _|| jk}|rztd}t| d| j d| j d| j d |S )	a  
        Check whether to stop training.

        Args:
            epoch (int): Current epoch of training
            fitness (float): Fitness value of current epoch

        Returns:
            (bool): True if training should stop, False otherwise
        NFr%   zEarlyStopping: z:Training stopped early as no improvement observed in last z( epochs. Best results observed at epoch z@, best model saved as best.pt.
To update EarlyStopping(patience=z^) pass a new patience value, i.e. `patience=300` or use `patience=0` to disable EarlyStopping.)r  rB  rC  rD  r   r	   r2   )r	  r  Zfitnessdeltastopprefixr   r   r   __call__  s$    


zEarlyStopping.__call__N)r@  )r  r  r  r  r
  rH  r   r   r   r   r?    s   
r?  )r   )r(   r   FT)FTr   )r   )r   )r   Fr   )r   r   )r   )r   r   r   )r   F)r  r(   N)r0  N)Pr*   r:  r   rJ   r   rc   r   copyr   r   pathlibr   typingr   numpyr   r   Ztorch.distributeddistributedr   Ztorch.nnrl   Ztorch.nn.functionalZ
functionalr   r)   r   r   r	   r
   r   r   r   r   r   Zultralytics.utils.checksr   r   ImportErrorr   r!   rR   Z	TORCH_2_4ZTORCHVISION_0_10ZTORCHVISION_0_11ZTORCHVISION_0_13ZTORCHVISION_0_18r   r   r   r   boolrH   r#   r.   rb   rd   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r*  r/  r   r?  r   r   r   r   <module>   s|   ,





g




	



&B
<