o
    Wh                     @   s&  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Zd dl	m
Z
 d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZE d dlFmGZGmHZHmIZImJZJmKZKmLZL d dlMmNZNmOZOmPZP d dlQmRZRmSZSmTZTmUZUmVZVmWZW d d	lXmYZY d d
lZm[Z[ d dl\m]Z]m^Z^m_Z_m`Z`maZambZbmcZcmdZd zd dleZeW n efy   dZeY nw G dd dej
jgZhG dd dehZiG dd deiZjG dd deiZkG dd deiZlG dd dehZmG dd deiZnG dd deiZoG dd deiZpG dd depekZqG d d! d!ej
jrZse jtd8d"d#ZuG d$d% d%ZvG d&d' d'ejwZxd9d)d*Zyd:d,d-Zzd:d.d/Z{d;d0d1Z|d2d3 Z}d4d5 Z~d6d7 ZdS )<    N)deepcopy)Path)check_class_names)8AIFIC1C2C2PSAC3C3TRELAN1OBBPSASPPSPPELANSPPFA2C2fAConvADown
BottleneckBottleneckCSPC2fC2fAttnC2fCIBC2fPSAC3GhostC3k2C3xCBFuseCBLinearClassifyConcatConvConv2ConvTransposeDetectDWConvDWConvTranspose2dFocusGhostBottleneck	GhostConvHGBlockHGStemImagePoolingAttnIndexLRPCHeadPoseRepC3RepConvRepNCSPELAN4RepVGGDWResNetLayerRTDETRDecoderSCDownSegmentTorchVisionWorldDetectYOLOEDetectYOLOESegment	v10Detect)DEFAULT_CFG_DICTDEFAULT_CFG_KEYSLOGGERcolorstremojis	yaml_load)check_requirementscheck_suffix
check_yaml)E2EDetectLossv8ClassificationLossv8DetectionLoss	v8OBBLoss
v8PoseLossv8SegmentationLoss)make_divisible)feature_visualization)fuse_conv_and_bnfuse_deconv_and_bninitialize_weightsintersect_dicts
model_info	scale_imgsmart_inference_mode	time_syncc                       s   e Zd ZdZdd ZdddZd dd	Zd
d Zdd Zd!ddZ	d"ddZ
d#ddZ fddZd!ddZd$ddZdd Z  ZS )%	BaseModelz]The BaseModel class serves as a base class for all the models in the Ultralytics YOLO family.c                 O   s:   t |tr| j|g|R i |S | j|g|R i |S )a0  
        Perform forward pass of the model for either training or inference.

        If x is a dict, calculates and returns the loss for training. Otherwise, returns predictions for inference.

        Args:
            x (torch.Tensor | dict): Input tensor for inference, or dict with image tensor and labels for training.
            *args (Any): Variable length argument list.
            **kwargs (Any): Arbitrary keyword arguments.

        Returns:
            (torch.Tensor): Loss if x is a dict (training), or network predictions (inference).
        )
