o
    -wi1S                     @   s^  d dl Z d dlmZ d dlmZmZmZmZ d dlmZ zd dl	Z
W n ey/   d dl
Z
Y nw d dlZd dlmZ zd dlZd dlZd dlmZ eejedkr[ejdd	d
 d dlmZ d dlmZ d dlmZmZmZ d dlm Z m!Z!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z, z
d dl-m.Z.mZ W n e/y   d dl0m.Z.mZ Y nw d dl1m2Z2m3Z3 d dl4m5Z5m6Z6 d dl7m8Z8m9Z9m:Z: d dl;m<Z<m=Z= W n e>y Z? ze@e? W Y dZ?[?ndZ?[?ww eee!e)e%f ZAeee"e*e&f ZBeee e(e$ef ZCG dd dZD								d%dedeEdeFdeFdeFd eFd!eeE d"eeF fd#d$ZGdS )&    N)datetime)CallableDictOptionalUnion)version)	telemetry)tqdmz8.0.238zThis integration is tested and supported for ultralytics v8.0.238 and below.
            Please report any issues to https://github.com/wandb/wandb/issues with the tag `yolov8`.F)repeat)YOLO)	Predictor)ClassificationPredictorClassificationTrainerClassificationValidator)DetectionPredictorDetectionTrainerDetectionValidator)PosePredictorPoseTrainerPoseValidator)SegmentationPredictorSegmentationTrainerSegmentationValidator)de_parallel)RANK__version__)plot_bbox_predictions!plot_detection_validation_results)plot_classification_predictions&plot_classification_validation_results)plot_mask_predictionsplot_sam_predictions$plot_segmentation_validation_results)plot_pose_predictionsplot_pose_validation_resultsc                   @   s   e Zd ZdZ				d&dededededed	d
fddZdd ZdefddZ	de
fddZde
fddZe defddZde
fddZdefddZe defddZdefd d!Zdefd"d#Zed	eeef fd$d%Zd
S )'WandBUltralyticsCallbacka  Stateful callback for logging to W&B.

    In particular, it will log model checkpoints, predictions, and
    ground-truth annotations with interactive overlays for bounding boxes
    to Weights & Biases Tables during training, validation and prediction
    for a `ultratytics` workflow.

    Example:
        ```python
        from ultralytics.yolo.engine.model import YOLO
        from wandb.yolov8 import add_wandb_callback

        # initialize YOLO model
        model = YOLO("yolov8n.pt")

        # add wandb callback
        add_wandb_callback(
            model, max_validation_batches=2, enable_model_checkpointing=True
        )

        # train
        model.train(data="coco128.yaml", epochs=5, imgsz=640)

        # validate
        model.val()

        # perform inference
        model(["img1.jpeg", "img2.jpeg"])
        ```

    Args:
        model: (ultralytics.yolo.engine.model.YOLO) YOLO Model of type
            `ultralytics.yolo.engine.model.YOLO`.
        epoch_logging_interval: (int) interval to log the prediction visualizations
            during training.
        max_validation_batches: (int) maximum number of validation batches to log to
            a table per epoch.
        enable_model_checkpointing: (bool) enable logging model checkpoints as
            artifacts at the end of eveny epoch if set to `True`.
        visualize_skeleton: (bool) visualize pose skeleton by drawing lines connecting
            keypoints for human pose.
       Fmodelepoch_logging_intervalmax_validation_batchesenable_model_checkpointingvisualize_skeletonreturnNc                 C   s~   || _ || _|| _|| _|j| _|j| _d|jv r#|jd dd nd | _| 	  | 
| g d| _d | _d | _d | _d S )Nr'   .r   )detectsegmentposeclassify)r(   r)   r*   r+   tasktask_map	overridessplit
model_name_make_tables_make_predictorsupported_taskspromptsrun_idtrain_epoch)selfr'   r(   r)   r*   r+    r>   c/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/integration/ultralytics/callback.py__init__   s    



