§
    l¬¸jî  ã                  ó†   — d dl mZ d dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ  G d„ d	e¦  «        ZdS )
é    )ÚannotationsN)ÚImage)Úclassify_transforms)ÚBasePredictor)ÚResults)ÚDEFAULT_CFGÚopsc                  óN   ‡ — e Zd ZdZeddfdˆ fd„Zˆ fd„Zdd
„Zˆ fd„Zd„ Z	ˆ xZ
S )ÚClassificationPredictora	  A class extending the BasePredictor class for prediction based on a classification model.

    This predictor handles the specific requirements of classification models, including preprocessing images and
    postprocessing predictions to generate classification results.

    Attributes:
        args (dict): Configuration arguments for the predictor.

    Methods:
        pre_transform: Resize and crop images on the host before the device-side conversion.
        preprocess: Convert input images to model-compatible format.
        postprocess: Process model predictions into Results objects.

    Examples:
        >>> from ultralytics.utils import ASSETS
        >>> from ultralytics.models.yolo.classify import ClassificationPredictor
        >>> args = dict(model="yolo26n-cls.pt", source=ASSETS)
        >>> predictor = ClassificationPredictor(overrides=args)
        >>> predictor.predict_cli()

    Notes:
        - Torchvision classification models can also be passed to the 'model' argument, i.e. model='resnet18'.
    NÚ
_callbacksúdict | Nonec                óf   •— t          ¦   «                              |||¦  «         d| j        _        dS )as  Initialize the ClassificationPredictor with the specified configuration and set task to 'classify'.

        This constructor initializes a ClassificationPredictor instance, which extends BasePredictor for classification
        tasks. It ensures the task is set to 'classify' regardless of input configuration.

        Args:
            cfg (dict): Default configuration dictionary containing prediction settings.
            overrides (dict, optional): Configuration overrides that take precedence over cfg.
            _callbacks (dict, optional): Dictionary of callback functions to be executed during prediction.
        ÚclassifyN)ÚsuperÚ__init__ÚargsÚtask)ÚselfÚcfgÚ	overridesr   Ú	__class__s       €úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/classify/predict.pyr   z ClassificationPredictor.__init__)   s.   ø€ õ 	‰Œ×Ò˜˜i¨Ñ4Ô4Ð4Ø#ˆŒ	Œˆˆó    c                óö  •— ddl m} t          ¦   «                              |¦  «         t	          | j        j        dd¦  «        }|�.t	          |j        d         dt          | j        ¦  «        ¦  «        nd}|t          | j        ¦  «        k    r| j        j        dk    r|nt          | j        ¦  «        | _        t	          | j        dd¦  «        }t          | j        ¦  «        |j        u oWt          t          t          |¦  «        ¦  «        |j        |j        |j        |j        fk    ot	          | j        dd¦  «        dk    }|r|                     |dd	…         ¦  «        nd| _        |r|d
         nd| _        dS )z9Set up source and inference mode and classify transforms.r   NÚ
transformsÚsizeÚpt© Úchannelsé   é   éÿÿÿÿ)Útorchvision.transformsr   r   Úsetup_sourceÚgetattrÚmodelÚmaxÚimgszÚformatr   ÚtypeÚComposeÚtupleÚmapÚResizeÚ
CenterCropÚToTensorÚ	NormalizeÚhost_transformsÚdevice_transform)r   ÚsourceÚTr   r   ÚtflÚsplitr   s          €r   r$   z$ClassificationPredictor.setup_source7   se  ø€ à*Ð*Ð*Ð*Ð*Ð*å‰Œ×Ò˜VÑ$Ô$Ð$Ý˜TœZÔ-¨|¸TÑBÔBˆ
ØMWÐMc�w�zÔ,¨QÔ/°½¸T¼Z¹¼ÑIÔIÐIÐimˆà¥# d¤j¡/¤/Ò1Ð1°d´jÔ6GÈ4Ò6OÐ6OˆJˆJÕUhÐimÔisÑUtÔUtð 	Œõ �d”o |°RÑ8Ô8ˆå�”Ñ!Ô! Q¤YÐ.ð 8Ý•c�$ ‘n”nÑ%Ô%¨!¬(°A´LÀ!Ä*ÈaÌkÐ)ZÒZð8å˜œ
 J°Ñ2Ô2°aÒ7ð 	ð
 6;ÐD˜qŸyšy¨¨R¨a¨R¬Ñ1Ô1Ð1ÀˆÔØ+0Ð :  B¤ °dˆÔÐÐr   Úimúlist[np.ndarray]Úreturnc                ó    ‡ — ˆ fd„|D ¦   «         S )zUResize and crop images on the host, leaving uint8 BGR for the device-side conversion.c           	     ó‚   •— g | ];}t          j        ‰                     t          j        |¦  «        ¦  «        ¦  «        ‘Œ<S r   )ÚnpÚarrayr2   r   Ú	fromarray©Ú.0Úxr   s     €r   ú
