§
    l¬¸jôh  ã                  ó  — 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Z d dlm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 d dl m!Z!m"Z"  G d„ de¦  «        Z# G d„ de¦  «        Z$ G d„ de¦  «        Z%dS )é    )Úannotations)ÚPath)ÚAnyN)Úget_cfg)Úload_inference_source)ÚModel)Úyolo)Úcheck_class_names)ÚBaseBackend)
ÚClassificationModelÚ
DepthModelÚDetectionModelÚOBBModelÚ	PoseModelÚSegmentationModelÚSemanticSegmentationModelÚ
WorldModelÚ
YOLOEModelÚYOLOESegModel)ÚROOTÚYAMLc                  ó>   ‡ — e Zd ZdZddˆ fd„Zedd„¦   «         Zˆ xZS )ÚYOLOa\  YOLO (You Only Look Once) object detection model.

    This class provides a unified interface for YOLO models, automatically switching to specialized model types
    (YOLOWorld or YOLOE) based on the model filename. It supports various computer vision tasks including object
    detection, instance segmentation, semantic segmentation, depth estimation, classification, pose estimation, and
    oriented bounding box detection.

    Attributes:
        model: The loaded YOLO model instance.
        task: The task type (detect, segment, semantic, depth, classify, pose, obb).
        overrides: Configuration overrides for the model.

    Methods:
        __init__: Initialize a YOLO model with automatic type detection.
        task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.

    Examples:
        Load a pretrained YOLO26n detection model
        >>> model = YOLO("yolo26n.pt")

        Load a pretrained YOLO26n segmentation model
        >>> model = YOLO("yolo26n-seg.pt")

        Initialize from a YAML configuration
        >>> model = YOLO("yolo26n.yaml")
    ú
yolo26n.ptNFÚmodelú
str | PathÚtaskú
str | NoneÚverboseÚboolc                óÚ  •— t          t          |t          t           f¦  «        r|nd¦  «        }d|j        v r<|j        dv r3t          ||¬¦  «        }t          |¦  «        | _        |j        | _        dS d|j        v r=|j        dv r4t          |||¬¦  «        }t          |¦  «        | _        |j        | _        dS t          ¦   «                              |||¬¦  «         t          | j        d¦  «        r$| j        j        d	                              ¦   «         nd}|sNt          | j        t          t           f¦  «        r-t          j        | j        ¦  «                             d
d¦  «        }d|v r3ddlm}  || ¦  «        }t          |¦  «        | _        |j        | _        dS t          | j        t(          ¦  «        r1t          | ¦  «        }t          |¦  «        | _        |j        | _        dS dS )a.  Initialize a YOLO model.

        This constructor initializes a YOLO model, automatically switching to specialized model types (YOLOWorld or
        YOLOE) based on the model filename.

        Args:
            model (str | Path): Model name or path to model file, i.e. 'yolo26n.pt', 'yolo26n.yaml'.
            task (str, optional): YOLO task specification, i.e. 'detect', 'segment', 'semantic', 'depth', 'classify',
                'pose', 'obb'. Defaults to auto-detection based on model.
            verbose (bool): Display model info on load.
        Ú z-world>   ú.ptú.ymlú.yaml)r   Úyoloe)r   r   ©r   r   r   r   éÿÿÿÿÚheadÚRTDETRr   )r*   N)r   Ú