z!WandBUltralyticsCallback.__init__c                 C   s$  | j dv r-g d}dg| }tjdg| d| _tjdg| d| _tjg dd| _d S | j dkrcg d}d	d
g| }|dd tjddg| d| _tjdg| d| _tjdg| d| _d S | j dkrg d}dg| }tjdg| d| _tjdg| d| _tjg dd| _d S d S )N)r.   r/   )
Data-IndexBatch-IndexImageMean-ConfidenceSpeedEpoch
Model-Namecolumns)rG   rC   zNum-ObjectsrD   rE   r1   )rC   zPredicted-CategoryzPrediction-ConfidencezTop-5-Prediction-CategorieszTop-5-Prediction-ConfindenceProbabilitiesrE   rA   rB      zGround-Truth-Categoryr0   )rA   rB   zImage-Ground-TruthImage-PredictionNum-InstancesrD   rE   )rG   rL   rM   rD   rE   )r2   wandbTabletrain_validation_tablevalidation_tableprediction_tableinsert)r=   validation_columnstrain_columnsclassification_columnsr>   r>   r?   r7      sL   


		


	z%WandBUltralyticsCallback._make_tablesc                 C   s`   t |j}d|d< | j| j d |d| _i | j_d| jj_d| jj_	d| jj_
d | jj_d S )Ng?conf	predictor)r4   F)copydeepcopyr4   r3   r2   rX   	callbacksargssavesave_txt	save_cropverbose)r=   r'   r4   r>   r>   r?   r8      s   


z(WandBUltralyticsCallback._make_predictortrainerc              
   C   s   t dt jj dd}|j|jtt| j	
 t|jj
 |jj|j t|jt  td	}|jd|j d }tj||td || t j|d|j gd	 d S )
Nrun__modelr'   )	epochbest_fitnessr'   emaupdates	optimizer
train_argsdater   rd   z.pt)pickle_moduleepoch_)aliases)rN   Artifactrunidrd   re   rY   rZ   r   r'   halfrf   rg   rh   
state_dictvarsr\   r   now	isoformatr   wdirtorchr]   pickleadd_filelog_artifact)r=   ra   model_checkpoint_artifactcheckpoint_dictcheckpoint_pathr>   r>   r?   _save_model   s"   


z$WandBUltralyticsCallback._save_modelc                 C   sR   t jtjd}d|j_W d    n1 sw   Y  t|jtj_	tjj
| _d S )N)ro   T)r   contextrN   ro   featureultralytics_yolov8rs   r\   configtrainrp   r;   )r=   ra   telr>   r>   r?   on_train_start  s
   
z'WandBUltralyticsCallback.on_train_startc              
   C   s  | j | jv r| j|jkr|j| _| jd | j dkr|j}|j}|j}t|j	
 j| _t|j	tjjjr:|j	j}n|j	}t| | j| _	| jj| j	dd | j dkrkt||| j| j| j| j| j|jd| _nC| j dkrt||| j| j| j| j|jd| _n,| j d	krt||| j| j| j| j|jd| _n| j d
krt|| j| j| j| j|jd| _| j r| !| |j	| j d S d S d S )Nr&   r   F)r'   r`   r0   )
dataloaderclass_label_mapr6   rX   r+   tabler)   rd   r/   )r   r   r6   rX   r   r)   rd   r.   r1   )r   r6   rX   r   r)   rd   )"r2   r9   r<   rd   r(   	validatorr   namesnextr'   
parametersdevice
isinstancerw   nnparallelDistributedDataParallelmodulerY   rZ   evaltorX   setup_modelr$   r6   r+   rP   r)   r"   r   r   r*   r~   )r=   ra   r   r   r   r'   r>   r>   r?   on_fit_epoch_end  sr   






	

	

z)WandBUltralyticsCallback.on_fit_epoch_endc                 C   s(   | j | jv rtjd| jidd d S d S )NzTrain-TableFcommit)r2   r9   rN   logrP   )r=   ra   r>   r>   r?   on_train_endH  s   z%WandBUltralyticsCallback.on_train_endr   c                 C   s.   t jpt j|jjp
dd|jj d d S  d S )NYOLOv8validation_)projectjob_type)rN   ro   initr\   r   r2   )r=   r   r>   r>   r?   on_val_startL  s   


z%WandBUltralyticsCallback.on_val_startc              	   C   s   | j | jv rn|}|j}|j}| j dkr%t||| j| j| j| j| j	d| _n=| j dkr:t
||| j| j| j| j	d| _n(| j dkrOt||| j| j| j| j	d| _n| j dkrbt|| j| j| j| j	d| _tjd| jid	d
 d S d S )Nr0   )r   r   r6   rX   r+   r   r)   r/   )r   r   r6   rX   r   r)   r.   r1   )r   r6   rX   r   r)   zValidation-TableFr   )r2   r9   r   r   r$   r6   rX   r+   rQ   r)   r"   r   r   rN   r   )r=   ra   r   r   r   r>   r>   r?   
on_val_endR  sT   


	



z#WandBUltralyticsCallback.on_val_endrX   c                 C   s\   t jpt j|jjp
dt|jd|jj d t|tr,t	
|j| _t jdgd| _d S d S )Nr   prediction_)r   r   r   rC   rH   )rN   ro   r   r\   r   rs   r2   r   SAMPredictorrY   rZ   r:   rO   rR   )r=   rX   r>   r>   r?   on_predict_start~  s   



