
    FPh)G                         d 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Zddl	Z
ddl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 ddlmZmZ dd	lmZ dd
lmZ edz  ddddddfdZ  G d d      Z!y)a  
Benchmark a YOLO model formats for speed and accuracy.

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

Format                  | `format=argument`         | Model
---                     | ---                       | ---
PyTorch                 | -                         | yolov8n.pt
TorchScript             | `torchscript`             | yolov8n.torchscript
ONNX                    | `onnx`                    | yolov8n.onnx
OpenVINO                | `openvino`                | yolov8n_openvino_model/
TensorRT                | `engine`                  | yolov8n.engine
CoreML                  | `coreml`                  | yolov8n.mlpackage
TensorFlow SavedModel   | `saved_model`             | yolov8n_saved_model/
TensorFlow GraphDef     | `pb`                      | yolov8n.pb
TensorFlow Lite         | `tflite`                  | yolov8n.tflite
TensorFlow Edge TPU     | `edgetpu`                 | yolov8n_edgetpu.tflite
TensorFlow.js           | `tfjs`                    | yolov8n_web_model/
PaddlePaddle            | `paddle`                  | yolov8n_paddle_model/
ncnn                    | `ncnn`                    | yolov8n_ncnn_model/
    N)Path)YOLO)	TASK2DATATASK2METRIC)export_formats)ASSETSLINUXLOGGERMACOSTQDMWEIGHTS_DIR)check_requirements
check_yolo)	file_size)select_devicez
yolov8n.pt   Fcpuc                    ddl dj                  j                  _        dj                  j                  _        t        |d      }t        | t        t        f      rt        |       } g }t        j                         }t               j                         D ]  \  }	\  }
}}}}d\  }}	 |	dk7  st        sJ d	       |	dk(  rt        s,t        s&J d
       |	dk(  rt        j                   dk  sJ d       d|j"                  v r	|sJ d       d|j"                  v r	|sJ d       |dk(  r| j$                  xs | j&                  }| }nB| j)                  |||||d      }t        || j*                        }|t        |      v sJ d       d}| j*                  dk7  s|	dk7  sJ d       |	dvsJ d       |	dk7  st-        j.                         dk(  sJ d       |j1                  t2        dz  |||        |xs t4        | j*                     }t6        | j*                     }|j9                  |d!|d|||d"      }|j:                  |   |j<                  d#   }}|j?                  |
d$tA        tC        |      d!      tA        |d%      tA        |d&      g        tM        |*       jO                  |d+d,d-d.g/      }t        | j$                        jP                  }
d0|
 d1| d2| d3t        j                         |z
  d4d5| d6}tI        jR                  |       tU        d7d8d9d:;      5 }|jW                  |       ddd       |r@t        |tX              r0||   jZ                  }|t]        fd<|D              s
J d=        |S # tD        $ ro}|rt#        |      tF        u sJ d'|
 d(|        tI        jJ                  d)|
 d(|        |j?                  |
|tA        tC        |      d!      ddg       Y d}~Vd}~ww xY w# 1 sw Y   xY w)>a  
    Benchmark a YOLO model across different formats for speed and accuracy.

    Args:
        model (str | Path | optional): Path to the model file or directory. Default is
            Path(SETTINGS['weights_dir']) / 'yolov8n.pt'.
        data (str, optional): Dataset to evaluate on, inherited from TASK2DATA if not passed. Default is None.
        imgsz (int, optional): Image size for the benchmark. Default is 160.
        half (bool, optional): Use half-precision for the model if True. Default is False.
        int8 (bool, optional): Use int8-precision for the model if True. Default is False.
        device (str, optional): Device to run the benchmark on, either 'cpu' or 'cuda'. Default is 'cpu'.
        verbose (bool | float | optional): If True or a float, assert benchmarks pass with given metric.
            Default is False.

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

    Example:
        ```python
        from ultralytics.utils.benchmarks import benchmark

        benchmark(model='yolov8n.pt', imgsz=640)
        ```
    r   N
   x   F)verbose)u   ❌N	   z'Edge TPU export only supported on Linuxz.TF.js export only supported on macOS and Linux   )   r   z2PaddlePaddle export only supported on Python<=3.10r   zinference not supported on CPUcudazinference not supported on GPU-)imgszformathalfint8devicer   )taskzexport failedu   ❎pose   z(GraphDef Pose inference is not supported)r   r   zinference not supported   Darwinz(inference only supported on macOS>=10.13zbus.jpg)r   r!   r      )databatchr   plotsr!   r   r    r   	inferenceu   ✅      zBenchmark failure for z: u#   ERROR ❌️ Benchmark failure for )r!   Formatu	   Status❔z	Size (MB)zInference time (ms/im))columnsz