isinstancedictlosspredict)selfxargskwargs r_   H/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/nn/tasks.pyforwardh   s   
zBaseModel.forwardFNc                 C   s   |r|  |S | ||||S )a  
        Perform a forward pass through the network.

        Args:
            x (torch.Tensor): The input tensor to the model.
            profile (bool): Print the computation time of each layer if True.
            visualize (bool): Save the feature maps of the model if True.
            augment (bool): Augment image during prediction.
            embed (list, optional): A list of feature vectors/embeddings to return.

        Returns:
            (torch.Tensor): The last output of the model.
        )_predict_augment_predict_once)r[   r\   profile	visualizeaugmentembedr_   r_   r`   rZ   z   s   
zBaseModel.predictc                    s   g g g }}| j D ]o}|jdkr(t|jtr|j n
 fdd|jD  |r1| | | |  |j| jv r? nd |rNt |j	|j|d |rz|j|v rz|t
jj ddd |jt|krzt
jt
|ddd	  S q S )
a  
        Perform a forward pass through the network.

        Args:
            x (torch.Tensor): The input tensor to the model.
            profile (bool): Print the computation time of each layer if True.
            visualize (bool): Save the feature maps of the model if True.
            embed (list, optional): A list of feature vectors/embeddings to return.

        Returns:
            (torch.Tensor): The last output of the model.
        c                        g | ]}|d kr
 n| qS rh   r_   .0jr\   yr_   r`   
<listcomp>        z+BaseModel._predict_once.<locals>.<listcomp>Nsave_dir   ru   ru   r   dimmodelfrW   int_profile_one_layerappendisaverM   typetorchnn
functionaladaptive_avg_pool2dsqueezemaxunbindcat)r[   r\   rd   re   rg   dt
embeddingsmr_   rn   r`   rc      s    

,"zBaseModel._predict_oncec                 C   s   t | jj d | |S )zFPerform augmentations on input image x and return augmented inference.zR does not support 'augment=True' prediction. Reverting to single-scale prediction.)r?   warning	__class____name__rc   )r[   r\   r_   r_   r`   rb      s   
zBaseModel._predict_augmentc              	   C   s  || j d kot|t}tr#tj||r| n|gddd d d nd}t }tdD ]}||r5| n| q,|t | d  || j d kr[t	
d	d
ddd
ddd
d t	
|d dd|dd|jdd|j  |rt	
t|dddd
ddd
d dS dS )a8  
        Profile the computation time and FLOPs of a single layer of the model on a given input.

        Args:
            m (torch.nn.Module): The layer to be profiled.
            x (torch.Tensor): The input data to the layer.
            dt (list): A list to store the computation time of the layer.
        rh   F)inputsverboser   g    eA   
   d   z	time (ms)z>10s GFLOPsparamsz  modulez10.2f10.0f  -z  TotalN)ry   rW   listthoprd   copyrU   ranger}   r?   infonpr   sum)r[   r   r\   r   cflopst_r_   r_   r`   r|      s   	2".*zBaseModel._profile_one_layerTc                 C   s   |   sw| j D ]g}t|tttfr2t|dr2t|tr!|  t	|j
|j|_
t|d |j|_t|trMt|drMt|j|j|_t|d |j|_t|trZ|  |j|_t|trg|  |j|_t|trp|  q	| j|d | S )z
        Fuse the `Conv2d()` and `BatchNorm2d()` layers of the model into a single layer for improved computation
        efficiency.

        Returns:
            (torch.nn.Module): The fused model is returned.
        bn)r   )is_fusedry   modulesrW   r!   r"   r%   hasattr
fuse_convsrN   convr   delattrforward_fusera   r#   rO   conv_transposer1   r3   fuser<   r   )r[   r   r   r_   r_   r`   r      s.   





zBaseModel.fuser   c                    s8   t dd tjj D  t fdd|  D |k S )a?  
        Check if the model has less than a certain threshold of BatchNorm layers.

        Args:
            thresh (int, optional): The threshold number of BatchNorm layers.

        Returns:
            (bool): True if the number of BatchNorm layers in the model is less than the threshold, False otherwise.
        c                 s   s     | ]\}}d |v r|V  qdS )NormNr_   rl   kvr_   r_   r`   	<genexpr>       z%BaseModel.is_fused.<locals>.<genexpr>c                 3   s    | ]}t | V  qd S N)rW   )rl   r   r   r_   r`   r          )tupler   r   __dict__itemsr   r   )r[   threshr_   r   r`   r      s   
zBaseModel.is_fused  c                 C   s   t | |||dS )a)  
        Print model information.

        Args:
            detailed (bool): If True, prints out detailed information about the model.
            verbose (bool): If True, prints out the model information.
            imgsz (int): The size of the image that the model will be trained on.
        )detailedr   imgsz)rR   )r[   r   r   r   r_   r_   r`   r      s   	zBaseModel.infoc                    sH   t  |} | jd }t|tr"||j|_||j|_||j|_| S )a  
        Apply a function to all tensors in the model that are not parameters or registered buffers.

        Args:
            fn (function): The function to apply to the model.

        Returns:
            (BaseModel): An updated BaseModel object.
        rh   )super_applyry   rW   r$   strideanchorsstrides)r[   fnr   r   r_   r`   r      s   

zBaseModel._applyc                 C   sp   t |tr	|d n|}|  }t||  }| j|dd |r6tdt| dt| j	  d dS dS )z
        Load weights into the model.

        Args:
            weights (dict | torch.nn.Module): The pre-trained weights to be loaded.
            verbose (bool, optional): Whether to log the transfer progress.
        ry   F)strictzTransferred /z items from pretrained weightsN)
rW   rX   float
state_dictrQ   load_state_dictr?   r   lenry   )r[   weightsr   ry   csdr_   r_   r`   load  s   *zBaseModel.loadc                 C   s@   t | dddu r|  | _|du r| |d n|}| ||S )
        Compute loss.

        Args:
            batch (dict): Batch to compute loss on.
            preds (torch.Tensor | List[torch.Tensor], optional): Predictions.
        	criterionNimg)getattrinit_criterionr   ra   r[   batchpredsr_   r_   r`   rY   !  s   
zBaseModel.lossc                 C   s   t d)z0Initialize the loss criterion for the BaseModel.z4compute_loss() needs to be implemented by task heads)NotImplementedErrorr[   r_   r_   r`   r   /     zBaseModel.init_criterion)FFFN)FFNT)r   )FTr   r   )r   
__module____qualname____doc__ra   rZ   rc   rb   r|   r   r   r   r   r   rY   r   __classcell__r_   r_   r   r`   rV   e   s    



 


rV   c                       sL   e Zd ZdZd fdd	Z fdd	ZedddZdd Zdd Z	  Z
S )DetectionModelzYOLO detection model.yolo11n.yaml   NTc              
      s  t    t|tr|nt|_jd d d dkr*td djd d d< |jd< |rK|jd krKtd	jd  d
|  |jd< t	t
j||d\__dd tjd D _jdd_tjd dd_jd  t trdj _ fdd}tfdd|td|D  _ j_   ntdg_t |rň  td dS dS )aZ  
        Initialize the YOLO detection model with the given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        backboner   r   SilencezYOLOv9 `Silence` module is deprecated in favor of torch.nn.Identity. Please delete local *.pt file and re-download the latest model checkpoint.znn.IdentitychannelsncOverriding model.yaml nc=	 with nc=chr   c                 S      i | ]}|| qS r_   r_   rl   r~   r_   r_   r`   
<dictcomp>P      z+DetectionModel.__init__.<locals>.<dictcomp>inplaceTrh   end2endF   c                    s>   j r
| d S t ttttfr| d S | S )z_Perform a forward pass through the model, handling different Detect subclass types accordingly.one2manyr   )r   ra   rW   r7   r;   r/   r   )r\   )r   r[   r_   r`   _forwardZ  s   *z)DetectionModel.__init__.<locals>._forwardc                    s   g | ]	} |j d   qS ))shaperl   r\   )sr_   r`   rp   `      z+DetectionModel.__init__.<locals>.<listcomp>ru        N)r   __init__rW   rX   yaml_model_loadyamlr?   r   r   parse_modelr   ry   r   r   namesgetr   r   r   r$   r   tensorzerosr   	bias_initTensorrP   )r[   cfgr   r   r   r   r   )r   r   r[   r`   r   7  s:   





*
zDetectionModel.__init__c           
         s   t | dds| jjdkrtd | |S |jdd }g d}g d}g }t||D ],\}}t|r8|	|n||t
| j d	}t |d
 }	| |	|||}	||	 q,| |}t|ddfS )z
        Perform augmentations on input image x and return augmented inference and train outputs.

        Args:
            x (torch.Tensor): Input image tensor.

        Returns:
            (torch.Tensor): Augmented inference output.
        r   Fr   zLModel does not support 'augment=True', reverting to single-scale prediction.r   N)ru   g(\?gq=
ףp?)Nr   N)gsr   rh   )r   r   r   r?   r   rc   r   ziprS   flipr{   r   r   r   rZ   _descale_predr}   _clip_augmentedr   r   )
r[   r\   img_sizer   rz   ro   sifixiyir   r_   r`   rb   l  s   


&
zDetectionModel._predict_augmentru   c           	      C   s   | ddddf  |  < |  ddd| j| d f|\}}}}|dkr,|d | }n
|dkr6|d | }t||||f|S )a  
        De-scale predictions following augmented inference (inverse operation).

        Args:
            p (torch.Tensor): Predictions tensor.
            flips (int): Flip type (0=none, 2=ud, 3=lr).
            scale (float): Scale factor.
            img_size (tuple): Original image size (height, width).
            dim (int): Dimension to split at.

        Returns:
            (torch.Tensor): De-scaled predictions.
        N   ru   r   r   r   )splitr   r   r   )	pflipsscaler
  rw   r\   ro   whclsr_   r_   r`   r    s   &zDetectionModel._descale_predc                    s   | j d j tdd t D }d}|d jd | tdd t|D  }|d dd| f |d< |d jd | t fd	dt|D  }|d d|df |d< |S )