isinstanceÚstrÚstemÚsuffixÚ	YOLOWorldÚtypeÚ	__class__Ú__dict__ÚYOLOEÚsuperÚ__init__Úhasattrr   Ú	_get_namer   Úread_metadataÚgetÚultralyticsr*   r   )	Úselfr   r   r   ÚpathÚnew_instancer)   r*   r1   s	           €úm/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/model.pyr5   zYOLO.__init__<   sà  ø€ õ �Z¨µµT¨{Ñ;Ô;ÐC�E�EÀÑDÔDˆØ�t”yÐ Ð  T¤[Ð4LÐ%LÐ%LÝ$ T°7Ð;Ñ;Ô;ˆLÝ! ,Ñ/Ô/ˆDŒNØ(Ô1ˆDŒMˆMˆMØ˜œ	Ð!Ð! d¤kÐ5MÐ&MÐ&MÝ  ¨D¸'ÐBÑBÔBˆLÝ! ,Ñ/Ô/ˆDŒNØ(Ô1ˆDŒMˆMˆMõ ‰GŒG×Ò 5¨t¸WÐÑEÔEÐEÝ7>¸t¼zÈ7Ñ7SÔ7SÐ[�4”:Ô# BÔ'×1Ò1Ñ3Ô3Ð3ÐY[ˆDØð M�J t¤zµC½°;Ñ?Ô?ð MÝ"Ô0°´Ñ<Ô<×@Ò@ÀÈÑLÔL�Ø˜4ÐÐØ.Ð.Ð.Ð.Ð.Ð.à%˜v d™|œ|�Ý!% lÑ!3Ô!3�”Ø ,Ô 5�”��Ý˜DœJ­
Ñ3Ô3ð 6Ý(¨™œ�Ý!% lÑ!3Ô!3�”Ø ,Ô 5�”��ð6ð 6ó    Úreturnúdict[str, dict[str, Any]]c                ó  — t           t          j        j        t          j        j        t          j        j        dœt          t          j        j        t          j        j	        t          j        j
        dœt          t          j        j        t          j        j        t          j        j        dœt           t          j        j        t          j        j        t          j        j        dœt*          t          j        j        t          j        j        t          j        j        dœt4          t          j        j        t          j        j        t          j        j        dœt>          t          j         j!        t          j         j"        t          j         j#        dœdœS )ú=Map head to model, trainer, validator, and predictor classes.)r   ÚtrainerÚ	validatorÚ	predictor)ÚclassifyÚdetectÚsegmentÚposeÚobbÚdepthÚsemantic)$r   r	   rG   ÚClassificationTrainerÚClassificationValidatorÚClassificationPredictorr   rH   ÚDetectionTrainerÚDetectionValidatorÚDetectionPredictorr   rI   ÚSegmentationTrainerÚSegmentationValidatorÚSegmentationPredictorr   rJ   ÚPoseTrainerÚPoseValidatorÚPosePredictorr   rK   Ú
OBBTrainerÚOBBValidatorÚOBBPredictorr   rL   ÚDepthTrainerÚDepthValidatorÚDepthPredictorr   rM   ÚSemanticSegmentationTrainerÚSemanticSegmentationValidatorÚSemanticSegmentationPredictor©r;   s    r>   Útask_mapzYOLO.task_mapb   s#  € õ
 -Ýœ=Ô>Ý!œ]ÔBÝ!œ]ÔBð	ð õ (Ýœ;Ô7Ý!œ[Ô;Ý!œ[Ô;ð	ð õ +Ýœ<Ô;Ý!œ\Ô?Ý!œ\Ô?ð	ð õ #Ýœ9Ô0Ý!œYÔ4Ý!œYÔ4ð	ð õ "Ýœ8Ô.Ý!œXÔ2Ý!œXÔ2ð	ð õ $Ýœ:Ô2Ý!œZÔ6Ý!œZÔ6ð	ð õ 3Ýœ=ÔDÝ!œ]ÔHÝ!œ]ÔHð	ð ðK+
ð +
ð +	
r?   )r   NF)r   r   r   r   r   r    ©r@   rA   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   Úpropertyrd   Ú__classcell__©r1   s   @r>   r   r       sm   ø€ € € € € ðð ð6$6ð $6ð $6ð $6ð $6ð $6ð $6ðL ð-
ð -
ð -
ñ „Xð-
ð -
ð -
ð -
ð -
r?   r   c                  óF   ‡ — e Zd ZdZddˆ fd
„Zedd„¦   «         Zdd„Zˆ xZS )r/   a¦  YOLO-World object detection model.

    YOLO-World is an open-vocabulary object detection model that can detect objects based on text descriptions without
    requiring training on specific classes. It extends the YOLO architecture to support real-time open-vocabulary
    detection.

    Attributes:
        model: The loaded YOLO-World model instance.
        task: Always set to 'detect' for object detection.
        overrides: Configuration overrides for the model.

    Methods:
        __init__: Initialize YOLOv8-World model with a pre-trained model file.
        task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.
        set_classes: Set the model's class names for detection.

    Examples:
        Load a YOLOv8-World model
        >>> model = YOLOWorld("yolov8s-world.pt")

        Set custom classes for detection
        >>> model.set_classes(["person", "car", "bicycle"])
    úyolov8s-world.ptFr   r   r   r    r@   ÚNonec                óð   •— t          ¦   «                              |d|¬¦  «         t          | j        d¦  «        s;t	          j        t          dz  ¦  «                             d¦  «        | j        _        dS dS )a¢  Initialize YOLOv8-World model with a pre-trained model file.

        Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default COCO
        class names.

        Args:
            model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
            verbose (bool): If True, prints additional information during initialization.
        rH   r'   Únameszcfg/datasets/coco8.yamlN)	r4   r5   r6   r   r   Úloadr   r9   rq   )r;   r   r   r1   s      €r>   r5   zYOLOWorld.__init__¬   su   ø€ õ 	‰Œ×Ò˜u¨8¸WÐÑEÔEÐEõ �t”z 7Ñ+Ô+ð 	XÝ#œy­Ð0IÑ)IÑJÔJ×NÒNÈwÑWÔWˆDŒJÔÐÐð	Xð 	Xr?   rA   c                óx   — dt           t          j        j        t          j        j        t          j        j        dœiS )rC   rH   ©r   rE   rF   rD   )r   r	   ÚworldÚWorldValidatorrH   rS   ÚWorldTrainerrc   s    r>   rd   zYOLOWorld.task_map¼   s6   € ð Ý#Ý!œZÔ6Ý!œ[Ô;Ýœ:Ô2ð	ð ð
ð 	
r?   Úclassesú	list[str]c                ó–   — | j                              |¦  «         d}||v r|                     |¦  «         || j         _        d| _        dS )z‰Set the model's class names for detection.

        Args:
            classes (list[str]): A list of categories i.e. ["person"].
        ú N)r   Úset_classesÚremoverq   rF   )r;   rx   Ú
