§
    l¬¸j   ã                  ó®   — d dl m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 d dlmZmZmZ d d	lmZ d d
lmZ dd„Z G d„ de¦  «        ZdS )é    )ÚannotationsN)ÚPath)ÚAny)Úbuild_yolo_dataset)ÚDetectionTrainer)Ú
WorldModel)ÚDEFAULT_CFGÚLOGGERÚRANK)Ú
torch_load)Úunwrap_modelÚreturnÚNonec                óÈ   — d„ t          | j        j        j        d                              ¦   «         ¦  «        D ¦   «         }| j        j                             |d¬¦  «         dS )zISet up model classes and text encoder at the end of the pretrain routine.c                óF   — g | ]}|                      d d¦  «        d         ‘ŒS )ú/é   r   )Úsplit)Ú.0Únames     ús/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/world/train.pyú
<listcomp>z+on_pretrain_routine_end.<locals>.<listcomp>   s+   € ÐhÐhÐh tˆT�ZŠZ˜˜QÑÔ Ô"ÐhÐhÐhó    ÚnamesF©Úcache_clip_modelN)ÚlistÚtest_loaderÚdatasetÚdataÚvaluesÚemaÚset_classes)Útrainerr   s     r   Úon_pretrain_routine_endr%      s^   € ð iÐh­t°GÔ4GÔ4OÔ4TÐU\Ô4]×4dÒ4dÑ4fÔ4fÑ/gÔ/gÐhÑhÔh€EØ„K„O×Ò ¸ÐÑ>Ô>Ð>Ð>Ð>r   c                  óX   ‡ — e Zd ZdZeddfd$ˆ fd„Zd%d&d„Zd'd(d„Zd)d„Zd*d!„Z	d+d#„Z
ˆ xZS ),ÚWorldTrainera˜  A trainer class for fine-tuning YOLO World models on close-set datasets.

    This trainer extends the DetectionTrainer to support training YOLO World models, which combine visual and textual
    features for improved object detection and understanding. It handles text embedding generation and caching to
    accelerate training with multi-modal data.

    Attributes:
        text_embeddings (dict[str, torch.Tensor] | None): Cached text embeddings for category names to accelerate
            training.
        model (WorldModel): The YOLO World model being trained.
        data (dict[str, Any]): Dataset configuration containing class information.
        args (Any): Training arguments and configuration.

    Methods:
        get_model: Return WorldModel initialized with specified config and weights.
        build_dataset: Build YOLO Dataset for training or validation.
        set_text_embeddings: Set text embeddings for datasets to accelerate training.
        generate_text_embeddings: Generate text embeddings for a list of text samples.
        preprocess_batch: Preprocess a batch of images and text for YOLOWorld training.

    Examples:
        Initialize and train a YOLO World model
        >>> from ultralytics.models.yolo.world import WorldTrainer
        >>> args = dict(model="yolov8s-world.pt", data="coco8.yaml", epochs=3)
        >>> trainer = WorldTrainer(overrides=args)
        >>> trainer.train()
    NÚ	overridesúdict[str, Any] | NoneÚ
_callbacksúdict | Nonec                ó¶   •— |€i }|                      d¦  «        rJ d|d         › d�¦   «         ‚t          ¦   «                              |||¦  «         d| _        dS )a%  Initialize a WorldTrainer object with given arguments.

        Args:
            cfg (dict[str, Any]): Configuration for the trainer.
            overrides (dict[str, Any], optional): Configuration overrides.
            _callbacks (dict, optional): Dictionary of callback functions.
        NÚcompilezTraining with 'model=Úmodelz' requires 'compile=False')ÚgetÚsuperÚ__init__Útext_embeddings)ÚselfÚcfgr(   r*   Ú	__class__s       €r   r1   zWorldTrainer.__init__7   sk   ø€ ð ÐØˆIØ—=’= Ñ+Ô+ÐsÐsÐ-sÀYÈwÔEWÐ-sÐ-sÐ-sÑsÔsÐ+Ý‰Œ×Ò˜˜i¨Ñ4Ô4Ð4Ø#ˆÔÐÐr   TÚweightsú
str | NoneÚverboseÚboolr   r   c                óP  — t          t          |t          ¦  «        r|d         n|| j        d         t	          | j        d         d¦  «        |o
t
          dk    ¬¦  «        }|r|                     |¦  «         t          | j        d         vr|  	                    dt          ¦  «         |S )a_  Return WorldModel initialized with specified config and weights.

        Args:
            cfg (dict[str, Any] | str, optional): Model configuration.
            weights (str, optional): Path to pretrained weights.
            verbose (bool): Whether to display model info.

        Returns:
            (WorldModel): Initialized WorldModel.
        Ú	yaml_fileÚchannelsÚncéP   éÿÿÿÿ)Úchr=   r8   r%   )
