
    j{              	      b   d Z ddlmZ ddlZddlZddl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ZddlZddlmZmZ ddlmZ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&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0 e"dz  ddddddddf	dZ1 G d d          Z2 G d d          Z3dS )a-  
Benchmark YOLO model formats for speed and accuracy.

Usage:
    from ultralytics.utils.benchmarks import ProfileModels, benchmark
    ProfileModels(['yolo26n.yaml', 'yolov8s.yaml']).run()
    benchmark(model='yolo26n.pt', imgsz=160)

Format                  | `format=argument`         | Model
---                     | ---                       | ---
PyTorch                 | -                         | yolo26n.pt
TorchScript             | `torchscript`             | yolo26n.torchscript
ONNX                    | `onnx`                    | yolo26n.onnx
OpenVINO                | `openvino`                | yolo26n_openvino_model/
TensorRT                | `engine`                  | yolo26n.engine
CoreML                  | `coreml`                  | yolo26n.mlpackage
TensorFlow SavedModel   | `saved_model`             | yolo26n_saved_model/
TensorFlow GraphDef     | `pb`                      | yolo26n.pb
TensorFlow Lite         | `tflite`                  | yolo26n.tflite
TensorFlow Edge TPU     | `edgetpu`                 | yolo26n_edgetpu.tflite
TensorFlow.js           | `tfjs`                    | yolo26n_web_model/
PaddlePaddle            | `paddle`                  | yolo26n_paddle_model/
MNN                     | `mnn`                     | yolo26n.mnn
NCNN                    | `ncnn`                    | yolo26n_ncnn_model/
IMX                     | `imx`                     | yolo26n_imx_model/
RKNN                    | `rknn`                    | yolo26n_rknn_model/
ExecuTorch              | `executorch`              | yolo26n_executorch_model/
    )annotationsN)deepcopyPath)YOLO	YOLOWorld)	TASK2DATATASK2METRIC)export_formats)
ARM64ASSETS
ASSETS_URL	IS_JETSONLINUXLOGGERMACOSTQDMWEIGHTS_DIRYAML)IS_PYTHON_3_13check_imgszcheck_requirements
check_yolois_rockchip)safe_download)	file_size)get_cpu_infoselect_devicez
yolo26n.pt   FcpuMbP? c	                  $ t          |          }t          |t                    r|d         |d         k    snn
J d            ddl}
|
j                            d           |
j                            d           |
j                            d           |
j                            d           |
j        	                    d           |
j        
                    d           t          |d	          }t          | t          t          f          rt          |           } |pt          | j                 }t"          | j                 }g }t%          j                    }|                                }|r6t)          t+                      d
                   }||v sJ d| d| d            t-          t+                                                       D ]\  }}}}}}d\  }}	 |r||k    r|dk    r| j        dk    s