backgrounds      r>   r|   zYOLOWorld.set_classesÈ   sQ   € ð 	Œ
×Ò˜wÑ'Ô'Ð'àˆ
Ø˜Ð Ð Ø�NŠN˜:Ñ&Ô&Ð&Ø"ˆŒ
ÔàˆŒˆˆr?   )rn   F)r   r   r   r    r@   ro   re   )rx   ry   r@   ro   )	rf   rg   rh   ri   r5   rj   rd   r|   rk   rl   s   @r>   r/   r/   “   s‡   ø€ € € € € ðð ð0Xð Xð Xð Xð Xð Xð Xð  ð	
ð 	
ð 	
ñ „Xð	
ðð ð ð ð ð ð ð r?   r/   c                  óº   ‡ — e Zd ZdZd+d,ˆ fd„Zed-d„¦   «         Zd„ Zd„ Z	 d.d/d„Z	d„ Z
d.d0d„Zd1d„Zd2d"„Zd3d#„Z	 	 	 d4d5d&„Zddddej        j        fd6ˆ fd*„Zˆ xZS )7r3   aÞ  YOLOE object detection and segmentation model.

    YOLOE is an enhanced YOLO model that supports both object detection and instance segmentation tasks with improved
    performance and additional features like visual and text positional embeddings.

    Attributes:
        model: The loaded YOLOE model instance.
        task: The task type (detect or segment).
        overrides: Configuration overrides for the model.

    Methods:
        __init__: Initialize YOLOE model with a pre-trained model file.
        task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.
        get_text_pe: Get text positional embeddings for the given texts.
        get_visual_pe: Get visual positional embeddings for the given image and visual features.
        set_vocab: Set vocabulary and class names for the YOLOE model.
        get_vocab: Get the vocabulary for the given class names, which become the model's classes as the head is fused.
        set_classes: Set the model's class names and embeddings for detection.
        save_prompt_embeddings: Save the current prompt embeddings and class names to an NPZ file.
        load_prompt_embeddings: Load prompt embeddings and class names from an NPZ file.
        val: Validate the model using text or visual prompts.
        predict: Run prediction on images, videos, directories, streams, etc.

    Examples:
        Load a YOLOE segmentation model
        >>> model = YOLOE("yoloe-11s-seg.pt")

        Predict with visual prompts, whose 'cls' holds one class index per box
        >>> from ultralytics.models.yolo.yoloe import YOLOEVPSegPredictor
        >>> prompts = {"bboxes": np.array([[10, 20, 100, 200]]), "cls": np.array([0])}
        >>> results = model.predict("image.jpg", visual_prompts=prompts, predictor=YOLOEVPSegPredictor)

        Re-parameterize into a prompt-free model, which no longer accepts prompts
        >>> names = ["person", "car", "dog"]
        >>> model.set_vocab(model.get_vocab(names), names)
    úyoloe-11s-seg.ptNFr   r   r   r   r   r    r@   ro   c                óP   •— t          ¦   «                              |||¬¦  «         dS )a_  Initialize YOLOE model with a pre-trained model file.

        Args:
            model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
            task (str, optional): Task type for the model. Auto-detected if None.
            verbose (bool): If True, prints additional information during initialization.
        r'   N)r4   r5   )r;   r   r   r   r1   s       €r>   r5   zYOLOE.__init__þ   s*   ø€ õ 	‰Œ×Ò˜u¨4¸ÐÑAÔAÐAÐAÐAr?   rA   c                óè   — t           t          j        j        t          j        j        t          j        j        dœt          t          j        j        t          j	        j
        t          j        j        dœdœS )rC   rt   )rH   rI   )r   r	   r&   ÚYOLOEDetectValidatorrH   rS   ÚYOLOETrainerr   ÚYOLOESegValidatorrI   rV   ÚYOLOESegTrainerrc   s    r>   rd   zYOLOE.task_map  s_   € õ
 $Ý!œZÔ<Ý!œ[Ô;Ýœ:Ô2ð	ð õ 'Ý!œZÔ9Ý!œ\Ô?Ýœ:Ô5ð	ð ð
ð 
ð 	
r?   c                ón   — t          | j        t          ¦  «        sJ ‚| j                             |¦  «        S )z3Get text positional embeddings for the given texts.)r+   r   r   Úget_text_pe)r;   Útextss     r>   rˆ   zYOLOE.get_text_pe  s0   € å˜$œ*¥jÑ1Ô1Ð1Ð1Ð1ØŒz×%Ò% eÑ,Ô,Ð,r?   c                óp   — t          | j        t          ¦  «        sJ ‚| j                             ||¦  «        S )aÂ  Get visual positional embeddings for the given image and visual features.

        This method extracts positional embeddings from visual features based on the input image. It requires that the
        model is an instance of YOLOEModel.

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

        Returns:
            (torch.Tensor): Visual positional embeddings.

        Examples:
            >>> model = YOLOE("yoloe-11s-seg.pt")
            >>> img = torch.rand(1, 3, 640, 640)
            >>> visual_features = torch.rand(1, 1, 80, 80)
            >>> pe = model.get_visual_pe(img, visual_features)
        )r+   r   r   Úget_visual_pe)r;   ÚimgÚvisuals      r>   r‹   zYOLOE.get_visual_pe  s4   € õ& ˜$œ*¥jÑ1Ô1Ð1Ð1Ð1ØŒz×'Ò'¨¨VÑ4Ô4Ð4r?   Úvocabútorch.nn.ModuleListrq   ry   Úone2one_vocabútorch.nn.ModuleList | Nonec                ó¤   — t          | j        t          ¦  «        sJ ‚t          |¦  «        }d| _        | j                             |||¬¦  «         dS )aÜ  Re-parameterize the model into a prompt-free one over the given class names.

        The vocabulary is the fused classification layer `get_vocab` returns for the same names, not the names
        themselves. The model must be an instance of YOLOEModel.

        Args:
            vocab (torch.nn.ModuleList): One-to-many fused classification layers returned by `get_vocab` for `names`.
            names (list[str]): List of class names that the model can detect or classify.
            one2one_vocab (torch.nn.ModuleList | None): One-to-one fused classification layers. When provided, both
                heads are built and `nms` keeps selecting between them; otherwise only the current head is built.

        Raises:
            AssertionError: If the model is not an instance of YOLOEModel.

        Examples:
            >>> model = YOLOE("yoloe-11s-seg.pt")
            >>> names = ["person", "car", "dog"]
            >>> model.set_vocab(model.get_vocab(names), names)
        N)rq   r�   )r+   r   r   r
   rF   Ú	set_vocab)r;   rŽ   rq   r�   s       r>   r“   zYOLOE.set_vocab5  sS   € õ, ˜$œ*¥jÑ1Ô1Ð1Ð1Ð1Ý! %Ñ(Ô(ˆØˆŒØŒ
×Ò˜U¨%¸}ÐÑMÔMÐMÐMÐMr?   c                ó|   — t          | j        t          ¦  «        sJ ‚d| _        | j                             |¦  «        S )zdGet the vocabulary for the given class names, which become the model's classes as the head is fused.N)r+   r   r   rF   Ú	get_vocab)r;   rq   s     r>   r•   zYOLOE.get_vocabP  s7   € å˜$œ*¥jÑ1Ô1Ð1Ð1Ð1ØˆŒØŒz×#Ò# EÑ*Ô*Ð*r?   rx   Ú
embeddingsútorch.Tensor | Nonec                ó–  — d|vsJ ‚t          | j        t          ¦  «        sJ ‚t          | j        j        t          ¦  «        r+t          | j        j                             ¦   «         ¦  «        nt          | j        j        ¦  «        }|€||k    r;|€|                      |¦  «        }| j                             ||¦  «         d| _	        dS dS )zòSet the model's class names and embeddings for detection.

        Args:
            classes (list[str]): A list of categories i.e. ["person"].
            embeddings (torch.Tensor, optional): Embeddings corresponding to the classes.
        r{   N)