r   Ú
isinstanceÚdictr    Úminr   Úloadr%   Ú	callbacksÚadd_callback)r3   r4   r6   r8   r.   s        r   Ú	get_modelzWorldTrainer.get_modelE   s©   € õ Ý *¨3µÑ 5Ô 5Ð>ˆC�ÔÐ¸3ØŒy˜Ô$Ý�4”9˜T”? BÑ'Ô'ØÐ*¥¨¢
ð	
ñ 
ô 
ˆð ð 	 Ø�JŠJ�wÑÔÐå"¨$¬.Ð9RÔ*SÐSÐSØ×ÒÐ7Õ9PÑQÔQÐQàˆr   ÚtrainÚimg_pathÚstrÚmodeÚbatchú
int | Nonec                ó0  — t          t          | j        r+t          | j        ¦  «        j                              ¦   «         nd¦  «        d¦  «        }t          | j        ||| j        ||dk    ||dk    ¬¦  «        }|dk    r|                      |g|¦  «         |S )a   Build YOLO Dataset for training or validation.

        Args:
            img_path (str): Path to the folder containing images.
            mode (str): `train` mode or `val` mode, users are able to customize different augmentations for each mode.
            batch (int, optional): Size of batches, this is for `rect`.

        Returns:
            (Any): YOLO dataset configured for training or validation.
        r   é    ÚvalrH   )rK   ÚrectÚstrideÚmulti_modal)	ÚmaxÚintr.   r   rR   r   Úargsr    Úset_text_embeddings)r3   rI   rK   rL   Úgsr   s         r   Úbuild_datasetzWorldTrainer.build_dataset`   sž   € õ •¸d¼jÐO•\ $¤*Ñ-Ô-Ô4×8Ò8Ñ:Ô:Ð:ÈaÑPÔPÐRTÑUÔUˆÝ$ØŒI�x ¨¬	¸À4È5Â=ÐY[ÐimÐqxÒixð
ñ 
ô 
ˆð �7Š?ˆ?Ø×$Ò$ g Y°Ñ6Ô6Ð6Øˆr   Údatasetsú	list[Any]r   c           
     óì   — i }|D ]g}t          |d¦  «        sŒ|                     |                      t          |j        ¦  «        |t          |j        ¦  «        j        ¬¦  «        ¦  «         Œh|| _        dS )a™  Set text embeddings for datasets to accelerate training by caching category names.

        This method collects unique category names from all datasets, then generates and caches text embeddings for
        these categories to improve training efficiency.

        Args:
            datasets (list[Any]): List of datasets from which to extract category names.
            batch (int | None): Batch size used for processing.

        Notes:
            This method collects category names from datasets that have the 'category_names' attribute,
            then uses the first dataset's image path to determine where to cache the generated text embeddings.
        Úcategory_names)Ú	cache_dirN)	ÚhasattrÚupdateÚgenerate_text_embeddingsr   r]   r   rI   Úparentr2   )r3   rZ   rL   r2   r   s        r   rW   z WorldTrainer.set_text_embeddingss   s“   € ð ˆØð 	ð 	ˆGÝ˜7Ð$4Ñ5Ô5ð ØØ×"Ò"Ø×-Ò-Ý˜Ô/Ñ0Ô0°%Å4ÈÔHXÑCYÔCYÔC`ð .ñ ô ñô ð ð ð
  /ˆÔÐÐr   Útextsú	list[str]rU   r^   r   údict[str, torch.Tensor]c                óx  — d}|d|                      dd¦  «                              dd¦  «        › d�z  }|                     ¦   «         rbt          j        d|› d�¦  «         t	          || j        ¬	¦  «        }t          |                     ¦   «         ¦  «        t          |¦  «        k    r|S t          j        d
