o
    &ÕjŸj  ã                   @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ G dd„ deƒZG dd„ deƒZ dS )é    N)Úaudio)Ú	embedding)Ú	histogram)Úhparam)Úimage)Ú	meta_data)Úpr_curve)Ú	roc_curve)Úscalar)Úscalars)Útext)Útranslate_graph)Úbfile)Úlogger)Úfigure_to_image)Úmerge_images)Úmd5)ÚRecordFileWriterc                   @   s`   e Zd ZdZdd„ Zdd„ Zddd„Zdd	d
„Zddd„Zddd„Z	dd„ Z
dd„ Zdd„ ZdS )ÚDummyFileWriterz8A fake file writer that writes nothing to the disk.
    c                 C   s
   || _ d S ©N©Ú_logdir)ÚselfÚlogdir© r   úW/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/writer/writer.pyÚ__init__,   s   
zDummyFileWriter.__init__c                 C   ó   | j S )z7Returns the directory where event file will be written.r   ©r   r   r   r   Ú
get_logdir/   ó   zDummyFileWriter.get_logdirNc                 C   ó   d S r   r   )r   ÚeventÚstepÚwalltimer   r   r   Ú	add_event3   ó   zDummyFileWriter.add_eventc                 C   r!   r   r   )r   ÚsummaryZglobal_stepr$   r   r   r   Úadd_summary6   r&   zDummyFileWriter.add_summaryc                 C   r!   r   r   )r   Zgraph_profiler$   r   r   r   Ú	add_graph9   r&   zDummyFileWriter.add_graphc                 C   r!   r   r   )r   Úgraphr$   r   r   r   Úadd_onnx_graph<   r&   zDummyFileWriter.add_onnx_graphc                 C   r!   r   r   r   r   r   r   Úflush?   r&   zDummyFileWriter.flushc                 C   r!   r   r   r   r   r   r   ÚcloseB   r&   zDummyFileWriter.closec                 C   r!   r   r   r   r   r   r   ÚreopenE   r&   zDummyFileWriter.reopen©NNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r(   r)   r+   r,   r-   r.   r   r   r   r   r   (   s    



r   c                   @   s$  e Zd ZdZ								d:dd„Zed	d
„ ƒZdd„ Zedd„ ƒZ				d;dd„Z	d<dd„Z
d<dd„Zd=dd„Zd<dd„Zd>dd„Z				d?d d!„Z							d@d"d#„Z	$	dAd%d&„ZdBd'd(„Zd<d)d*„Z			dCd+d,„Z			dCd-d.„ZdDd0d1„Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ ZdS )EÚ	LogWriterzÔLog writer to write vdl records to log file.

    The class `LogWriter` provides APIs to create record file and add records to
    it. The class updates log file asynchronously without slowing down training.
    NÚ é
   éx   Tc	                 K   s”   |s ddl m }
 |
 ¡  d¡}d|krd| }tj d|| ¡}|| _|| _|| _|| _	|| _
|	| _|| _d| _i | _|  ¡  i | _| j|d dS )	a•  Create a instance of class `LogWriter` and create a vdl log file with
        given args.

        Args:
            logdir (string): Directory of log file. Default is
                `runs/**current_time**.**comment**`.
            comment (string): Suffix appended to the default `logdir`.It has no
                effect if `logidr` is assigned.
            max_queue (int): Size of queue for pending records.
            flush_secs (int): The duration to flush the pending records in queue
                to disk.
            filename_suffix (string): Suffix added to vdl log file.
            write_to_disk (boolean): Write to disk if it is True.
        r   )Údatetimez%b%d_%H-%M-%Sr5   Ú.ÚrunsN)Údisplay_name)r8   ÚnowÚstrftimeÚosÚpathÚjoinr   Ú
_max_queueÚ_flush_secsÚ_filename_suffixÚ_write_to_diskÚkwargsÚ
_file_nameÚ_file_writerÚ_all_writersÚ_get_file_writerÚloggersÚadd_meta)r   r   ÚcommentZ	max_queueÚ
flush_secsÚfilename_suffixZwrite_to_diskr;   Ú	file_namerE   r8   Úcurrent_timer   r   r   r   P   s$   zLogWriter.__init__c                 C   r   r   r   r   r   r   r   r   |   r    zLogWriter.logdirc                 C   sz   | j st| jd| _| j | j| ji¡ | jS | ji u s!| jd u r:t| j| j| j| j	| j
d| _| j | j| ji¡ | jS )N)r   )r   Zmax_queue_sizerM   rN   Úfilename)rD   r   r   rG   rH   Úupdater   rA   rB   rC   rF   r   r   r   r   rI   €   s   ûzLogWriter._get_file_writerc                 C   s
   | j  ¡ S r   )rG   Úget_filenamer   r   r   r   rO      s   
zLogWriter.file_nameÚmeta_data_tagr   c                 C   óJ   d|v rt dƒ‚|du rtt ¡ d ƒn|}|  ¡  t||||d¡ dS )zñAdd a meta to vdl record file.

        Args:
            tag (string): Data identifier
            display_name (string): Display name of `runs`.
            step (int): Step of meta.
            walltime (int): Wall time of scalar
        ú%ú% can't appear in tag!Néè  )Útagr;   r#   r$   )ÚRuntimeErrorÚroundÚtimerI   Ú
add_recordr   )r   rY   r;   r#   r$   r   r   r   rK   ”   s   üÿzLogWriter.add_metac                 C   rU   )a¾  Add a scalar to vdl record file.

        Args:
            tag (string): Data identifier
            value (float): Value of scalar
            step (int): Step of scalar
            walltime (int): Wall time of scalar

        Example:
            for index in range(1, 101):
                writer.add_scalar(tag="train/loss", value=index*0.2, step=index)
                writer.add_scalar(tag="train/lr", value=index*0.5, step=index)
        rV   rW   NrX   )rY   Úvaluer#   r$   )rZ   r[   r\   rI   r]   r
   )r   rY   r^   r#   r$   r   r   r   Ú
add_scalar«   s   ÿzLogWriter.add_scalarc                 C   sd   d|v rt dƒ‚t|tƒst dƒ‚|du rtt ¡ d ƒn|}t||||ƒD ]	}|  ¡  |¡ q&dS )a,  Add a group of scalars to vdl record file.

        Args:
            main_tag (string): Data identifier
            tag_scalar_dict (float): A dict to provide multi-values with tags
            step (int): Step of scalar
            walltime (int): Wall time of scalar

        Example:
            import math
            for index in range(1, 101):
                alpha = index*2*math.pi/100
                tval = {'sin':math.sin(alpha), 'cos':math.cos(alpha)}
                writer.add_scalars(tag="sin_and_cos", tag_value=tval, step=index)
        rV   rW   ztag_value must be a dict!NrX   )rZ   Ú
isinstanceÚdictr[   r\   r   rI   r]   )r   Zmain_tagZtag_scalar_dictr#   r$   Úrecordr   r   r   Úadd_scalars¿   s   
ÿzLogWriter.add_scalarsÚHWCc              	   C   sL   d|v rt dƒ‚|du rtt ¡ d ƒn|}|  ¡  t|||||d¡ dS )a  Add an image to vdl record file.

        Args:
            tag (string): Data identifier
            img (np.ndarray): Image represented by a numpy.array
            step (int): Step of image
            walltime (int): Wall time of image
            dataformats (string): Format of image

        Example:
            from PIL import Image
            import numpy as np

            I = Image.open("./test.png")
            I_array = np.array(I)
            writer.add_image(tag="lll", img=I_array, step=0)
        rV   rW   NrX   )rY   Úimage_arrayr#   r$   Údataformats)rZ   r[   r\   rI   r]   r   )r   rY   Úimgr#   r$   rf   r   r   r   Ú	add_image×   s   ûÿzLogWriter.add_imagec                 C   sR   d|v rt dƒ‚|du rtt ¡ d ƒn|}t|ƒ}|  ¡  t||||d¡ dS )aH  Add an figure to vdl record file.

        Args:
            tag (string): Data identifier
            figure (matplotlib.figure.Figure): Image represented by a Figure
            step (int): Step of image
            walltime (int): Wall time of image
            dataformats (string): Format of image

        Example:
            form matplotlib import pyplot as plt
            import numpy as np

            x = np.arange(100)
            y = x ** 2 + 1
            plt.plot(x, y)
            fig = plt.gcf()
            writer.add_figure(tag="lll", figure=fig, step=0)
        rV   rW   NrX   )rY   re   r#   r$   )rZ   r[   r\   r   rI   r]   r   )r   rY   Zfigurer#   r$   rg   r   r   r   Ú
add_figureô   s   ÿzLogWriter.add_figurec                 C   rU   )a{  Add an text to vdl record file.
        Args:
            tag (string): Data identifier
            text_string (string): Value of text
            step (int): Step of text
            walltime (int): Wall time of text
        Example:
            for index in range(1, 101):
                writer.add_text(tag="train/loss", text_string=str(index) + 'text', step=index)
        rV   rW   NrX   )rY   Útext_stringr#   r$   )rZ   r[   r\   rI   r]   r   )r   rY   rj   r#   r$   r   r   r   Úadd_text  s   þÿzLogWriter.add_textéÿÿÿÿç      ð?c           	      C   sT   d|v rt dƒ‚|du rtt ¡ d ƒn|}t||||d}| j|||||d dS )a—  Add an image to vdl record file.

        Args:
            tag (string): Data identifier
            imgs (np.ndarray): Image represented by a numpy.array
            step (int): Step of image
            rows (int): Number of rows, -1 means as close as possible to the square
            scale (float): Image zoom scale
            walltime (int): Wall time of image
            dataformats (string): Format of image

        Example:
            from PIL import Image
            import numpy as np

            I = Image.open("./test.png")
            I_array = np.array([I, I, I])
            writer.add_image_matrix(tag="lll", imgs=I_array, step=0)
        rV   rW   NrX   )Úimgsrf   ÚscaleÚrows)rY   rg   r#   r$   rf   )rZ   r[   r\   r   rh   )	r   rY   rn   r#   rp   ro   r$   rf   rg   r   r   r   Úadd_image_matrix"  s   ÿ
ûzLogWriter.add_image_matrixc	           
   
   C   sð   d|v rt dƒ‚|du r|r|}t d¡ |du r"|r"|}t d¡ |du r/|r/|}t d¡ t|tjƒr9| ¡ }t|tjƒrC| ¡ }t|d tƒrW|sWdd	„ tt	|ƒƒD ƒ}d}	|du ret
t ¡ d
 ƒn|}|  ¡  t|||||	|d¡ dS )a©	  Add embeddings to vdl record file.

        Args:
            tag (string): Data identifier
            mat (numpy.array or list): A matrix which each row is
                feature of labels.
            metadata (numpy.array or list): A 1D or 2D matrix of labels
            metadata_header (numpy.array or list): Meta data of labels.
            walltime (int): Wall time of embeddings.
            labels (numpy.array or list): Obsolete parameter, use `metadata` to
                replace it.
            hot_vectors (numpy.array or list): Obsolete parameter, use `mat` to
                replace it.
            labels_meta (numpy.array or list): Obsolete parameter, use
                `metadata_header` to replace it.
        Example 1:
            mat = [
            [1.3561076367500755, 1.3116267195134017, 1.6785401875616097],
            [1.1039614644440658, 1.8891609992484688, 1.32030488587171],
            [1.9924524852447711, 1.9358920727142739, 1.2124401279391606],
            [1.4129542689796446, 1.7372166387197474, 1.7317806077076527],
            [1.3913371800587777, 1.4684674577930312, 1.5214136352476377]]

            metadata = ["label_1", "label_2", "label_3", "label_4", "label_5"]
            # or like this
            # metadata = [["label_1", "label_2", "label_3", "label_4", "label_5"]]

            writer.add_embeddings(tag='default',
                                  metadata=metadata,
                                  mat=mat,
                                  walltime=round(time.time() * 1000))

        Example 2:
            mat = [
            [1.3561076367500755, 1.3116267195134017, 1.6785401875616097],
            [1.1039614644440658, 1.8891609992484688, 1.32030488587171],
            [1.9924524852447711, 1.9358920727142739, 1.2124401279391606],
            [1.4129542689796446, 1.7372166387197474, 1.7317806077076527],
            [1.3913371800587777, 1.4684674577930312, 1.5214136352476377]]

            metadata = [["label_a_1", "label_a_2", "label_a_3", "label_a_4", "label_a_5"],
                      ["label_b_1", "label_b_2", "label_b_3", "label_b_4", "label_b_5"]]

            metadata_header = ["label_a", "label_2"]

            writer.add_embeddings(tag='default',
                                  metadata=metadata,
                                  metadata_header=metadata_header,
                                  mat=mat,
                                  walltime=round(time.time() * 1000))
        rV   rW   NzeParameter `hot_vectors` in function `add_embeddings` will be deprecated in future, use `mat` instead.zeParameter `labels` in function `add_embeddings` will be deprecated in future, use `metadata` instead.zqParameter `labels_meta` in function `add_embeddings` will be deprecated in future, use `metadata_header` instead.r   c                 S   s   g | ]}d | ‘qS )zlabel_%dr   )Ú.0Úir   r   r   Ú
<listcomp>œ  s    z,LogWriter.add_embeddings.<locals>.<listcomp>rX   )rY   ÚlabelsÚlabels_metaÚhot_vectorsr#   r$   )rZ   r   Úwarningr`   ÚnpZndarrayÚtolistÚlistÚrangeÚlenr[   r\   rI   r]   r   )
r   rY   ZmatÚmetadataZmetadata_headerr$   ru   rw   rv   r#   r   r   r   Úadd_embeddingsI  sB   <
ÿÿúÿzLogWriter.add_embeddingsé@  c              	   C   s`   d|v rt dƒ‚|du rtt ¡ d ƒn|}t|tƒr t |¡}|  ¡  t	|||||d¡ dS )ap  Add an audio to vdl record file.

        Args:
            tag (string): Data identifier
            audio (np.ndarray or list): audio represented by a numpy.array
            step (int): Step of audio
            sample_rate (int): Sample rate of audio
            walltime (int): Wall time of audio

        Example:
            import wave

            CHUNK = 4096
            f = wave.open(audio_path, "rb")
            wavdata = []
            chunk = f.readframes(CHUNK)
            while chunk:
                data = np.frombuffer(chunk, dtype='uint8')
                wavdata.extend(data)
                chunk = f.readframes(CHUNK)
            audio_data = np.array(wavdata)

            writer.add_audio(tag="audio_test",
                             audio_array=audio_data,
                             step=0,
                             sample_rate=8000)
        rV   rW   NrX   )rY   Úaudio_arrayÚsample_rater#   r$   )
rZ   r[   r\   r`   r{   ry   ÚarrayrI   r]   r   )r   rY   r   r#   r‚   r$   r   r   r   Ú	add_audio©  s   !

ûÿzLogWriter.add_audioc              	   C   s^   d|v rt dƒ‚tj||d\}}|du rtt ¡ d ƒn|}|  ¡  t|||||d¡ dS )a¥  Add an histogram to vdl record file.

        Args:
            tag (string): Data identifier
            value (np.ndarray or list): value represented by a numpy.array or list
            step (int): Step of histogram
            walltime (int): Wall time of audio
            buckets (int): Number of buckets, default is 10

        Example:
            values = np.arange(0, 1000)
            with LogWriter(logdir="./log/histogram_test/train") as writer:
                for index in range(5):
                    writer.add_histogram(tag='default',
                                         values=values+index,
                                         step=index)
        rV   rW   )ZbinsNrX   )rY   ÚhistÚ	bin_edgesr#   r$   )rZ   ry   r   r[   r\   rI   r]   )r   rY   Úvaluesr#   r$   Zbucketsr…   r†   r   r   r   Úadd_histogram×  s   ûÿzLogWriter.add_histogramc                 C   sh   t |ƒtur
tdƒ‚t |ƒturtdƒ‚|du r tt ¡ d ƒn|}|  ¡  tt	| j
ƒ|||d¡ dS )aÂ  Add an histogram to vdl record file.

        Args:
            hparams_dict (dictionary): Each key-value pair in the dictionary is the
              name of the hyper parameter and it's corresponding value. The type of the value
              can be one of `string`, `float` or `int`.
            metrics_list (list): Name of all metrics.
            walltime (int): Wall time of hparams.

        Examples::
            from visualdl import LogWriter

            # Remember use add_scalar to log your metrics data!
            with LogWriter('./log/hparams_test/train/run1') as writer:
                writer.add_hparams({'lr': 0.1, 'bsize': 1, 'opt': 'sgd'}, ['hparam/accuracy', 'hparam/loss'])
                for i in range(10):
                    writer.add_scalar('hparam/accuracy', i, i)
                    writer.add_scalar('hparam/loss', 2*i, i)

            with LogWriter('./log/hparams_test/train/run2') as writer:
                writer.add_hparams({'lr': 0.2, 'bsize': 2, 'opt': 'relu'}, ['hparam/accuracy', 'hparam/loss'])
                for i in range(10):
                    writer.add_scalar('hparam/accuracy', 1.0/(i+1), i)
                    writer.add_scalar('hparam/loss', 5*i, i)
        z!hparam_dict should be dictionary!zmetric_list should be list!NrX   )ÚnameZhparam_dictZmetric_listr$   )Útypera   Ú	TypeErrorr{   r[   r\   rI   r]   r   r   rO   )r   Zhparams_dictZmetrics_listr$   r   r   r   Úadd_hparamsõ  s   üÿzLogWriter.add_hparamsc                 C   óP   d|v rt dƒ‚|du rtt ¡ d ƒn|}|  ¡  t|||||||d¡ dS )aü  Add an precision-recall curve to vdl record file.

        Args:
            tag (string): Data identifier
            labels (np.ndarray or list): Binary labels for each element.
            predictions (np.ndarray or list): The probability that an element
                be classified as true.
            step (int): Step of pr curve.
            weights (float): Multiple of data to display on the curve.
            num_thresholds (int): Number of thresholds used to draw the curve.
            walltime (int): Wall time of pr curve.

        Example:
            with LogWriter(logdir="./log/pr_curve_test/train") as writer:
                for index in range(3):
                    labels = np.random.randint(2, size=100)
                    predictions = np.random.rand(100)
                    writer.add_pr_curve(tag='default',
                                        labels=labels,
                                        predictions=predictions,
                                        step=index)
        rV   rW   NrX   ©rY   ru   Úpredictionsr#   r$   Únum_thresholdsÚweights)rZ   r[   r\   rI   r]   r   ©r   rY   ru   r   r#   r   r‘   r$   r   r   r   Úadd_pr_curve  s   ùÿzLogWriter.add_pr_curvec                 C   r   )aõ  Add an ROC curve to vdl record file.
        Args:
            tag (string): Data identifier
            labels (numpy.ndarray or list): Binary labels for each element.
            predictions (numpy.ndarray or list): The probability that an element
                be classified as true.
            step (int): Step of pr curve.
            weights (float): Multiple of data to display on the curve.
            num_thresholds (int): Number of thresholds used to draw the curve.
            walltime (int): Wall time of pr curve.
        Example:
            with LogWriter(logdir="./log/roc_curve_test/train") as writer:
                for index in range(3):
                    labels = np.random.randint(2, size=100)
                    predictions = np.random.rand(100)
                    writer.add_roc_curve(tag='default',
                                        labels=labels,
                                        predictions=predictions,
                                        step=index)
        rV   rW   NrX   rŽ   )rZ   r[   r\   rI   r]   r	   r’   r   r   r   Úadd_roc_curveG  s   ùÿzLogWriter.add_roc_curveFc              
   C   sz   zt |||ƒ}W n ty } z	td |¡ƒ |‚d}~ww t | jdt ¡ | jf ¡}t 	|d¡}| 
|¡ | ¡  dS )a/  
        Add a model graph to vdl graph file.
        Args:
            model (paddle.nn.Layer): Model to draw.
            input_spec (list[paddle.static.InputSpec|Tensor]): Describes the input                 of the saved model's forward arguments.
            verbose (bool): Whether to print some graph statistic information in console.
        Note:
            Paddlepaddle is required to use add_graph interface.
        Example:
            import paddle
            import paddle.nn as nn
            import paddle.nn.functional as F
            from visualdl import LogWriter
            class MyNet(nn.Layer):
                def __init__(self):
                    super(MyNet, self).__init__()
                    self.conv1 = nn.Conv2D(in_channels=1, out_channels=20, kernel_size=5, stride=1, padding=2)
                    self.max_pool1 = nn.MaxPool2D(kernel_size=2, stride=2)
                    self.conv2 = nn.Conv2D(in_channels=20, out_channels=20, kernel_size=5, stride=1, padding=2)
                    self.max_pool2 = nn.MaxPool2D(kernel_size=2, stride=2)
                    self.fc = nn.Linear(in_features=980, out_features=10)
                def forward(self, inputs):
                    x = self.conv1(inputs)
                    x = F.relu(x)
                    x = self.max_pool1(x)
                    x = self.conv2(x)
                    x = F.relu(x)
                    x = self.max_pool2(x)
                    x = paddle.reshape(x, [x.shape[0], -1])
                    x = self.fc(x)
                    return x
            net = MyNet()
            with LogWriter(logdir="./log/graph_test/") as writer:
                writer.add_graph(
                    model=net,
                    input_spec=[paddle.static.InputSpec([-1, 1, 28, 28], 'float32')],
                    verbose=True)
        z%Failed to save model graph, error: {}Nzvdlgraph.%010d.log%sÚw)r   Ú	ExceptionÚprintÚformatr   r@   r   r\   rC   ZBFileÚwriter-   )r   ÚmodelZ
input_specÚverboseÚresultÚeZgraph_file_nameÚwriterr   r   r   r)   p  s   (€þþ
zLogWriter.add_graphc                 C   s*   | j i u rdS | j  ¡ D ]}| ¡  qdS )z)Flush all data in cache to disk.
        N)rH   r‡   r,   ©r   rž   r   r   r   r,   ¤  s
   

ÿzLogWriter.flushc                 C   s>   | j i u rdS | j  ¡ D ]
}| ¡  | ¡  qd| _i | _ dS )z4Close all writers after flush data to disk.
        N)rH   r‡   r,   r-   rG   rŸ   r   r   r   r-   ¬  s   


zLogWriter.closec                 C   s   | S r   r   r   r   r   r   Ú	__enter__·  r&   zLogWriter.__enter__c                 C   s   |   ¡  d S r   )r-   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__º  s   zLogWriter.__exit__)Nr5   r6   r7   r5   Tr5   r5   )rT   r5   r   Nr   )Nrd   r/   )rl   rm   Nrd   )NNNNNNN)r€   N)Nr6   )r6   NN)F)r0   r1   r2   r3   r   Úpropertyr   rI   rO   rK   r_   rc   rh   ri   rk   rq   r   r„   rˆ   rŒ   r“   r”   r)   r,   r-   r    r¤   r   r   r   r   r4   I   sr    
ø,


ü





ù)
ød
û
.
,
ù0
ù
)4r4   )!r>   r\   Únumpyry   Z!visualdl.component.base_componentr   r   r   r   r   r   r   r	   r
   r   r   Zvisualdl.component.graphr   Zvisualdl.ior   Zvisualdl.server.logr   Zvisualdl.utils.figure_utilr   Zvisualdl.utils.img_utilr   Zvisualdl.utils.md5_utilr   Zvisualdl.writer.record_writerr   Úobjectr   r4   r   r   r   r   Ú<module>   s.   !