r+   r   r   rq   ÚdictÚlistÚvaluesrˆ   r|   rF   )r;   rx   r–   rq   s       r>   r|   zYOLOE.set_classesV  sÉ   € ð ˜'Ð!Ð!Ð!Ð!Ý˜$œ*¥jÑ1Ô1Ð1Ð1Ð1Ý3=¸d¼jÔ>NÕPTÑ3UÔ3UÐq•�T”ZÔ%×,Ò,Ñ.Ô.Ñ/Ô/Ð/Õ[_Ð`dÔ`jÔ`pÑ[qÔ[qˆØÐ! U¨gÒ%5Ð%5ØÐ!Ø!×-Ò-¨gÑ6Ô6�
ØŒJ×"Ò" 7¨JÑ7Ô7Ð7Ø!ˆDŒNˆNˆNð	 &6Ð%5r?   r,   c                ó  — t          | j        dd¦  «        p| j        }t          |t          t
          f¦  «        r|n| j        j        d         }t          |¦  «        j        }|                     d¦  «        r
|dd…         n|S )zNReturn the checkpoint identifier used to bind prompt embeddings to this model.Úpt_pathNÚ	yaml_filez-segéüÿÿÿ)	Úgetattrr   Ú	ckpt_pathr+   r,   r   Úyamlr-   Úendswith)r;   Úsourcer   s      r>   Ú_prompt_embedding_modelzYOLOE._prompt_embedding_modelg  ss   € å˜œ Y°Ñ5Ô5ÐG¸¼ˆÝ% f­sµD¨kÑ:Ô:Ð\��ÀÄ
ÄÐP[Ô@\ˆÝ�V‘”Ô!ˆØ"Ÿ^š^¨FÑ3Ô3Ð>ˆu�S�b�SŒzˆz¸Ð>r?   Úfiler   c           	     óD  — t          | j        t          ¦  «        sJ ‚t          | j        dd¦  «        }t          |t          j        ¦  «        r|j        dk    s|j        d         dk    rt          d¦  «        ‚t          | j        j	        t          ¦  «        r+t          | j        j	                             ¦   «         ¦  «        nt          | j        j	        ¦  «        }|j        d         t          |¦  «        k    s&t	          j        |¦  «                             ¦   «         st          d¦  «        ‚t!          |¦  «        }|j                             ¦   «         dk    rt          d	|j        › d
�¦  «        ‚t'          j        ||                     ¦   «                              ¦   «                              ¦   «                              ¦   «         t'          j        |t&          j        ¬¦  «        t'          j        |                      ¦   «         t&          j        ¬¦  «        ¬¦  «         |S )a6  Save the current prompt embeddings and class names to an NPZ file.

        Args:
            file (str | Path): Destination NPZ file path.

        Returns:
            (Path): Path to the saved NPZ file.

        Raises:
            ValueError: If prompt embeddings have not been set or are invalid.
        ÚpeNé   r   é   z7Prompt embeddings must be set before they can be saved.zEPrompt embeddings must be finite and match the number of class names.z.npzz7Prompt embedding file must have an '.npz' suffix, not 'ú'.)Údtype)r–   rq   r   )r+   r   r   r    ÚtorchÚTensorÚndimÚshapeÚ
