§
    l¬¸jÄ  ã                  ó¤   — d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z
 ddlmc mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ  G d
„ de¦  «        ZdS )z+Depth estimation validator for YOLO models.é    )Úannotations)ÚPath)ÚAnyN)ÚDetectionValidator)ÚLOGGERÚRANK)ÚDepthMetrics)Úplot_imagesc                  ó‚   ‡ — e Zd ZdZ	 	 	 	 ddˆ fd	„Zd d„Zd!ˆ fd„Zd"d„Zd#d„Zd$d„Z	d%d„Z
d%d„Zd%d„Zd&d„Zd'd„Zˆ xZS )(ÚDepthValidatorz©Validator for YOLO depth estimation models.

    Computes standard depth metrics: delta1, abs_rel, rmse, silog. Uses validation loss as the primary training signal.
    NÚsave_dirústr | Path | NoneÚ
_callbacksúdict | NoneÚreturnÚNonec                óh   •— t          ¦   «                              ||||¦  «         d| j        _        dS )zInitialize DepthValidator.ÚdepthN)ÚsuperÚ__init__ÚargsÚtask)ÚselfÚ
dataloaderr   r   r   Ú	__class__s        €úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/depth/val.pyr   zDepthValidator.__init__   s0   ø€ õ 	‰Œ×Ò˜ X¨t°ZÑ@Ô@Ð@Ø ˆŒ	Œˆˆó    Úmodelútorch.nn.Modulec                ó–   — t          | j                             d¦  «        pd¬¦  «        | _        | j                             ¦   «          dS )zGInitialize the DepthMetrics accumulator with the dataset's depth range.Ú	max_depthg      Y@)r!   N)r	   ÚdataÚgetÚmetricsÚclear_stats)r   r   s     r   Úinit_metricszDepthValidator.init_metrics$   sA   € å#¨d¬i¯mªm¸KÑ.HÔ.HÐ.QÈEÐRÑRÔRˆŒØŒ× Ò Ñ"Ô"Ð"Ð"Ð"r   Úbatchúdict[str, Any]c                ó„   •— t          ¦   «                              |¦  «        }|d                              ¦   «         |d<   |S )uQ   Preprocess batch â€” move to device, normalize images, and keep depth as float32.r   )r   Ú
preprocessÚfloat)r   r'   r   s     €r   r*   zDepthValidator.preprocess)   s8   ø€ å‘”×"Ò" 5Ñ)Ô)ˆØ˜wœ×-Ò-Ñ/Ô/ˆˆg‰Øˆr   Úpredsútorch.Tensorc                ó   — |S )u5   No NMS needed for depth â€” return predictions as-is.© )r   r,   s     r   ÚpostprocesszDepthValidator.postprocess/   s   € àˆr   c                óz  — |d         }|j         dk    r|                     d¦  «        }|j         dk    r|                     d¦  «        }|j        dd…         |j        dd…         k    r7t          j        |                     ¦   «         |j        dd…         dd¬¦  «        }| j                             ||¦  «         dS )	z%Accumulate depth metrics for a batch.r   é   é   éþÿÿÿNÚbilinearT)ÚsizeÚmodeÚalign_corners)ÚndimÚ	unsqueezeÚshapeÚFÚinterpolater+   r$   Úupdate_stats)r   r,   r'   Úgt_depths       r   Úupdate_metricszDepthValidator.update_metrics3   s³   € à˜”>ˆØŒ=˜AÒÐØ×)Ò)¨!Ñ,Ô,ˆHØŒ:˜Š?ˆ?Ø—O’O AÑ&Ô&ˆEØŒ;�r�s�sÔ˜xœ~¨b¨c¨cÔ2Ò2Ð2Ý”M %§+¢+¡-¤-°h´nÀRÀSÀSÔ6IÐPZÐjnÐoÑoÔoˆEØŒ×!Ò! %¨Ñ2Ô2Ð2Ð2Ð2r   údict[str, float]c                óL   — | j                              ¦   «          | j         j        S )zÕFinalize and return the metrics dict.

        Cross-rank metric reduction is handled by gather_stats() (called before this on all ranks);
        this runs on rank 0 with the already-summed accumulators.
        )r$   ÚprocessÚresults_dict©r   s    r   Ú	get_statszDepthValidator.get_stats>   s#   € ð 	Œ×ÒÑÔÐØŒ|Ô(Ð(r   c                óF  — t           dk    st          j        ¦   «         sdS | j        j        }|�|                     | j        ¦  «        n%t          j        dt          j	        | j        ¬¦  «        }t          j
        | j        j        gt          j	        | j        ¬¦  «        }t          j        |t          j        j        ¬¦  «         t          j        |t          j        j        ¬¦  «         || j        _        t          |                     ¦   «         ¦  «        | j        _        dS )aü  Sum depth metric accumulators across DDP ranks onto rank 0.

        Validation is sharded (ContiguousDistributedSampler gives each rank a distinct chunk of the
        val set), so each rank holds only its shard's summed statistics. All-reduce the sums so
        rank 0's get_stats() computes metrics over the full val set instead of a single shard.
        Overrides DetectionValidator.gather_stats(), which reduces detection-specific stats/box
        attributes that DepthMetrics does not have.
        éÿÿÿÿNé   )ÚdtypeÚdevice)Úop)r   ÚdistÚis_initializedr$   Ú_totalsÚtorK   ÚtorchÚzerosÚfloat64ÚtensorÚ_countÚ
