o
    ej                     @   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   FD:\Projects\ConvertPro\env\Lib\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	LogWriterzLog 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 )zAdd 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   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.   !