
    /jq                    N    d dl mZ d dlZd dlmZmZ d dlZ G d de          ZdS )    )annotationsN)ABCabstractmethodc                  T    e Zd ZdZddd	Zedd            Zedd            ZddZdS )BaseBackenda  Base class for all inference backends.

    This abstract class defines the interface that all inference backends must implement. It provides common
    functionality for model loading, metadata processing, and device management.

    Attributes:
        model: The underlying inference model or runtime session.
        device (torch.device): The device to run inference on.
        fp16 (bool): Whether to use FP16 (half-precision) inference.
        nhwc (bool): Whether the model expects NHWC input format instead of NCHW.
        stride (int): Model stride, typically 32 for YOLO models.
        names (dict): Dictionary mapping class indices to class names.
        task (str | None): The task type (detect, segment, classify, pose, obb).
        batch (int): Batch size for inference.
        imgsz (tuple): Input image size as (height, width).
        channels (int): Number of input channels, typically 3 for RGB.
        end2end (bool): Whether the model includes end-to-end NMS post-processing.
        dynamic (bool): Whether the model supports dynamic input shapes.
        metadata (dict): Model metadata dictionary containing export configuration.
    Fweightstr | torch.nn.Moduledevicetorch.device | strfp16boolc                    || _         || _        d| _        d| _        i | _        d| _        d| _        d| _        d| _        d| _	        i | _
        d| _        |                     |           dS )ak  Initialize the base backend with common attributes and load the model.

        Args:
            weight (str | torch.nn.Module): Path to the model weights file or a PyTorch module instance.
            device (torch.device | str): Device to run inference on (e.g., 'cpu', 'cuda:0').
            fp16 (bool): Whether to use FP16 half-precision inference.
        F    N      )r
   r   nhwcstridenamestaskbatchchannelsend2enddynamicmetadatamodel
load_model)selfr   r
   r   s       a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/nn/backends/base.py__init__zBaseBackend.__init__!   sp     		
	

    returnNonec                    t           )zLoad the model from a weights file or module instance.

        Args:
            weight (str | torch.nn.Module): Path to model weights or a PyTorch module.
        NotImplementedError)r   r   s     r   r   zBaseBackend.load_model7   s
     "!r    imtorch.Tensor!torch.Tensor | list[torch.Tensor]c                    t           )a  Run inference on the input image tensor.

        Args:
            im (torch.Tensor): Input image tensor in BCHW format, normalized to [0, 1].

        Returns:
            (torch.Tensor | list[torch.Tensor]): Model output as a single tensor or list of tensors.
        r$   )r   r&   s     r   forwardzBaseBackend.forward@   s
     "!r    r   dict | Nonec                   |sdS || _         |                                D ]L\  }}|dv rt          |          ||<   |dv r,t          |t                    rt          j        |          ||<   M|                    dd          p)|                    di                               dd          |d<   |                    di                               d| j                  |d<   |                                D ]\  }}t          | ||           dS )	a  Process and apply model metadata to backend attributes.

        Handles type conversions for common metadata fields (e.g., stride, batch, names) and sets them as
        instance attributes. Also resolves end-to-end NMS and dynamic shape settings from export args.

        Args:
            metadata (dict | None): Dictionary containing metadata key-value pairs from model export.
        N>   r   r   r   >   argsimgszr   r   	kpt_names	kpt_shaper   Fr-   nmsr   )
r   itemsint
isinstancestrastliteral_evalgetr   setattr)r   r   kvs       r   apply_metadatazBaseBackend.apply_metadataL   s1     	F ! NN$$ 	2 	2DAq333!!ffUUUZdefhkZlZlU!.q11 'll9e<<jVUW@X@X@\@\]bdi@j@j&ll6266::9dlSS NN$$ 	  	 DAqD!Q	  	 r    N)F)r   r	   r
   r   r   r   )r   r	   r!   r"   )r&   r'   r!   r(   )r   r+   r!   r"   )	__name__
__module____qualname____doc__r   r   r   r*   r<    r    r   r   r      s         *         , " " " ^" 	" 	" 	" ^	"           r    r   )
__future__r   r6   abcr   r   torchr   rA   r    r   <module>rE      s    # " " " " " 



 # # # # # # # # ]  ]  ]  ]  ] # ]  ]  ]  ]  ] r    