all_reduceÚReduceOpÚSUMr+   Úitem)r   ÚtotalsÚcounts      r   Úgather_statszDepthValidator.gather_statsG   sæ   € õ �2Š:ˆ:�TÔ0Ñ2Ô2ˆ:ØˆFØ”Ô%ˆà&,Ð&8ˆF�IŠI�d”kÑ"Ô"Ð"½e¼kÈ!ÕSXÔS`ÐimÔitÐ>uÑ>uÔ>uð 	õ ”˜dœlÔ1Ð2½%¼-ÐPTÔP[Ð\Ñ\Ô\ˆÝŒ˜¥4¤=Ô#4Ð5Ñ5Ô5Ð5ÝŒ˜¥$¤-Ô"3Ð4Ñ4Ô4Ð4Ø%ˆŒÔÝ# E§J¢J¡L¤LÑ1Ô1ˆŒÔÐÐr   c                óN  — | j         j        }| j        �t          | j        j        ¦  «        n| j        pd}d}t          j        |d||                     dd¦  «        |                     dd¦  «        |                     dd¦  «        |                     d	d¦  «        fz  ¦  «         dS )
a  Log the headline depth metrics in the detection-style aligned table format.

        Columns line up with get_desc(): Class, Images, delta1, abs_rel, rmse, silog.
        Uses "depth_val" as the row label (depth has no classes, where detection prints "all").
        Nr   z %22s%11i%11.4g%11.4g%11.4g%11.4gÚ	depth_valzmetrics/delta1g        zmetrics/abs_relzmetrics/rmsezmetrics/silog)	r$   rD   r   ÚlenÚdatasetÚseenr   Úinfor#   )r   ÚrÚn_imagesÚpfs       r   Úprint_resultszDepthValidator.print_results\   s²   € ð ŒLÔ%ˆØ37´?Ð3N•3�t”Ô.Ñ/Ô/Ð/ÐUYÔU^ÐUcÐbcˆØ+ˆÝŒØàØØ—’Ð&¨Ñ,Ô,Ø—’Ð'¨Ñ-Ô-Ø—’�n cÑ*Ô*Ø—’�o sÑ+Ô+ðññ
	
ô 
	
ð 
	
ð 
	
ð 
	
r   c                óJ   — | j         | j        _         | j        | j        _        dS )z#Set final values for metrics speed.N)Úspeedr$   r   rE   s    r   Úfinalize_metricszDepthValidator.finalize_metricsq   s    € à!œZˆŒÔØ $¤ˆŒÔÐÐr   Ústrc                ó   — ddz  S )z$Return description for progress bar.z%22s%11s%11s%11s%11s%11s)ÚClassÚImagesÚdelta1Úabs_relÚrmseÚsilogr/   rE   s    r   Úget_desczDepthValidator.get_descv   s   € à#Ð'`Ñ`Ð`r   ÚniÚintc           	     óx   — t          d|i|d         |d         | j        d|› d�z  | j        | j        ¬¦  «         dS )a  Save predicted depth overlays to val_batch{ni}_pred.jpg.

        Depth has no boxes/classes, so the detection-style plotter is replaced with a depth heatmap overlay
        through the shared ``plot_images`` path, matching the semantic-segmentation visualization style.
        r   ÚimgÚim_fileÚ	val_batchz	_pred.jpg)ÚlabelsÚimagesÚpathsÚfnameÚnamesÚon_plotN)r
   r   r}   r~   )r   r'   r,   rs   s       r   Úplot_predictionszDepthValidator.plot_predictionsz   s\   € õ 	Ø˜UÐ#Ø˜”<Ø˜	Ô"Ø”-Ð";¨bÐ";Ð";Ð";Ñ;Ø”*Ø”Lð	
ñ 	
ô 	
ð 	
ð 	
ð 	
r   )NNNN)r   r   r   r   r   r   )r   r   r   r   )r'   r(   r   r(   )r,   r-   r   r-   )r,   r-   r'   r(   r   r   )r   rA   )r   r   )r   rj   )r'   r(   r,   r-   rs   rt   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r*   r0   r@   rF   r\   rf   ri   rr   r   Ú__classcell__)r   s   @r   r   r      s0  ø€ € € € € ðð ð Ø&*ØØ"&ð	!ð 	!ð 	!ð 	!ð 	!ð 	!ð 	!ð#ð #ð #ð #ð
ð ð ð ð ð ðð ð ð ð	3ð 	3ð 	3ð 	3ð)ð )ð )ð )ð2ð 2ð 2ð 2ð*
ð 
ð 
ð 
ð*.ð .ð .ð .ð
að að að að
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   r   )rƒ   Ú
__future__r   Úpathlibr   Útypingr   rQ   Útorch.distributedÚdistributedrM   Útorch.nn.functionalÚnnÚ
functionalr<   Úultralytics.models.yolo.detectr   Úultralytics.utilsr   r   Úultralytics.utils.metricsr	   Úultralytics.utils.plottingr
   r   r/   r   r   ú<module>r‘      s  ðà 1Ð 1à "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ø Ð Ð Ð Ð Ð Ð Ð Ð à =Ð =Ð =Ð =Ð =Ð =Ø *Ð *Ð *Ð *Ð *Ð *Ð *Ð *Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2ðt
ð t
ð t
ð t
ð t
Ð'ñ t
ô t
ð t
ð t
ð t
r   