J d            nc|dk    rt0          rt2          r
J d            nD|dk    rt0          rt2          r
J d            n%|dk    rt4          st0          rt2          r
J d            |dk    rt6          r
J d            |dv rt          | t8                    r
J d            |dk    rSt          | t8                    r
J d            | j        dk    s
J d            t0          rt:          rt4          s
J d            |dk    rt          | t8                    r
J d             |d!k    rt          | t8                    r
J d"            |d#k    rRt          | t8                    r
J d$            | j        d%v s
J d&            d'|                                 v s
J d(            |d)k    rHt          | t8                    r
J d*            t0          s
J d+            t?                      r
J d,            |d-k    rt          | t8                    r
J d.            d/|j         v r|s
J d0            d1|j         v r|s
J d2            |d3k    r%| j!        p| j"        p| j#        }tI          |           }nS tI          |           j%        db||||||dd4|	}t          || j        5          }|t          |          v s
J d6            d7}| j        d8k    s|dk    s
J d9            |d:vs
J d;            |dk    s!tM          j'                    d<k    s
J d=            |(                    tR          d>z  |||d?           |*                    |d|d|||dd@A	  	        }|j+        |         |j,        dB         }}t[          dC||z   z  dD          }|.                    |dEt[          t_          |          d          t[          |dF          t[          |dD          |g           # t`          $ r}|r&tA          |          tb          u sJ dG| dH|             te          j3        dG| dH|            |.                    ||t[          t_          |          d          dddg           Y d}~d}~ww xY wti          |I           |
5                    |dJdKdL|dMdNgdOP          }|6                    dQdR          }|7                    |
8                                9                    |
j:                  ;                    d3                    }| j#        }t%          j                    |z
  }dS} dT| dU| dV| dW|dXdY|  dZ| dZ}!te          j<        |!           t{          d[d\d]d^_          5 }"|">                    |!           ddd           n# 1 swxY w Y   |rYt          |t~                    rD||         @                                }#|$tq          $fd`|#D                       sJ da$             |S )ca  Benchmark a YOLO model across different formats for speed and accuracy.

    Args:
        model (str | Path): Path to the model file or directory.
        data (str | None): Dataset to evaluate on, inherited from TASK2DATA if not passed.
        imgsz (int): Image size for the benchmark.
        half (bool): Use half-precision for the model if True.
        int8 (bool): Use int8-precision for the model if True.
        device (str): Device to run the benchmark on, either 'cpu' or 'cuda'.
        verbose (bool | float): If True or a float, assert benchmarks pass with given metric.
        eps (float): Epsilon value for divide by zero prevention.
        format (str): Export format for benchmarking. If not supplied all formats are benchmarked.
        **kwargs (Any): Additional keyword arguments for exporter.

    Returns:
        (polars.DataFrame): A Polars DataFrame with benchmark results for each format, including file size, metric, and
            inference time.

    Examples:
        Benchmark a YOLO model with default settings:
        >>> from ultralytics.utils.benchmarks import benchmark
        >>> benchmark(model="yolo26n.pt", imgsz=640)
    r      Tz'benchmark() only supports square imgsz.NASCII_BORDERS_ONLY_CONDENSEDF)verboseArgumentzExpected format to be one of z, but got 'z'.)   ❌Npbobbz.TensorFlow GraphDef not supported for OBB taskedgetpuz3Edge TPU export only supported on non-aarch64 Linuxtfjsz)TF.js export not supported on ARM64 Linuxcoremlz;CoreML export only supported on macOS and non-aarch64 Linuxz#CoreML not supported on Python 3.13>   r*   r-   tfliter,   saved_modelz;YOLOWorldv2 TensorFlow exports not supported by onnx2tf yetpaddlez,YOLOWorldv2 Paddle exports not supported yetzBPaddle OBB bug https://github.com/PaddlePaddle/Paddle/issues/72024z3Windows and Jetson Paddle exports not supported yetmnnz)YOLOWorldv2 MNN exports not supported yetncnnz*YOLOWorldv2 NCNN exports not supported yetimxz%YOLOWorldv2 IMX exports not supported>   posedetectsegmentclassifyzbIMX export is only supported for detection, classification, pose estimation and segmentation tasksC2fz*IMX only supported for YOLOv8n and YOLO11nrknnz*YOLOWorldv2 RKNN exports not supported yetzRKNN only supported on Linuxz1RKNN Inference only supported on Rockchip devices
executorchz0YOLOWorldv2 ExecuTorch exports not supported yetr    zinference not supported on CPUcudazinference not supported on GPU-)imgszformathalfint8datadevicer'   )taskzexport failedu   ❎r5   z(GraphDef Pose inference is not supported>   r-   r,   zinference not supportedDarwinz(inference only supported on macOS>=10.13zbus.jpg)r>   rC   r@   r'   r!   )	rB   batchr>   plotsrC   r@   rA   r'   conf	inference     u   ✅   zBenchmark failure for : )rC   Formatu	   Status❔z	Size (MB)zInference time (ms/im)FPSrow)schemaorient )offsetud   Benchmarks legend:  - ✅ Success  - ❎ Export passed but validation failed  - ❌️ Export failedz
Benchmarks complete for z on z
 at imgsz=z (z.2fzs)

