Ë
    Fêñi¯  ã                   ó  — d dl mZmZmZmZmZ d dlmZ 	 erJ ‚ed   du sJ ‚da ed«      Z	d dl
mZ d dlZd dlmZ dd	ed
eddfd„Z e«       dd„«       Zdd„Zdd„Zdd„Zdd„ZereeeedœZyi Zy# eeeef$ r dZY ŒCw xY w)é    )ÚLOGGERÚSETTINGSÚTESTS_RUNNINGÚcolorstrÚtorch_utils)Úsmart_inference_modeÚtensorboardTNzTensorBoard: )Údeepcopy)ÚSummaryWriterÚscalarsÚstepÚreturnc                 óp   — t         r0| j                  «       D ]  \  }}t         j                  |||«       Œ yy)aË  Log scalar values to TensorBoard.

    Args:
        scalars (dict): Dictionary of scalar values to log to TensorBoard. Keys are scalar names and values are the
            corresponding scalar values.
        step (int): Global step value to record with the scalar values. Used for x-axis in TensorBoard graphs.

    Examples:
        Log training metrics
        >>> metrics = {"loss": 0.5, "accuracy": 0.95}
        >>> _log_scalars(metrics, step=100)
    N)ÚWRITERÚitemsÚ
add_scalar)r   r   ÚkÚvs       úi/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/callbacks/tensorboard.pyÚ_log_scalarsr      s7   € õ Ø—M‘M“Oò 	*‰DˆAˆqÜ×Ñ˜a  DÕ)ñ	*ð ó    c           	      ój  — | j                   j                  }t        |t        «      r||fn|}t	        | j
                  j                  «       «      }t        j                  ddg|¢­|j                  |j                  ¬«      }	 | j
                  j                  «        t        j                  t        j                  j                  t!        j"                  | j
                  «      |d¬«      g «       t%        j&                  t(        › d�«       y# t*        $ �r}	 t-        t!        j"                  | j
                  «      «      }|j                  «        |j/                  d¬«      }|j1                  «       D ]  }t3        |d	«      sŒd
|_        d|_        Œ  ||«       t        j                  t        j                  j                  ||d¬«      g «       t%        j&                  t(        › d�«       n<# t*        $ r+}t%        j8                  t(        › d|› d|› �«       Y d}~nd}~ww xY wY d}~yY d}~yd}~ww xY w)a  Log model graph to TensorBoard.

    This function attempts to visualize the model architecture in TensorBoard by tracing the model with a dummy input
    tensor. It first tries a simple method suitable for YOLO models, and if that fails, falls back to a more complex
    approach for models like RTDETR that may require special handling.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The trainer object containing the model to visualize. Must
            have attributes model and args with imgsz.

    Notes:
        This function requires TensorBoard integration to be enabled and the global WRITER to be initialized.
        It handles potential warnings from the PyTorch JIT tracer and attempts to gracefully handle different
        model architectures.
    é   é   )ÚdeviceÚdtypeF)Ústrictu#   model graph visualization added âœ…N)ÚverboseÚexportTÚtorchscriptz)TensorBoard graph visualization failure: z -> )ÚargsÚimgszÚ