Benchmarks complete for z on z
 at imgsz=z (.2fzs)

zbenchmarks.logaignorezutf-8)errorsencodingc              3   L   K   | ]  }j                  |      s|kD    y w)N)notna).0xfloorpds     gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/utils/benchmarks.py	<genexpr>zbenchmark.<locals>.<genexpr>   s      =g!1u9gs   $
$z%Benchmark failure: metric(s) < floor )/pandasoptionsdisplaymax_columnswidthr   
isinstancestrr   r   timer   iterrowsr	   r   sysversion_infotype	ckpt_pathcfgexportr"   platformsystempredictr   r   r   valresults_dictspeedappendroundr   	ExceptionAssertionErrorr
   warningr   	DataFramenameinfoopenwritefloatarrayall)modelr(   r   r   r    r!   r   yt0irY   r   suffixr   gpuemojifilenameexported_modelkeyresultsmetricrR   edfsfmetricsr:   r;   s                              @@r<   	benchmarkrq   -   s
   B %'BJJ""BJJ651F%#t%U
A	B/=/?/H/H/J++D&&#s%x,	O6UM$MM?BwW'WW~b'''1g3gg1#<<<s$<<<s } ??7eii!& <<eFTXagqv<w!%hUZZ!@X.??.E ::'16]3]]1G#>%>>#6X__.(:f<ff:""6I#5U6X\"] 09UZZ0Dejj)C$((d/0/4/406.2.216 ) 8G $005w}}[7QEFHHdE58)<a#@%PQBRTYZ_abTcdeS 0Kb f	a(KcKc!d	eB %%D
$TF$tfJugR		VXHXY\G]]abdaeeghA
KKN	Hw	G1	
 
H :gu-S'--=g==nAfglfm?nn=I+  	OAw.0V4J4&PRSTRU2VV0NN@bLMHHdE58)<a#@$MNN		O 
H	Gs&   ,G M<O7<	O4A$O//O47P c                       e Zd ZdZ	 	 	 	 	 	 	 ddefdZd Zd ZdefdZ	dd	Z
dd
edefdZddedefdZd Zd Zd Zy)ProfileModelsaz  
    ProfileModels class for profiling different models on ONNX and TensorRT.

    This class profiles the performance of different models, provided their paths. The profiling includes parameters such as
    model speed and FLOPs.

    Attributes:
        paths (list): Paths of the models to profile.
        num_timed_runs (int): Number of timed runs for the profiling. Default is 100.
        num_warmup_runs (int): Number of warmup runs before profiling. Default is 10.
        min_time (float): Minimum number of seconds to profile for. Default is 60.
        imgsz (int): Image size used in the models. Default is 640.

    Methods:
        profile(): Profiles the models and prints the result.

    Example:
        ```python
        from ultralytics.utils.benchmarks import ProfileModels

        ProfileModels(['yolov8n.yaml', 'yolov8s.yaml'], imgsz=640).profile()
        ```
    Npathsc	                     || _         || _        || _        || _        || _        || _        || _        |xs5 t        j                  t        j                  j                         rdnd      | _        y)a  
        Initialize the ProfileModels class for profiling models.

        Args:
            paths (list): List of paths of the models to be profiled.
            num_timed_runs (int, optional): Number of timed runs for the profiling. Default is 100.
            num_warmup_runs (int, optional): Number of warmup runs before the actual profiling starts. Default is 10.
            min_time (float, optional): Minimum time in seconds for profiling a model. Default is 60.
            imgsz (int, optional): Size of the image used during profiling. Default is 640.
            half (bool, optional): Flag to indicate whether to use half-precision floating point for profiling. Default is True.
            trt (bool, optional): Flag to indicate whether to profile using TensorRT. Default is True.
            device (torch.device, optional): Device used for profiling. If None, it is determined automatically. Default is None.
        r   r   N)rt   num_timed_runsnum_warmup_runsmin_timer   r   trttorchr!   r   is_available)	selfrt   rv   rw   rx   r   r   ry   r!   s	            r<   __init__zProfileModels.__init__   s^    , 
,. 
	W%**2I2I2KQQV W    c           	         | j                         }|st        d       yg }g }|D ]  }|j                  d      }|j                  dv rt	        t        |            }|j                          |j                         }| j                  r]| j                  j                  dk7  rD|j                         s4|j                  d| j                  | j                  | j                  d      }|j                  d	| j                  | j                  d
