
    h[+                     |   S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
r
S SKJrJr  S SKJr  S SKJrJrJr  S SKJrJrJrJrJrJr  S S	KJrJrJr  S
 rS r \
RB                  RE                  \(       + SS9S 5       r#\
RB                  RH                  \
RB                  RE                  \(       + SS9\
RB                  RK                  S\" \SS/SS/SS/SS/SS/5       V VVVVVs/ s H+  u  pp#pEU(       a	  U(       a  M  U S:X  a	  U(       a  M&  XX#XE4PM-     snnnnnn 5      S 5       5       5       r&\
RB                  RH                  \
RB                  RK                  S\" \SS/S/S/SS/SS/SS/5       V VVVVVVs/ s H=  u  pp#pFnU(       a	  U(       a  M  U S:X  a	  U(       a  M'  U(       a	  \(       d  M7  XX#XFU4PM?     snnnnnnn 5      S 5       5       r'\
RB                  RH                  \
RB                  RK                  S\" \SS/S/SS/SS/SS/5       V VVVVVs/ s H  u  pp#pEU S:X  a	  U(       a  M  XX#XE4PM     snnnnnn 5      S 5       5       r(\
RB                  RH                  \
RB                  RE                  \(       + SS9\
RB                  RE                  \(       + SS9\
RB                  RE                  \RR                  SS9\
RB                  RK                  S\" \SS/SS/SS/SS/S/5       V VVVVVs/ s HJ  u  pp#pTU(       a	  U(       a  M  U S:w  a	  U(       a  M&  U(       a	  U(       a  M6  U S:X  a	  U(       a  ME  XX#XT4PML     snnnnnn 5      S 5       5       5       5       5       r*\
RB                  RH                  \
RB                  RE                  \RV                  (       + SS9\
RB                  RE                  \(       + =(       d    \S S9\
RB                  RK                  S\" \S/SS/SS/S/SS/5       V VVVVVs/ s HK  u  pp#pEU(       a	  U(       a  M  U S:X  a	  U(       a  M&  \(       a	  U(       a  M6  U(       a	  \(       d  MF  XX#XE4PMM     snnnnnn 5      S! 5       5       5       5       r,\
RB                  RE                  \(       + SS9\
RB                  RE                  \S"S9\
RB                  RE                  \=(       a    \S#S9\
RB                  RE                  \RR                  SS9S$ 5       5       5       5       r-\
RB                  RE                  \RV                  (       + SS9\
RB                  RE                  \(       + S%S9S& 5       5       r.\
RB                  RE                  SS'S9\
RB                  RE                  \(       + S(S9S) 5       5       r/\
RB                  RE                  SS*S9S+ 5       r0\
RB                  RH                  S, 5       r1\
RB                  RH                  \
RB                  RK                  S-\" \SS/SS/SS/5       V VVVs/ s H  u  pp4U(       a	  U(       a  M  XX44PM     snnnn 5      S. 5       5       r2\
RB                  RH                  S/ 5       r3\
RB                  RH                  \
RB                  RK                  S0\4" \" \SS/S/5      5      5      S1 5       5       r5\
RB                  RE                  SS2S9\
RB                  RE                  \(       + =(       d    \S3S9S4 5       5       r6gs  snnnnnn f s  snnnnnnn f s  snnnnnn f s  snnnnnn f s  snnnnnn f s  snnnn f )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                  h    [        [        5      R                  SSSS9n [        U 5      " [        SS9  g)zOTest YOLO model export to TorchScript format for compatibility and correctness.torchscriptF    )formatoptimizeimgszr   Nr	   r   exportr   files    L/home/james-whalen/.local/lib/python3.13/site-packages/tests/test_exports.pytest_export_torchscriptr"      s+    ;]U"MDJvR     c                  h    [        [        5      R                  SSSS9n [        U 5      " [        SS9  g)z8Test YOLO model export to ONNX format with dynamic axes.onnxTr   )r   dynamicr   r   Nr   r   s    r!   test_export_onnxr'       s+    ;VTDDJvR r#   zOpenVINO requires torch>=2.1)reasonc                  f    [        [        5      R                  SSS9n [        U 5      " [        SS9  g)zFTest YOLO export to OpenVINO format for model inference compatibility.openvinor   r   r   r   Nr   r   s    r!   test_export_openvinor,   &   s+     ;Zr:DJvR r#   z%task, dynamic, int8, half, batch, nmsTF      classifyc                 |   [        [        U    5      R                  SSUUUU[        U    US9n[        (       aM  [        U5      nUR                  UR                  UR                   S[        R                  " 5        35      5      n[        U5      " [        /U-  U(       a  SOSUS9  [        R                  " USS9  g	)
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 w Dz{{4>>TYYKq*GHIJx%Wr"EJ
MM$d+r#   z/task, dynamic, int8, half, batch, simplify, nmsc                     [        [        U    5      R                  SSXX4XVS9n[        U5      " [        /U-  U(       a  SOSS9  [	        U5      R                  5         g)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   sZ     
4 !((R$^f ) D 	Jx%Wr"=Jr#   c           
          [        [        U    5      R                  SSXX4US9n[        U5      " [        /U-  U(       a  SOSS9  [	        U5      R                  5         g)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   sZ     
4 !((B`c ) 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           
          [        [        U    5      R                  SSUUUUUS9n[        U5      " [        /U-  SS9  [        R
                  " U5        g)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 	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           
          [        [        U    5      R                  SSXX4US9n[        U5      " [        /U-  SS9  [	        U5      R                  5         g)zLTest YOLO export to TFLite format considering various export configurations.tfliter   rJ   r   NrF   rA   s          r!   test_export_tflite_matrixrS      sV    $ 
4 !((r7D[^ ) D 	Jx%r*Jr#   zCoreML not supported on Windowsz%CoreML not supported on aarch64 Linuxc                     [         R                  " 5       [         R                  " 5       p[        U 5         [        U5         [	        [
        5      R                  SSSS9  [        (       a1  [	        [
        5      R                  SSS9n[	        U5      " [        SS9  SSS5        SSS5        U R                  5       UR                  5       -   nSU;  d
   S	U 35       eS
U;  d   S5       eg! , (       d  f       NU= f! , (       d  f       N^= f)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     [[]BKKMF		 /&"9U(B?5;%%XR%@DJvR(	 #:	  __!22F& L$CF8"LL 2&@eBee@ #:"9	 	 s$   C<AC+C<+
C9	5C<<
D
zGTest disabled as TF suffers from install conflicts on Windows and macOSc                  j    [        [        5      n U R                  SSS9n[        U5      " [        SS9  g)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JvR r#   zTest disabledz6TF suffers from install conflicts on Windows and macOSc                  j    [        [        5      n U R                  SSS9n[        U5      " [        SS9  g)z8Test YOLO export to TensorFlow's Protobuf (*.pb) format.pbr   r+   r   Nr   r^   s     r!   test_export_pbrc      s-     KE<<t2<.DJvR r#   zITest disabled as Paddle protobuf and ONNX protobuf requirements conflict.c                  >    [        [        5      R                  SSS9  g)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                  f    [        [        5      R                  SSS9n [        U 5      " [        SS9  g)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JvR r#   ztask, int8, half, batchc                     [        [        U    5      R                  SSXUS9n[        U5      " [        /U-  SS9  [	        U5      R                  5         g)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      sM     
4 !((Rd]b(cDJx%r*Jr#   c                  f    [        [        5      R                  SSS9n [        U 5      " [        SS9  g)z Test YOLO export to NCNN format.ncnnr   r+   r   Nr   r   s    r!   test_export_ncnnro      s+     ;V26DJvR r#   ztask, half, batchc                     [        [        U    5      R                  SSXS9n[        U5      " [        /U-  SS9  [        R
                  " USS9  g)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      sH     
4 !((bt(YDJx%r*
MM$d+r#   zKTest disabled as keras and tensorflow version conflicts with TFlite export.z"Skipping test on Windows and Macosc                  b    [        S5      n U R                  SSS9n[        U5      " [        SS9  g)zTest YOLO export to IMX format.z
yolov8n.ptimxr   r+   r   N)r	   r   r   r^   s     r!   test_export_imxrt     s.     E<<uB</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
 6
1D4u $	 	0 %)J$63	 	0E/6
	,	 J ,* 5 @GD%=5'E7QFT5MDRW=@
 @
;D4u $	 	: %)J$63	 	: EHPZ	 	:ES9@
		  + 6=E4=5'E4=1a&4-6
 6
1D4u 
"s	 	0E/6
		  I&PQ
N+OPF))2WX+ 6=D%=4-$uPQs6
	 	6
1D4s 	 	0
 !c 	0 S 	0 # 	0C/6
	 Y Q R " 555>cdIb   + 6=E7T5MD%=1#e}6
 6
1D4u $	 	0 %)J$63	 	0 EJc	 	0 X[cm	 	0E/6
			 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 	"T!'[  ! ! ,d754-RSQT3U.VW, X , D!noI&/ST! U p!g@ (.	DNs~   )\\	\ \\*\:
\#\<	\\"\"+\";\"
	\"\,%\,4\,\,	\,\63\6