
    /j	                    n    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  G d d	e          ZdS )
    )annotations)PathN)LOGGER)check_requirements   )BaseBackendc                  "    e Zd ZdZddZdd
ZdS )NCNNBackendzTencent NCNN inference backend for mobile and embedded deployment.

    Loads and runs inference with Tencent NCNN models (*_ncnn_model/ directories). Optimized for mobile platforms with
    optional Vulkan GPU acceleration when available.
    weight
str | PathreturnNonec                   t          j        d| d           t          dd           ddl}|| _        |                                | _        t          | j        t                    r| j        
                    d          rpd	| j        j        _        | j                            t          | j                            d
          d                              t!          j        d          | _        nd| j        j        _        t#          |          }|                                s"t'          |                    d                    }| j                            t          |                     | j                            t          |                    d                               |j        dz  }|                                r0ddlm} |                     |                    |                     dS dS )zLoad an NCNN model from a .param/.bin file pair or model directory.

        Args:
            weight (str | Path): Path to the .param file or directory containing NCNN model files.
        zLoading z for NCNN inference...ncnnz	--no-deps)cmdsr   NvulkanT:r   cpuFz*.paramz.binzmetadata.yaml)YAML)r   infor   r   pyncnnNetnet
isinstancedevicestr
startswithoptuse_vulkan_computeset_vulkan_deviceintsplittorchr   is_filenextglob
load_param
load_modelwith_suffixparentexistsultralytics.utilsr   apply_metadataload)selfr   r   wmetadata_filer   s         a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/nn/backends/ncnn.pyr(   zNCNNBackend.load_model   s    	=v===>>>64444::<< dk3'' 	4DK,B,B8,L,L 	4.2DHL+H&&s4;+<+<S+A+A!+D'E'EFFF,u--DKK.3DHL+LLyy{{ 	(QVVI&&''ACFF###Cf 5 566777 ?2!! 	:......		- 8 899999	: 	:    imtorch.Tensorlist[np.ndarray]c                   | j                             |d                                                                                   }| j                                        5                     | j                                        d         |           fdt          | j        	                                          D             }ddd           n# 1 swxY w Y   |S )a
  Run inference using the NCNN runtime.

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

        Returns:
            (list[np.ndarray]): Model predictions as a list of numpy arrays, one per output layer.
        r   c                v    g | ]5}t          j                            |          d                    d         6S )r   N)nparrayextract).0xexs     r2   
<listcomp>z'NCNNBackend.forward.<locals>.<listcomp>G   s7    [[[a"**Q--*++D1[[[r3   N)
r   Matr   numpyr   create_extractorinputinput_namessortedoutput_names)r/   r4   mat_inyr>   s       @r2   forwardzNCNNBackend.forward:   s    A!2!2!4!455X&&(( 	\BHHTX))++A.777[[[[6$(BWBWBYBY;Z;Z[[[A	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ 	\ s   A&CCCN)r   r   r   r   )r4   r5   r   r6   )__name__
__module____qualname____doc__r(   rI    r3   r2   r
   r
      sG         !: !: !: !:F     r3   r
   )
__future__r   pathlibr   rA   r9   r#   r,   r   ultralytics.utils.checksr   baser   r
   rN   r3   r2   <module>rS      s    # " " " " "            $ $ $ $ $ $ 7 7 7 7 7 7      8 8 8 8 8+ 8 8 8 8 8r3   