CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hukaixuan19970627

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: hukaixuan19970627/yolov5_obb
Path: blob/master/utils/loggers/wandb/__pycache__/wandb_utils.cpython-39.pyc
Views: 475
a

fx�a�i�	@sNdZddlZddlZddlZddlmZddlmZddlm	Z	ddl
Z
ddlmZee��
�ZejdZee�ejvr�ej�ee��ddlmZmZdd	lmZmZmZzddlZeed
�s�J�Wneefy�dZYn0ee� dd��Z!d
Z"e"fdd�Z#dd�Z$dd�Z%dd�Z&dd�Z'dd�Z(Gdd�d�Z)eej*fdd��Z+dS)z<Utilities and tools for tracking runs with Weights & Biases.�N)�contextmanager)�Path)�Dict)�tqdm�)�LoadImagesAndLabels�img2label_paths)�LOGGER�
check_dataset�
check_file�__version__�RANK�����zwandb-artifact://cCs|t|�d�S)N)�len)�from_string�prefix�r�F/home/test/Persons/hukaixuan/yolov5/utils/loggers/wandb/wandb_utils.py�
remove_prefix srcCs&d�|�dd��}t|���r"|S|S)Nz_wandb.�.�)�join�rsplitr�is_file)Zdata_config_fileZwandb_configrrr�check_wandb_config_file$srcCs�d}d}t|�r�|�d�r�t|dd��}t�|�}Wd�n1sF0Yt|dt�oj|d�t�}t|dt�o�|d�t�}|s�|r�|St	|�SdS)NF�.yaml�ignore��errors�train�val)
r�endswith�open�yaml�	safe_load�
isinstance�str�
startswith�WANDB_ARTIFACT_PREFIXr
)�	data_fileZis_trainset_wandb_artifactZis_valset_wandb_artifact�f�	data_dictrrr�check_wandb_dataset+s(��r,cCs>tt|t��}|j}|jj}|jjj}d|d}||||fS)N�run_�_model)rrr(�stem�parent)�run_path�run_id�project�entity�model_artifact_namerrr�get_run_info;s
r6cCs�tdvrt|�ndt|jt�r�|j�t�r�tdvr�t|j�\}}}}t�	�}|�
|d|d|d�}|��}tt|�d�|_
dSdS)N)rr�/�:latest�last.ptT)r
�process_wandb_config_ddp_moder%�resumer&r'r(r6�wandb�Api�artifact�downloadr�weights)�optr4r3r2r5�apir>�modeldirrrr�check_wandb_resumeDsrDcCsftt|j�dd��}t�|�}Wd�n1s20Yd\}}t|dt�r�|d�t�r�t	�
�}|�t|d�d|j
�}|��}t|�d}t|�|d<t|dt��r|d�t��rt	�
�}|�t|d�d|j
�}|��}t|�d}	t|	�|d<|�s|�rbtt|�d�}
t|
d	��}t�||�Wd�n1�sR0Y|
|_dS)
Nrr�NNr�:�data/images/r zwandb_local_data.yaml�w)r"r�datar#r$r%r&r'r(r<r=r>r�artifact_aliasr?r�	safe_dump)rAr*r+Z	train_dirZval_dirrB�train_artifact�
train_path�val_artifact�val_pathZ
ddp_data_pathrrrr:Rs(( ,r:c@s�eZdZdZd#dd�Zdd�Zdd	�Zd
d�Zdd
�Zd$dd�Z	d%dd�Z
dd�Zd&ee
eefed�dd�Zdd�Zdd�Zdd�Zd'dd �Zd!d"�ZdS)(�WandbLoggeraLog training runs, datasets, models, and predictions to Weights & Biases.

    This logger sends information to W&B at wandb.ai. By default, this information
    includes hyperparameters, system configuration and metrics, model metrics,
    and basic data metrics and analyses.

    By providing additional command line arguments to train.py, datasets,
    models and predictions can also be logged.

    For more on how this logger is used, see the Weights & Biases documentation:
    https://docs.wandb.com/guides/integrations/yolov5
    N�Trainingc
Cs�||_ttsdntj|_|_d\|_|_d\|_|_d|_d\|_	|_
g|_d|_d|_
d|_d|_t|jt�r�|j�t�r�t|j�\}}}}t|}ts�Jd��tj|||ddd�|_||_n\|j�rtj�stj|d|jdkr�d	n
t|j�j|j|jd
k�r|jnd||dd�ntj|_|j�r�|jdk�r�|j�rN|j�sN|�|�|_|j�r�t|jt��r�|j�t��r�t|jjj�|_nt |j!�|_n4t |j!�|_|j�p�|j|_|jjj"d
|jidd�|�#|�|jdk�r�|jj�"ddi�|�|�|_dS)ak
        - Initialize WandbLogger instance
        - Upload dataset if opt.upload_dataset is True
        - Setup trainig processes if job_type is 'Training'

        arguments:
        opt (namespace) -- Commandline arguments for this run
        run_id (str) -- Run ID of W&B run to be resumed
        job_type (str) -- To set the job_type for this run

       NrE�z"install wandb to resume wandb runs�allowT)�idr3r4r;�allow_val_change�
