
    hm              	       @   S SK r S SKrS SKrS SKJr  S SKJr  S SKrS SKrS SK	r	S SK
r
S SKJr  S SKJrJrJrJrJrJrJr  S SKJrJr  S SKJrJr  S SKJr  S S	KJr  S S
KJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/J0r0  S SK1J2r2  S SK3J4r4J5r5  \/" \5      r6S r7S r8S r9\	Rt                  Rw                  \6(       + SS9S 5       r<\	Rt                  Rw                  SSS9\	Rt                  Rw                  \6(       + SS9S 5       5       r=\	Rt                  Rw                  SSS9\	Rt                  Rw                  \6(       + SS9S 5       5       r>\	Rt                  R                  S\5      S 5       r@\	Rt                  R                  S\5      S 5       rAS rB\	Rt                  R                  \	Rt                  Rw                  \)(       + SS9\	Rt                  Rw                  \0" 5       SS9S 5       5       5       rD\	Rt                  Rw                  \)(       + SS9\	Rt                  Rw                  \6(       + SS9\	Rt                  R                  S\5      S 5       5       5       rE\	Rt                  R                  S \5      S!\FS"\FS#\FS$S4S% j5       rG\	Rt                  Rw                  \%=(       d    \&S&S9S' 5       rH\	Rt                  Rw                  \)(       + SS9S( 5       rI\	Rt                  R                  S)S*S/5      S+ 5       rJS, rK\	Rt                  Rw                  \,S-S9S. 5       rLS/ rM\	Rt                  R                  S\5      S\F4S0 j5       rNS1 rO\	Rt                  Rw                  \)(       + SS9S2 5       rP\	Rt                  Rw                  \)(       + SS9S3 5       rQS4 rRS5 rSS6 rTS7 rUS8 rV\	Rt                  Rw                  \,S9S9S: 5       rWS; rXS< rYS= rZ\	Rt                  R                  S> 5       r[S? r\S@ r]\	Rt                  Rw                  \)(       + SS9SA 5       r^\	R                  SB 5       r`\	Rt                  R                  SC/ SDQ5      SE 5       ra\	Rt                  R                  \	Rt                  Rw                  \)(       + SS9SF 5       5       rbSG rc\	Rt                  Rw                  \.R                  SHS9\	Rt                  Rw                  \.R                  =(       a    \'=(       a    \ SIS9SJ 5       5       rf\	Rt                  Rw                  \5(       + SKS9\	Rt                  Rw                  \.R                  SLS9\	Rt                  Rw                  \.R                  =(       a    \'=(       a    \ SMS9SN 5       5       5       rgSO rhSP ri\	Rt                  R                  SQ\5      S!\FS\FS#\FS$S4SR j5       rjg)S    N)copy)Path)Image)CFGMODELMODELSSOURCESOURCES_LISTTASK_MODEL_DATATMP)RTDETRYOLO)	TASK2DATATASKS)load_inference_source)check_det_dataset)ARM64ASSETS
ASSETS_URLDEFAULT_CFGDEFAULT_CFG_PATH	IS_JETSONIS_RASPBERRYPILINUXLOGGERONLINEROOTWEIGHTS_DIRWINDOWSYAMLchecksis_dir_writeableis_github_action_running)download)
TORCH_1_11
TORCH_1_13c                  2    [        [        5      n U " SSSS9  g)z(Test the forward pass of the YOLO model.N    T)sourceimgszaugment)r   r   models    K/home/james-whalen/.local/lib/python3.13/site-packages/tests/test_python.pytest_model_forwardr/   -   s    IE	R.    c                  p   [        [        5      n U R                  SSS9  U R                  5       n U R	                  [        5      n U R                  S5        U R                  5         U R                  S5        U R                  5         U R                  nU R                  nU R                  nU R                  ng)zVTest various methods and properties of the YOLO model to ensure correct functionality.T)verbosedetailedcpuon_train_startN)r   r   inforeset_weightsloadtofuseclear_callbackreset_callbacksnamesdevice
transformstask_map)r-   _s     r.   test_model_methodsrB   3   s    KE 
JJtdJ+!EJJuE	HHUO	JJL	)*	 	AAAAr0   c                  p    SSK Jn   U " 5       n[        R                  " SSSS5      nUR	                  USS9ng)	z^Test profiling of the YOLO model with `profile=True` to assess performance and resource usage.r   )DetectionModel      @   T)profileN)ultralytics.nn.tasksrD   torchrandnpredict)rD   r-   imrA   s       r.   test_model_profilerN   G   s3    3E	Q2r	"Bb$'Ar0   zdirectory is not writeable)reasonc                      [         S-  n [        U S5       n[         H  nUR                  U S35        M     SSS5        [	        [
        5      " U SS9n[        U5      S:X  d   eg! , (       d  f       N4= f)zVTest YOLO predictions with file, directory, and pattern sources listed in a text file.zsources_multi_row.txtw