z
        Clip YOLO augmented inference tails.

        Args:
            y (List[torch.Tensor]): List of detection tensors.

        Returns:
            (List[torch.Tensor]): Clipped detection tensors.
        rh   c                 s       | ]}d | V  qdS r  Nr_   r   r_   r_   r`   r         z1DetectionModel._clip_augmented.<locals>.<genexpr>ru   r   c                 s   r  r  r_   r   r_   r_   r`   r     r  .Nc                 3   s     | ]}d  d |  V  qdS )r  ru   Nr_   r   nlr_   r`   r     r   )ry   r  r   r   r   )r[   ro   ger~   r_   r  r`   r	    s   
(,zDetectionModel._clip_augmentedc                 C   s   t | ddr
t| S t| S )z5Initialize the loss criterion for the DetectionModel.r   F)r   rF   rH   r   r_   r_   r`   r     s   zDetectionModel.init_criterion)r   r   NT)ru   )r   r   r   r   r   rb   staticmethodr  r	  r   r   r_   r_   r   r`   r   4  s    5r   c                       *   e Zd ZdZd
 fdd	Zdd	 Z  ZS )OBBModelz'YOLO Oriented Bounding Box (OBB) model.yolo11n-obb.yamlr   NTc                       t  j||||d dS )aL  
        Initialize YOLO OBB model with given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        r  r   r   r   Nr   r   r[   r  r   r   r   r   r_   r`   r        
zOBBModel.__init__c                 C      t | S )z,Initialize the loss criterion for the model.)rI   r   r_   r_   r`   r     r   zOBBModel.init_criterion)r   r   NTr   r   r   r   r   r   r   r_   r_   r   r`   r        r  c                       r  )SegmentationModelzYOLO segmentation model.yolo11n-seg.yamlr   NTc                    r!  )aW  
        Initialize YOLOv8 segmentation model with given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        r"  Nr#  r$  r   r_   r`   r     r%  zSegmentationModel.__init__c                 C   r&  )z8Initialize the loss criterion for the SegmentationModel.)rK   r   r_   r_   r`   r     r   z SegmentationModel.init_criterion)r*  r   NTr'  r_   r_   r   r`   r)    r(  r)  c                       s*   e Zd ZdZd fdd	Zd	d
 Z  ZS )	PoseModelzYOLO pose model.yolo11n-pose.yamlr   NNNTc                    sh   t |ts	t|}t|r(t|t|d kr(td|d  d|  ||d< t j||||d dS )ak  
        Initialize YOLOv8 Pose model.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            data_kpt_shape (tuple): Shape of keypoints data.
            verbose (bool): Whether to display model information.
        	kpt_shapez Overriding model.yaml kpt_shape=z with kpt_shape=r"  N)	rW   rX   r   anyr   r?   r   r   r   )r[   r  r   r   data_kpt_shaper   r   r_   r`   r     s   
zPoseModel.__init__c                 C   r&  )z0Initialize the loss criterion for the PoseModel.)rJ   r   r_   r_   r`   r     r   zPoseModel.init_criterion)r,  r   Nr-  Tr'  r_   r_   r   r`   r+    s    r+  c                       s>   e Zd ZdZd fdd	Zdd	 Zed
d Zdd Z  Z	S )ClassificationModelzYOLO classification model.yolo11n-cls.yamlr   NTc                    s   t    | |||| dS )ae  
        Initialize ClassificationModel with YAML, channels, number of classes, verbose flag.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        N)r   r   
_from_yamlr$  r   r_   r`   r     s   

zClassificationModel.__init__c                 C   s   t |tr|nt|| _| jd| }| jd< |r5|| jd kr5td| jd  d|  || jd< n|sB| jddsBtdtt	| j||d\| _
| _tdg| _d	d
 t| jd D | _|   dS )aS  
        Set YOLOv8 model configurations and define the model architecture.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        r   r   r   r   NzFnc not specified. Must specify nc in model.yaml or function arguments.r   ru   c                 S   r   r_   r_   r   r_   r_   r`   r     r   z2ClassificationModel._from_yaml.<locals>.<dictcomp>)rW   rX   r   r   r   r?   r   
ValueErrorr   r   ry   r   r   r  r   r   r   r$  r_   r_   r`   r3    s   
zClassificationModel._from_yamlc                 C   s  t t| dr	| jn|  d \}}t|tr+|jj|kr)tj	
|jj||_dS dS t|tj	j
rG|j|krEt| |tj	
|j| dS dS t|tj	jrdd |D }tj	j
|v rt|d |ddd tj	j
 }|| j|krtj	
|| j|||< dS dS tj	j|v rt|d |ddd tj	j }|| j|krtj	j|| j||| j|| j|| jdud||< dS dS dS dS )z
        Update a TorchVision classification model to class count 'n' if required.

        Args:
            model (torch.nn.Module): Model to update.
            nc (int): New number of classes.
        ry   rh   c                 S   s   g | ]}t |qS r_   )r   r   r_   r_   r`   rp   /  r   z7ClassificationModel.reshape_outputs.<locals>.<listcomp>ru   N)bias)r   r   ry   named_childrenrW   r   linearout_featuresr   r   Linearin_featuressetattr
