
    .h[+                     Z   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mZ d
 Zd Z e
jB                  jE                  e d      d        Z#e
jB                  jH                  e
jB                  jE                  e d      e
jB                  jK                  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
jB                  jH                  e
jB                  jK                  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|rer	| ||||||f$ c}}}}}}}       d               Z'e
jB                  jH                  e
jB                  jK                  d eeddgdgddgddgddg      D  cg c]  \  } }}}}}| dk(  r|s| |||||f c}}}}}}       d               Z(e
jB                  jH                  e
jB                  jE                  e d      e
jB                  jE                  e d      e
jB                  jE                  ejR                  d      e
jB                  jK                  d eeddgddgddgddgdg      D  cg c]'  \  } }}}}}|r|s| dk7  r|s|r|s| dk(  r|s| |||||f) c}}}}}}       d                                    Z*e
jB                  jH                  e
jB                  jE                  ejV                   d      e
jB                  jE                  e xs ed       e
jB                  jK                  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|rer| |||||f& c}}}}}}       d!                             Z,e
jB                  jE                  e d      e
jB                  jE                  ed"      e
jB                  jE                  exr ed#      e
jB                  jE                  ejR                  d      d$                             Z-e
jB                  jE                  ejV                   d      e
jB                  jE                  e d%      d&               Z.e
jB                  jE                  dd'      e
jB                  jE                  e d(      d)               Z/e
jB                  jE                  dd*      d+        Z0e
jB                  jH                  d,        Z1e
jB                  jH                  e
jB                  jK                  d- eeddgddgddg      D  cg c]  \  } }}}|r|s| |||f c}}}}       d.               Z2e
jB                  jH                  d/        Z3e
jB                  jH                  e
jB                  jK                  d0 e4 eeddgdg                  d1               Z5e
jB                  jE                  dd2      e
jB                  jE                  e xs ed3      d4               Z6yc c}}}}}} w c c}}}}}}} w c c}}}}}} w c c}}}}}} w c c}}}}}} w c c}}}} w )5    N)redirect_stderrredirect_stdout)product)Path)MODELSOURCE)YOLO)	TASK2DATA
TASK2MODELTASKS)ARM64IS_RASPBERRYPILINUXMACOSWINDOWSchecks)
TORCH_1_11
TORCH_1_13	TORCH_2_1c                  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    P/var/www/html/ai-service/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>=2.1)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           
      v   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   r3   Tignore_errorsN)r	   r   r   r
   r   r   rename	with_stemstemuuiduuid4r   shutilrmtreetaskr&   r1   r2   r3   r5   r    s          r!   test_export_openvino_matrixrC   -   s     
4 !((t_ ) 	D  Dz{{4>>TYYKq*GHIDJx%Wr"EJ
MM$d+r#   z/task, dynamic, int8, half, batch, simplify, nmsc           
          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&   r1   r2   r3   simplifyr5   r7   r   Nr	   r   r   r   r   unlink)rB   r&   r1   r2   r3   rE   r5   r    s           r!   test_export_onnx_matrixrH   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&   r1   r2   r3   r5   r7   r   NrF   rA   s          r!   test_export_torchscript_matrixrK   b   sa     
4 !((BdUZ`c ) D DJx%Wr"=Jr#   z(CoreML inference only supported on macOSz"CoreML export requires torch>=1.11z#CoreML not supported in Python 3.13z%task, dynamic, int8, half, nms, batchdetectc           	          t        t        |          j                  dd|||||      } t        |      t        g|z  d       t	        j
                  |       y)zHTest YOLO export to CoreML format with various parameter configurations.coremlr   rJ   r   Nr	   r   r   r   r?   r@   )rB   r&   r1   r2   r5   r3   r    s          r!   test_export_coreml_matrixrP   v   s]    & 
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   rJ   r   NrF   rA   s          r!   test_export_tflite_matrixrS      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.rN   Tr   )r   r5   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_coremlr\      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.rR   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_pbrc      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_paddlerg      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_mnnrj      s-     ;U"5DDJvR r#   ztask, int8, half, batchc                     t        t        |          j                  dd|||      } t        |      t        g|z  d       t	        |      j                          y)zITest YOLO export to MNN format considering various export configurations.ri   r   )r   r   r1   r2   r3   r   NrF   )rB   r1   r2   r3   r    s        r!   test_export_mnn_matrixrl      sR     
4 !((RdQU]b(cDDJx%r*J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_ncnnro      s-     ;V26DDJvR r#   ztask, half, batchc                     t        t        |          j                  dd||      } t        |      t        g|z  d       t	        j
                  |d       y)zJTest YOLO export to NCNN format considering various export configurations.rn   r   )r   r   r2   r3   r   Tr8   NrO   )rB   r2   r3   r    s       r!   test_export_ncnn_matrixrq      sM     
4 !((btSX(YDDJx%r*
MM$d+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_imxrt     s0     E<<uB</DDJvR r#   )7rV   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"   r'   markskipifr,   slowparametrizerC   rH   rK   IS_PYTHON_3_13rP   IS_PYTHON_MINIMUM_3_10rS   r\   r`   rc   rg   rj   rl   ro   listrq   rt   )rB   r&   r1   r2   r3   r5   rE   s   0000000r!   <module>r      s{   
   7      8 8  L K!! 	M*HI! J! 	M*HI+ 6=D%=4-$Au6
 1D'4uc $DJ$63	 
wdE3/	,	 J ,* 5 @GD%=5'E7QFT5MDRW=@
 ;D'4uh $DJ$63CPZ	 
wdE8S9 		  + 6=E4=5'E4=1a&4-6
 1D'4uc 
"s	 
wdE3/		  I&PQ
N+OPF))2WX+ 6=D%=4-$uPQs6
	 	1D'4sE !cS# 
wdC/	 Y Q R " 555>cdIb   + 6=E7T5MD%=1#e}6
 1D'4uc $DJ$63EcWZcm	 
wdE3/			 e   
N+OPG$EFEOe,STF))2WXf Y U G Qf  555>cdI&op! q e! D1I&^_! ` 2! D!lm2 n2
 ! !  (/utUmdE]UVXYTZ'[ #D$e 
tT5!  ! ! ,d754-RSQT3U.VW, X , D!noI&/ST! U p!g@ (.	DNs$   '!Y2-'Y<3Z,Z+)ZZ%