ValueErrorrq   r™   rš   r›   ÚlenÚisfiniteÚallr   r.   ÚlowerÚnpÚsavez_compressedÚdetachÚcpuÚfloatÚnumpyÚasarrayÚstr_r¥   )r;   r¦   r–   rq   s       r>   Úsave_prompt_embeddingszYOLOE.save_prompt_embeddingsn  sË  € õ ˜$œ*¥jÑ1Ô1Ð1Ð1Ð1Ý˜TœZ¨¨tÑ4Ô4ˆ
Ý˜*¥e¤lÑ3Ô3ð 	X°z´È!Ò7KÐ7KÈzÔO_Ð`aÔObÐfgÒOgÐOgÝÐVÑWÔWÐWÝ3=¸d¼jÔ>NÕPTÑ3UÔ3UÐq•�T”ZÔ%×,Ò,Ñ.Ô.Ñ/Ô/Ð/Õ[_Ð`dÔ`jÔ`pÑ[qÔ[qˆØÔ˜AÔ¥# e¡*¤*Ò,Ð,µE´NÀ:Ñ4NÔ4N×4RÒ4RÑ4TÔ4TÐ,ÝÐdÑeÔeÐeå�D‰zŒzˆØŒ;×ÒÑÔ &Ò(Ð(ÝÐfÐW[ÔWbÐfÐfÐfÑgÔgÐgÝ
ÔØØ!×(Ò(Ñ*Ô*×.Ò.Ñ0Ô0×6Ò6Ñ8Ô8×>Ò>Ñ@Ô@Ý”*˜U­"¬'Ð2Ñ2Ô2Ý”*˜T×9Ò9Ñ;Ô;Å2Ä7ÐKÑKÔKð		
ñ 	
ô 	
ð 	
ð ˆr?   c           	     ó&  — t          | j        t          ¦  «        sJ ‚t          j        |d¬¦  «        5 }t          |j        ¦  «        h d£k    rt          d¦  «        ‚|d         |d         |d         }}}ddd¦  «         n# 1 swxY w Y   |j        d	k    s|j	        j
        d
k    rt          d¦  «        ‚t          |                     ¦   «         ¦  «        }||                      ¦   «         k    r(t          d|› d|                      ¦   «         › d�¦  «        ‚|j        dk    s|j	        j
        d
k    rt          d¦  «        ‚|j	        t          j        k    s|j        dk    s|j        d	         dk    rt          d¦  «        ‚|j        d         t!          |¦  «        k    s&|j        d         | j        j        d         j        k    rt          d¦  «        ‚t          j        |¦  «                             ¦   «         st          d¦  «        ‚|                      |                     ¦   «         t-          j        |                     ¦   «         ¦  «                             t5          | j                             ¦   «         ¦  «        j        ¦  «        ¦  «         dS )a(  Load prompt embeddings and class names from a model-bound NPZ file.

        Args:
            file (str | Path): Source NPZ file path created by :meth:`save_prompt_embeddings`.

        Raises:
            ValueError: If the file is invalid or belongs to a different YOLOE architecture.
        F)Úallow_pickle>   r   rq   r–   zFPrompt embedding file must contain 'embeddings', 'names', and 'model'.r–   rq   r   Nr   ÚUz:Prompt embedding model identifier must be a scalar string.zPrompt embeddings for model 'z' cannot be loaded into 'r«   rª   zDPrompt embedding class names must be a one-dimensional string array.r©   zNPrompt embeddings must be a float32 array with shape (1, classes, dimensions).é   r(   zSPrompt embedding shape does not match the class names or model embedding dimension.z2Prompt embeddings must contain only finite values.)r+   r   r   r¶   rr   ÚsetÚfilesr±   r¯   r¬   Úkindr,   Úitemr¥   Úfloat32r°   r²   Úembedr³   r´   r|   Útolistr­   Ú
from_numpyÚcopyÚtoÚnextÚ
parametersÚdevice)r;   r¦   Údatar–   rq   r   Ú
model_names          r>   Úload_prompt_embeddingszYOLOE.load_prompt_embeddings�  s¥  € õ ˜$œ*¥jÑ1Ô1Ð1Ð1Ð1ÝŒW�T¨Ð.Ñ.Ô.ð 	X°$Ý�4”:‰ŒÐ"BÐ"BÐ"BÒBÐBÝ Ð!iÑjÔjÐjØ'+¨LÔ'9¸4À¼=È$ÈwÌ-˜u˜ˆJð	Xð 	Xð 	Xñ 	Xô 	Xð 	Xð 	Xð 	Xð 	Xð 	Xð 	Xøøøð 	Xð 	Xð 	Xð 	Xð
 Œ:˜Š?ˆ?˜eœkÔ.°#Ò5Ð5ÝÐYÑZÔZÐZÝ˜Ÿš™œÑ&Ô&ˆ
