§
    l¬¸jœ  ã                  óš   — d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZ dd
lmZ  G d„ de¦  «        ZdS )z)Depth estimation trainer for YOLO models.é    )Úannotations)Úcopy)ÚPath)ÚAny)Úyolo)ÚDetectionTrainer)Ú
DepthModel)ÚDEFAULT_CFGÚLOGGERÚRANK)Úplt_settingsc                  ó~   ‡ — e Zd ZdZeddfdˆ fd	„Zddd„Zdˆ fd„Zdd„Z e	¦   «         dd„¦   «         Z
dˆ fd„Zˆ xZS )ÚDepthTrainera˜  Trainer for YOLO depth estimation models.

    Multi-source training (list of img_paths) is handled transparently by the base DetectionTrainer/BaseDataset.

    Examples:
        >>> from ultralytics.models.yolo.depth import DepthTrainer
        >>> args = dict(model="yolo26s-depth.yaml", data="nyu-depth.yaml", epochs=100)
        >>> trainer = DepthTrainer(overrides=args)
        >>> trainer.train()
    NÚ	overridesúdict[str, Any] | NoneÚ
_callbacksúdict | NoneÚreturnÚNonec                ó`   •— |€i }d|d<   t          ¦   «                              |||¦  «         dS )zInitialize DepthTrainer.NÚdepthÚtask)ÚsuperÚ__init__)ÚselfÚcfgr   r   Ú	__class__s       €ús/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/depth/train.pyr   zDepthTrainer.__init__   s<   ø€ ð ÐØˆIØ#ˆ	�&ÑÝ‰Œ×Ò˜˜i¨Ñ4Ô4Ð4Ð4Ð4ó    Tr   ú
str | NoneÚweightsÚverboseÚboolr	   c                ó´   — t          || j                             dd¦  «        | j        d         |ot          dv ¬¦  «        }|r|                     |¦  «         |S )zBReturn a DepthModel initialized with the given config and weights.Úchannelsé   Únc¾   r   éÿÿÿÿ)Úchr'   r"   )r	   ÚdataÚgetr   Úload)r   r   r!   r"   Úmodels        r   Ú	get_modelzDepthTrainer.get_model&   sb   € åØ�D”I—M’M *¨aÑ0Ô0°T´Y¸t´_ÈgÐNiÕZ^ÐbiÐZið
ñ 
ô 
ˆð ð 	 Ø�JŠJ�wÑÔÐØˆr   Úbatchúdict[str, Any]c                ó„   •— t          ¦   «                              |¦  «        }|d                              ¦   «         |d<   |S )z=Preprocess batch: normalize images and keep depth as float32.r   )r   Úpreprocess_batchÚfloat)r   r0   r   s     €r   r3   zDepthTrainer.preprocess_batch/   s8   ø€ å‘”×(Ò(¨Ñ/Ô/ˆØ˜wœ×-Ò-Ñ/Ô/ˆˆg‰Øˆr   úyolo.depth.DepthValidatorc                óŠ   — t           j                             | j        | j        t          | j        ¦  «        | j        ¬¦  «        S )z-Return a DepthValidator for model validation.)Úsave_dirÚargsr   )r   r   ÚDepthValidatorÚtest_loaderr7   r   r8   Ú	callbacks)r   s    r   Úget_validatorzDepthTrainer.get_validator5   s;   € åŒz×(Ò(ØÔ t¤}½4ÀÄ	¹?¼?ÐW[ÔWeð )ñ 
ô 
ð 	
r   c                óÈ  — ddl m} ddl}t          j        d| j        dz  › d�¦  «         | j        j        }t          |j	        ¦  «        }|dk    rt          j
        d¦  «         dS t          d|¦  «        }|                     d|dz
  |¦  «                             t          ¦  «        }t          dd	|z  ¦  «        }g }|D ]¡}	|                     |	¦  «        }
|
€Œ|
|
dk                                  ¦   «         }|j        dk    rŒD|j        |k    r=||                     d|j        dz
  |¦  «                             t          ¦  «                 }|                     |¦  «         Œ¢|st          j
        d
¦  «         dS |                     |¦  «        }t+          |                     ¦   «         ¦  «        t+          |                     |d¦  «        ¦  «        }}t+          |                     ¦   «         ¦  «        t+          |                     |¦  «        ¦  «        t+          |                     ¦   «         ¦  «        }}}|                     dddd¬¦  «        \  }}|                     |d|t          ||dz   ¦  «        fd¬¦  «         |                     |dddd|d›d�¬¦  «         |                     |dddd|d›d�¬¦  «         |                     d¦  «         |                     d¦  «         |                     d¦  «         |                      dd ¬!¦  «         d"|› d#|d›d$|                     ¦   «         d›d%|d›d�	}| !                    d&d'||j"        d(d)d*d+d,d-d.d/œ¬0¦  «         |j#         $                    ¦   «         D ]}| %                    d ¦  «         Œ| j        dz  }| &                    |d1¬2¦  «         | '                    ¦   «          | j(        r|  (                    |¦  «         dS dS )3a  Plot the training-set GT depth distribution to ``labels.jpg``.

        The depth analog of the detection/semantic label plots. The inherited DetectionTrainer
        version concatenates per-image ``bboxes``/``cls`` (all empty for depth) and hands them to
        ``plot_labels``, whose reductions raise "zero-size array to reduction operation maximum
        which has no identity". Instead, sample GT depth maps from the training set and plot a
        histogram of valid (``> 0``) depth values, annotated with basic statistics.
        r   NzPlotting labels to z
labels.jpgz...z)No depth maps found, skipping label plot.iè  é   i@B z1No valid depth values found, skipping label plot.g     àX@)é   é   T)ÚfigsizeÚtight_layoutéd   g�íµ ÷Æ°>z#3b7dd8)ÚbinsÚrangeÚcolorz#d8643bz--g      ø?zmean z.2fz m)rF   Ú	linestyleÚ	linewidthÚlabelz#3bd86bzmedian z	Depth (m)ÚPixelsz"Training Labels Depth Distributionzupper rightF)ÚlocÚframeonzimages: z
min: z m
max: z m
std: g\�Âõ(\ï?gffffffæ?ÚrightÚtopé	   ÚroundÚwhiteg333333ã?Únone)ÚboxstyleÚ	facecolorÚalphaÚ	edgecolor)Ú	transformÚhaÚvaÚfontsizeÚbboxéÈ   )Údpi))Úmatplotlib.pyplotÚpyplotÚnumpyr   Úinfor7   Útrain_loaderÚdatasetÚlenÚim_filesÚwarningÚminÚlinspaceÚastypeÚintÚmaxÚ_load_depthÚravelÚsizeÚappendÚconcatenater4   Ú
percentileÚmeanÚmedianÚstdÚsubplotsÚhistÚaxvlineÚ
set_xlabelÚ
set_ylabelÚ	set_titleÚlegendÚtextÚ	transAxesÚspinesÚvaluesÚset_visibleÚsavefigÚcloseÚon_plot)r   ÚpltÚnprc   ÚnÚsample_sizeÚindicesÚper_map_capr   ÚidxÚdÚvÚvminÚvmaxrr   rs   rt   Ú_ÚaxÚstatsÚspineÚfnames                         r   Úplot_training_labelsz!DepthTrainer.plot_training_labels;   s  € ð 	(Ð'Ð'Ð'Ð'Ð'ØÐÐÐåŒÐK¨$¬-¸,Ñ*FÐKÐKÐKÑLÔLÐLØÔ#Ô+ˆÝ�Ô Ñ!Ô!ˆØ�Š6ˆ6ÝŒNÐFÑGÔGÐGØˆFå˜$ ‘l”lˆØ—+’+˜a  Q¡¨Ñ4Ô4×;Ò;½CÑ@Ô@ˆÝ˜!˜Y¨+Ñ5Ñ6Ô6ˆØˆØð 		ð 		ˆCØ×#Ò# CÑ(Ô(ˆAØˆyØØ�!�a’%”—’Ñ Ô ˆAØŒv˜Š{ˆ{ØØŒv˜Ò#Ð#Ø�b—k’k ! Q¤V¨a¡Z°Ñ=Ô=×DÒDÅSÑIÔIÔJ�Ø�MŠM˜!ÑÔÐÐàð 	ÝŒNÐNÑOÔOÐOØˆFà—’ Ñ'Ô'ˆÝ˜6Ÿ:š:™<œ<Ñ(Ô(­%°·²¸fÀdÑ0KÔ0KÑ*LÔ*LˆdˆÝ! &§+¢+¡-¤-Ñ0Ô0µ%¸¿	º	À&Ñ8IÔ8IÑ2JÔ2JÍEÐRX×R\ÒR\ÑR^ÔR^ÑL_ÔL_�cˆfˆà—’˜Q ¨6À�ÑEÔE‰ˆˆ2Ø
�Š�˜S¨­s°4¸À¹Ñ/EÔ/EÐ(FÈiˆÑXÔXÐXØ
�
Š
�4˜y°DÀCÐOcÐW[ÐOcÐOcÐOcÐOcˆ
ÑdÔdÐdØ
�
Š
�6 °dÀcÐQiÐ[aÐQiÐQiÐQiÐQiˆ
ÑjÔjÐjØ
�Š�kÑ"Ô"Ð"Ø
�Š�hÑÔÐØ
�ŠÐ9Ñ:Ô:Ð:Ø
�	Š	�m¨Uˆ	Ñ3Ô3Ð3Øh˜;ÐhÐh¨tÐhÐhÐhÀ&Ç*Â*Á,Ä,ÐhÐhÐhÐ]`ÐhÐhÐhÐhˆØ
�ŠØØØØ”lØØØØ%°GÀcÐX^Ð_Ð_ð 	ñ 		
ô 		
ð 		
ð ”Y×%Ò%Ñ'Ô'ð 	%ð 	%ˆEØ×Ò˜eÑ$Ô$Ð$Ð$à” Ñ,ˆØ�Š�E˜sˆÑ#Ô#Ð#Ø�	Š	‰ŒˆØŒ<ð 	 Ø�LŠL˜ÑÔÐÐÐð	 ð 	 r   c                ó  •— t          ¦   «                              ¦   «          t          dvrdS 	 ddlm} t          j        d¦  «         | j                             ¦   «         r| j        n| j	        }| j        | j	        fD �]G}|                     ¦   «         �r/| j
        j        r||k    r| j        nd}| j        | j
        j                 }d}t          |t           t"          f¦  «        r‚	 t#          |¦  «                             ¦   «                              t#          | j        d         ¦  «                             ¦   «         ¦  «                             ¦   «         }n# t*          $ r Y nw xY w ||| j        | j        || j                             d¦  «        || j                             d¦  «        pd	¬