<listcomp>z9ClassificationPredictor.pre_transform.<locals>.<listcomp>L   s;   ø€ ÐOÐOÐOÀq•”˜×-Ò-­e¬o¸aÑ.@Ô.@ÑAÔAÑBÔBÐOÐOÐOr   r   )r   r8   s   ` r   Úpre_transformz%ClassificationPredictor.pre_transformJ   s   ø€ àOÐOÐOÐOÈBÐOÑOÔOÐOr   c                óØ  •‡ — ‰ j         €Žt          |t          j        ¦  «        stt          j        ˆ fd„|D ¦   «         d¦  «        }|                     ‰ j        j        ¦  «        }‰ j        j        r| 	                    ¦   «         n| 
                    ¦   «         S t          |t          j        ¦  «        }t          ¦   «                              |¦  «        }|r|n‰                       |¦  «        S )zVConvert input images to model-compatible tensor format with appropriate normalization.Nc           
     ó˜   •— g | ]F}‰                      t          j        t          j        |t          j        ¦  «        ¦  «        ¦  «        ‘ŒGS r   )r   r   r?   Úcv2ÚcvtColorÚCOLOR_BGR2RGBr@   s     €r   rC   z6ClassificationPredictor.preprocess.<locals>.<listcomp>Q   s?   ø€ ÐqÐqÐqÐhi˜tŸš­u¬½s¼|ÈAÍsÔO`Ñ?aÔ?aÑ/bÔ/bÑcÔcÐqÐqÐqr   r   )r3   Ú
isinstanceÚtorchÚTensorÚstackÚtor&   ÚdeviceÚfp16ÚhalfÚfloatr   Ú
preprocess)r   ÚimgÚ	is_tensorr   s   `  €r   rS   z"ClassificationPredictor.preprocessN   sÄ   øø€ àÔ Ð(µ¸CÅÄÑ1NÔ1NÐ(Ý”+ÐqÐqÐqÐqÐmpÐqÑqÔqÐstÑuÔuˆCØ—&’&˜œÔ*Ñ+Ô+ˆCØ!%¤¤ÐA�3—8’8‘:”:�:°c·i²i±k´kÐAÝ˜s¥E¤LÑ1Ô1ˆ	Ý‰gŒg× Ò  Ñ%Ô%ˆØÐ?ˆsˆs T×%:Ò%:¸3Ñ%?Ô%?Ð?r   c                ó  ‡ — t          |t          ¦  «        st          j        |¦  «        dddd…f         }t          |t          t          f¦  «        r|d         n|}ˆ fd„t          ||‰ j        d         ¦  «        D ¦   «         S )aÄ  Process predictions to return Results objects with classification probabilities.

        Args:
            preds (torch.Tensor): Raw predictions from the model.
            img (torch.Tensor): Input images after preprocessing.
            orig_imgs (list[np.ndarray] | torch.Tensor): Original images before preprocessing.

        Returns:
            (list[Results]): List of Results objects containing classification results for each image.
        .Nr"   r   c                óR   •— g | ]#\  }}}t          ||‰j        j        |¬ ¦  «        ‘Œ$S ))ÚpathÚnamesÚprobs)r   r&   rY   )rA   ÚpredÚorig_imgÚimg_pathr   s       €r   rC   z7ClassificationPredictor.postprocess.<locals>.<listcomp>g   sE   ø€ ð 
ð 
ð 
á(��h õ �H 8°4´:Ô3CÈ4ÐPÑPÔPð
ð 
ð 
r   )rJ   Úlistr	   Úconvert_torch2numpy_batchr,   ÚzipÚbatch)r   ÚpredsrT   Ú	orig_imgss   `   r   Úpostprocessz#ClassificationPredictor.postprocessX   s”   ø€ õ ˜)¥TÑ*Ô*ð 	LÝÔ5°iÑ@Ô@ÀÀdÀdÈÀdÀÔKˆIå& u­tµU¨mÑ<Ô<ÐG��a”�À%ˆð
ð 
ð 
ð 
å,/°°yÀ$Ä*ÈQÄ-Ñ,PÔ,Pð
ñ 
ô 
ð 	
r   )r   r   )r8   r9   r:   r9   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r$   rD   rS   rd   Ú__classcell__)r   s   @r   r   r      s´   ø€ € € € € ðð ð0 '°$ÐRVð $ð $ð $ð $ð $ð $ð $ð;ð ;ð ;ð ;ð ;ð&Pð Pð Pð Pð@ð @ð @ð @ð @ð
ð 
ð 
ð 
ð 
ð 
ð 
r   r   )Ú
__future__r   rG   Únumpyr=   rK   ÚPILr   Úultralytics.data.augmentr   Úultralytics.engine.predictorr   Úultralytics.engine.resultsr   Úultralytics.utilsr   r	   r   r   r   r   ú<module>rq      sÕ   ðð #Ð "Ð "Ð "Ð "Ð "à 
€
€
€
Ø Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð à 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø .Ð .Ð .Ð .Ð .Ð .Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .ðZ
ð Z
ð Z
ð Z
ð Z
˜mñ Z
ô Z
ð Z
ð Z
ð Z
r   