Ø˜×5Ò5Ñ7Ô7Ò7Ð7ÝØw°
ÐwÐwÐUY×UqÒUqÑUsÔUsÐwÐwÐwñô ð ð Œ:˜Š?ˆ?˜eœkÔ.°#Ò5Ð5ÝÐcÑdÔdÐdØÔ�rœzÒ)Ð)¨Z¬_ÀÒ-AÐ-AÀZÔEUÐVWÔEXÐ\]ÒE]ÐE]ÝÐmÑnÔnÐnØÔ˜AÔ¥# e¡*¤*Ò,Ð,°
Ô0@ÀÔ0CÀtÄzÔGWÐXZÔG[ÔGaÒ0aÐ0aÝÐrÑsÔsÐsÝŒ{˜:Ñ&Ô&×*Ò*Ñ,Ô,ð 	SÝÐQÑRÔRÐRØ×Ò˜Ÿš™œ­Ô)9¸*¿/º/Ñ:KÔ:KÑ)LÔ)L×)OÒ)OÕPTÐUYÔU_×UjÒUjÑUlÔUlÑPmÔPmÔPtÑ)uÔ)uÑvÔvÐvÐvÐvs   ³ABÂBÂBÚload_vpÚ
refer_datac                ó   — d| i}|                      d¦  «        €|                      ¦   «         |d<   i | j        ¥|¥|¥ddi¥} |p|                      d¦  «        || j        ¬¦  «        } || j        ||¬¦  «         |j        | _        |j        S )	a1  Validate the model using text or visual prompts.

        Args:
            validator (callable, optional): A callable validator function. If None, a default validator is loaded.
            load_vp (bool): Whether to load visual prompts. If False, text prompts are used.
            refer_data (str, optional): Path to the reference data for visual prompts.
            **kwargs (Any): Additional keyword arguments to override default settings.

        Returns:
            (dict): Validation statistics containing metrics computed during validation.
        ÚrectrÐ   NÚmodeÚvalrE   )ÚargsÚ
_callbacks)r   rÓ   rÔ   )r9   Ú	_val_dataÚ	overridesÚ_smart_loadÚ	callbacksr   Úmetrics)r;   rE   rÓ   rÔ   ÚkwargsÚcustomrÙ   s          r>   rØ   z	YOLOE.val­  s¬   € ð$ ˜g˜+Ð&ˆØ�:Š:�fÑÔÐ%Ø!Ÿ^š^Ñ-Ô-ˆF�6‰NØD�$”.ÐD FÐD¨fÐD°f¸eÐDÐDˆà?�YÐ? $×"2Ò"2°;Ñ"?Ô"?ÀdÐW[ÔWeÐfÑfÔfˆ	Øˆ	˜œ
¨GÀ
ÐKÑKÔKÐKØ Ô(ˆŒØÔ Ð r?   ÚstreamÚvisual_promptsú/dict[str, np.ndarray | list[np.ndarray]] | Nonec                óž  •‡— |�|ni }t          |¦  «        �ræd|v rd|v sJ d|                     ¦   «         › �¦   «         ‚|d         |d         }}t          d„ ||fD ¦   «         ¦  «        s
J d¦   «         ‚t          |¦  «        t          |¦  «        cxk    rdk    sn J d¦   «         ‚t          j        j                             |¦  «        }	t          |t          j	        ¦  «        r|j
        d	k    s
J d
¦   «         ‚t          |t          t          f¦  «        p$t          |t          j        ¦  «        o
|j
        d	k    }