Sequentialr   indexConv2dout_channelsin_channelskernel_sizer   r5  )ry   r   namer   typesr~   r_   r_   r`   reshape_outputs  s2   $	

$$&z#ClassificationModel.reshape_outputsc                 C   s   t  S )z:Initialize the loss criterion for the ClassificationModel.)rG   r   r_   r_   r`   r   ;  s   z"ClassificationModel.init_criterion)r2  r   NT)
r   r   r   r   r   r3  r  rD  r   r   r_   r_   r   r`   r1    s    
r1  c                       s>   e Zd ZdZd fdd	Zdd	 Zdd
dZdddZ  ZS )RTDETRDetectionModelaR  
    RTDETR (Real-time DEtection and Tracking using Transformers) Detection Model class.

    This class is responsible for constructing the RTDETR architecture, defining loss functions, and facilitating both
    the training and inference processes. RTDETR is an object detection and tracking model that extends from the
    DetectionModel base class.

    Methods:
        init_criterion: Initializes the criterion used for loss calculation.
        loss: Computes and returns the loss during training.
        predict: Performs a forward pass through the network and returns the output.
    rtdetr-l.yamlr   NTc                    r!  )a7  
        Initialize the RTDETRDetectionModel.

        Args:
            cfg (str | dict): Configuration file name or path.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Print additional information during initialization.
        r"  Nr#  r$  r   r_   r`   r   N  r%  zRTDETRDetectionModel.__init__c                 C   s   ddl m} || jddS )z;Initialize the loss criterion for the RTDETRDetectionModel.r   )RTDETRDetectionLossT)r   use_vfl)ultralytics.models.utils.lossrG  r   )r[   rG  r_   r_   r`   r   Z  s   z#RTDETRDetectionModel.init_criterionc                    sl  t | ds
|  | _|d }t|}|d   fddt|D }|d j|jtjd	d|d	 j|jd
 j|jtjd	d|d}|du rP| j
||dn|}| jrW|n|d \}}}	}
}|du rid\}}ntj||d dd\}}tj||d dd\}}t|	d|g}t|
d|g}| j||f||||dt tjfdddD |jd
fS )aK  
        Compute the loss for the given batch of data.

        Args:
            batch (dict): Dictionary containing image and label data.
            preds (torch.Tensor, optional): Precomputed model predictions.

        Returns:
            (tuple): A tuple containing the total loss and main three losses in a tensor.
        r   r   	batch_idxc                    s   g | ]
} |k   qS r_   )r   itemr   )rJ  r_   r`   rp   r  s    z-RTDETRDetectionModel.loss.<locals>.<listcomp>r  )dtyperh   bboxesdevice)r  rM  rJ  	gt_groupsN)r   ru   r-  dn_num_splitr   rv   r   )	dn_bboxes	dn_scoresdn_metac                    s   g | ]} |   qS r_   )detach)rl   r   )rY   r_   r`   rp     s    )	loss_giou
loss_class	loss_bbox)r   r   r   r   r   torO  r   longviewrZ   trainingr  r   	unsqueezer   values	as_tensor)r[   r   r   r   bsrP  targets
dec_bboxes
dec_scores
enc_bboxes
enc_scoresrT  rR  rS  r_   )rJ  rY   r`   rY   `  s2   


zRTDETRDetectionModel.lossFc                    s&  g g g }}| j dd D ]o}	|	jdkr,t|	jtr!|	j n
 fdd|	jD  |r5| |	 | |	  |	j| jv rC nd |rRt |	j	|	j|d |r~|	j|v r~|t
jj ddd |	jt|kr~t
jt
|ddd	  S q| j d }
|
fd
d|
jD |  S )aH  
        Perform a forward pass through the model.

        Args:
            x (torch.Tensor): The input tensor.
            profile (bool): If True, profile the computation time for each layer.
            visualize (bool): If True, save feature maps for visualization.
            batch (dict, optional): Ground truth data for evaluation.
            augment (bool): If True, perform data augmentation during inference.
            embed (list, optional): A list of feature vectors/embeddings to return.

        Returns:
            (torch.Tensor): Model's output tensor.
        Nrh   c                    ri   rj   r_   rk   rn   r_   r`   rp     rq   z0RTDETRDetectionModel.predict.<locals>.<listcomp>rr   rt   ru   r   rv   c                       g | ]} | qS r_   r_   rk   )ro   r_   r`   rp     r   rx   )r[   r\   rd   re   r   rf   rg   r   r   r   headr_   rn   r`   rZ     s$   
,"
zRTDETRDetectionModel.predict)rF  r   NTr   FFNFN)	r   r   r   r   r   r   rY   rZ   r   r_   r_   r   r`   rE  @  s    
-rE  c                       s@   e Zd ZdZd fdd	Zdd	d
ZdddZdddZ  ZS )
WorldModelzYOLOv8 World Model.yolov8s-world.yamlr   NTc                    s2   t d|pdd| _d| _t j||||d dS )aP  
        Initialize YOLOv8 world model with given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        ru   P      Nr"  )r   randn	txt_feats
clip_modelr   r   r$  r   r_   r`   r     s   
zWorldModel.__init__rk  c                    s  zddl }W n ty   td ddl }Y nw t| dds(|r(|dd | _|r-| jn|dd  t  j}|	|
|} fdd||D }t|dkrY|d ntj|dd	}||jd
ddd }|dt||jd | _t|| jd _dS )a-  
        Set classes in advance so that model could do offline-inference without clip model.

        Args:
            text (List[str]): List of class names.
            batch (int): Batch size for processing text tokens.
            cache_clip_model (bool): Whether to cache the CLIP model.
        r   Nz+git+https://github.com/ultralytics/CLIP.gitro  zViT-B/32c                       g | ]	}  | qS r_   encode_textrU  rl   tokenry   r_   r`   rp     r   z*WorldModel.set_classes.<locals>.<listcomp>ru   rv   r   rh   T)r  rw   keepdim)clipImportErrorrC   r   r   ro  next
parametersrO  tokenizerY  r  r   r   r   normreshaper   rn  ry   r   )r[   textr   cache_clip_modelrw  rO  
text_tokenrn  r_   ru  r`   set_classes  s&   	
"zWorldModel.set_classesFc                    s  |du r| j n|j j jd}t|t ks| jd jr(| jd dd}|	 }g g g }}	| jD ]}
|
j
dkrTt|
j
trI|
j
 n
 fdd|
j
D  |r]| |
 | t|
trh|
 | nt|
trs|
 | nt|
tr~|
 |}n|
  |
j| jv r nd |rt |
j|
j|d |r|
j|v r|	tjj ddd |
jt|krtjt|	d	dd
  S q7 S )a[  
        Perform a forward pass through the model.

        Args:
            x (torch.Tensor): The input tensor.
            profile (bool): If True, profile the computation time for each layer.
            visualize (bool): If True, save feature maps for visualization.
            txt_feats (torch.Tensor, optional): The text features, use it if it's given.
            augment (bool): If True, perform data augmentation during inference.
            embed (list, optional): A list of feature vectors/embeddings to return.

        Returns:
            (torch.Tensor): Model's output tensor.
        NrO  rL  rh   r   c                    ri   rj   r_   rk   rn   r_   r`   rp     rq   z&WorldModel.predict.<locals>.<listcomp>rr   rt   ru   rv   )rn  rY  rO  rL  r   ry   exportexpandr   clonerz   rW   r{   r|   r   r9   r,   r}   r~   r   rM   r   r   r   r   r   r   r   r   r   )r[   r\   rd   re   rn  rf   rg   ori_txt_featsr   r   r   r_   rn   r`   rZ     s4    

,


"zWorldModel.predictc                 C   s>   t | ds
|  | _|du r| j|d |d d}| ||S )r   r   Nr   rn  )rn  )r   r   r   ra   r   r_   r_   r`   rY     s
   

zWorldModel.loss)rj  r   NT)rk  Trh  r   )	r   r   r   r   r   r  rZ   rY   r   r_   r_   r   r`   ri    s    

+ri  c                       sv   e Zd ZdZd fdd	Ze dd
dZe dd Zdd Zdd Z	dd Z
dd Z		dddZdddZ  ZS )
YOLOEModelzYOLOE detection model.yoloe-v8s.yamlr   NTc                    r!  )aI  
        Initialize YOLOE model with given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        r"  Nr#  r$  r   r_   r`   r     r%  zYOLOEModel.__init__rk  Fc           
         s   ddl m} t| j j}t| dds|r|d|d| _|r"| jn|d|d  |} fdd|	|D }t