runs/train�YOLOv5�exp)�configr;r3r4�name�job_typerTrUrQr+)rUzDataset Creation�upload_dataset)$r[r<�run�	wandb_runrNrL�train_artifact_path�val_artifact_path�result_artifact�	val_table�result_table�bbox_media_panel_images�val_table_path_map�max_imgs_to_logZwandb_artifact_data_dictr+r%r;r&r'r(r6�initr3rr/r4rZr\�check_and_upload_dataset�dictrYr,rI�update�setup_training)�selfrAr2r[r4r3r5rrr�__init__xsl
��
���
zWandbLogger.__init__cCsntsJd��|�|j|j|jdkr&dn
t|j�j�}t|dd��}t�	|�}Wd�n1s`0Y|S)a+
        Check if the dataset format is compatible and upload it as W&B artifact

        arguments:
        opt (namespace)-- Commandline arguments for current run

        returns:
        Updated dataset info dictionary where local dataset paths are replaced by WAND_ARFACT_PREFIX links.
        zInstall wandb to upload datasetrVrWrrN)
r<�log_dataset_artifactrI�
single_clsr3rr/r"r#r$)rlrA�config_pathr*Zwandb_data_dictrrrrh�s
�(z$WandbLogger.check_and_upload_datasetc
Cs�id|_|_|j|_t|jt�r�|�|�\}}|r�t|�d|_|j	j
}t|j�|j|j|j|j
|jf\|_|_|_|_|_
|_|j}|jdur�|�|�d�|j�\|_|_|�|�d�|j�\|_|_|jdur�t|j�d}t|�|d<|jdu�rt|j�d}t|�|d<|jdu�r�t�dtjjdd	�|_gd
�}|�|jd�t�|�|_|j�d�|_|j du�r�|�!�|jdk�r�|j
d
k�r�|j
d
nd|_|_|jdu�o�|jdu}	|	�r�||_dS)a�
        Setup the necessary processes for training YOLO models:
          - Attempt to download model checkpoint and dataset artifacts if opt.resume stats with WANDB_ARTIFACT_PREFIX
          - Update data_dict, to contain info of previous run if resumed and the paths of dataset artifact if downloaded
          - Setup log_dict, initialize bbox_interval

        arguments:
        opt (namespace) -- commandline arguments for this run

        rr9Nrr rGr-�	_progress�
evaluation��epochrTzground truth�
prediction�namesr�
r)"�log_dict�
current_epoch�
bbox_intervalr%r;r&�download_model_artifactrr@r^rY�save_period�
batch_size�epochs�hypr+rN�download_dataset_artifact�getrJr_rLr`r<�Artifactr]rTra�extend�Tablercrbre�map_val_table_path)
rlrArC�_rYr+rMrO�columnsZtrain_from_artifactrrrrk�sP��
��
"zWandbLogger.setup_trainingcCsdt|t�r`|�t�r`tt|t�d|�}t�|���	dd��}|dusPJd��|�
�}||fSdS)a�
        download the model checkpoint artifact if the path starts with WANDB_ARTIFACT_PREFIX

        arguments:
        path -- path of the dataset to be used for training
        alias (str)-- alias of the artifact to be download/used for training

        returns:
        (str, wandb.Artifact) -- path of the downladed dataset and it's corresponding artifact object if dataset
        is found otherwise returns (None, None)
        rF�\r7Nz+'Error: W&B dataset artifact doesn't exist'rE)r%r&r'r(rrr<�use_artifact�as_posix�replacer?)rl�path�aliasZ
artifact_pathZdataset_artifactZdatadirrrrr�sz%WandbLogger.download_dataset_artifactcCsr|j�t�rnt�t|jt�d�}|dus2Jd��|��}|j�d�}|j�d�}|du}|rfJd��||fSdS)z�
        download the model checkpoint artifact if the resume path starts with WANDB_ARTIFACT_PREFIX

        arguments:
        opt (namespace) -- Commandline arguments for this run
        r8Nz'Error: W&B model artifact doesn't exist�epochs_trained�total_epochsz6training is finished, can only resume incomplete runs.rE)	r;r'r(r<r�rr?�metadatar�)rlrA�model_artifactrCr�r�Zis_finishedrrrr{sz#WandbLogger.download_model_artifactFc
