o
    xiDS                  
   @  s:  d dl mZ d dlZd dlmZ d dlmZmZ d dlmZ zd dl	Z
W n ey1   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,d)d*ZEdS )-    )annotationsN)datetime)Callable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/d0ddZdd Zd1ddZd2ddZd2ddZe	
 d3ddZd2ddZd4d!d"Ze	
 d5d#d$Zd6d'd(Zd6d)d*Zed7d,d-Zd.S )8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modelr
   epoch_logging_intervalintmax_validation_batchesenable_model_checkpointingboolvisualize_skeletonreturnNonec                 C  s   || _ || _|| _|| _|j| _|j| _d|jv r%t|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strsplit
model_name_make_tables_make_predictorsupported_taskspromptsrun_idtrain_epoch)selfr&   r'   r)   r*   r,    rA   Z/home/ubuntu/.local/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)r0   r1   )
Data-IndexBatch-IndexImageMean-ConfidenceSpeedEpoch
Model-Namecolumns)rJ   rF   zNum-ObjectsrG   rH   r3   )rF   zPredicted-CategoryzPrediction-ConfidencezTop-5-Prediction-CategorieszTop-5-Prediction-ConfindenceProbabilitiesrH   rD   rE      zGround-Truth-Categoryr2   )rD   rE   zImage-Ground-TruthImage-PredictionNum-InstancesrG   rH   )rJ   rO   rP   rG   rH   )r4   wandbTabletrain_validation_tablevalidation_tableprediction_tableinsert)r@   validation_columnstrain_columnsclassification_columnsrA   rA   rB   r:      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)r6   F)copydeepcopyr6   r5   r4   r[   	callbacksargssavesave_txt	save_cropverbose)r@   r&   r6   rA   rA   rB   r;      s   


z(WandBUltralyticsCallback._make_predictortrainerTRAINER_TYPEc              
   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   rh   z.pt)pickle_moduleepoch_)aliases)rQ   Artifactrunidrh   ri   r\   r]   r   r&   halfrj   rk   rl   
state_dictvarsr_   r   now	isoformatr   wdirtorchr`   pickleadd_filelog_artifact)r@   rd   model_checkpoint_artifactcheckpoint_dictcheckpoint_pathrA   rA   rB   _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)rs   T)r   contextrQ   rs   featureultralytics_yolov8rw   r_   configtrainrt   r>   )r@   rd   telrA   rA   rB   on_train_start  s
   
z'WandBUltralyticsCallback.on_train_startr   c              
   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&   rc   r2   )
dataloaderclass_label_mapr9   r[   r,   tabler)   rh   r1   )r   r   r9   r[   r   r)   rh   r0   r3   )r   r9   r[   r   r)   rh   )"r4   r<   r?   rh   r'   	validatorr   namesnextr&   
parametersdevice
isinstancer{   nnparallelDistributedDataParallelmoduler\   r]   evaltor[   setup_modelr#   r9   r,   rS   r)   r!   r   r   r*   r   )r@   rd   r   r   r   r&   rA   rA   rB   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)r4   r<   rQ   logrS   )r@   rd   rA   rA   rB   on_train_endJ  s   z%WandBUltralyticsCallback.on_train_endr   VALIDATOR_TYPEc                 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)rQ   rs   initr_   r   r4   )r@   r   rA   rA   rB   on_val_startN  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 )Nr2   )r   r   r9   r[   r,   r   r)   r1   )r   r   r9   r[   r   r)   r0   r3   )r   r9   r[   r   r)   zValidation-TableFr   )r4   r<   r   r   r#   r9   r[   r,   rT   r)   r!   r   r   rQ   r   )r@   rd   r   r   r   rA   rA   rB   
on_val_endT  sT   


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

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

z#WandBUltralyticsCallback.on_val_endr[   PREDICTOR_TYPEc                 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   rF   rK   )rQ   rs   r   r_   r   rw   r4   r   SAMPredictorr\   r]   r=   rR   rU   )r@   r[   rA   rA   rB   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 )Nr2   r1   r0   r3   zPrediction-TableFr   )rw   r_   rQ   r   prediction_configsr4   r<   r   resultsr"   r9   r,   rU   r   r   r   r   r    r=   r   r   r   )r@   r[   resultrA   rA   rB   on_predict_end  s>   









z'WandBUltralyticsCallback.on_predict_enddict[str, Callable]c                 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@   rA   rA   rB   r^     s   z"WandBUltralyticsCallback.callbacksN)r%   r%   FF)r&   r
   r'   r(   r)   r(   r*   r+   r,   r+   r-   r.   )r&   r
   )rd   re   )rd   r   )r   r   )rd   r   )r[   r   )r-   r   )__name__
__module____qualname____doc__rC   r:   r;   r   r   r{   no_gradr   r   r   r   r   r   propertyr^   rA   rA   rA   rB   r$   Z   s*    .
I



<

+

r$   r%   Tr&   r
   r'   r(   r*   r+   enable_train_validation_loggingenable_validation_loggingenable_prediction_loggingr)   
int | Noner,   bool | Nonec                 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$   r\   r]   r^   popitemsadd_callbackrQ   	termerror)r&   r'   r*   r   r   r   r)   r,   wandb_callbackr^   _eventcallback_fnrA   rA   rB   add_wandb_callback  s2   @





r   )r%   FTTTr%   T)r&   r
   r'   r(   r*   r+   r   r+   r   r+   r   r+   r)   r   r,   r   )F
__future__r   r\   r   typingr   r   	packagingr   dillr|   ImportErrorrQ   wandb.sdk.libr   r{   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Errorre   r   r   r$   r   rA   rA   rA   rB   <module>   s    

	  b