|d	krC|d ntj|dd
}|dt
||jd }|rZ|S | jr_J | jd }	t|	tskJ |	|S )a  
        Set classes in advance so that model could do offline-inference without clip model.

        Args:
            text (List[str]): List of class names.
            batch (int): Batch size for processing text tokens.
            cache_clip_model (bool): Whether to cache the CLIP model.
            without_reprta (bool): Whether to return text embeddings cooperated with reprta module.

        Returns:
            (torch.Tensor): Text positional embeddings.
        r   )build_text_modelro  Nzmobileclip:bltrN  c                    rp  r_   rq  rs  ru  r_   r`   rp   =  r   z*YOLOEModel.get_text_pe.<locals>.<listcomp>ru   rv   rh   )ultralytics.nn.text_modelr  ry  ry   rz  rO  r   ro  r{  r  r   r   r   r}  r   r\  rW   r:   get_tpe)
r[   r~  r   r  without_reprtar  rO  r  rn  rg  r_   ru  r`   get_text_pe&  s   
"


zYOLOEModel.get_text_pec                 C   s   | ||ddS )z
        Get visual embeddings.

        Args:
            img (torch.Tensor): Input image tensor.
            visual (torch.Tensor): Visual features.

        Returns:
            (torch.Tensor): Visual positional embeddings.
        T)vpe
return_vper_   )r[   r   visualr_   r_   r`   get_visual_peH  s   zYOLOEModel.get_visual_pec                 C   s   | j rJ | jd }t|tsJ t|  j}| tdd| j	d | j	d 
| tdd tt||j|jD | jd _t|j|jD ]\}}t|tjsTJ t|tjs\J |d= |d= qHt|| jd _t|| _dS )z
        Set vocabulary for the prompt-free model.

        Args:
            vocab (nn.ModuleList): List of vocabulary items.
            names (List[str]): List of class names.
        rh   ru   r   r   c                 s   s6    | ]\}\}}}t ||d  |d  |dkdV  qdS )rh   r   )enabledN)r.   )rl   r~   r  pflocr_   r_   r`   r   g  s
    
z'YOLOEModel.set_vocab.<locals>.<genexpr>N)r\  ry   rW   r:   ry  rz  rO  r   emptyr]   rY  r   
ModuleList	enumerater  cv3cv2lrpcr<  r   r   r   r   )r[   vocabr   rg  rO  loc_headcls_headr_   r_   r`   	set_vocabV  s   

&
zYOLOEModel.set_vocabc                 C   s   | j rJ | jd }t|tsJ |jrJ | |}| || t| j j	}|
| j| t }|jD ]}t|tjsCJ ||d  q9|S )z
        Get fused vocabulary layer from the model.

        Args:
            names (list): List of class names.

        Returns:
            (nn.ModuleList): List of vocabulary modules.
        rh   )r\  ry   rW   r:   r   r  r  ry  rz  rO  r   perY  r   r  r  r<  r}   )r[   r   rg  tperO  r  r  r_   r_   r`   	get_vocabs  s   





zYOLOEModel.get_vocabc                 C   sJ   t | jd drJ d|jdksJ || _t|| jd _t|| _dS )z
        Set classes in advance so that model could do offline-inference without clip model.

        Args:
            names (List[str]): List of class names.
            embeddings (torch.Tensor): Embeddings tensor.
        rh   r  z^Prompt-free model does not support setting classes. Please try with Text/Visual prompt models.r   N)r   ry   ndimr  r   r   r   r   )r[   r   r   r_   r_   r`   r    s   zYOLOEModel.set_classesc              
   C   sr   g }|dur|j dksJ || |dur"|j dksJ || |s2|t| dtddd tj|ddS )a  
        Get class positional embeddings.

        Args:
            tpe (torch.Tensor, optional): Text positional embeddings.
            vpe (torch.Tensor, optional): Visual positional embeddings.

        Returns:
            (torch.Tensor): Class positional embeddings.
        Nr   r  ru   rk  rl  rv   )r  r}   r   r   r  r   )r[   r  r  all_per_   r_   r`   
get_cls_pe  s   

zYOLOEModel.get_cls_pec	                    s  g g g }	}
 j d }| jD ]}|jdkr-t|jtr"|j n
 fdd|jD  |r6| | |	 t|tr|durE| |nd}|rX|dusOJ | jrTJ |  S | 	|
||j d j d jd}|j d |ksv|jr}||dd}| | n|  |j| jv r nd |rt |j|j|d |r|j|v r|
tjj ddd |jt|krtjt|
d	dd
  S q S )a  
        Perform a forward pass through the model.

        Args:
            x (torch.Tensor): The input tensor.
            profile (bool): If True, profile the computation time for each layer.
            visualize (bool): If True, save feature maps for visualization.
            tpe (torch.Tensor, optional): Text positional embeddings.
            augment (bool): If True, perform data augmentation during inference.
            embed (list, optional): A list of feature vectors/embeddings to return.
            vpe (torch.Tensor, optional): Visual positional embeddings.
            return_vpe (bool): If True, return visual positional embeddings.

        Returns:
            (torch.Tensor): Model's output tensor.
        r   rh   c                    ri   rj   r_   rk   rn   r_   r`   rp     rq   z&YOLOEModel.predict.<locals>.<listcomp>Nr  rr   rt   ru   rv   )r   ry   rz   rW   r{   r|   r:   get_vper\  r  r  rY  rO  rL  r  r  r}   r~   r   rM   r   r   r   r   r   r   r   r   r   )r[   r\   rd   re   r  rf   rg   r  r  r   r   br   cls_per_   rn   r`   rZ     s6   