Cs�tjdtjjddt|�|d|j|j|j|d�d�}|jt|d�dd�tj	|d	d
dt|j
�|rldnd
gd�t�d|d���dS)a�
        Log the model checkpoint as W&B artifact

        arguments:
        path (Path)   -- Path of directory containing the checkpoints
        opt (namespace) -- Command line arguments for this run
        epoch (int)  -- Current epoch number
        fitness_score (float) -- fitness score for current epoch
        best_model (boolean) -- Boolean representing if the current checkpoint is the best yet.
        r-r.�modelr)Zoriginal_urlr�zsave periodr3r��
fitness_score)�typer�r9�rZ�latest�last�epoch �best���aliaseszSaving model artifact on epoch N)
r<r�r]rTr&r|r3r~�add_file�log_artifactryr	�info)rlr�rArtr��
best_modelr�rrr�	log_model*s�
�zWandbLogger.log_modelcCs|jjj}t|t�o|dk}t|�|_t|j�}|r>ddgfnt|d�|df\}}	dd�t	|	�D�}	|s�|�
d�r�|jt|dd	dd
�|	dd�nd|_
|�
d�r�ttt|�d�|d<|�
d�r�|jt|dd	dd
�|	dd�nd|_|�
d��rttt|�d�|d<t|�}
|�s�|�r(|
jn|
jd
d}
td�|
}
|�dd�|�dd�t|
d��,}t�||�t�d|
���Wd�n1�s�0Y|jdk�r�|�s�|j�|j
�|j�|j�|j��|j�
d�|_|��n|j�|j
�|j�|j�|
S)a�
        Log the dataset as W&B artifact and return the new data file with W&B links

        arguments:
        data_file (str) -- the .yaml file with information about the dataset like - path, classes etc.
        single_class (boolean)  -- train multi-class data as single-class
        project (str) -- project name. Used to construct the artifact path
        overwrite_config (boolean) -- overwrites the data.yaml file if set to true otherwise creates a new
        file with _wandb postfix. Eg -> data_wandb.yaml

        returns:
        the new .yaml file with artifact links. it can be used to start training directly from artifacts
        r r�item�ncrvcSsi|]\}}||�qSrr)�.0�k�vrrr�
<dictcomp>U�z4WandbLogger.log_dataset_artifact.<locals>.<dictcomp>rT)�rectr}r�NZ_wandbrrIr?r�rHzCreated dataset config file rQ)r^rYr\r%r&r
r+ri�int�	enumerater��create_dataset_tablerrLr(rrNr/�popr"r#rKr	r�r[r�r��waitrbr�)rlr)ror3Zoverwrite_configr\Zlog_val_onlyrIr�rvr�r*rrrrnBs`


&�
���
�
���0�

z WandbLogger.log_dataset_artifactcCs@i|_t�d�tt|jj��D]\}}|d|j|d<q dS)z�
        Map the validation dataset Table like name of file -> it's id in the W&B Table.
        Useful for - referencing artifacts for evaluation.
        zMapping datasetrrN)rer	r�r�rrbrI)rl�irIrrrr�{s
zWandbLogger.map_val_table_path�dataset)r��class_to_idrZc	Cs�tj|dd�}t|jt�r4t|j���r4t|jg�nd}|sFt|j�n|}|D]�}t|���r�|j	|dd�d�
|j�dd��}|j	|d	d�qN|j|d
t|�j
d�tt|g�d�}|��r�|jt|�d|j
d�ndqNtjgd
�d�}	t�dd�|��D��}
tt|��D]�\}\}}
}}gi}}|
dd�dd�f��D]T^}}t|�}|�|d|dg|d|dd�|d||d��||||<�qFd||d�i}|	�|tj||
|d�t|���t|�j
��q|�|	|�|S)ar
        Create and return W&B artifact containing W&B Table of the dataset.

        arguments:
        dataset -- instance of LoadImagesAndLabels class used to iterate over the data to build Table
        class_to_id -- hash map that maps class ids to labels
        name -- name of the artifact

        returns:
        dataset artifact to be logged or used
        r�)rZr�Nzdata/imagesr��labels�imagesrzdata/labelsrGrzdata/labels/)rTZtrain_image�ClassesrZ)r�cSsg|]\}}||d��qS�)rTrZr�r�rTrZrrr�
<listcomp>�r�z4WandbLogger.create_dataset_table.<locals>.<listcomp>�r)�middle�width�heightz%s)�position�class_id�box_captionZground_truth��box_data�class_labels)�classes�boxes)r<r�r%r�r&r�is_dirr�	img_filesZadd_dirrrr�rZr�existsr�r��itemsr��tolistr��append�add_data�Image�list�values�add)rlr�r�rZr>r��img_fileZlabels_path�
label_file�table�	class_set�si�imgr��paths�shapesr�Zimg_classes�cls�xywhr�rrrr��sB
*���
 "
