
    |h%                        d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
Z
d dlmZmZ d dlmZ d dlmZmZ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
 Zd Ze
j@                  jC                  e d      d        Z"e
j@                  jF                  e
j@                  jC                  e d      e
j@                  jI                  d eeddgddgddgddgddg      D  cg c]  \  } }}}}}|r|s| dk(  r|s| |||||f c}}}}}}       d                      Z%e
j@                  jF                  e
j@                  jI                  d eeddgdgdgddgddgddg      D  cg c]'  \  } }}}}}}|r|s| dk(  r|s| dk(  r|rer	| ||||||f) c}}}}}}}       d               Z&e
j@                  jF                  e
j@                  jI                  d eedgdgdgddgddg      D  cg c]  \  } }}}}}| dk(  r|s| |||||f c}}}}}}       d               Z'e
j@                  jF                  e
j@                  jC                  e d      e
j@                  jC                  e d      e
j@                  jC                  ejP                  d      e
j@                  jI                  d eedgddgddgdg      D  cg c]  \  } }}}}|r|s| ||||f c}}}}}       d                                    Z)e
j@                  jF                  e
j@                  jC                  ejT                   d      e
j@                  jC                  e xs ed       e
j@                  jI                  d eedgddgddgdgddg      D  cg c]   \  } }}}}}|r|s| dk(  r|ser|s| |||||f" c}}}}}}       d!                             Z+e
j@                  jC                  e d      e
j@                  jC                  ed"      e
j@                  jC                  exr ed#      e
j@                  jC                  ejP                  d      d$                             Z,e
j@                  jC                  ejT                   d      e
j@                  jC                  e d%      d&               Z-e
j@                  jC                  dd'      e
j@                  jC                  e d(      d)               Z.e
j@                  jC                  dd*      d+        Z/e
j@                  jF                  d,        Z0e
j@                  jF                  d-        Z1e
j@                  jC                  dd.      e
j@                  jC                  e xs ed/      d0               Z2yc c}}}}}} w c c}}}}}}} w c c}}}}}} w c c}}}}} w c c}}}}}} w )1    N)redirect_stderrredirect_stdout)product)Path)MODELSOURCE)YOLO)	TASK2DATA
TASK2MODELTASKS)ARM64IS_RASPBERRYPILINUXMACOSWINDOWSchecks)	TORCH_1_9
TORCH_1_13c                  t    t        t              j                  ddd      }  t        |       t        d       y)zOTest YOLO model export to TorchScript format for compatibility and correctness.torchscriptF    )formatoptimizeimgszr   Nr	   r   exportr   files    Q/var/www/html/test/engine/venv/lib/python3.12/site-packages/tests/test_exports.pytest_export_torchscriptr!      s-    ;]U"MDDJvR     c                  t    t        t              j                  ddd      }  t        |       t        d       y)z8Test YOLO model export to ONNX format with dynamic axes.onnxTr   )r   dynamicr   r   Nr   r   s    r    test_export_onnxr&       s-    ;VTDDDJvR r"   zOpenVINO requires torch>=1.13)reasonc                  r    t        t              j                  dd      }  t        |       t        d       y)zFTest YOLO export to OpenVINO format for model inference compatibility.openvinor   r   r   r   Nr   r   s    r    test_export_openvinor+   &   s-     ;Zr:DDJvR r"   z%task, dynamic, int8, half, batch, nmsTF      classifyc           
      t   t        t        |          j                  dd||||t        |    |      }t        rLt        |      }|j                  |j                  |j                   dt        j                                      } t        |      t        g|z  |rdnd       t        j                  |d       y	)
zQTest YOLO model export to OpenVINO under various configuration matrix conditions.r)   r   )r   r   r%   int8halfbatchdatanms-@   r   T)ignore_errorsN)r	   r   r   r
   r   r   rename	with_stemstemuuiduuid4r   shutilrmtreetaskr%   r0   r1   r2   r4   r   s          r    test_export_openvino_matrixrA   -   s     
4 !((t_ ) 	D  Dz{{4>>TYYKq*GHIDJx%Wr"=
MM$d+r"   z/task, dynamic, int8, half, batch, simplify, nmsobbc           
          t        t        |          j                  dd||||||      } t        |      t        g|z  |rdnd       t	        |      j                          y)zKTest YOLO export to ONNX format with various configurations and parameters.r$   r   )r   r   r%   r0   r1   r2   simplifyr4   r6   r   Nr	   r   r   r   r   unlink)r@   r%   r0   r1   r2   rD   r4   r   s           r    test_export_onnx_matrixrG   N   sc     