isinstanceÚintÚnextÚmodelÚ
parametersÚtorchÚzerosr   r   Úevalr   Ú	add_graphÚjitÚtracer   Úunwrap_modelr   ÚinfoÚPREFIXÚ	Exceptionr
   ÚfuseÚmodulesÚhasattrr   ÚformatÚwarning)Útrainerr"   ÚpÚimÚe1r&   ÚmÚe2s           r   Ú_log_tensorboard_graphr=   *   s¸  € ð$ �L‰L×Ñ€EÜ(¨´Ô4ˆU�E‰N¸%€EÜˆW�]‰]×%Ñ%Ó'Ó(€AÜ	�‰�a˜�^˜U‘^¨A¯H©H¸A¿G¹GÔ	D€Bð]Ø�‰×ÑÔÜ×ÑœŸ™Ÿ™¬×)AÑ)AÀ'Ç-Á-Ó)PÐRTÐ]b˜ÓcÐegÔhÜ�‰”v�hÐAÐBÔCØøÜó ]ð	]Üœ[×5Ñ5°g·m±mÓDÓEˆEØ�J‰JŒLØ—J‘J u�JÓ-ˆEØ—]‘]“_ò -�Ü˜1˜hÕ'Ø#�A”HØ,�A•Hð-ñ �"ŒIÜ×ÑœUŸY™YŸ_™_¨U°B¸u˜_ÓEÀrÔJÜ�K‰Kœ6˜(Ð"EÐFÕGøÜò 	]Ü�N‰Nœf˜XÐ%NÈrÈdÐRVÐWYÐVZÐ[×\Ñ\ûð	]úÜ\ô Hûð]úsE   ÂB	D Ä
H2ÄA)G,ÆA*G,Ç+H-Ç,	H Ç5!HÈH-ÈH È H-È-H2c                 ó
  — t         rI	 t        t        | j                  «      «      at	        j
                  t        › d| j                  › d�«       yy# t        $ r(}t	        j                  t        › d|› �«       Y d}~yd}~ww xY w)z2Initialize TensorBoard logging with SummaryWriter.z!Start with 'tensorboard --logdir z!', view at http://localhost:6006/z=TensorBoard not initialized correctly, not logging this run. N)	r   ÚstrÚsave_dirr   r   r/   r0   r1   r6   )r7   Úes     r   Úon_pretrain_routine_startrB   X   sx   € åð	hä"¤3 w×'7Ñ'7Ó#8Ó9ˆFÜ�K‰Kœ6˜(Ð"CÀG×DTÑDTÐCUÐUvÐwÕxð	 øô
 ò 	hÜ�N‰Nœf˜XÐ%bÐcdÐbeÐf×gÑgûð	hús   ˆAA Á	BÁA=Á=Bc                 ó(   — t         rt        | «       yy)zLog TensorBoard graph.N)r   r=   ©r7   s    r   Úon_train_startrE   c   s   € åÜ˜wÕ'ð r   c                 ó²   — t        | j                  | j                  d¬«      | j                  dz   «       t        | j                  | j                  dz   «       y)z5Log scalar statistics at the end of a training epoch.Útrain)Úprefixr   N)r   Úlabel_loss_itemsÚtlossÚepochÚlrrD   s    r   Úon_train_epoch_endrM   i   sA   € ä�×)Ñ)¨'¯-©-ÀÐ)ÓHÈ'Ï-É-ÐZ[ÑJ[Ô\Ü�—‘˜WŸ]™]¨QÑ.Õ/r   c                 óJ   — t        | j                  | j                  dz   «       y)z+Log epoch metrics at end of training epoch.r   N)r   ÚmetricsrK   rD   s    r   Úon_fit_epoch_endrP   o   s   € ä�—‘ '§-¡-°!Ñ"3Õ4r   )rB   rE   rP   rM   )r   )r   N)Úultralytics.utilsr   r   r   r   r   Úultralytics.utils.torch_utilsr   r   r0   Úcopyr
   r(   Útorch.utils.tensorboardr   ÚImportErrorÚAssertionErrorÚ	TypeErrorÚAttributeErrorÚdictr$   r   r=   rB   rE   rM   rP   Ú	callbacks© r   r   ú<module>r\      sÞ   ð÷ UÕ TÝ >ðÙÐÐØ�MÑ" dÑ*Ð*Ð*Ø€FÙ�oÓ&€Fõ ãÝ5ñ*˜$ð * cð *°$ó *ñ$ Óò*]ó ð*]óZhó(ó0ó5ñ ð &?Ø(Ø,Ø0ñ	ñ 
ð 
ñ 
øðE 	�^ Y°Ð?ò ð ‚Mðús   –'A3 Á3BÂ B