��z WandbLogger.create_dataset_tablec	CsTt�dd�|��D��}g}dgt|jd�}i}|��D]��^}}	}
|	dkr<t|
�}
|�|d|d|d|dd	�|
||
�d
|	d��d|	id
d��||
|	7<|
|vr�||
d7<q<d||
<q<|��D]}||||||<q�d||d�i}|j	t
|�j}
|jj
|j|
|jj|
dtj|jj|
d||d�g|�R�dS)ak
        Build evaluation Table. Uses reference from validation dataset table.

        arguments:
        predn (list): list of predictions in the native space in the format - [xmin, ymin, xmax, ymax, confidence, class]
        path (str): local path of the current evaluation image
        names (dict(int, str)): hash map that maps class ids to labels
        cSsg|]\}}||d��qSr�rr�rrrr��r�z5WandbLogger.log_training_progress.<locals>.<listcomp>rrvg�?rr�r�ZminXZminYZmaxXZmaxY� �.3f�class_score�pixel�r�r�r��scores�domain�predictionsr�)r�r�N)r<r�r�rr+r�r�r��keysrerrZrcr�ryrbrIr�)rl�prednr�rvr�r�Zavg_conf_per_classZpred_class_count�xyxy�confr�Z
pred_classr�rTrrr�log_training_progress�s<	��

��z!WandbLogger.log_training_progresscs�|jr|jr|�||��t|j�|jkr�|jdkr�|j|jdkr��fdd�|��D�}d|�d�i}|j�	t
j|||jd��dS)a�
        Log validation data for one image. updates the result Table if validation dataset is uploaded and log bbox media panel

        arguments:
        pred (list): list of scaled predictions in the format - [xmin, ymin, xmax, ymax, confidence, class]
        predn (list): list of predictions in the native space - [xmin, ymin, xmax, ymax, confidence, class]
        path (str): local path of the current evaluation image
        rcsVg|]N�^}}}|d|d|d|dd�t|��|�d|d��d|idd	��qS)
rrr�rr�r�r�r�r�r�)r�)r�r�r�r��rvrrr��s
��z-WandbLogger.val_one_image.<locals>.<listcomp>r�r�)r��captionN)
rbrcr�rrdrfryrzr�r�r<r�rZ)rl�predr�r�rv�imr�r�rr�r�
val_one_image�s	
�zWandbLogger.val_one_imagecCs&|jr"|��D]\}}||j|<qdS)z�
        save the metrics to the logging dictionary

        arguments:
        log_dict (Dict) -- metrics/media to be logged in current step
        N)r^r�rx)rlrx�key�valuerrr�log�szWandbLogger.logcCs6|j�r2t���|jr"|j|jd<zt�|j�WnDtyv}z,t�d|���|j�	�d|_WYd}~n
d}~00i|_g|_Wd�n1s�0Y|j
�r2|j
�|jd�tj
|j
dddt|j�|r�dnd	gd
�t�d|ji�gd�}|�|jd
�t�|�|_t�dtjjdd�|_
dS)z�
        commit the log_dict, model artifacts and Tables to W&B and flush the log_dict.

        arguments:
        best_result (boolean): Boolean representing if the result of this evaluation is best or not
        ZBoundingBoxDebuggerz]An error occurred in wandb logger. The training will proceed without interruption. More info
N�resultr�r�r�r�r�r�rrrsrvr-rq)r^�all_logging_disabledrdrxr<r��
BaseExceptionr	r��finishrar�rcr�r&ryr�r+r�r�r]rT)rl�best_result�er�rrr�	end_epoch�s0�
$
�zWandbLogger.end_epochcCsL|jrH|jr>t��t�|j�Wd�n1s40Ytj��dS)zC
        Log metrics if any and finish the current W&B run
        N)r^rxr�r<r�r]r�)rlrrr�
finish_runs
*zWandbLogger.finish_run)NrQ)F)F)r�)F)�__name__�
__module__�__qualname__�__doc__rmrhrkr�r{r�rnr�rrr�r&r�r�r�r�r�r�rrrrrPjs
H2

9
*)
 rPc	cs:tjjj}t�|�zdVWt�|�nt�|�0dS)a@ source - https://gist.github.com/simon-weber/7853144
    A context manager that will prevent any logging messages triggered during the body from being processed.
    :param highest_level: the maximum logging level in use.
      This would only need to be changed if a custom level greater than CRITICAL is defined.
    N)�logging�root�manager�disable)Z
highest_levelZprevious_levelrrrr�$s


r�),r�r��os�sys�
contextlibr�pathlibr�typingrr#r�__file__�resolve�FILE�parents�ROOTr&r�r��utils.datasetsrr�
utils.generalr	r
rr<�hasattr�ImportError�AssertionErrorr��getenvr
r(rrr,r6rDr:rP�CRITICALr�rrrr�<module>sB

	=