|› d�¦  «         | j        €J ‚t          | j        ¦  «         
                    ||d¬¦  «        }t          t          ||                     d¦  «        ¦  «        ¦  «        }t          j        ||¦  «         |S )au  Generate text embeddings for a list of text samples.

        Args:
            texts (list[str]): List of text samples to encode.
            batch (int): Batch size for processing.
            cache_dir (Path): Directory to save/load cached embeddings.

        Returns:
            (dict[str, torch.Tensor]): Dictionary mapping text samples to their embeddings.
        zclip:ViT-B/32Útext_embeddings_ú:Ú_r   z.ptzReading existed cache from 'ú')Úmap_locationzCaching text embeddings to 'NFr   r   )ÚreplaceÚexistsr
   Úinfor   ÚdeviceÚsortedÚkeysr.   r   Úget_text_perB   ÚzipÚsqueezeÚtorchÚsave)r3   rc   rL   r^   r.   Ú
cache_pathÚtxt_mapÚ	txt_featss           r   ra   z%WorldTrainer.generate_text_embeddingsŒ   s4  € ð  ˆØÐ!b°E·M²MÀ#ÀsÑ4KÔ4K×4SÒ4SÐTWÐY\Ñ4]Ô4]Ð!bÐ!bÐ!bÑbˆ
Ø×ÒÑÔð 	ÝŒKÐD°zÐDÐDÐDÑEÔEÐEÝ  ¸$¼+ÐFÑFÔFˆGÝ�g—l’l‘n”nÑ%Ô%­°©¬Ò6Ð6Ø�ÝŒÐ@°:Ð@Ð@Ð@ÑAÔAÐAØŒzÐ%Ð%Ð%Ý  ¤Ñ,Ô,×8Ò8¸ÀÐX]Ð8Ñ^Ô^ˆ	Ý•s˜5 )×"3Ò"3°AÑ"6Ô"6Ñ7Ô7Ñ8Ô8ˆÝŒ
�7˜JÑ'Ô'Ð'Øˆr   údict[str, Any]c                óp  ‡ — t          j        ‰ |¦  «        }t          t          j        |d         Ž ¦  «        }t          j        ˆ fd„|D ¦   «         ¦  «                             ‰ j        ‰ j        j	        dv¬¦  «        }| 
                    t          |d         ¦  «        d|j        d         ¦  «        |d<   |S )z=Preprocess a batch of images and text for YOLOWorld training.rc   c                ó*   •— g | ]}‰j         |         ‘ŒS © )r2   )r   Útextr3   s     €r   r   z1WorldTrainer.preprocess_batch.<locals>.<listcomp>«   s!   ø€ Ð NÐ NÐ NÀ Ô!5°dÔ!;Ð NÐ NÐ Nr   >   ÚcpuÚmps)Únon_blockingr?   ry   )r   Úpreprocess_batchr   Ú	itertoolsÚchainru   ÚstackÚtoro   ÚtypeÚreshapeÚlenÚshape)r3   rL   rc   ry   s   `   r   r‚   zWorldTrainer.preprocess_batch¥   s±   ø€ å Ô1°$¸Ñ>Ô>ˆõ •Y”_ e¨G¤nÐ5Ñ6Ô6ˆÝ”KÐ NÐ NÐ NÐ NÈÐ NÑ NÔ NÑOÔO×RÒRØŒK d¤kÔ&6¸nÐ&Lð Sñ 
ô 
ˆ	ð '×.Ò.­s°5¸´>Ñ/BÔ/BÀBÈ	ÌÐXZÔH[Ñ\Ô\ˆˆkÑØˆr   )r(   r)   r*   r+   )NNT)r6   r7   r8   r9   r   r   )rH   N)rI   rJ   rK   rJ   rL   rM   )rZ   r[   rL   rM   r   r   )rc   rd   rL   rU   r^   r   r   re   )rL   rz   r   rz   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r1   rG   rY   rW   ra   r‚   Ú__classcell__)r5   s   @r   r'   r'      sÄ   ø€ € € € € ðð ð8 'È4Ðkoð $ð $ð $ð $ð $ð $ð $ðð ð ð ð ð6ð ð ð ð ð&/ð /ð /ð /ð2ð ð ð ð2
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   r'   )r   r   )Ú
__future__r   rƒ   Úpathlibr   Útypingr   ru   Úultralytics.datar   Úultralytics.models.yolo.detectr   Úultralytics.nn.tasksr   Úultralytics.utilsr	   r
   r   Úultralytics.utils.patchesr   Úultralytics.utils.torch_utilsr   r%   r'   r}   r   r   ú<module>r™      s  ðð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€à /Ð /Ð /Ð /Ð /Ð /Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø +Ð +Ð +Ð +Ð +Ð +Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6ð?ð ?ð ?ð ?ðUð Uð Uð Uð UÐ#ñ Uô Uð Uð Uð Ur   