4 !((Rt$e^flo ) D DJx%Wr"=Jr"   c           	          t        t        |          j                  dd|||||      } t        |      t        g|z  |rdnd       t	        |      j                          y)zITest YOLO model export to TorchScript format under varied configurations.r   r   r   r   r%   r0   r1   r2   r4   r6   r   NrE   r?   s          r    test_export_torchscript_matrixrJ   b   sa     
4 !((BdUZ`c ) D DJx%Wr"=Jr"   z(CoreML inference only supported on macOSz+CoreML>=7.2 not supported with PyTorch<=1.8z#CoreML not supported in Python 3.13z task, dynamic, int8, half, batchc                     t        t        |          j                  dd||||      } t        |      t        g|z  d       t	        j
                  |       y)zHTest YOLO export to CoreML format with various parameter configurations.coremlr   )r   r   r%   r0   r1   r2   r   N)r	   r   r   r   r=   r>   )r@   r%   r0   r1   r2   r   s         r    test_export_coreml_matrixrM   t   sZ     
4 !(( ) D DJx%r*
MM$r"   z#TFLite export requires Python>=3.10zUTest disabled as TF suffers from install conflicts on Windows, macOS and Raspberry Pic           	          t        t        |          j                  dd|||||      } t        |      t        g|z  d       t	        |      j                          y)zLTest YOLO export to TFLite format considering various export configurations.tfliter   rI   r   NrE   r?   s          r    test_export_tflite_matrixrP      s]    $ 
4 !((r7DPU[^ ) D DJx%r*Jr"   zCoreML not supported on Windowsz%CoreML not supported on aarch64 Linuxc                     t        j                         t        j                         }} t        |       5  t        |      5  t	        t
              j                  ddd       t        r7t	        t
              j                  dd      } t	        |      t        d       ddd       ddd       | j                         |j                         z   }d|vs
J d	|        d
|vsJ d       y# 1 sw Y   LxY w# 1 sw Y   PxY w)z7Test YOLO export to CoreML format and check for errors.rL   Tr   )r   r4   r   r*   r   NErrorzCoreML export produced errors: z%You will not be able to run predict()z!CoreML export has predict() error)
ioStringIOr   r   r	   r   r   r   r   getvalue)stdoutstderrr   outputs       r    test_export_coremlrY      s     [[]BKKMFF		  )/&"9 )U(B?;%%XR%@DDJvR(	) ) __!22F& L$CF8"LL 2&@eBee@) ) ) )s$   C6 AC*C6*C3	/C66C?zGTest disabled as TF suffers from install conflicts on Windows and macOSc                  v    t        t              } | j                  dd      } t        |      t        d       y)zRTest YOLO export to TFLite format under specific OS and Python version conditions.rO   r   r*   r   Nr   modelr   s     r    test_export_tfliter]      s/     KE<<xr<2DDJvR r"   zTest disabledz6TF suffers from install conflicts on Windows and macOSc                  v    t        t              } | j                  dd      } t        |      t        d       y)z8Test YOLO export to TensorFlow's Protobuf (*.pb) format.pbr   r*   r   Nr   r[   s     r    test_export_pbr`      s/     KE<<t2<.DDJvR r"   zITest disabled as Paddle protobuf and ONNX protobuf requirements conflict.c                  D    t        t              j                  dd       y)zGTest YOLO export to Paddle format, noting protobuf conflicts with ONNX.paddler   r*   N)r	   r   r    r"   r    test_export_paddlerd      s     	Khb1r"   c                  r    t        t              j                  dd      }  t        |       t        d       y)zaTest YOLO export to MNN format (WARNING: MNN test must precede NCNN test or CI error on Windows).mnnr   r*   r   Nr   r   s    r    test_export_mnnrg      s-     ;U"5DDJvR r"   c                  r    t        t              j                  dd      }  t        |       t        d       y)z Test YOLO export to NCNN format.ncnnr   r*   r   Nr   r   s    r    test_export_ncnnrj      s-     ;V26DDJvR r"   zKTest disabled as keras and tensorflow version conflicts with TFlite export.z"Skipping test on Windows and Macosc                  n    t        d      } | j                  dd      } t        |      t        d       y)zTest YOLO export to IMX format.z
yolov8n.ptimxr   r*   r   N)r	   r   r   r[   s     r    test_export_imxrm      s0     E<<uB</DDJvR r"   )3rS   r=   r;   
contextlibr   r   	itertoolsr   pathlibr   pytesttestsr   r   ultralyticsr	   ultralytics.cfgr
   r   r   ultralytics.utilsr   r   r   r   r   r   ultralytics.utils.torch_utilsr   r   r!   r&   markskipifr+   slowparametrizerA   rG   rJ   IS_PYTHON_3_13rM   IS_PYTHON_MINIMUM_3_10rP   rY   r]   r`   rd   rg   rj   rm   )r@   r%   r0   r1   r2   r4   rD   s   0000000r    <module>r}      sw   
   7      8 8  @!! 
N+JK! L! 
N+JK+ 6=D%=4-$Au6
 1D'4uc $DJ$63	 
wdE3/	,	 L ,* 5 @GD%=5'E7QFT5MDRW=@
 ;D'4uh $DJ$63DEMVYbl	 
wdE8S9 		  + 6=UUGeWW\V]`acd_ehlnsgt5u 1D'4uc
"s 
wdE3/  I&PQ	M*WXF))2WX& 18wuX\^cWdghfi0j ,D'4u 
wdE* Y Y R  555>cdIb   + 6=E7T5MD%=1#e}6
 1D'4uc $DJ$63Ec	 
wdE3/			 e   	M*WXG$EFEOe,STF))2WXf Y U G Yf  555>cdI&op! q e! D1I&^_! ` 2! D!lm2 n2
 ! ! ! ! D!noI&/ST! U p!e@ (*8s   %!V+,V(4V3
V=%W