¦  «        }||k    r	|�|| _        �ŒIdS # t4          $ r8}t          j        dt9          |¦  «        j        › d|› d�¦  «         Y d}~dS d}~ww xY w)aŸ  Run the standard final evaluation, then calibrate the saved checkpoints.

        After training, fits the scale-only log-affine (``cal_a``/``cal_b``) on the validation
        set and writes it into best.pt/last.pt, so the model outputs metric-scaled depth out of
        the box. When ``plots`` is set, also writes ``val_batch{ni}_calibrated.jpg``
        (RGB | GT | raw | calibrated) comparison panels.
        r(   Nr>   )Úcalibrate_checkpointz7Calibrating depth output scale on the validation set...ÚpathÚhashÚ	max_depthg      Y@)Úplot_dirÚdataset_hashÚvalidation_splitr™   zCalibration skipped (z: z!); checkpoints left uncalibrated.)r   Ú
final_evalr   Ú	calibrater–   r   ra   ÚbestÚexistsÚlastr8   Úplotsr7   r+   ÚsplitÚ
isinstanceÚstrr   ÚresolveÚrelative_toÚas_posixÚ
ValueErrorr:   Údevicer,   Údepth_calibrationÚ	Exceptionrf   ÚtypeÚ__name__)
r   r–   Ú	plot_ckptÚckptrš   Úvalidation_pathrœ   Ú
provenanceÚer   s
            €r   r�   zDepthTrainer.final_eval‚   s6  ø€ õ 	‰Œ×ÒÑÔÐÝ�wÐÐØˆFð 	mØ7Ð7Ð7Ð7Ð7Ð7åŒKÐQÑRÔRÐRØ%)¤Y×%5Ò%5Ñ%7Ô%7ÐF˜œ	˜	¸T¼YˆIØœ D¤IÐ.ð <ñ <�Ø—;’;‘=”=ñ <Ø04´	´Ð_ÀDÈIÒDUÐDU˜tœ}˜}Ð[_�HØ&*¤i°´	´Ô&@�OØ'+Ð$Ý! /µC½°;Ñ?Ô?ð 	!ð!å $ _Ñ 5Ô 5ß!(¢¡¤ß!,¢­T°$´)¸FÔ2CÑ-DÔ-D×-LÒ-LÑ-NÔ-NÑ!OÔ!Oß!)¢¡¤ð	 -Ð,øõ  *ð !ð !ð !Ø ˜Dð!øøøà!5Ð!5ØØÔ(ØœØ!)Ø%)¤Y§]¢]°6Ñ%:Ô%:Ø)9Ø"&¤)§-¢-°Ñ"<Ô"<Ð"EÀð"ñ "ô "�Jð ˜yÒ(Ð(¨ZÐ-CØ1;˜Ô.ùð3<ð <øõ4 ð 	mð 	mð 	mÝŒNÐkµ4¸±7´7Ô3CÐkÐkÀqÐkÐkÐkÑlÔlÐlÐlÐlÐlÐlÐlÐløøøøð	møøøs>   ®B7G Ã&A0EÅG Å
E$Å!G Å#E$Å$A"G Ç
H
Ç-HÈH
)r   r   r   r   r   r   )NNT)r   r    r!   r    r"   r#   r   r	   )r0   r1   r   r1   )r   r5   )r   r   )r®   Ú
__module__Ú__qualname__Ú__doc__r
   r   r/   r3   r<   r   r”   r�   Ú__classcell__)r   s   @r   r   r      sô   ø€ € € € € ð	ð 	ð À$Ðbfð5ð 5ð 5ð 5ð 5ð 5ð 5ðð ð ð ð ðð ð ð ð ð ð
ð 
ð 
ð 
ð €\�^„^ðD ð D ð D ñ „^ðD ðL+mð +mð +mð +mð +mð +mð +mð +mð +mð +mr   r   N)r¶   Ú
__future__r   r   Úpathlibr   Útypingr   Úultralytics.modelsr   Úultralytics.models.yolo.detectr   Úultralytics.nn.tasksr	   Úultralytics.utilsr
   r   r   Úultralytics.utils.plottingr   r   © r   r   ú<module>rÁ      s  ðà /Ð /à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à #Ð #Ð #Ð #Ð #Ð #Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø +Ð +Ð +Ð +Ð +Ð +Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3ð\mð \mð \mð \mð \mÐ#ñ \mô \mð \mð \mð \mr   