,

("zYOLOEModel.predictc                 C   t   t | dsddlm} |dddu}|r|| n|  | _|du r4| j|d |dd|ddd}| ||S )	r   r   r   )TVPDetectLossvisualsNr   rn  r  r  )r   ultralytics.utils.lossr  r   r   r   ra   )r[   r   r   r  visual_promptr_   r_   r`   rY        
$zYOLOEModel.loss)r  r   NT)rk  FF)FFNFNNFr   )r   r   r   r   r   rT   r  r  r  r  r  r  rZ   rY   r   r_   r_   r   r`   r    s    !

0r  c                       s,   e Zd ZdZd
 fdd	Zddd	Z  ZS )YOLOESegModelzYOLOE segmentation model.yoloe-v8s-seg.yamlr   NTc                    r!  )aV  
        Initialize YOLOE segmentation model with given config and parameters.

        Args:
            cfg (str | dict): Model configuration file path or dictionary.
            ch (int): Number of input channels.
            nc (int, optional): Number of classes.
            verbose (bool): Whether to display model information.
        r"  Nr#  r$  r   r_   r`   r     r%  zYOLOESegModel.__init__c                 C   r  )	r   r   r   )TVPSegmentLossr  Nr   rn  r  )r   r  r  r   r   r   ra   )r[   r   r   r  r  r_   r_   r`   rY     r  zYOLOESegModel.loss)r  r   NTr   )r   r   r   r   r   rY   r   r_   r_   r   r`   r    s    r  c                       s*   e Zd ZdZ fddZdddZ  ZS )EnsemblezEnsemble of models.c                    s   t    dS )z!Initialize an ensemble of models.Nr#  r   r   r_   r`   r     s   zEnsemble.__init__Fc                    s,    fdd| D }t |d}|dfS )a  
        Generate the YOLO network's final layer.

        Args:
            x (torch.Tensor): Input tensor.
            augment (bool): Whether to augment the input.
            profile (bool): Whether to profile the model.
            visualize (bool): Whether to visualize the features.

        Returns:
            (tuple): Tuple containing the concatenated predictions and None.
        c                    s   g | ]}| d  qS )r   r_   )rl   modulerf   rd   re   r\   r_   r`   rp   ,      z$Ensemble.forward.<locals>.<listcomp>r   N)r   r   )r[   r\   rf   rd   re   ro   r_   r  r`   ra     s   zEnsemble.forward)FFF)r   r   r   r   r   ra   r   r_   r_   r   r`   r    s    r  c           
   	   c   s    | du ri } |du ri }ddl }ddlm} zJ| D ]!\}}|dd\}}|dd\}}	t|||t|||	 q|  D ]\}}|||j|< qBdV  W | D ]}||jv r_|j|= qTdS | D ]}||jv ro|j|= qdw )a  
    Context manager for temporarily adding or modifying modules in Python's module cache (`sys.modules`).

    This function can be used to change the module paths during runtime. It's useful when refactoring code,
    where you've moved a module from one location to another, but you still want to support the old import
    paths for backwards compatibility.

    Args:
        modules (dict, optional): A dictionary mapping old module paths to new module paths.
        attributes (dict, optional): A dictionary mapping old module attributes to new module attributes.

    Examples:
        >>> with temporary_modules({"old.module": "new.module"}, {"old.module.attribute": "new.module.attribute"}):
        >>> import old.module  # this will now import new.module
        >>> from old.module import attribute  # this will now import new.module.attribute

    Note:
        The changes are only in effect inside the context manager and are undone once the context manager exits.
        Be aware that directly manipulating `sys.modules` can lead to unpredictable results, especially in larger
        applications or libraries. Use this function with caution.
    Nr   )import_module.ru   )sys	importlibr  r   rsplitr;  r   r   )
r   
attributesr  r  oldnew
old_moduleold_attr
new_modulenew_attrr_   r_   r`   temporary_modules6  s0   

r  c                   @   s    e Zd ZdZdd Zdd ZdS )	SafeClasszAA placeholder class to replace unknown classes during unpickling.c                 O      dS )z6Initialize SafeClass instance, ignoring all arguments.Nr_   r[   r]   r^   r_   r_   r`   r   j     zSafeClass.__init__c                 O   r  )z/Run SafeClass instance, ignoring all arguments.Nr_   r  r_   r_   r`   __call__n  r  zSafeClass.__call__N)r   r   r   r   r   r  r_   r_   r_   r`   r  g  s    r  c                       s    e Zd ZdZ fddZ  ZS )SafeUnpicklerz>Custom Unpickler that replaces unknown classes with SafeClass.c                    s   d}||v rt  ||S tS )z
        Attempt to find a class, returning SafeClass if not among safe modules.

        Args:
            module (str): Module name.
            name (str): Class name.

        Returns:
            (type): Found class or SafeClass.
        )r   collectionszcollections.abcbuiltinsmathnumpy)r   
find_classr  )r[   r  rB  safe_modulesr   r_   r`   r  v  s   	zSafeUnpickler.find_class)r   r   r   r   r  r   r_   r_   r   r`   r  s  s    r  Fc                 C   sr  ddl m} t| dd || }zStddddd	d
ddd; |rLtd}t|_dd |_t	|d}t
j||d}W d   n1 sFw   Y  nt
j|dd}W d   n1 s]w   Y  W n> ty } z2|jdkr{ttd|  d|t|  d|j d|j d t|j t
j|dd}W Y d}~nd}~ww t|tstd|  d d|ji}||fS )a  
    Attempts to load a PyTorch model with the torch.load() function. If a ModuleNotFoundError is raised, it catches the
    error, logs a warning message, and attempts to install the missing module via the check_requirements() function.
    After installation, the function again attempts to load the model using torch.load().

    Args:
        weight (str): The file path of the PyTorch model.
        safe_only (bool): If True, replace unknown classes with SafeClass during loading.

    Returns:
        ckpt (dict): The loaded model checkpoint.
        file (str): The loaded filename.

    Examples:
        >>> from ultralytics.nn.tasks import torch_safe_load
        >>> ckpt, file = torch_safe_load("path/to/best.pt", safe_only=True)
    r   )attempt_download_assetz.pt)filesuffixzultralytics.utilszultralytics.models.yolozultralytics.data)zultralytics.yolo.utilszultralytics.yolo.v8zultralytics.yolo.dataztorch.nn.Identityz#ultralytics.nn.tasks.DetectionModelz$ultralytics.utils.loss.E2EDetectLoss)z$ultralytics.nn.modules.block.Silencez*ultralytics.nn.tasks.YOLOv10DetectionModelz$ultralytics.utils.loss.v10DetectLoss)r   r  safe_picklec                 S   s   t |  S r   )r  r   )file_objr_   r_   r`   <lambda>  s    z!torch_safe_load.<locals>.<lambda>rb)pickle_moduleNcpu)map_locationmodelsu   ERROR ❌️ aw   appears to be an Ultralytics YOLOv5 model originally trained with https://github.com/ultralytics/yolov5.