| j                  d      }n%|j                  dk(  r| j                  |      }|}n| j!                  t        |            }	| j#                  t        |            }
|j%                  | j'                  |j(                  |
|	|             |j%                  | j+                  |j(                  |
|	|              | j-                  |       |S )z_Logs the benchmarking results of a model, checks metrics against floor and returns the results.z'No matching *.pt or *.onnx files found.Nz.engine).pt.yaml.ymlr   engineF)r   r   r   r!   r   onnxT)r   r   r   simplifyr!   r   z.onnx)	get_filesprintwith_suffixrd   r   rD   fuserZ   ry   r!   rI   is_filerL   r   r   get_onnx_model_infoprofile_tensorrt_modelprofile_onnx_modelrS   generate_table_rowstemgenerate_results_dictprint_table)r|   files
table_rowsoutputfileengine_filer`   
model_info	onnx_filet_enginet_onnxs              r<   profilezProfileModels.profile   s    ;<
D**95K{{66SY

"ZZ\
88 0 0E 9+BUBUBW"',,h48II59ZZ6:kk7<	 #/ #>K
 "LL.2ii/3zz260416 ) 8	 '!55d;
 	223{3CDH,,S^<Fd55diiS]^_MM$44TYYR\]^7 : 	$r~   c                 *   g }| j                   D ]  }t        |      }|j                         rLg d}|j                  |D cg c]*  }t	        j                  t        ||z              D ]  }| , c}}       j|j                  dv r|j                  t        |             |j                  t	        j                  t        |                    t        dt        |              t        |      D cg c]  }t        |       c}S c c}}w c c}w )zGReturns a list of paths for all relevant model files given by the user.)z*.ptz*.onnxz*.yaml>   r   r   r   zProfiling: )
rt   r   is_dirextendglobrD   rd   rS   r   sorted)r|   r   path
extensionsextr   s         r<   r   zProfileModels.get_files   s    JJD:D{{}9
j`jsTYYsSWZ]S]E_TdE_dj`a 88SY'TYYs4y12  	F5M?+,'-e}5}tT
}55 a 6s   /D
5Dr   c                      y)z~Retrieves the information including number of layers, parameters, gradients and FLOPs for an ONNX model
        file.
        )        r   r   r    )r|   r   s     r<   r   z!ProfileModels.get_onnx_model_info  s    
 "r~   c                    t        j                  |      }t        |      D ]b  }t        j                  |      t        j                  |      }}|||||z  z
  kD  ||||z  z   k  z     }t        |      t        |      k(  r |S |}d |S )z[Applies an iterative sigma clipping algorithm to the given data times number of iterations.)npr^   rangemeanstdlen)r|   r(   sigma	max_iters_r   r   clipped_datas           r<   iterative_sigma_clippingz&ProfileModels.iterative_sigma_clipping  s    xx~y!Arvvd|#Dus{(:!:tdUUX[FX?X YZL< CI-  D " r~   r   epsc                    | j                   rt        |      j                         syt        |      }t        j
                  j                  | j                  | j                  d      j                  t        j                        }d}t        d      D ]\  }t        j                         }t        | j                        D ]  } ||| j                  d        t        j                         |z
  }^ t        t        | j                  ||z   z  | j                  z        | j                   dz        }g }	t#        t        |      |      D ]8  } ||| j                  d      }
|	j%                  |
d   j&                  d	          : | j)                  t	        j*                  |	      d
d      }	t	        j,                  |	      t	        j.                  |	      fS )zZProfiles the TensorRT model, measuring average run time and standard deviation among runs.)r   r   r   r   F)r   r   2   descr   r+   r-   r   r   )ry   r   r   r   r   randomrandr   astypefloat32r   rE   rw   maxrT   rx   rv   r   rS   rR   r   r^   r   r   )r|   r   r   r`   
input_dataelapsedr   
start_timenum_runs	run_timesrj   s              r<   r   z$ProfileModels.profile_tensorrt_model  sm   xxtK088: [!YY^^DJJ

A>EEbjjQ
 qAJ4//0j

EB 1iikJ.G	  uT]]gm<t?S?SSTVZViVilnVno 	eHoK8AJdjj%HGWQZ--k:; 9 11"((92EQZ[1\	wwy!266)#444r~   c                 f   t        d       ddl}|j                         }|j                  j                  |_        d|_        |j                  ||dg      }|j                         d   }|j                  }d|v rt        j                  }nbd|v rt        j                  }nMd	|v rt        j                  }n8d
|v rt        j                  }n#d|v rt        j                  }nt!        d|       t        j"                  j$                  |j&                   j)                  |      }	|j*                  }
|j-                         d   j*                  }d}t/        d      D ]\  }t1        j0                         }t/        | j2                        D ]  }|j5                  |g|
|	i        t1        j0                         |z
  }^ t7        t9        | j:                  ||z   z  | j2                  z        | j<                        }g }t?        t/        |      |      D ]T  }t1        j0                         }|j5                  |g|