zbenchmarks.logaignoreutf-8)errorsencodingc              3  L   K   | ]}t          j        |          |k    V  d S N)npisnan).0xfloors     a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/benchmarks.py	<genexpr>zbenchmark.<locals>.<genexpr>   s4      AARXa[[A1u9AAAAAA    z%Benchmark failure: metric(s) < floor  )Ar   
isinstancelistpolarsConfigset_tbl_colsset_tbl_rowsset_tbl_width_charsset_tbl_hide_column_data_typesset_tbl_hide_dataframe_shapeset_tbl_formattingr   strr   r   r	   rD   r
   timelower	frozensetr   zipvaluesr   r   r   r   r   r   __str__r   typept_path	ckpt_path
model_namer   exportplatformsystempredictr   valresults_dictspeedroundappendr   	ExceptionAssertionErrorr   errorr   	DataFramewith_row_indexwith_columnsallcastString	fill_nullinfoopenwritefloatto_numpy)%modelrB   r>   r@   rA   rC   r'   epsr?   kwargsplkeyyt0
format_argformatsnamesuffixr    gpu_emojifilenameexported_modelresultsmetricr   fpsedf
df_displaydtlegendsfmetricsra   s%                                       @rb   	benchmarkr   7   sN	   F E#-eT#:#:o58uQxFoDI2I2I!!"%%%I,,T222I**4000I  !?@@@65111F%#t%% U(9UZ(D
ej
!C
A	BJ eN,,Z899   "d'"d"dV`"d"d"d   -0.2B2B2I2I2K2K-L RU RU)ffc3%xP	U jF22 ~~zU***,\****9$$aUaa,aaa*a6!!!YeYY.YYY,8##ttutt7ttt5!!)PP+PPP)KKK%eY77vv9vvv7!!%eY77gg9ggg7zU***,p***pipEpp;ppp9%eY77dd9ddd7%eY77ee9eee7%eY77``9```7z%NNNNx ONN ///1]///%eY77ee9eee7<<<<<u&==]]*]]](%%%eY77kk9kkk7##<<<<<s$$<<<<<s }} =OEOOu?O!)%18E??1 T4X^hm qw  "&hUZ!@!@!@X......E :''6T>>>;e>>9!44446O444X%%):):h)F)F)FHr)F)FF""6I#5U6X\fk"lll %(( ) 
 
G $05w}[7QEF,a00CHHdE58)<)<a#@#@%PQBRBRTYZ_abTcTcehijjjj 	U 	U 	U WAww.0002V42V2VST2V2V000L=$==!==>>>HHdE58)<)<a#@#@$dSTTTTTTTT		U f	a;SJbdi jsx	y	yB			3q		)	)Bry!9!9!C!CC!H!HIIJD	r	BsFlTlltllulllllTZll^hlllA
KNNN	Hw	G	G	G 1	


                s:gu-- sS'""$$AAAAgAAAAArrCrkpCrCrrrAs2   W'O#W
YA8YY]11]58]5c                  J    e Zd ZdZd ZddZdddZedd            ZddZ	dS )RF100Benchmarka  Benchmark YOLO model performance on the RF100 dataset collection.

    This class provides functionality to download, process, and evaluate YOLO models on the RF100 datasets.

    Attributes:
        ds_names (list[str]): Names of datasets used for benchmarking.
        ds_cfg_list (list[Path]): List of paths to dataset configuration files.
        rf (Roboflow | None): Roboflow instance for accessing datasets.
        val_metrics (list[str]): Metrics used for validation.

    Methods:
        set_key: Set Roboflow API key for accessing datasets.
        parse_dataset: Parse dataset links and download datasets.
        fix_yaml: Fix train and validation paths in YAML files.
        evaluate: Evaluate model performance on validation results.
    c                B    g | _         g | _        d| _        g d| _        dS )z^Initialize the RF100Benchmark class for benchmarking YOLO model performance on RF100 datasets.Nclassimagestargets	precisionrecallmap50map95)ds_namesds_cfg_listrfval_metrics)selfs    rb   __init__zRF100Benchmark.__init__   s,    bbbrd   api_keyrp   c                R    t          d           ddlm}  ||          | _        dS )a  Set Roboflow API key for processing.

        Args:
            api_key (str): The API key.

        Examples:
            Set the Roboflow API key for accessing datasets:
            >>> benchmark = RF100Benchmark()
            >>> benchmark.set_key("your_roboflow_api_key")
        roboflowr   )Roboflow)r   N)r   r   r   r   )r   r   r   s      rb   set_keyzRF100Benchmark.set_key   s<     	:&&&%%%%%%(7+++rd   datasets_links.txtds_link_txtc                   t           j                            d          r(t          j        d          t          j        d          fnt          j        d           t          j        d           t          j        d           t          t           d           t          |d          5 }|D ]}	 t          j        d|                                          \  }}}}}| j                            |           | d| }	t          |	                                          sT| j                            |                              |                              |                              d           nt+          j        d	           | j                            t          j                    |	z  d