This model is NOT forwards compatible with YOLOv8 at https://github.com/ultralytics/ultralytics.
Recommend fixes are to train a new model using the latest 'ultralytics' package or to run a command with an official Ultralytics model, i.e. 'yolo predict model=yolo11n.pt'z appears to require 'zK', which is not in Ultralytics requirements.
AutoInstall will run now for 'z' but this feature will be removed in the future.
Recommend fixes are to train a new model using the latest 'ultralytics' package or to run a command with an official Ultralytics model, i.e. 'yolo predict model=yolo11n.pt'z
The file 'z' appears to be improperly saved or formatted. For optimal results, use model.save('filename.pt') to correctly save YOLO models.ry   )ultralytics.utils.downloadsr  rD   r  rC  
ModuleTyper  	Unpicklerr   openr   ModuleNotFoundErrorrB  	TypeErrorrA   r?   r   rC   rW   rX   ry   )weight	safe_onlyr  r  r  rz   ckptr  r_   r_   r`   torch_safe_load  sd   



	



r  Tc           
         s  t   t| tr
| n| gD ]O}t|\}}d|v r!i t|d nd}|dp+|d | }||_||_	t
||_t|dsHtdg|_ |rWt|drW|  n|  q  D ]}t|drl||_qat|tjjr{t|d	s{d|_qat d
kr d S td|  d dD ]}	t |	t d |	 q tttdd  D  j _t fdd D sJ ddd  D   S )a  
    Load an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a.

    Args:
        weights (str | List[str]): Model weights path(s).
        device (torch.device, optional): Device to load model to.
        inplace (bool): Whether to do inplace operations.
        fuse (bool): Whether to fuse model.

    Returns:
        (torch.nn.Module): Loaded model.
    
train_argsNemary   r         @@r   r   recompute_scale_factorru   rh   zEnsemble created with 
)r   r   r   r   c                 S   s   g | ]}|j  qS r_   )r   r   rl   r   r_   r_   r`   rp     s    z(attempt_load_weights.<locals>.<listcomp>c                 3   s     | ]} d  j |j kV  qdS )r   Nr   r  ensembler_   r`   r   	  r   z'attempt_load_weights.<locals>.<genexpr>zModels differ in class counts c                 S   s   g | ]}|j qS r_   r  r  r_   r_   r`   rp   	  s    ) r  rW   r   r  r=   r   rY  r   r]   pt_pathguess_model_tasktaskr   r   r   r   r}   r   evalr   r   r   Upsampler  r   r?   r   r;  r   r{   argmaxall)
r   rO  r   r   wr  r]   ry   r   r   r_   r  r`   attempt_load_weights  s2   

*
&.r  c                 C   s   t | \}} i t|di }|dp|d | }dd | D |_| |_t||_	t
|ds<tdg|_|rIt
|drI|  n| }| D ]}t
|d	r\||_qQt|tjjrkt
|d
skd|_qQ||fS )aL  
    Load a single model weights.

    Args:
        weight (str): Model weight path.
        device (torch.device, optional): Device to load model to.
        inplace (bool): Whether to do inplace operations.
        fuse (bool): Whether to fuse model.

    Returns:
        (tuple): Tuple containing the model and checkpoint.
    r  r  ry   c                 S   s   i | ]\}}|t v r||qS r_   )r>   r   r_   r_   r`   r     r  z+attempt_load_one_weight.<locals>.<dictcomp>r   r  r   r   r  N)r  r=   r   rY  r   r   r]   r  r  r  r   r   r   r   r   r  r   r   rW   r   r  r  )r  rO  r   r   r  r]   ry   r   r_   r_   r`   attempt_load_one_weight  s    

"
r  c                    s  ddl }d}td}fdddD \}}}fddd	D \}	}
}|rCd
}|s<t| d }td| d || \}	}
}|rXt|t_	|rXt
td d|  |rst
dddddddddddddd gg g d }}}th ttttttttttttttttttttt t!t"t#t$t%t&j'j(t)t*t+t,t-t.t/}ttttttt"t#t$t%t*t+tt.tt/h}t0d d  D ]\\}} dv rt1t&j'd d nd!v rt1t2d"j3d#d nt4  t0 D ]3\}}t5|t6rJt78t9 |t: v r3t: | n|;| |< W d   n	1 sEw   Y  q|d$krZt<t=||	 d$n| }}|v r|  d }}||kr{t>t?|||
 d%}t"u rt>t? d$ |d& |
 d% d$< t@ d& d$krt<t=t? d& |d& d' |
 d$n d&  d&< ||g d$d  |v rȈ Ad&| d$}tu rd(}|d)v rd d < t/u rd(}|d*v r Bd+ t.u rd(}ntCu r| g  nttDtEhv r/|  d  d$ }}}|||g d&d  tEu r. Ad,| d$}nˈtFu rD d  r= d$ n d$ d, }nt&j'jGu rQ| g ntHu rbtIfd-d|D }nttJtKtLtMtNtOtPtQtRh	v r Sfd.d/|D  tMu stNu rt>t? d& ||
 d% d&< tJtLtMtNtOtPhv r|_TnUtUu r Ad$fd0d/|D  nBtVu rЈ d }| }||g d$d  n*tWu r܈|d  }nttXtYhv r d }| }g  d$d  n| }|d$krt&j'jZ fd1dt[|D  n  }t6d%d2 \d3d}tId4d |] D |_^|||__|_`|_a|rVt