Nr(   r)   r*      )r   openr
   writer   r   len)filefsrcresultss       r.   test_predict_txtr\   P   sg     ((D	dCACGGse2J   
 5kR0Gw<1	 
s   "A&&
A4Tzdisabled for testingc                  P   [         S-  n [        U SSS9 n[        R                  " U5      nUR	                  S/5        UR                  [         Vs/ s H  o3/PM     sn5        SSS5        [        [        5      " U SS9n[        U5      S	:X  d   egs  snf ! , (       d  f       N9= f)
zITest YOLO predictions with sources listed in multiple rows of a CSV file.zsources_multi_row.csvrQ    newliner)   Nr(   rS   rT   )
r   rU   csvwriterwriterow	writerowsr
   r   r   rW   )rX   rY   rb   rZ   r[   s        r.   test_predict_csv_multi_rowre   [   s     ((D	dC	$A
#<8<C%<89 
% 5kR0Gw<1 9 
%	$s   <BB
BB
B%c                      [         S-  n [        U SSS9 n[        R                  " U5      nUR	                  [
        5        SSS5        [        [        5      " U SS9n[        U5      S:X  d   eg! , (       d  f       N4= f)	zHTest YOLO predictions with sources listed in a single row of a CSV file.zsources_single_row.csvrQ   r^   r_   Nr(   rS   rT   )	r   rU   ra   rb   rc   r
   r   r   rW   )rX   rY   rb   r[   s       r.   test_predict_csv_single_rowrg   h   sh     ))D	dC	$A% 
% 5kR0Gw<1	 
%	$s   ,A//
A=
model_namec                    U S:X  a  SOSn[        [        U -  5      n[        R                  " [	        [
        5      US:X  a  [        R                  O[        R                  S9n[        U" [        R                  " [
        5      SSSS95      S:X  d   e[        U" USSSS95      S:X  d   e[        U" [        R                  " S	USS45      SS
95      S	:X  d   e[        U" X3/SSSS95      S	:X  d   e[        [        U" X3/SSSS95      5      S	:X  d   e[        U" [        R                  " SSU5      R                  5       R!                  ["        R$                  5      SS
95      S:X  d   e[	        [
        5      ['        [
        5      [(        (       a	  [*         S3O[
        U[        R                  " [
        5      ["        R                  " SSU4["        R$                  S9/n[        U" USSS95      [        U5      :X  d   eg)z^Test YOLO model predictions on various image input types and sources, including online images.zyolo11n-grayscale.ptrE   rF   )flagsTr(   )r)   saver2   r*   )r)   rk   save_txtr*      r*   )r)   rk   streamr*   i@    z/zidane.jpg?token=123dtyper   )r*   classesN)r   r   cv2imreadstrr	   IMREAD_GRAYSCALEIMREAD_COLORrW   r   rU   rJ   randlistzerosnumpyastypenpuint8r   r   r   )rh   channelsr-   rM   batchs        r.   test_predict_imgr   t   s    "88qaHz)*E	CKx1}s';';RURbRb	cBuEJJv.T4rRSWXXXXuBTDCDIIIuUZZHb" 56bABaGGGuRH4$bIJaOOOtE"TLMNRSSSSuU[[c84::<CCBHHMUWXY]^^^^FV06:,+,F


6
#sH%RXX6E uU"a01SZ???r0   r-   c                 <    [        [        U -  5      " [        SSS9  g)zfTest model prediction methods with 'visualize=True' to generate and display prediction visualizations.r(   T)r*   	visualizeN)r   r   r	   r,   s    r.   test_predict_visualizer      s     	u	fB$?r0   c            	      X   [         R                  " [        5      n [        S-  nUR	                  SSS9  US-  nUS-  nUS-  nUS-  nU R                  S5      R                  U5        U R                  S	5      R                  U5        U R                  U5        U R                  U5        [        [        5      nX2XE4 Hj  n[         R                  " U5      [        R                  " [        U5      5      U4 H  nU" USSS
S9n	[        U	5      S:X  a  M   e   UR                  5         Ml     g)zbTest YOLO prediction on SOURCE converted to greyscale and 4-channel images with various filenames.im4Tparentsexist_okzgreyscale.jpgz4ch.pngu$   non_UTF_测试文件_tést_image.jpgzimage with spaces.jpgLRGBAr(   )rk   r2   r*   rE   N)r   rU   r	   r   mkdirconvertrk   r   r   rt   ru   rv   rW   unlink)
rM   	directorysource_greyscalesource_rgbasource_non_utfsource_spacesr-   rY   r)   r[   s
             r.   test_predict_grey_and_4chr      s   	F	BeIOOD4O0 ?2i'K!GGN 77MJJsO)*JJvK(GGNGGM KENIjjmSZZA%7:FFt2FGw<1$$$ ; 	

	 Jr0   zenvironment is offlinez:No auth https://github.com/JuanBindez/pytubefix/issues/166c                      [        [        5      n  U R                  SSSS9  g! [        R                  R
                  [        4 a#  n[        R                  " SU 35         SnAgSnAff = f)zUTest YOLO model on a YouTube video stream, handling potential network-related errors.zhttps://youtu.be/G17sBkb38XQ`   T)r*   rk   zYouTube Test Error: N)r   r   rL   urlliberror	HTTPErrorConnectionErrorr   )r-   es     r.   test_youtuber      s[    
 KE14BTJLL""O4 1+A3/001s   # $A*A%%A*c           	      f   U S:X  a  g[          S3n[        U 5      n U R                  USSS9  U R                  USSSS	9  [        / S
Q/ SQ5       H_  u  p#[        R
                  " [        S-  5      n[        SU S3-  n[        R                  " U0 UEUSUS.E5        U R                  USUS9  Ma     g)z
Test streaming tracking on a short 10 frame video using ByteTrack tracker and different GMC methods.

Note imgsz=160 required for tracking for higher confidence and better matches.
yolo11n-cls.ptNz/decelera_portrait_min.mov   zbytetrack.yaml)r*   trackerzbotsort.yamlT)r*   r   save_frames)orbsiftecc)autor   r   zcfg/trackers/botsort.yamlzbotsort-.yaml)
gmc_method	with_reidr-   )	r   r   trackzipr    r8   r   r   rk   )r-   	video_urlgmcreidmdefault_argscustom_yamls         r.   test_track_streamr      s       ,89IKE	KK	.>K?	KK	n$KO 02TU
yy(C!CDhse511		+e,ecPT_defIS+>	 Vr0   ztask,weight,datataskweightdatareturnc                 J   [        U5      nS H  nUR                  USUS9nUR                  5         UR                  5         UR	                  5         UR
                  R                  5         UR
                  R                  5         UR
                  R	                  5         M     g)z+Test the validation mode of the YOLO model.>   FTr(   )r   r*   plotsN)r   valto_dfto_csvto_jsonconfusion_matrix)r   r   r   r-   r   metricss         r.   test_valr      s~     LE))Ru)=  &&(  '')  ((* r0   z&Edge devices not intended for trainingc            
      d    [        [        5      n U R                  SSSSSSSS9  U " [        5        g	)
zKTest training the YOLO model from scratch using the provided configuration.
coco8.yamlrm   r(   diskrE   r-   )r   epochsr*   cacher   close_mosaicnameN)r   r   trainr	   r,   s    r.   test_train_scratchr      s0     IE	KK\!2V2\]dkKl	&Mr0   c                  X    [        [        S-  5      n U R                  [         S3SSS9  g)z9Test training the YOLO model using NDJSON format dataset.
yolo11n.ptz/coco8-ndjson.ndjsonrE   r(   )r   r   r*   N)r   r   r   r   r,   s    r.   test_train_ndjsonr      s-     |+,E	KK
|#78"KMr0   sclsFc                 l    [        [        S-  5      nUR                  SSSSSSSU S9  U" [        5        g	)
zGTest training of the YOLO model starting from a pre-trained checkpoint.yolo11n-seg.ptzcoco8-seg.yamlrE   r(   ram      ?r   )r   r   r*   r   
copy_pastemixupr   
single_clsN)r   r   r   r	   )r   r-   s     r.   test_train_pretrainedr      sB     //0E	KKar3VY`anr   
&Mr0   c                      [         S-  S-  R                  S5       HU  n SU R                  ;   a-  [        (       a   [	        U R                  5      " [
        SS9nM>  M@  [        U R                  5        MW     g)z]Test YOLO model creation for all available YAML configurations in the `cfg/models` directory.cfgmodelsz*.yamlrtdetrrp   rn   N)r   rglobr   r%   r   r	   r   )mrA   s     r.   test_all_model_yamlsr      sW    UlX%,,X6qvvz166N65  L 7r0   zPWindows slow CI export bug https://github.com/ultralytics/ultralytics/pull/16003c                      [        [        5      n U R                  SSSSS9  U R                  SS9  U R	                  [
        SS9  U R                  SS9  g	)
zUTest the complete workflow including training, validation, prediction, and exporting.r   rE   r(   SGD)r   r   r*   	optimizerrn   torchscriptformatN)r   r   r   r   rL   r	   exportr,   s    r.   test_workflowr     sM     KE	KK\!2KG	IIBI	MM&M#	LLL&r0   c                  *   S n [        [        5      nUR                  SU 5        [        [        S9nUR
                  nUR                  USSS9nU H?  u  pVn[        SUR                  5        [        SU5        UR                  n[        U5        MA     g)	zGTest callback functionality during YOLO prediction setup and execution.c                    U R                   u  pn[        U[        5      (       a  UOU/n[        [	        U5      5       Vs/ s H  o0R
                  R                  PM     nn[        U R                  X$5      U l        gs  snf )zKCallback function that handles operations at the end of a prediction batch.N)	r   
isinstancerz   rangerW   datasetbsr   r[   )	predictorpathim0srA   r   s        r.   on_predict_batch_end=test_predict_callback_and_setup.<locals>.on_predict_batch_end  sg    !A!$--tD6,1#d),<=,<q"",<=	 1 14<	 >s    A=r   )r)   Tr   )ro   r*   test_callbackN)
r   r   add_callbackr   r	   r   rL   printshapeboxes)r   r-   r   r   r[   rim0r   s           r.   test_predict_callback_and_setupr     s    = KE	-/CD#62G	BmmGDm<G
osyy)or"e	 r0   c                    U S:X  a	  [          S3O[        n[        [        U -  5      " X/SS9nU GH  n[	        U5      (       d   SU  S35       eUR                  5       R                  5       n[        U[	        U5      UR                  5        UR                  S[        R                  S9nUR                  [        S	-  S
S9  UR                  [        S-  S9  UR                  SS9  UR!                  5         UR#                  S
S9  UR%                  S
S
[        S-  S9  UR%                  S
S
S9  [        U[	        U5      UR                  5        GM!     g)zATest YOLO model results processing and output in various formats.zyolo11n-obb.ptz
/boats.jpgr   rn   'z' results should not be empty!r4   )r>   rr   zruns/tests/label.txtT)txt_file	save_confzruns/tests/crops/)save_dirrF   )decimals)	normalizezresults_plot_save.jpg)pilrk   filename)confr   N)r   r	   r   r   rW   r4   r|   r   r   r9   rJ   float32rl   r   	save_cropr   r   r   plot)r-   rM   r[   r   s       r.   test_resultsr  "  s    ',/?&?J<z	"VB;&'<G1vv@5'!?@@vEEGMMOaQ DDU]]D3	

C"88D
I	S#667		
			D	!	4dS3J-JK	D%aQ  r0   c                  *  ^
^ [         [        S-  /n [        [        S-  5      " U SSSS9n[	        US   R
                  5      nU GH  m[	        TR                  5      R                  nTR                  R                  R                  5       R                  5       nUTR                  R                  S5      (       a  / SQO/ S	Q:X  d   eUS
U S3-  nUR                  5       (       d   e[        TR                  R                  5      [        UR!                  5       R#                  5        Vs/ s H  of(       d  M  UPM     sn5      :X  d   e[%        US-  R'                  5       5      m
T
 VVs/ s H  owR)                  S5        H  oPM     M     n	nn[+        U
U4S jU 5       5      (       d   e[        U	 Vs/ s H  oUR,                  ;   d  M  UPM     sn5      [        TR                  R                  5      :X  a  GM   e   gs  snf s  snnf s  snf )zLTest output from prediction args for saving YOLO detection labels and crops.z
zidane.jpgr   r   T)r*   rl   r  r   zbus.jpg)r   rT   r   r   )r   r   r   zlabels/z.txtcrops*c              3      >#    U  H=  nTR                   R                  U5      T Vs1 s H  o"R                  iM     sn;   v   M?     g s  snf 7f)N)r=   getr   ).0cd	crop_dirsr   s      r.   	<genexpr>(test_labels_and_crops.<locals>.<genexpr>I  s6     S(Q177;;q>i%@iffi%@@(%@s   $AAAN)r	   r   r   r   r   r   r   stemr   clsinttolistendswithexistsrW   r   	read_text
splitlinesrz   iterdirgloballr   )imgsr[   	save_pathim_namecls_idxslabelslineprY   
crop_filesr  r   s             @@r.   test_labels_and_cropsr#  6  s   F\)*D;-.t3Y]^GWQZ(()Iqvv,##77;;??$++-AFFOOI,F,FLIVVVwwit44}}177<< C&:J:J:L:W:W:Y(b:Y$]a:Y(b$cccc)g-6689	!*@AFF3KqaKa
@S(SSSSSz?z!->Az?@CDUUUU!  )c A @s   +
H9H,#H
8H
H
c                  >   SSK Jn   SSKJn  SSKJn  [         Hc  n[        [        U   5      R                  S5      n[        SU 3S[        S9  U" [        U-  US	9nUR                  S
S9  UR                  5         Me     U " [        S-  5        U" [        S-  5        g)z`Test utility functions in ultralytics/data/utils.py, including dataset stats and auto-splitting.r   )	autosplit)HUBDatasetStats)zip_directoryz.zipz=https://github.com/ultralytics/hub/raw/main/example_datasets/F)unzipdirr   T)rk   coco8zcoco8/images/valN)ultralytics.data.splitr%  ultralytics.data.utilsr&  ultralytics.utils.downloadsr'  r   r   r   with_suffixr$   r   get_jsonprocess_images)r%  r&  r'  r   rX   statss         r.   test_data_utilsr3  N  s     169
 IdO$008PQUPVW_djmnd
6D!  cGm#**+r0   c                  v    SSK Jn Jn  [        [         S3[
        S9  U" [
        [
        S-  SSSS9  U " 5         g	)
zNTest dataset conversion functions from COCO to YOLO format and class mappings.r   coco80_to_coco91_classconvert_cocoz/instances_val2017.json)r)  yolo_labelsTF)
labels_dirr   use_segmentsuse_keypoints	cls91to80N)ultralytics.data.converterr6  r7  r$   r   r   r5  s     r.   test_data_converterr>  c  s7     P
|23=C#*=D`equvr0   c                  T    SSK Jn   U " [        [        S-  [        S-  [        S-  S9  g)zJTest automatic annotation of data using detection and segmentation models.r   auto_annotater   zmobile_sam.ptauto_annotate_labels)	det_model	sam_model
output_dirN)ultralytics.data.annotatorrA  r   r   r   r@  s    r.   test_data_annotatorrG  m  s)    8,///	r0   c                  h    SSK Jn   U " 5       nSUl        [        [        5      nSUl        U" U5        g)z!Test event sending functionality.r   )EventsTtestN)ultralytics.utils.eventsrI  enabledr   r   mode)rI  eventsr   s      r.   test_eventsrO  y  s+    /XFFN
{
CCH
3Kr0   c                  f   SSK Jn JnJn  [        R
                  " [        5         U " SS0SS05        SSS5        U" 5         [        R                  " 5       [        R                  R                  SS	5      -  R                  S
S9  S Vs/ s H
  o2" U5      PM       ng! , (       d  f       Np= fs  snf )zNTest configuration initialization utilities from the 'ultralytics.cfg' module.r   )check_dict_alignmentcopy_default_cfgsmart_valuearE   brm   Nr   z
_copy.yamlF)
missing_ok>   nonetruefalse)ultralytics.cfgrQ  rR  rS  
contextlibsuppressSyntaxErrorr   cwdr   r   replacer   )rQ  rR  rS  xs       r.   test_cfg_initra    s    SS			[	)c1XQx0 
*	XXZ"''//FFNNZ_N`676[^67	 
*	) 8s   B	B.
B+c                  0    SSK Jn Jn  U " 5         U" 5         g)z9Test initialization utilities in the Ultralytics library.r   get_ubuntu_versionr#   N)ultralytics.utilsrd  r#   rc  s     r.   test_utils_initrf    s    Nr0   c                  
   [         R                  " S5        [         R                  " S5        [         R                  " SS/SS9  [         R                  " SS9  [         R
                  " SS	5        [         R                  " 5         g
)z_Test various utility checks for filenames, git status, requirements, image sizes, and versions.z
yolov5n.ptr|   iX  rE   )max_dimT)warnultralyticsz8.0.0N)r!   check_yolov5u_filenamecheck_requirementscheck_imgszcheck_imshowcheck_version
print_args r0   r.   test_utils_checksrr    s\    
!!,/
g&
Sz1-
T"
0
r0   z3Windows profiling is extremely slow (cause unknown)c                  B    SSK Jn   U " S/SSSSS9R                  5         g)	zVBenchmark model performance using 'ProfileModels' from 'ultralytics.utils.benchmarks'.r   ProfileModelszyolo11n.yamlr(   rE   rF   )r*   min_timenum_timed_runsnum_warmup_runsN)ultralytics.utils.benchmarksru  runrt  s    r.   test_utils_benchmarksr{    s$     ;>""q\]^bbdr0   c                      SSK Jn   SSKJnJnJn  [        R                  " SSSS5      nU " SSSSS9nU" XE/S	S
9  U" U5        U" 5         g)zGTest Torch utility functions including profiling and FLOP calculations.r   )Conv)get_flops_with_torch_profilerprofile_ops	time_syncrE   rG      rm   )ksrF   )nN)ultralytics.nn.modules.convr}  ultralytics.utils.torch_utilsr~  r  r  rJ   rK   )r}  r~  r  r  r`  r   s         r.   test_utils_torchutilsr    sH    0ccAr2r"ARqAA3!!!$Kr0   c                  b   SSK Jn JnJnJnJnJnJnJnJ	nJ
n	Jn
  U" S[        R                  " S/5      5        [        R                  " SS5      n[        R                  " X" U" U5      5      5        [        R                  " X" U" U5      5      5        [        R                  " X" U" U5      5      5        [        R                  " X" U" U5      5      5        [        R                  " SS5      n[        R                   " S5      S-  US	S	2S4'   [        R                  " X" U" U5      5      S
S9  g	)zJTest utility operations for coordinate transformations and normalizations.r   )	ltwh2xywh	ltwh2xyxymake_divisible	xywh2ltwh	xywh2xyxy
xywhn2xyxyxywhr2xyxyxyxy	xyxy2ltwh	xyxy2xywh
xyxy2xywhnxyxyxyxy2xywhr      
            NgMbP?)rtol)ultralytics.utils.opsr  r  r  r  r  r  r  r  r  r  r  rJ   tensorry   allcloserK   )r  r  r  r  r  r  r  r  r  r  r  r   s               r.   test_utils_opsr    s        2u||QC()JJr1E	NN5)Ie$456	NN5*Z%678	NN5)Ie$456	NN5)Ie$456JJr1E++b/B&E!Q$K	NN5.)>?dKr0   c                      SSK Jn JnJnJn  U " [
        5        U" [
        5        U" [        S-  5        [        S-  nUR                  SSS9  U" U5       n[        U5        SSS5        g! , (       d  f       g= f)zMTest file handling utilities including file age, date, and paths with spaces.r   )file_age	file_dateget_latest_runspaces_in_pathrunszpath/with spacesTr   N)
ultralytics.utils.filesr  r  r  r  r	   r   r   r   r   )r  r  r  r  r   new_paths         r.   test_utils_filesr    s\    [[Vf4&=!##DJJtdJ+		h 
		s   A**
A8c                  T   SSK Jn Jn  SSKJn  U " [
        S9nU" SUS9   [        R                  " [
        5         U" [        R                  " S5      [        S-  5        S	S	S	5        S	S	S	5        UR                  S
:X  d   S5       eg	! , (       d  f       N.= f! , (       d  f       N7= f)z=Test torch_save backoff when _torch_save raises RuntimeError.r   )	MagicMockpatch)
torch_save)side_effectz%ultralytics.utils.patches._torch_save)newrE   ztest.ptNr  z9torch_save was not attempted the expected number of times)unittest.mockr  r  ultralytics.utils.patchesr  RuntimeErrorpytestraisesrJ   r{   r   
call_count)r  r  r  mocks       r.   test_utils_patches_torch_saver    sy     /4.D	6D	A]]<(u{{1~sY7 ) 
B ??a\!\\ )( 
B	As"   B%B B
B	B
B'c                  
   SSK Jn JnJnJnJn  Su  pV[        R                  " SUSS5      nU" XV5      " U5        U" XV5      " U5        U" XV5      " U5        U " U5      " U5        U" XV5      nUR                  5         U" U5        g)zSTest Convolutional Neural Network modules including CBAM, Conv2, and ConvTranspose.r   )CBAMConv2ConvTransposeDWConvTranspose2dFocusr     r  r  N)	r  r  r  r  r  r  rJ   r{   
fuse_convs)	r  r  r  r  r  c1c2r`  r   s	            r.   test_nn_modules_convr    sq    ``FBAr2r"A ba "!	"M!HQK 	bALLNaDr0   c                      SSK Jn JnJnJnJn  Su  pV[        R                  " SUSS5      nU " XV5      " U5        U" XV5      " U5        U" XV5      " U5        U" XV5      " U5        U" XV5      " U5        g)z*Test various neural network block modules.r   )C1C3TRBottleneckCSPC3GhostC3xr  r  r  N)ultralytics.nn.modules.blockr  r  r  r  r  rJ   r{   )r  r  r  r  r  r  r  r`  s           r.   test_nn_modules_blockr    s_    RRFBAr2r"A rJqMKNLOBOA"!r0   c                  L    SSK Jn Jn  SSKJn  U " 5         U" 5         U" SSSS9  g)	z%Test Ultralytics HUB functionalities.r   )export_fmts_hublogout)smart_requestGETzhttps://github.comT)progressN)ultralytics.hubr  r  ultralytics.hub.utilsr  )r  r  r  s      r.   test_hubr    s"     83
H%-=r0   c                  H    [         R                  " [        [        5      5      $ )z2Load and return an image from a predefined source.)rt   ru   rv   r	   rq  r0   r.   imager    s     ::c&k""r0   z)auto_augment, erasing, force_color_jitter))N        F)randaugmentr   T)augmixg?F)autoaugmentr  Tc                 P   SSK Jn  U" SSSSSSSUSS	S	UUS
9nU" [        R                  " [        R
                  " U [        R                  5      5      5      nUR                  S:X  d   e[        R                  " U5      (       d   eUR                  [        R                  :X  d   eg)zJTest classification transforms during training with various augmentations.r   )classify_augmentations   )r   r   r   )g{Gz?g      ?)g      ?gUUUUUU?r   gQ?g?)sizemeanstdscaleratiohflipvflipauto_augmenthsv_hhsv_shsv_vforce_color_jittererasing)rF   r  r  N)ultralytics.data.augmentr  r   	fromarrayrt   cvtColorCOLOR_BGR2RGBr   rJ   	is_tensorrr   r  )r  r  r  r  r  	transformtransformed_images          r.   test_classify_transforms_trainr  $  s     @&$!-I  "%//#,,ucFWFW2X"YZ""m333??,----""emm333r0   c            	      x    [        S5      R                  SSSSSSS9  [        S	5      R                  S
SSSSSS9  g)z,Tune YOLO model for performance improvement.zyolo11n-pose.ptzcoco8-pose.yamlFr(   rE   rm   r4   )r   r   r*   r   
iterationsr>   r   
imagenet10N)r   tunerq  r0   r.   test_model_tuner  H  sM     		  &7uBWXefot u	\bQR_`inor0   c                     [        [        5      n [        [        S-  5      n[        /[        [        /4 HS  n[	        U R                  USS95      [	        U5      :X  d   e[	        UR                  USS95      [	        U5      :X  a  MS   e   g)z4Test YOLO model embeddings extraction functionality.r   r(   rS   N)r   r   r   r	   rW   embed)model_detectmodel_segmentr   s      r.   test_model_embeddingsr  P  s    ;L'778MFF++<%%U"%=>#e*LLL=&&e2&>?3u:MMM ,r0   z3YOLOWorld with CLIP is not supported in Python 3.12zDYOLOWorld with CLIP is not supported in Python 3.8 and aarch64 Linuxc            	      
   [        [        S-  5      n U R                  SS/5        U " [        SS9  [        [        S-  5      n U R	                  SSS	S
SS9  SSKJn  [        S5      n U R	                  SS/0SS/0S.SS	S
SUS9  g)z)Test YOLO world models with CLIP support.zyolov8s-world.pttreewindow{Gz?r   zyolov8s-worldv2.ptz
dota8.yamlrE   r(   r   )r   r   r*   r   r   r   )WorldTrainerFromScratchzyolov8s-worldv2.yaml	yolo_datar   r   )r   r   r*   r   r   trainerN)r   r   set_classesr	   r   )ultralytics.models.yolo.world.train_worldr   )r-   r   s     r.   test_yolo_worldr  Z  s     112E	vx()	&t334E 
KK   R'(E	KK#l^4kL>=Z['  r0   z$YOLOE with CLIP requires torch>=1.13z/YOLOE with CLIP is not supported in Python 3.12z@YOLOE with CLIP is not supported in Python 3.8 and aarch64 Linuxc            	         [        [        S-  5      n SS/nU R                  XR                  U5      5        U " [        SS9  SSKJn  SSKJn  [        [        R                  " / S	Q/ S
Q/5      [        R                  " SS/5      S9nU R                  [        UUS9  U" [        S-  5      n U R                  SSS9  U R                  SSSS9  SSKJnJn  U" S5      n U R!                  SSSUSS9  U" S5      n U R!                  [        [        S/S9[        S/S9S9SSUSS9  U" [        S-  5      n U R                  [        5        U" S5      n U R                  SSS9  g)z*Test YOLOE models with MobileClip support.zyoloe-11s-seg.ptpersonbusr  r  r   )YOLOE)YOLOEVPSegPredictor)gq=
ףk@g\y@gHzu@gQ̊@)x   i  r   i  rE   )bboxesr  )visual_promptsr   zcoco128-seg.yamlr(   r   r*   T)r   load_vpr*   )YOLOEPESegTrainerYOLOESegTrainerFromScratch)r   r   r   r  r*   zyoloe-11s-seg.yaml)r  r  zyoloe-11s-seg-pf.ptN)r   r   r  get_text_per	   rj  r
  ultralytics.models.yolo.yoloer  dictr~   arrayrL   r   r  r  r   )r-   r=   r
  r  visualsr  r  s          r.   
test_yoloer  ~  sy    112EuE	e..u56	&t!A xx8:NOPHHaVG 
MM%   + 223E	II%RI0	II%t2I> \$%E	KK!   &'E	KK(:';<$RdQeBfg*   + 556E	MM&$%E	II%RI0r0   c                      [        S5      n U R                  SSSSSS9  U R                  SSS9  U R                  SSSSS	9  U " [        5        g
)zFTest YOLOv10 model training, validation, and prediction functionality.zyolov10n.yamlr   rE   r(   r   r   r   r*   r   r   r  T)r*   rl   r  r+   N)r   r   r   rL   r	   r,   s    r.   test_yolov10r    sM    !E	KK\!2AVKT	II<rI*	MMTT4MH	&Mr0   c                      [        S5      n U R                  SSSSSS9  U R                  SS9  [        R                  " S[        R
                  S	9nU R                  USS
S
S
S9  U R                  SS9  g)zQTest YOLO model multi-channel training, validation, and prediction functionality.r   zcoco8-multispectral.yamlrE   r(   r   r  r   )r(   r(   r  rq   Tr)   r*   rl   r  r+   onnxr   N)r   r   r   r~   r{   r   rL   r   )r-   rM   s     r.   test_multichannelr     sl    E	KK/RS[aKb	II-I.	,bhh	/B	MM2dMS	LLLr0   ztask,model,datac                 F   U S:X  a  g[        [        5      [        U5      R                   S3-  n[        U5      nSUS'   [        R
                  " X25        S H<  n[        US   5      X$   -  R                  S5       H  nUR                  5         M     M>     [        U5      nUR                  USS	SS
9  UR                  US9  [        R                  " S[        R                  S9nUR                  US	SSSS9  UR                  SS9n[        XpS9nUR                  US	S9  g)zMTest YOLO model grayscale training, validation, and prediction functionality.classifyNz-grayscale.yamlrE   r   >   r   r   r   z*.npyr(   )r   r   r*   r   r  )r(   r(   rE   rq   Tr  r  r   r*  rS   )r   r   r  r   r    rk   r  r   r   r   r   r~   r{   r   rL   r   )r   r-   r   grayscale_datasplitnpy_filerM   export_models           r.   test_grayscaler'    s    z#YDJOO#4O!DDNT"DDIIn#!d6l+dk9??HHOO I " KE	KK^ARaKH	II>I"	+RXX	.B	MM2dMS<<v<.L)E	MM2M&r0   )kr[  ra   r   r   pathlibr   rt   r|   r~   r  rJ   PILr   testsr   r   r   r	   r
   r   r   rj  r   r   rZ  r   r   ultralytics.data.buildr   r-  r   re  r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r.  r$   r  r%   r&   IS_TMP_WRITEABLEr/   rB   rN   markskipifr\   re   rg   parametrizer   r   r   slowr   r   rv   r   r   r   r   r   r   r   r  r#  r3  r>  rG  rO  ra  rf  rr  r{  r  r  r  r  r  r  r  fixturer  r  r  r  IS_PYTHON_3_12IS_PYTHON_3_8r  r  r  r   r'  rq  r0   r.   <module>r4     s>    
    
     P P P $ , 8 4    & 1 @#C( /(( ((1MN O D!78((1MN O 9 D!78((1MN O 9 v.@ /@, &)@ *@
2 J'?@,.7st1 u A 1 J'?@((1MN&)? * O A?* +_=+3 + +3 +4 + >+ I/8`a b J'?@N AN %/ 0 G$vw' x'. &)! ! *!&V0 J'?@, A,( J'?@ A	8 G$YZe [e
L: ] ]	& J'?@> A> # #
 /446 J'?@p A pN F))2gh
,U,uQ  	 i
> 
N+QRF))2cd
,U,uM  91	 e S91x  *O<' 'S ' ' ' ='r0   