z             # t2          $ r Y w xY w	 ddd           n# 1 swxY w Y   | j        | j        fS )a  Parse dataset links and download datasets.

        Args:
            ds_link_txt (str): Path to the file containing dataset links.

        Returns:
            (tuple[list[str], list[Path]]): List of dataset names and list of paths to dataset configuration files.

        Examples:
            >>> benchmark = RF100Benchmark()
            >>> benchmark.set_key("api_key")
            >>> benchmark.parse_dataset("datasets_links.txt")
        zrf-100zultralytics-benchmarksz/datasets_links.txtrX   rZ   z/+r=   yolov8zDataset already downloaded.z	data.yamlN)ospathexistsshutilrmtreemkdirchdirr   r   r   resplitstripr   r   r   r   	workspaceprojectversiondownloadr   r   r   cwdr   )
r   r   fileliner   _urlr   r   r   proj_versions
             rb   parse_datasetzRF100Benchmark.parse_dataset  s    :<9Q9Qix	 	 "(8"4"455WYW_`hWiWi

)***888999+000 	D  
;=8D$**,,;W;W8AtYM((111&-#9#9#9#9L--4466 C)))44<<WEEMMgVV__`hiiii$ABBB$++DHJJ,E,STTTT    H	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 }d...s7   ,G3DF=;G=
GG
GGG"Gr   r   c                l    t          j        |           }d|d<   d|d<   t          j        ||            dS )z8Fix the train and validation paths in a given YAML file.ztrain/imagestrainzvalid/imagesr   N)r   loaddump)r   	yaml_datas     rb   fix_yamlzRF100Benchmark.fix_yaml)  s=     IdOO	+	')	%	)T"""""rd   	yaml_pathval_log_fileeval_log_filelist_indintc                f   g d}t          j        |          d         t          |d          5 }|                                }g }|D ]t	          fd|D                       r                    d          t          t          d                     d D             |                    fd	D                        	 d
d
d
           n# 1 swxY w Y   d}	t          |          dk    r.t          j        d           |D ]}
|
d         dk    r|
d         }	n-t          j        d           t          d |D                       }	t          |dd          5 }|                    | j        |          d|	 d           d
d
d
           n# 1 swxY w Y   t          |	          S )a  Evaluate model performance on validation results.

        Args:
            yaml_path (str): Path to the YAML configuration file.
            val_log_file (str): Path to the validation log file.
            eval_log_file (str): Path to the evaluation log file.
            list_ind (int): Index of the current dataset in the list.

        Returns:
            (float): The mean average precision (mAP) value for the evaluated model.

        Examples:
            Evaluate a model on a specific dataset
            >>> benchmark = RF100Benchmark()
            >>> benchmark.evaluate("path/to/data.yaml", "path/to/val_log.txt", "path/to/eval_log.txt", 0)
        )u   🚀u   ⚠️u   💡r)   namesrX   r   c              3      K   | ]}|v V  	d S r\   re   )r_   symbolr   s     rb   rc   z*RF100Benchmark.evaluate.<locals>.<genexpr>H  s'      AA&v~AAAAAArd   rS   c                    | dk    S )Nr"   re   )r   s    rb   <lambda>z)RF100Benchmark.evaluate.<locals>.<lambda>K  s
    #) rd   c                8    g | ]}|                     d           S )rU   )r   )r_   r   s     rb   
<listcomp>z+RF100Benchmark.evaluate.<locals>.<listcomp>L  s"    :::Q1774==:::rd   c           	   3     K   | ]J}|v s|d k    dvdvd         d         d         d         d         d         d	         d
V  KdS )r   z(AP)z(AR)r   r$   rK      rL         r   Nre   )r_   r   class_namesentriess     rb   rc   z*RF100Benchmark.evaluate.<locals>.<genexpr>M  s       " " K''AJJ6;P;PU[cjUjUj ")")!*#*1:%,QZ")!*!(!(  VkUjUjUj" "rd   N        r$   zMultiple dicts foundr   r   r   zSingle dict foundc              3  &   K   | ]}|d          V  dS )r   Nre   )r_   ress     rb   rc   z*RF100Benchmark.evaluate.<locals>.<genexpr>b  s&      >>C3w<>>>>>>rd   rV   rM   rU   )r   r   r   	readlinesanyr   rg   filterextendlenr   r   nextr   r   r   )r   r   r   r   r   skip_symbolsr   lines
eval_linesmap_vallstr   r   r   s              @@@rb   evaluatezRF100Benchmark.evaluate1  s   " 988i	**73,111 	QKKMMEJ  AAAALAAAAA **S//v&;&;WEEFF::':::!! " " " " " %" " "    	 	 	 	 	 	 	 	 	 	 	 	 	 	 	, z??QK.///! + +w<5((!'lG+ K+,,,>>:>>>>>G-w777 	?1GGt}X.=='===>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? W~~s$   BCC C&'FF FN)r   rp   )r   )r   rp   )r   r   )r   rp   r   rp   r   rp   r   r   )
__name__
__module____qualname____doc__r   r   r   staticmethodr   r  re   rd   rb   r   r      s         "c c c, , , , !/ !/ !/ !/ !/F # # # \#6 6 6 6 6 6rd   r   c                      e Zd ZdZ	 	 	 	 	 	 	 d2d3dZd Zd Zed4d            Zed5d6d!            Z	d7d8d%Z
ed&             Zd7d9d'Zd:d.Zed:d/            Zed;d1            ZdS )<ProfileModelsa*  ProfileModels class for profiling different models on ONNX and TensorRT.

    This class profiles the performance of different models, returning results such as model speed and FLOPs.

    Attributes:
        paths (list[str]): Paths of the models to profile.
        num_timed_runs (int): Number of timed runs for the profiling.
        num_warmup_runs (int): Number of warmup runs before profiling.
        min_time (float): Minimum number of seconds to profile for.
        imgsz (int): Image size used in the models.
        half (bool): Flag to indicate whether to use FP16 half-precision for TensorRT profiling.
        trt (bool): Flag to indicate whether to profile using TensorRT.
        device (torch.device): Device used for profiling.

    Methods:
        run: Profile YOLO models for speed and accuracy across various formats.
        get_files: Get all relevant model files.
        get_onnx_model_info: Extract metadata from an ONNX model.
        iterative_sigma_clipping: Apply sigma clipping to remove outliers.
        profile_tensorrt_model: Profile a TensorRT model.
        profile_onnx_model: Profile an ONNX model.
        generate_table_row: Generate a table row with model metrics.
        generate_results_dict: Generate a dictionary of profiling results.
        print_table: Print a formatted table of results.

    Examples:
        Profile models and print results
        >>> from ultralytics.utils.benchmarks import ProfileModels
        >>> profiler = ProfileModels(["yolo26n.yaml", "yolov8s.yaml"], imgsz=640)
        >>> profiler.run()
    d   
   <     TNpaths	list[str]num_timed_runsr   num_warmup_runsmin_timer   r>   r@   booltrtrC   torch.device | str | Nonec	                    || _         || _        || _        || _        || _        || _        || _        t          |t          j	                  r|nt          |          | _	        dS )a[  Initialize the ProfileModels class for profiling models.

        Args:
            paths (list[str]): List of paths of the models to be profiled.
            num_timed_runs (int): Number of timed runs for the profiling.
            num_warmup_runs (int): Number of warmup runs before the actual profiling starts.
            min_time (float): Minimum time in seconds for profiling a model.
            imgsz (int): Size of the image used during profiling.
            half (bool): Flag to indicate whether to use FP16 half-precision for TensorRT profiling.
            trt (bool): Flag to indicate whether to profile using TensorRT.
            device (torch.device | str | None): Device used for profiling. If None, it is determined automatically.

        Notes:
            FP16 'half' argument option removed for ONNX as slower on CPU than FP32.
        N)r  r  r  r  r>   r@   r  rf   torchrC   r   )	r   r  r  r  r  r>   r@   r  rC   s	            rb   r   zProfileModels.__init__  s`    4 
,. 
	 *65< @ @[ffmTZF[F[rd   c           	        |                                  }|st          j        d           g S g }g }|D ]}|                    d          }|j        dv rt          t          |                    }|                                 |                    | j	                  }| j
        rM| j        j        dk    r=|                                s)|                    d| j        | j	        | j        d          }|                    d	| j	        | j        d
          }n%|j        dk    r|                     |          }|}n	|                     t          |                    }	|                     t          |                    }
|                    |                     |j        |
|	|                     |                    |                     |j        |
|	|                     |                     |           |S )a  Profile YOLO models for speed and accuracy across various formats including ONNX and TensorRT.

        Returns:
            (list[dict]): List of dictionaries containing profiling results for each model.

        Examples:
            Profile models and print results
            >>> from ultralytics.utils.benchmarks import ProfileModels
            >>> profiler = ProfileModels(["yolo26n.yaml", "yolo11s.yaml"])
            >>> results = profiler.run()
        z'No matching *.pt or *.onnx files found.z.engine   .pt.yml.yamlr>   r    engineF)r?   r@   r>   rC   r'   onnx)r?   r>   rC   r'   z.onnx)	get_filesr   warningwith_suffixr   r   rp   fuser   r>   r  rC   rw   is_filer{   r@   get_onnx_model_infoprofile_tensorrt_modelprofile_onnx_modelr   generate_table_rowstemgenerate_results_dictprint_table)r   files
table_rowsoutputr   engine_filer   
model_info	onnx_filet_enginet_onnxs              rb   runzProfileModels.run  s       	NDEEEI
 	_ 	_D**955K{666SYY

"ZZdjZ99
8  0E 9 9+BUBUBWBW 9"',,'!Y"j#{ % #/ # #K "LL!*;!	 )  		 ''!55d;;
 		223{3C3CDDH,,S^^<<Fd55diS]^^___MM$44TYR\]]^^^^$$$rd   c                   g }| j         D ]t                                                    r&g d}|                    fd|D                        Kj        dv r#|                    t                               w|                    t          j        t                                         t          j	        dt          |                      d t          |          D             S )zReturn a list of paths for all relevant model files given by the user.

        Returns:
            (list[Path]): List of Path objects for the model files.
        )z*.ptz*.onnxz*.yamlc                b    g | ]+}t          j         t          |z                      D ]}|,S re   )globrp   )r_   extr   r   s      rb   r   z+ProfileModels.get_files.<locals>.<listcomp>  s=    ```sTYsSWZ]S]E_E_``Td````rd   r  zProfiling: c                ,    g | ]}t          |          S re   r   )r_   r   s     rb   r   z+ProfileModels.get_files.<locals>.<listcomp>  s    555tT

555rd   )r  r   is_dirr   r   r   rp   r:  r   r   sorted)r   r/  
extensionsr   s      @rb   r#  zProfileModels.get_files  s     J 	3 	3D::D{{}} 3999
````j```aaaa 888SYY''''TYs4yy1122221&--1122255ve}}5555rd   r4  rp   c                    dS )z\Extract metadata from an ONNX model file including layers, parameters, gradients, and FLOPs.)r   r   r   r   re   )r4  s    rb   r(  z!ProfileModels.get_onnx_model_info  s
     "!rd   rK   r   rB   
np.ndarraysigma	max_itersc                (   t          j        |           } t          |          D ]m}t          j        |           t          j        |           }}| | |||z  z
  k    | |||z  z   k     z           }t          |          t          |           k    r n|} n| S )a{  Apply iterative sigma clipping to data to remove outliers.

        Args:
            data (np.ndarray): Input data array.
            sigma (float): Number of standard deviations to use for clipping.
            max_iters (int): Maximum number of iterations for the clipping process.

        Returns:
            (np.ndarray): Clipped data array with outliers removed.
        )r]   arrayrangemeanstdr   )rB   rB  rC  r   rG  rH  clipped_datas          rb   iterative_sigma_clippingz&ProfileModels.iterative_sigma_clipping  s     x~~y!! 	  	 Arvd||#Dus{(:!:tdUUX[FX?X YZL<  CII--DDrd   r!   r2  r   c                h   | j         r!t          |                                          sdS t          |          }t	          j        | j        | j        dft          j                  }d}t          d          D ]U}t          j	                    }t          | j
                  D ]} ||| j        d           t          j	                    |z
  }Vt          t          | j        ||z   z  | j
        z            | j        dz            }g }	t          t          |          |          D ];} ||| j        d          }
|	                    |
d	         j        d
                    <|                     t	          j        |	          dd          }	t	          j        |	          t	          j        |	          fS )aw  Profile YOLO model performance with TensorRT, measuring average run time and standard deviation.

        Args:
            engine_file (str): Path to the TensorRT engine file.
            eps (float): Small epsilon value to prevent division by zero.

        Returns:
            (tuple[float, float]): Mean and standard deviation of inference time in milliseconds.
        )r   r   r   )dtyper   F)r>   r'   2   descr   rI   rK   rB  rC  )r  r   r'  r   r]   zerosr>   uint8rF  rq   r  maxr   r  r  r   r   r   rJ  rE  rG  rH  )r   r2  r   r   
input_dataelapsedr   
start_timenum_runs	run_timesr   s              rb   r)  z$ProfileModels.profile_tensorrt_model  s    x 	tK0088:: 	8 [!!Xtz4:q9JJJ
 q 	/ 	/AJ4/00 C Cj
EBBBBBikkJ.GG uT]gm<t?SSTTVZVilnVnoo 	eHooK888 	< 	<AeJdj%HHHGWQZ-k:;;;;11"(92E2EQZ[1\\	wy!!26)#4#444rd   c                6    t          d | D                        S )z<Check whether the tensor shape in the ONNX model is dynamic.c              3  L   K   | ]}t          |t                    o|d k    V   dS )r   N)rf   r   )r_   dims     rb   rc   z.ProfileModels.check_dynamic.<locals>.<genexpr>:  s5      QQSz#s++8qQQQQQQrd   )r   )tensor_shapes    rb   check_dynamiczProfileModels.check_dynamic7  s$     QQLQQQQQQQrd   c                   t          dg           ddl}|                                }|j        j        |_        d|_        |                    ||dg          }i }|                                D ]Y}|j	        }| 
                    |j                  rt          |j                  dk    rA| 
                    |j        dd                   rt          d	|j         d
|j                   t          |j                  dk    rdd| j        | j        fndg|j        dd         R }	n|j        }	d|v rt           j        }
nVd|v rt           j        }
nEd|v rt           j        }
n4d|v rt           j        }
n#d|v rt           j        }
nt          d|           t!          j        j        |	                     |
          }|j        }|||<   [|                                d         j        }d}t5          d          D ]Y}t7          j                    }t5          | j                  D ]}|                    |g|           t7          j                    |z
  }Zt=          t?          | j         ||z   z  | j        z            | j!                  }g }tE          t5          |          |          D ]X}t7          j                    }|                    |g|           |#                    t7          j                    |z
  dz             Y| $                    t!          j%        |          dd          }t!          j&        |          t!          j'        |          fS )at  Profile an ONNX model, measuring average inference time and standard deviation across multiple runs.

        Args:
            onnx_file (str): Path to the ONNX model file.
            eps (float): Small epsilon value to prevent division by zero.

        Returns:
            (tuple[float, float]): Mean and standard deviation of inference time in milliseconds.
        )onnxruntimezonnxruntime-gpur   N   CPUExecutionProvider)	providersrL   r$   zUnsupported dynamic shape z of r   float16r   doubleint64int32zUnsupported ONNX datatype r   rN  rJ   rK   r   rP  )(r   r_  SessionOptionsGraphOptimizationLevelORT_ENABLE_ALLgraph_optimization_levelintra_op_num_threadsInferenceSession
get_inputsrw   r]  shaper   
ValueErrorr   r>   r]   rc  float32float64re  rf  randomrandastypeget_outputsrF  rq   r  r7  rS  r   r  r  r   r   rJ  rE  rG  rH  )r   r4  r   ortsess_optionssessinput_data_dictinput_tensor
input_typeinput_shapeinput_dtyperT  
input_nameoutput_namerU  r   rV  rW  rX  s                      rb   r*  z ProfileModels.profile_onnx_model<  sx    	>?@@@!!!! ))++030J0Y-,-)##I|H^G_#`` OO-- 	5 	5L%*J!!,"455 1|)**a//D4F4F|GYZ[Z\Z\G]4^4^/$%m,BT%m%mZfZk%m%mnnn69,:L6M6MQR6R6RQ4:tz22YZXt]i]opqprpr]sXtXt  +0 J&& jJ&& jZ'' jJ&& hJ&& h !Jj!J!JKKK5<<[IIJ%*J*4OJ''&&((+0 q 	/ 	/AJ4/00 9 9+8888ikkJ.GG uT]gm<t?SSTTVZVijj 	eHooI666 	@ 	@AJHHk]O444dikkJ6$>????11"(92E2EQZ[1\\	wy!!26)#4#444rd   rz   r6  tuple[float, float]r5  r3  !tuple[float, float, float, float]c                    |\  }}}}d|dd| j          d|d         dd|d         dd	|d         dd|d         dd	|d
z  dd|ddS )a  Generate a table row string with model performance metrics.

        Args:
            model_name (str): Name of the model.
            t_onnx (tuple): ONNX model inference time statistics (mean, std).
            t_engine (tuple): TensorRT engine inference time statistics (mean, std).
            model_info (tuple): Model information (layers, params, gradients, flops).

        Returns:
            (str): Formatted table row string with model metrics.
        z| 18sz | z | - | r   z.1f   ±r$   z ms | g    .Az |r   )	r   rz   r6  r5  r3  _layersparams
_gradientsflopss	            rb   r+  z ProfileModels.generate_table_row  s    $ .8*UIp I IDJ I Ivayp I Iq	p I I]efg]hp I I{HI I&,slHI I;@HI I I	
rd   c                    |\  }}}}| |t          |d          t          |d         d          t          |d         d          dS )a  Generate a dictionary of profiling results.

        Args:
            model_name (str): Name of the model.
            t_onnx (tuple): ONNX model inference time statistics (mean, std).
            t_engine (tuple): TensorRT engine inference time statistics (mean, std).
            model_info (tuple): Model information (layers, params, gradients, flops).

        Returns:
            (dict): Dictionary containing profiling results.
        r   r   )z
model/namezmodel/parameterszmodel/GFLOPszmodel/speed_ONNX(ms)zmodel/speed_TensorRT(ms))r   )rz   r6  r5  r3  r  r  r  r  s           rb   r-  z#ProfileModels.generate_results_dict  sT    $ .8*U$ &!%OO$)&)Q$7$7(-hqk1(=(=
 
 	
rd   r0  c                   t           j                                        rt           j                            d          nd}ddddt	                       dd| d	d
dg}dd                    d |D                       z   dz   }dd                    d |D                       z   dz   }t          j        d|            t          j        |           | D ]}t          j        |           dS )zPrint a formatted table of model profiling results.

        Args:
            table_rows (list[str]): List of formatted table row strings.
        r   GPUModelzsize<br><sup>(pixels)zmAP<sup>val<br>50-95zSpeed<br><sup>CPU (z) ONNX<br>(ms)zSpeed<br><sup>z TensorRT<br>(ms)zparams<br><sup>(M)zFLOPs<br><sup>(B)|c              3  "   K   | ]
}d | d V  dS )rS   Nre   r_   hs     rb   rc   z,ProfileModels.print_table.<locals>.<genexpr>  s*      ::QA::::::rd   c              3  @   K   | ]}d t          |          dz   z  V  dS )r=   rK   N)r   r  s     rb   rc   z,ProfileModels.print_table.<locals>.<genexpr>  s1      "G"G!3#a&&1*#5"G"G"G"G"G"Grd   z

N)r  r<   is_availableget_device_namer   joinr   r   )r0  r   headersheader	separatorrP   s         rb   r.  zProfileModels.print_table  s    05z/F/F/H/HSej((+++e#"@,..@@@3S333 
 sxx::'::::::S@#(("G"Gw"G"G"GGGG#M	O6OO$$$I 	 	CK	 	rd   )r  r  r  r  TTN)r  r  r  r   r  r   r  r   r>   r   r@   r  r  r  rC   r  )r4  rp   )rK   r   )rB   rA  rB  r   rC  r   )r!   )r2  rp   r   r   )r4  rp   r   r   )rz   rp   r6  r  r5  r  r3  r  )r0  r  )r  r  r  r	  r   r7  r#  r
  r(  rJ  r)  r]  r*  r+  r-  r.  re   rd   rb   r  r  j  sd        F "!,0!\ !\ !\ !\ !\F4 4 4l6 6 6( " " " \"     \(#5 #5 #5 #5 #5J R R \RF5 F5 F5 F5 F5P
 
 
 
0 
 
 
 \
4    \  rd   r  )4r	  
__future__r   r:  r   r|   r   r   rq   copyr   pathlibr   numpyr]   
torch.cudar  ultralyticsr   r   ultralytics.cfgr	   r
   ultralytics.engine.exporterr   ultralytics.utilsr   r   r   r   r   r   r   r   r   r   ultralytics.utils.checksr   r   r   r   r   ultralytics.utils.downloadsr   ultralytics.utils.filesr   ultralytics.utils.torch_utilsr   r   r   r   r  re   rd   rb   <module>r     s-   : # " " " " "  				  				                       ' ' ' ' ' ' ' ' 2 2 2 2 2 2 2 2 6 6 6 6 6 6 q q q q q q q q q q q q q q q q q q q q q q q q m m m m m m m m m m m m m m 5 5 5 5 5 5 - - - - - - E E E E E E E E 
$	
		c c c cLJ J J J J J J JZd d d d d d d d d drd   