dt6|d|d|j^d5d|dt6 d |Bfd6dt5|t@rf|gn|D  |S| dkrwg S| qt&j'jZ| tb|fS )7a/  
    Parse a YOLO model.yaml dictionary into a PyTorch model.

    Args:
        d (dict): Model dictionary.
        ch (int): Input channels.
        verbose (bool): Whether to print model details.

    Returns:
        (tuple): Tuple containing the PyTorch model and sorted list of output layers.
    r   NTinfc                 3   s    | ]}  |V  qd S r   r   r   dr_   r`   r   C  r   zparse_model.<locals>.<genexpr>)r   
activationscalesc                 3   s    | ]	}  |d V  qdS )g      ?Nr  r   r  r_   r`   r   D  s    )depth_multiplewidth_multipler.  r  z'no model scale passed. Assuming scale='z'.zactivation:r   r  r   z>3fromz>20nr   z>10r   r  z<45	argumentsz<30rh   r   rg  znn.r   ztorchvision.ops.torchvision   ru      r   r   Fmlxlx)Tg333333?r  c                 3   s    | ]} | V  qd S r   r_   r   r   r_   r`   r     r  c                    rf  r_   r_   r   r  r_   r`   rp     r   zparse_model.<locals>.<listcomp>c                    rf  r_   r_   r   r  r_   r`   rp     r   c                 3   s    | ]}  V  qd S r   r_   )rl   r   )r]   r   r_   r`   r     r  r   z	__main__.c                 s   s    | ]}|  V  qd S r   )numelr   r_   r_   r`   r     r  r   c                 3   s     | ]}|d kr|  V  qdS )rh   Nr_   r   )r~   r_   r`   r     r   )castr   r   r   keysr?   r   r  r!   default_actr   r@   	frozensetr   r#   r)   r   r(   r   r   r   r   r%   r'   r   r   r   r   r   r2   r   r   r   r   r   r	   r
   r   r   r   ConvTranspose2dr&   r   r0   r   r6   r   r   r  r   
__import__opsglobalsrW   str
contextlibsuppressr4  localsliteral_evalr   roundrL   minr{   insertextendr   r+   r*   r4   BatchNorm2dr    r   r$   r9   r:   r7   r;   r/   r   r,   r<   r}   legacyr5   r   r   r8   r-   r<  r   replacerz  r   r~   rz   r   sorted)r  r   r   r	  r  max_channelsr   actr  depthwidthr.  r  layersr   c2base_modulesrepeat_modulesrz   r   rm   an_c1cmm_r   r_   )r]   r   r  r~   r   r`   r   2  s  

2	
 !"&&
&$


 B









 



2:*

r   c                 C   s   t | } | jdd dD v r+tdd| j}td| j d| d | || j } td	d
t| }t	|ddp=t	| }t
|}t| |d< t| |d< |S )z
    Load a YOLOv8 model from a YAML file.

    Args:
        path (str | Path): Path to the YAML file.

    Returns:
        (dict): Model dictionary.
    c                 s   s*    | ]}d D ]}d| | dV  qqdS ))   r  yolov6Nr_   )rl   r\   r  r_   r_   r`   r     s   ( z"yaml_model_load.<locals>.<genexpr>nsmlxz(\d+)([nslmx])6(.+)?$z	\1\2-p6\3z8Ultralytics YOLO P6 models now use -p6 suffix. Renaming z to r  z(\d+)([nslmx])(.+)?$z\1\3F)hardr  	yaml_file)r   stemresubr?   r   	with_namer  r  rE   rB   guess_model_scale)pathnew_stemunified_pathr0  r  r_   r_   r`   r     s   
r   c                 C   s0   zt dt| jdW S  ty   Y dS w )a  
    Extract the size character n, s, m, l, or x of the model's scale from the model path.

    Args:
        model_path (str | Path): The path to the YOLO model's YAML file.

    Returns:
        (str): The size character of the model's scale (n, s, m, l, or x).
    zyolo(e-)?[v]?\d+([nslmx])r   r   )r2  searchr   r1  groupAttributeError)
model_pathr_   r_   r`   r5    s
   
r5  c              	   C   s  dd }t | tr"tt || W  d   S 1 sw   Y  t | tjjrdD ]}tt t|d W  d     S 1 sEw   Y  q+dD ]}tt |t|W  d     S 1 sgw   Y  qM| 	 D ]0}t |t
tfr} dS t |tr dS t |tr d	S t |tr d
S t |ttttfr dS qqt | ttfrt| } d| jv sd| jv rdS d| jv sd| jv rdS d| jv sd	| jv rd	S d| jv sd
| jv rd
S d| jv rdS td dS )a  
    Guess the task of a PyTorch model from its architecture or configuration.

    Args:
        model (torch.nn.Module | dict): PyTorch model or model configuration in YAML format.

    Returns:
        (str): Task of the model ('detect', 'segment', 'classify', 'pose', 'obb').
    c                 S   sT   | d d d   }|dv rdS d|v rdS d|v rdS |dkr"dS |d	kr(d	S d
S )zGuess from YAML dictionary.rg  rh   r   >   fcr  classify
classifierr>  detectsegmentposeobbN)lower)r  r   r_   r_   r`   cfg2task  s   z"guess_model_task.<locals>.cfg2taskN)z
model.argszmodel.model.argszmodel.model.model.argsr  )z
model.yamlzmodel.model.yamlzmodel.model.model.yamlrA  r>  rB  rC  r@  z-segz-clsz-posez-obbzUnable to automatically guess model task, assuming 'task=detect'. Explicitly define task for your model, i.e. 'task=detect', 'segment', 'classify','pose' or 'obb'.)rW   rX   r  r  	Exceptionr   r   Moduler  r   r7   r;   r   r/   r   r$   r9   r:   r<   r  r   r1  partsr?   r   )ry   rE  r\   r   r_   r_   r`   r    sT   
 
&
&



r  r-  )F)NTFr   )r  pickler2  rC  r   r   pathlibr   r   torch.nnr   ultralytics.nn.autobackendr   ultralytics.nn.modulesr   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    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   ultralytics.utilsr=   r>   r?   r@   rA   rB   ultralytics.utils.checksrC   rD   rE   r  rF   rG   rH   rI   rJ   rK   ultralytics.utils.opsrL   ultralytics.utils.plottingrM   ultralytics.utils.torch_utilsrN   rO   rP   rQ   rR   rS   rT   rU   r   rx  rG  rV   r   r  r)  r+  r1  rE  ri  r  r  r  r  contextmanagerr  r  r  r  r  r  r  r   r   r5  r  r_   r_   r_   r`   <module>   s^    : ( P Joh `"0

L
1
% 3