|	i       |jA                  t1        j0                         |z
  dz         V | jC                  t        jD                  |      dd      }t        jF                  |      t        jH                  |      fS )zProfiles an ONNX model by executing it multiple times and returns the mean and standard deviation of run
        times.
        onnxruntimer   N   CPUExecutionProvider)	providersfloat16r]   doubleint64int32zUnsupported ONNX datatype r   r   r   i  r-   r%   r   )%r   r   SessionOptionsGraphOptimizationLevelORT_ENABLE_ALLgraph_optimization_levelintra_op_num_threadsInferenceSession
get_inputsrI   r   r   r   float64r   r   
ValueErrorr   r   shaper   rY   get_outputsr   rE   rw   runr   rT   rx   rv   r   rS   r   r^   r   r   )r|   r   r   ortsess_optionssessinput_tensor
input_typeinput_dtyper   
input_nameoutput_namer   r   r   r   r   s                    r<   r   z ProfileModels.profile_onnx_model:  sI    	=)! ))+030J0J0Y0Y-,-)##I|H^G_#`(+!&&
 
"**K
"**K#**K
"((K
"((K9*FGGYY^^\%7%78??L
!&&
&&(+00 qAJ4//0+Z(@A 1iikJ.G	  uT]]gm<t?S?SSTVZViVij 	eHoI6AJHHk]Z$<=diikJ6$>? 7
 11"((92EQZ[1\	wwy!266)#444r~   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 )z`Generates a formatted string for a table row that includes model performance and metric details.z| 18sz | z | - | r   r0   u    ± r'   z ms | g    .Az.1fz |)r   	r|   
model_namer   r   r   layersparams	gradientsflopss	            r<   r   z ProfileModels.generate_table_rowq  s    +5(	5Js#3tzzl'&)CVTUYWZO[abjklbmnqarrvw  AB  xC  DG  wH  HN  OU  X[  O[  \_  N`  `c  di  jm  cn  np  q  	qr~   c                 h    |\  }}}}||t        |d      t        |d   d      t        |d   d      dS )z]Generates a dictionary of model details including name, parameters, GFLOPS and speed metrics.r   r   )z
model/namezmodel/parameterszmodel/GFLOPszmodel/speed_ONNX(ms)zmodel/speed_TensorRT(ms))rT   r   s	            r<   r   z#ProfileModels.generate_results_dictv  sE    +5(	5$ &!%O$)&)Q$7(-hqk1(=? 	?r~   c                     t         j                  j                         rt         j                  j                  d      nd}d| d}d}t	        d|        t	        |       |D ]  }t	        |        y)zfFormats and prints a comparison table for different models with given statistics and performance data.r   GPUzh| Model | size<br><sup>(pixels) | mAP<sup>val<br>50-95 | Speed<br><sup>CPU ONNX<br>(ms) | Speed<br><sup>z< TensorRT<br>(ms) | params<br><sup>(M) | FLOPs<br><sup>(B) |z|-------------|---------------------|--------------------|------------------------------|-----------------------------------|------------------|-----------------|z

N)rz   r   r{   get_device_namer   )r|   r   re   header	separatorrows         r<   r   zProfileModels.print_table  sr    /4zz/F/F/Hejj((+e{|  |A  A}  ~ y	VHoiC#J r~   )d   r   <   i  TTN)r-   r   )gMbP?)__name__
__module____qualname____doc__listr}   r   r   rD   r   r   r]   r   r   r   r   r   r   r~   r<   rs   rs      s    4 !$!#XX>(T6 "S "	5# 5E 5:55C 55e 55nq
?	r~   rs   )"r   r   rM   rG   rE   pathlibr   numpyr   
torch.cudarz   ultralyticsr   ultralytics.cfgr   r   ultralytics.engine.exporterr   ultralytics.utilsr   r	   r
   r   r   r   ultralytics.utils.checksr   r   ultralytics.utils.filesr   ultralytics.utils.torch_utilsr   rq   rs   r   r~   r<   <module>r      sh   2   
      2 6 M M C - 7  ,.iXp pr~   