|	r|€|
s
J d¦   «         ‚|	Š‰rt          t          ||¦  «        ¦  «        n||fg}‰rKt          |¦  «        t          |¦  «        k    s+J dt          |¦  «        › dt          |¦  «        › d�¦   «         ‚t          ˆfd„|D ¦   «         ¦  «        s
J d¦   «         ‚d„ |D ¦   «         }t          |¦  «        s
J d¦   «         ‚t!          |¦  «        }t#          | j        ¦  «        |ur�t'          i | j        ¥|¥¬¦  «        } || j        j        dd|                     d| j                             d|du ¦  «        ¦  «        d|j        |j        |j        dœ| j        ¬¦  «        | _        || j        j        d         _        d„ t;          |¦  «        D ¦   «         | j        _        | j                             |                      ¦   «         ¦  «         | j         !                    | j        | j        j"        j#        ¬¦  «         |€B|�@tI          |¦  «        }|j%        dv r(tM          tO          |¦  «        ¦  «        d         d         }|�s| j         (                    |¦  «        }| j         )                    | j        j        |¦  «         t          | j        t          j*        j+        ¦  «        rdnd| _        d| _        n+t          | j        t          j        j        ¦  «        rd| _        d | j        d!<    tY          ¦   «         j-        ||fi |¤ŽS )"a´  Run prediction on images, videos, directories, streams, etc.

        Args:
            source (str | int | PIL.Image | np.ndarray, optional): Source for prediction. Accepts image paths, directory
                paths, URL/YouTube streams, PIL images, numpy arrays, or webcam indices.
            stream (bool): Whether to stream the prediction results. If True, results are yielded as a generator as they
                are computed.
            visual_prompts (dict[str, np.ndarray | list[np.ndarray]]): Dictionary containing visual prompts for the
                model. Must include 'bboxes' and 'cls' keys when non-empty, holding either flat arrays or one array per
                image for an explicit list, tuple, or 4-D tensor source with no refer_image.
            refer_image (str | PIL.Image | np.ndarray, optional): Reference image for visual prompts.
            predictor (callable): Custom predictor class for visual prompt predictions. Defaults to
                YOLOEVPDetectPredictor.
            **kwargs (Any): Additional keyword arguments passed to the predictor.

        Returns:
            (list | generator): List of Results objects or generator of Results objects if stream=True.

        Examples:
            >>> model = YOLOE("yoloe-11s-seg.pt")
            >>> results = model.predict("path/to/image.jpg")
            >>> # With visual prompts, whose 'cls' holds one class index per box
            >>> from ultralytics.models.yolo.yoloe import YOLOEVPSegPredictor
            >>> prompts = {"bboxes": np.array([[10, 20, 100, 200]]), "cls": np.array([0])}
            >>> results = model.predict("path/to/image.jpg", visual_prompts=prompts, predictor=YOLOEVPSegPredictor)
        NÚbboxesÚclsz7Expected 'bboxes' and 'cls' in visual prompts, but got c              3  ó`   K  — | ])}t          |d ¦  «        ot          |dd¦  «        dk    V — Œ*dS )Ú__len__r¯   rª   r   N)r6   r    ©Ú.0Úxs     r>   ú	<genexpr>z YOLOE.predict.<locals>.<genexpr>ò  sB   è è € ÐfÐfÈq•w˜q )Ñ,Ô,ÐJµ¸¸FÀAÑ1FÔ1FÈÒ1JÐfÐfÐfÐfÐfÐfr?   z5Expected non-scalar 'bboxes' and 'cls' visual promptsr   z7Expected an equal, non-zero number of boxes and classesé   z#4-D NumPy sources are not supportedz\Expected flat 'bboxes' and 'cls' arrays for a non-sequence source or when refer_image is setz.Expected one prompt per source image, but got z prompts for z imagesc              3  ó  •K  — | ]€\  }}t          |d d¦  «        dk    oc‰ p|j        dd…         dk    oMt          |d d¦  «        dk    o8t          |¦  «        t          |¦  «        k    ot          d„ |D ¦   «         ¦  «        V — Œ�dS )r¯   rÂ   rª   N)rî   c              3  óx   K  — | ]5}t          j        |¦  «        ot          |t          t          f¦  «         V — Œ6d S ©N)r¶   Úisscalarr+   r,   Úbytesrê   s     r>   rí   z*YOLOE.predict.<locals>.<genexpr>.<genexpr>  s@   è è € ÐVÐVÈq�œ A™œÐJ­z¸!½cÅ5¸\Ñ/JÔ/JÐ+JÐVÐVÐVÐVÐVÐVr?   )r    r°   r²   r´   )rë   ÚbÚcÚmultis      €r>   rí   z YOLOE.predict.<locals>.<genexpr>  sÆ   øè è € ð ð ñ �A�qõ ˜˜6 1Ñ%Ô%¨Ò*ð WØ�YÐ5 !¤'¨!¨"¨"¤+°Ò"5ðWå˜A˜v qÑ)Ô)¨QÒ.ðWõ ˜‘F”F�c !™fœfÒ$ðWõ ÐVÐVÐTUÐVÑVÔVÑVÔVðð ð ð ð ð r?   z>Expected non-string scalar class indices for each bounding boxc           
     ó¨   — g | ]O\  }}t          t          t          |t          j        ¦  «        r|                     ¦   «         n|¦  «        ¦  «        ‘ŒPS © )r²   rÃ   r+   r¶   ÚndarrayrÉ   )rë   Ú_rõ   s      r>   ú
<listcomp>z!YOLOE.predict.<locals>.<listcomp>  sL   € ÐdÐdÐdÑVZÐVWÐYZ��S­z¸!½R¼ZÑ/HÔ/HÐ!O §¢¡¤ ÈaÑPÔPÑQÔQÐdÐdÐdr?   z%Expected at least one class per image)rÜ   ÚpredictFr   rª   )r   r×   Úsaver   ÚbatchrÏ   ÚquantizeÚimgsz)rÜ   rÚ   r(   c                ó   — g | ]}d |› �‘ŒS )Úobjectrø   )rë   Úis     r>   rû   z!YOLOE.predict.<locals>.<listcomp>  s   € ÐEÐEÐE° ¨  ÐEÐEÐEr?   )r   r   >   Úvideorâ   rI   rH   TÚagnostic_nms).r²   Úkeysr´   r	   r&   ÚYOLOEVPDetectPredictorÚis_per_imager+   r¶   rù   r¯   rš   Útupler­   r®   ÚzipÚmaxr0   rF   r   rÜ   r   r   r9   rÏ   rÿ   r   rÞ   ÚncÚrangerq   Úset_promptsrË   Úsetup_modelrÙ   r   r   r×   rÍ   ÚiterÚget_vper|   rI   rV   r4   rü   )r;   r¤   râ   rã   Úrefer_imagerF   rà   ræ   rx   ÚnestedÚper_image_sourceÚpairsÚ	per_imageÚnum_clsrÙ   ÚdatasetÚvperö   r1   s                    @€r>   rü   zYOLOE.predictÉ  s´  øø€ ðF ,:Ð+E˜˜È2ˆÝˆ~ÑÔñ A	"Ø˜~Ð-Ð-°%¸>Ð2IÐ2IÐ2IØaÈ.×J]ÒJ]ÑJ_ÔJ_ÐaÐañ 3JÔ2IÐIð -¨XÔ6¸ÀuÔ8M�GˆFÝÐfÐfÐU[Ð]dÐTeÐfÑfÔfÑfÔfð ð ØGñô Ðfõ �v‘;”;¥# g¡,¤,Ð2Ð2Ò2Ð2°Ò2Ð2Ð2Ð2Ð2Ð4mÑ2Ô2Ð2Ý”ZÔ6×CÒCÀNÑSÔSˆFÝ! &­"¬*Ñ5Ô5Ðp¸¼ÈÒ9IÐ9IÐ9IÐKpÑ9IÔ9IÐIÝ)¨&µ4½°-Ñ@Ô@ð  Ý˜6¥5¤<Ñ0Ô0ÐE°V´[ÀAÒ5Eð ð ð  +Ð"5Ð:JÐ"5Ð"5Ønñ #6Ô"5ÐKð ˆEØ27ÐP•D�˜V WÑ-Ô-Ñ.Ô.Ð.¸vÀwÐ>OÐ=PˆEØð ¥ E¡
¤
­c°&©k¬kÒ 9Ð 9Ð 9ØnÅÀUÁÄÐnÐnÕZ]Ð^dÑZeÔZeÐnÐnÐnñ !:Ô 9Ð9õ ð ð ð ð ð "ðñ ô ñ ô ð Pð Pð PñPô Pð ð eÐdÐ^cÐdÑdÔdˆIÝ�y‘>”>ÐJÐJÐ#JÑJÔJ�>Ý˜)‘n”nˆGÝ�D”NÑ#Ô#¨9Ð4Ð4ÝÐ)E¨D¬NÐ)E¸fÐ)EÐFÑFÔF�Ø!* à $¤
¤Ø )Ø %Ø#)§:¢:¨i¸¼×9KÒ9KÈIÐWbÐfjÐWjÑ9kÔ9kÑ#lÔ#lØ!"Ø"&¤+Ø$(¤MØ!%¤ð	ð 	ð  $œ~ð"ñ "ô "�”ð '.ˆDŒJÔ˜RÔ Ô#ØEÐEµe¸G±n´nÐEÑEÔEˆDŒJÔØŒN×&Ò& ~×':Ò':Ñ'<Ô'<Ñ=Ô=Ð=ØŒN×&Ò&¨T¬ZÀÄÔATÔA\Ð&Ñ]Ô]Ð]àÐ" vÐ'9Ý/°Ñ7Ô7�Ø”<Ð#6Ð6Ð6å"&¥t¨G¡}¤}Ñ"5Ô"5°aÔ"8¸Ô";�KØÐ&Ø”n×,Ò,¨[Ñ9Ô9�Ø”
×&Ò& t¤zÔ'7¸Ñ=Ô=Ð=Ý)3°D´NÅDÄLÔDfÑ)gÔ)gÐu˜I˜IÐmu�”	Ø!%�”øÝ˜œ­¬
Ô(IÑJÔJð 	"Ø!ˆDŒNØ)-ˆŒ�~Ñ&à�u‰wŒwŒ˜v vÐ8Ð8°Ð8Ð8Ð8r?   )r€   NF)r   r   r   r   r   r    r@   ro   re   rñ   )rŽ   r�   rq   ry   r�   r‘   r@   ro   )rx   ry   r–   r—   r@   ro   )r@   r,   )r¦   r   r@   r   )r¦   r   r@   ro   )NFN)rÓ   r    rÔ   r   )râ   r    rã   rä   )rf   rg   rh   ri   r5   rj   rd   rˆ   r‹   r“   r•   r|   r¥   r¾   rÒ   rØ   r	   r&   r  rü   rk   rl   s   @r>   r3   r3   Ø   sˆ  ø€ € € € € ð#ð #ðJBð Bð Bð Bð Bð Bð Bð ð
ð 
ð 
ñ „Xð
ð"-ð -ð -ð
5ð 5ð 5ð. imðNð Nð Nð Nð Nð6+ð +ð +ð"ð "ð "ð "ð "ð"?ð ?ð ?ð ?ðð ð ð ð>wð wð wð wðD ØØ!%ð	!ð !ð !ð !ð !ð< ØØJNØØ”*Ô3ðh9ð h9ð h9ð h9ð h9ð h9ð h9ð h9ð h9ð h9ð h9r?   r3   )&Ú
__future__r   Úpathlibr   Útypingr   r»   r¶   r­   Úultralytics.cfgr   Úultralytics.data.buildr   Úultralytics.engine.modelr   Úultralytics.modelsr	   Úultralytics.nn.autobackendr
   Úultralytics.nn.backends.baser   Úultralytics.nn.tasksr   r   r   r   r   r   r   r   r   r   Úultralytics.utilsr   r   r   r/   r3   rø   r?   r>   ú<module>r%     sô  ðð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø €€€à #Ð #Ð #Ð #Ð #Ð #Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø *Ð *Ð *Ð *Ð *Ð *Ø #Ð #Ð #Ð #Ð #Ð #Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4ðð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð ð )Ð (Ð (Ð (Ð (Ð (Ð (Ð (ðp
ð p
ð p
ð p
ð p
ˆ5ñ p
ô p
ð p
ðfBð Bð Bð Bð B�ñ Bô Bð BðJY9ð Y9ð Y9ð Y9ð Y9ˆEñ Y9ô Y9ð Y9ð Y9ð Y9r?   