z)WandBUltralyticsCallback.on_predict_startc                 C   s   t |jtj_| j| jv rrt|jD ]S}| jdkr%t	|| j
| j| j| _q| jdkrHt|tr9t|| j
| j| _qt|trGt|| j| j| _q| jdkrWt|| j
| j| _q| jdkret|| j
| j| _qtjd| jidd d S d S )Nr0   r/   r.   r1   zPrediction-TableFr   )rs   r\   rN   r   prediction_configsr2   r9   r	   resultsr#   r6   r+   rR   r   r   r    r   r!   r:   r   r   r   )r=   rX   resultr>   r>   r?   on_predict_end  s>   









z'WandBUltralyticsCallback.on_predict_endc                 C   s"   | j | j| j| j| j| j| jdS )zgProperty contains all the relevant callbacks to add to the YOLO model for the Weights & Biases logging.r   r   r   r   r   r   r   r   )r=   r>   r>   r?   r[     s   z"WandBUltralyticsCallback.callbacks)r&   r&   FF)__name__
__module____qualname____doc__r   intboolr@   r7   r8   TRAINER_TYPEr~   r   rw   no_gradr   r   r   VALIDATOR_TYPEr   r   PREDICTOR_TYPEr   r   propertyr   strr   r[   r>   r>   r>   r?   r%   X   sB    .
I
<+
r%   r&   Tr'   r(   r*   enable_train_validation_loggingenable_validation_loggingenable_prediction_loggingr)   r+   c                 C   s   t dv rGtt| ||||}|j}	|s|	d}
|	d}
|s*|	d}
|	d}
|s6|	d}
|	d}
|	 D ]
\}}| || q:| S t	d | S )	a.	  Function to add the `WandBUltralyticsCallback` callback to the `YOLO` model.

    Example:
        ```python
        from ultralytics.yolo.engine.model import YOLO
        from wandb.yolov8 import add_wandb_callback

        # initialize YOLO model
        model = YOLO("yolov8n.pt")

        # add wandb callback
        add_wandb_callback(
            model, max_validation_batches=2, enable_model_checkpointing=True
        )

        # train
        model.train(data="coco128.yaml", epochs=5, imgsz=640)

        # validate
        model.val()

        # perform inference
        model(["img1.jpeg", "img2.jpeg"])
        ```

    Args:
        model: (ultralytics.yolo.engine.model.YOLO) YOLO Model of type
            `ultralytics.yolo.engine.model.YOLO`.
        epoch_logging_interval: (int) interval to log the prediction visualizations
            during training.
        enable_model_checkpointing: (bool) enable logging model checkpoints as
            artifacts at the end of eveny epoch if set to `True`.
        enable_train_validation_logging: (bool) enable logging the predictions and
            ground-truths as interactive image overlays on the images from
            the validation dataloader to a `wandb.Table` along with
            mean-confidence of the predictions per-class at the end of each
            training epoch.
        enable_validation_logging: (bool) enable logging the predictions and
            ground-truths as interactive image overlays on the images from the
            validation dataloader to a `wandb.Table` along with
            mean-confidence of the predictions per-class at the end of
            validation.
        enable_prediction_logging: (bool) enable logging the predictions and
            ground-truths as interactive image overlays on the images from the
            validation dataloader to a `wandb.Table` along with mean-confidence
            of the predictions per-class at the end of each prediction.
        max_validation_batches: (Optional[int]) maximum number of validation batches to log to
            a table per epoch.
        visualize_skeleton: (Optional[bool]) visualize pose skeleton by drawing lines connecting
            keypoints for human pose.

    Returns:
        An instance of `ultralytics.yolo.engine.model.YOLO` with the `WandBUltralyticsCallback`.
    )r   r   r   r   r   r   r   zThe RANK of the process to add the callbacks was neither 0 or -1. No Weights & Biases callbacks were added to this instance of the YOLO model.)
r   r%   rY   rZ   r[   popitemsadd_callbackrN   	termerror)r'   r(   r*   r   r   r   r)   r+   wandb_callbackr[   _eventcallback_fnr>   r>   r?   add_wandb_callback  s2   @





r   )r&   FTTTr&   T)HrY   r   typingr   r   r   r   	packagingr   dillrx   ImportErrorrN   wandb.sdk.libr   rw   ultralytics	tqdm.autor	   parser   termwarnultralytics.modelsr   ultralytics.models.sam.predictr   r    ultralytics.models.yolo.classifyr   r   r   ultralytics.models.yolo.detectr   r   r   ultralytics.models.yolo.poser   r   r   ultralytics.models.yolo.segmentr   r   r   ultralytics.utils.torch_utilsr   ultralytics.yolo.utilsr   ModuleNotFoundErrorultralytics.utils(wandb.integration.ultralytics.bbox_utilsr   r   2wandb.integration.ultralytics.classification_utilsr   r   (wandb.integration.ultralytics.mask_utilsr    r!   r"   (wandb.integration.ultralytics.pose_utilsr#   r$   	ExceptioneErrorr   r   r   r%   r   r   r   r>   r>   r>   r?   <module>   s    

	  b