U
    9%eZ                  4   @   sZ  d dl Z d dlZd dlZd dlmZ d dlZd dlZd dlm	Z	 d dl
mZmZ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mZ dddddddddddddddddddgZeeZ ej!d ej"d ej#d!ej$d"ej%d"ej&d#ej'd#ej(d$ej)d%ej*d%ej+d&ej,d&ej-d'ej.d'ej/d'ej0d(ej1d)ej2d)ej3d*ej4d*ej5d+ej6d,ej7d,ej)d-ej8d-ej9d-iZ:d.d/ Z;dQd2d3Z<dRd4dZ=dSd6dZ>d7d Z?d8d Z@dTd9dZAdUd:dZBdVd<dZCdWd>dZDdXd?dZEdYd@dZFdZdBdZGdCd ZHd[dEdZIdFd ZJdGd ZKd\dIdZLd]dJdZMd^dKdZNdLdM ZOdNdO ZPd_dPdZQdS )`    N)Optional)
struct_pb2)HistogramProtoSummarySummaryMetadata)TensorProto)TensorShapeProto)
layout_pb2)PrCurvePluginData)TextPluginData   )make_np)_prepare_videoconvert_to_HWChparamsscalarhistogram_raw	histogrammake_histogramimageimage_boxes
draw_boxes
make_imagevideo
make_videoaudiocustom_scalarstexttensor_protopr_curve_rawpr_curvecompute_curvemesh)ZDT_HALFZhalf_val)Z	DT_BFLOAT	float_val)DT_FLOATr#   )	DT_DOUBLE
double_val)ZDT_INT8int_val)DT_UINT8r'   )ZDT_INT16r'   )ZDT_INT32r'   )ZDT_INT64Z	int64_val)ZDT_COMPLEX32scomplex_val)ZDT_COMPLEX64r)   )ZDT_BOOLZbool_val)ZDT_COMPLEX128dcomplex_val)r(   Z
uint32_valc                 C   s,   t | tjs|  n| }|jtjkr(dS dS )Nr      )
isinstancenpZndarraynumpydtypeuint8)tensorZ	converted r2   ^/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/utils/tensorboard/summary.py_calc_scale_factorR   s    r4   black   c	                 C   s   ddl m}	m}
 |
 }|	| }||||f\}}}}|j||f||f||f||f||fg||d |r|}||\}}td| }|j	||| d|  f|| |fg|d |j
|| || | f|||d | S )Nr   )	ImageDraw	ImageFont)widthfillg?r6   )r:   )r:   font)PILr7   r8   Zload_defaultZDrawlinegetsizer-   ceilZ	rectangler   )r   ZxminZyminZxmaxZymaxdisplay_strcolorZ
color_textZ	thicknessr7   r8   r;   ZdrawleftrighttopbottomZtext_bottom
text_widthZtext_heightmarginr2   r2   r3   _draw_single_boxW   s4    
 
rH   c                    s  ddl }ddlm}m}m}m mm} ddlm	}m
}	m}
m}m} ddlm}m}m} ttsttd tdt|tstd td|pi }t|tstd| D ]H\}kst|trtfd	d
|D std d dqg }| } D ]\}|dkr&qt|ttfr||j _|krhtjdd | D d}nd}| ||!d|d qt|t"r||j _#|krtjdd | D d}nd}| ||!d|d qt|t$rJ||j _%|kr(tjdd | D d}nd}| ||!d|d qt||j&rt'|d }||j _| ||!dd qt(dq|||	d}t)t)j*|
|+ dd}t,t,j!||dgd} fdd|- D }|||d}|||	d }t)t)j*|
|+ dd}t,t,j!||dgd}||!d!d"}|||	d#}t)t)j*|
|+ dd}t,t,j!||dgd}|||fS )$a2  Outputs three `Summary` protocol buffers needed by hparams plugin.
    `Experiment` keeps the metadata of an experiment, such as the name of the
      hyperparameters and the name of the metrics.
    `SessionStartInfo` keeps key-value pairs of the hyperparameters
    `SessionEndInfo` describes status of the experiment e.g. STATUS_SUCCESS

    Args:
      hparam_dict: A dictionary that contains names of the hyperparameters
        and their values.
      metric_dict: A dictionary that contains names of the metrics
        and their values.
      hparam_domain_discrete: (Optional[Dict[str, List[Any]]]) A dictionary that
        contains names of the hyperparameters and all discrete values they can hold

    Returns:
      The `Summary` protobufs for Experiment, SessionStartInfo and
        SessionEndInfo
    r   N)DataType
Experiment
HParamInfo
MetricInfo
MetricNameStatus)EXPERIMENT_TAGPLUGIN_DATA_VERSIONPLUGIN_NAMESESSION_END_INFO_TAGSESSION_START_INFO_TAG)HParamsPluginDataSessionEndInfoSessionStartInfoz>parameter: hparam_dict should be a dictionary, nothing logged.z>parameter: metric_dict should be a dictionary, nothing logged.zIparameter: hparam_domain_discrete should be a dictionary, nothing logged.c                 3   s    | ]}t |t  V  qd S )N)r,   type.0d)hparam_dictkr2   r3   	<genexpr>   s     zhparams.<locals>.<genexpr>z"parameter: hparam_domain_discrete[z/] should be a list of same type as hparam_dict[z].c                 S   s   g | ]}t j|d qS ))number_valuer   ValuerX   r2   r2   r3   
<listcomp>   s   zhparams.<locals>.<listcomp>)valuesZDATA_TYPE_FLOAT64)namerW   domain_discretec                 S   s   g | ]}t j|d qS ))string_valuer_   rX   r2   r2   r3   ra      s   ZDATA_TYPE_STRINGc                 S   s   g | ]}t j|d qS ))
bool_valuer_   rX   r2   r2   r3   ra     s   ZDATA_TYPE_BOOL)rc   rW   z=value should be one of int, float, str, bool, or torch.Tensor)Zsession_start_infoversionplugin_namecontentplugin_data)tagmetadatavaluec                    s   g | ]} |d dqS )rm   )rc   r2   )rY   r\   )rL   rM   r2   r3   ra   %  s     )Zhparam_infosZmetric_infos)Z
experimentrg   ZSTATUS_SUCCESS)status)Zsession_end_inforg   ).torchZ#tensorboard.plugins.hparams.api_pb2rI   rJ   rK   rL   rM   rN   Z$tensorboard.plugins.hparams.metadatarO   rP   rQ   rR   rS   Z+tensorboard.plugins.hparams.plugin_data_pb2rT   rU   rV   r,   dictloggerwarning	TypeErroritemslistallintfloatr   r^   r   Z	ListValueappendr`   strre   boolrf   ZTensorr   
ValueErrorr   
PluginDataSerializeToStringr   keys)r[   Zmetric_dictZhparam_domain_discreters   rI   rJ   rK   rN   rO   rP   rQ   rR   rS   rT   rU   rV   vZhpsZssird   rj   smdZmtsexpZseir2   )rL   rM   r[   r\   r3   r      s     








   Fc           	      C   s   t | }|jdks0td|j d|j dt|}|rt|gdd}|r\t|gdd}tjd	d
}t|d}t	t	j
| ||dgdS t	t	j
| |dgdS dS )a  Outputs a `Summary` protocol buffer containing a single scalar value.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Args:
      name: A name for the generated node. Will also serve as the series name in
        TensorBoard.
      tensor: A real numeric Tensor containing a single value.
      collections: Optional list of graph collections keys. The new summary op is
        added to these collections. Defaults to `[GraphKeys.SUMMARIES]`.
      new_style: Whether to use new style (tensor field) or old style (simple_value
        field). New style could lead to faster data loading.
    Returns:
      A scalar `Tensor` of type `string`. Which contains a `Summary` protobuf.
    Raises:
      ValueError: If tensor has the wrong shape or type.
    r   zBTensor should contain one element (0 dimensions). Was given size: z and z dimensions.r$   )r#   r/   r%   )r&   r/   Zscalarsri   rk   rm   r1   rn   ro   )rm   Zsimple_valueN)r   squeezendimAssertionErrorsizer|   r   r   r   r   r`   )	rc   r1   collections	new_styleZdouble_precisionr   r   rl   r   r2   r2   r3   r   =  s*    

c                    s      j dkrtd jtkrt j d }|dksB|dkrVt   }n6   dkrn 	 g}n   dkrg }n   }dt j d dt
 fd	d
t  D d||i}tf |}nt|  d j tjdd}t|d}ttj| ||dgdS )a  Outputs a `Summary` protocol buffer containing the full tensor.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Args:
      name: A name for the generated node. Will also serve as the series name in
        TensorBoard.
      tensor: Tensor to be converted to protobuf
    Returns:
      A tensor protobuf in a `Summary` protobuf.
    Raises:
      ValueError: If tensor is too big to be converted to protobuf, or
                     tensor data type is not supported
    l        zAtensor is bigger than protocol buffer's hard limit of 2GB in sizer   r)   r*   r   r/   tensor_shapec                    s   g | ]}t j j| d qS )r   )r   Dimshape)rY   ir1   r2   r3   ra     s   z tensor_proto.<locals>.<listcomp>dimz has unsupported tensor dtype r1   r   rk   rm   rn   r1   ro   )Znumelitemsizer   r/   _TENSOR_TYPE_MAPrs   Zview_as_realflattentolistitemr   ranger   r   r   r   r   r`   )rm   r1   Zproto_val_fieldZproto_val_contentsZtensor_proto_argsr   rl   r   r2   r   r3   r   g  s:    
  

 
c           	   	   C   s,   t |||||||d}ttj| |dgdS )a  Outputs a `Summary` protocol buffer with a histogram.
    The generated
    [`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
    has one summary value containing a histogram for `values`.
    Args:
      name: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      min: A float or int min value
      max: A float or int max value
      num: Int number of values
      sum: Float or int sum of all values
      sum_squares: Float or int sum of squares for all values
      bucket_limits: A numeric `Tensor` with upper value per bucket
      bucket_counts: A numeric `Tensor` with number of values per bucket
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    minmaxnumsumsum_squaresZbucket_limitZbucketrm   Zhistoro   )r   r   r`   )	rc   r   r   r   r   r   Zbucket_limitsZbucket_countshistr2   r2   r3   r     s    	c                 C   s0   t |}t|t||}ttj| |dgdS )ad  Outputs a `Summary` protocol buffer with a histogram.
    The generated
    [`Summary`](https://www.tensorflow.org/code/tensorflow/core/framework/summary.proto)
    has one summary value containing a histogram for `values`.
    This op reports an `InvalidArgument` error if any value is not finite.
    Args:
      name: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      values: A real numeric `Tensor`. Any shape. Values to use to
        build the histogram.
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    r   ro   )r   r   astyper|   r   r`   )rc   rb   binsmax_binsr   r2   r2   r3   r     s    c              	   C   s  | j dkrtd| d} tj| |d\}}t|}|dk	r||kr|| }|| }|dkrztj|d|| ggddd}|d|jdd}t|j d	 f|j	}|dd| |dd< |d |d< |}t
t|d}	tj|	d|	d d	 gd
d\}
}t|
}
t|d	 }~	|
dkr,||
d	 | ntdg|d| g}||
|d	  }|j dksl|j dkrttd| | }t|  |  t| |  || | dS )zDConvert values into a histogram proto using logic from histogram.cc.r   zThe input has no element.)r   NZconstant)Z	pad_widthmodeZconstant_values)Zaxisr   rC   )Zsidez1The histogram is empty, please file a bug report.r   )r   r   reshaper-   r   lenpadr   emptyr/   cumsumZgreaterZsearchsortedr{   Zconcatenatedotr   r   r   r   )rb   r   r   countsZlimitsZnum_binsZsubsamplingZsubsampling_remainderZ
new_limitsZ
cum_countsstartendZsum_sqr2   r2   r3   r     sP    

 0
NCHWc                 C   s`   t |}t||}t|}|tj}|| ddtj}t||d}t	t	j
| |dgdS )aJ  Outputs a `Summary` protocol buffer with images.
    The summary has up to `max_images` summary values containing images. The
    images are built from `tensor` which must be 3-D with shape `[height, width,
    channels]` and where `channels` can be:
    *  1: `tensor` is interpreted as Grayscale.
    *  3: `tensor` is interpreted as RGB.
    *  4: `tensor` is interpreted as RGBA.
    The `name` in the outputted Summary.Value protobufs is generated based on the
    name, with a suffix depending on the max_outputs setting:
    *  If `max_outputs` is 1, the summary value tag is '*name*/image'.
    *  If `max_outputs` is greater than 1, the summary value tags are
       generated sequentially as '*name*/image/0', '*name*/image/1', etc.
    Args:
      tag: A name for the generated node. Will also serve as a series name in
        TensorBoard.
      tensor: A 3-D `uint8` or `float32` `Tensor` of shape `[height, width,
        channels]` where `channels` is 1, 3, or 4.
        'tensor' can either have values in [0, 1] (float32) or [0, 255] (uint8).
        The image() function will scale the image values to [0, 255] by applying
        a scale factor of either 1 (uint8) or 255 (float32). Out-of-range values
        will be clipped.
    Returns:
      A scalar `Tensor` of type `string`. The serialized `Summary` protocol
      buffer.
    r   r+   )rescalerm   r   ro   )r   r   r4   r   r-   float32clipr0   r   r   r`   )rm   r1   r   dataformatsscale_factorr   r2   r2   r3   r     s    
CHWc                 C   sd   t |}t||}t |}|tjt| }t|ddtj|||d}t	t	j
| |dgdS )z0Outputs a `Summary` protocol buffer with images.r   r+   )r   roislabelsr   ro   )r   r   r   r-   r   r4   r   r   r0   r   r`   )rm   Ztensor_imageZtensor_boxesr   r   r   r   r2   r2   r3   r   *  s    
c              
   C   sf   |j d }t|}|D ]J}t| ||df ||df ||df ||df |d krRd n|| dd} q| S )Nr   r   r6      Red)r@   rA   )r   r   rH   )Z
disp_imageZboxesr   Z	num_boxesZlist_gtr   r2   r2   r3   r   ;  s    




	c                 C   s   ddl m} | j\}}}t|| }t|| }	|| }
|dk	rPt|
||d}
z|jj}W n tk
rv   |j	}Y nX |

|	|f|}
ddl}| }|
j|dd | }|  tj||||dS )z<Convert a numpy representation of an image to Image protobufr   )ImageN)r   ZPNG)formatheightr9   Z
colorspaceZencoded_image_string)r<   r   r   r{   Z	fromarrayr   Z
ResamplingZLANCZOSAttributeError	ANTIALIASresizeioBytesIOsavegetvaluecloser   )r1   r   r   r   r   r   r9   ZchannelZscaled_heightZscaled_widthr   r   r   outputZimage_stringr2   r2   r3   r   L  s.    
   c                 C   s\   t |}t|}t|}|tj}|| ddtj}t||}t	t	j
| |dgdS )Nr   r+   r   ro   )r   r   r4   r   r-   r   r   r0   r   r   r`   )rm   r1   fpsr   r   r2   r2   r3   r   i  s    
c                 C   sX  zdd l }W n tk
r*   td Y d S X zddl m} W n  tk
r\   tdd Y d S X dd l}| j\}}}}|jt| |d}	|jddd	j	}
z|	j
|
dd d
 W nL tk
r   z|	j
|
ddd W n" tk
r   |	j
|
dd Y nX Y nX t|
d}| }W 5 Q R X zt|
 W n  tk
rD   td Y nX tj||||dS )Nr   zadd_video needs package moviepy)editorz6moviepy is installed, but can't import moviepy.editor.z2Some packages could be missing [imageio, requests])r   z.gifF)suffixdelete)verboseru   )r   progress_bar)r   rbz5The temporary file used by moviepy cannot be deleted.r   )moviepyImportErrorprintr   tempfiler   ZImageSequenceClipry   NamedTemporaryFilerc   Z	write_gifrw   openreadosremoveOSErrorru   rv   r   r   )r1   r   r   Zmpyr   thwcr   filenamefZtensor_stringr2   r2   r3   r   t  sH       D  c           
   	   C   s   t |}| }t| dkr4td |dd}|jdksFtd|t	tj
j d}dd l}dd l}| }||d0}|d |d || ||j W 5 Q R X | }|  tj|d|jd |d	d
}	ttj| |	dgdS )Nr   z4warning: audio amplitude out of range, auto clipped.r   z%input tensor should be 1 dimensional.z<i2r   wbr6   z	audio/wav)sample_rateZnum_channelsZlength_framesZencoded_audio_stringcontent_type)rm   r   ro   )r   r   absr   r   r   r   r   r-   Ziinfoint16r   r   waver   r   ZsetnchannelsZsetsampwidthZsetframerateZwriteframesdatar   r   r   ZAudior   r`   )
rm   r1   r   arrayr   r   ZfioZ
wave_writeZaudio_stringr   r2   r2   r3   r     s2    


c                 C   s  g }|   D ]\}}g }|  D ]\}}|d }|d dkrt|dksLttjtjj|d |d |d dgd}tj||d}	ntj|d	}
tj||
d
}	||	 q |tj	||d qtj
|d} tjdd}t|d}td|  gt d}ttjd||dgdS )Nr   r   ZMarginr   r6   )rp   lowerupper)Zseries)titlerG   rq   )r   	multiline)r   chart)categoryr   r   rk   	DT_STRINGr/   Z
string_valr   Zcustom_scalars__config__r   ro   )rx   r   r   r	   ZMarginChartContentZSeriesZChartZMultilineChartContentr}   ZCategoryZLayoutr   r   r   r   r   r   r`   )Zlayout
categoriesr\   r   ZchartsZ
chart_nameZchart_meatadatatagsZmgccr   Zmlccrl   r   r1   r2   r2   r3   r     s@      
c                 C   sf   t jdtdd d}t |d}td|jddgttjd	d
gdd}ttj	| d ||dgdS )Nr   r   )rg   rh   rk   r   utf_8)encodingr   r   r   r   z/text_summaryr   ro   )
r   r   r   r   r   encoder   r   r   r`   )rm   r   rl   r   r1   r2   r2   r3   r     s     
   c	                 C   s   |dkrd}t ||||||f}	td|d }
tjd|
d}t|d}td|	d t	t	j
|	jd d	t	j
|	jd
 d	gdd}ttj| ||dgdS )Nr   r   rg   num_thresholds	pr_curvesrh   rk   r$   r   r   r   r   r/   r#   r   r   ro   )r-   stackr
   r   r   r   r   r   r   r   r   r   r   r`   )rm   tpfptnfn	precisionrecallr   weightsr   pr_curve_plugin_datarl   r   r1   r2   r2   r3   r     s,     
 

c           
      C   s   t |d}t||||d}td|d }tjd|d}t|d}td|d	 t	t	j
|jd d
t	j
|jd d
gdd}	ttj| ||	dgdS )Nr   )r   r  r   r   r   rh   rk   r$   r   r   r   r   r   r   ro   )r   r!   r
   r   r   r   r   r   r   r   r   r   r   r`   )
rm   r   predictionsr   r  r   r  rl   r   r1   r2   r2   r3   r      s4    
    
 

c                 C   s
  d}|d krd}t t ||d  }| t j}d|d f}t j||||| d\}}	t j|||d| | d\}
}	t |d d d d d d }t |
d d d d d d }|d | }|d | }|t |||  }|t |||  }t ||||||fS )NgHz>g      ?r   r   )r   r   r  r   )	r-   int32floorr   float64r   r   maximumr   )r   r	  r   r  Z_MINIMUM_COUNTZbucket_indicesZfloat_labelsZhistogram_rangeZ
tp_buckets_Z
fp_bucketsr  r  r  r  r  r  r2   r2   r3   r!   &  s2    


c              	   C   s   ddl }ddlm} ||}|j| ||||j||d}	td|d t	t	j
|jd dt	j
|jd dt	j
|jd	 dgd
d}tj|| |||	d}
|
S )a  Creates a tensor summary with summary metadata.

    Args:
      name: Uniquely identifiable name of the summary op. Could be replaced by
        combination of name and type to make it unique even outside of this
        summary.
      display_name: Will be used as the display name in TensorBoard.
        Defaults to `name`.
      description: A longform readable description of the summary data. Markdown
        is supported.
      tensor: Tensor to display in summary.
      content_type: Type of content inside the Tensor.
      components: Bitmask representing present parts (vertices, colors, etc.) that
        belong to the summary.
      json_config: A string, JSON-serialized dictionary of ThreeJS classes
        configuration.

    Returns:
      Tensor summary with metadata.
    r   Nrn   )json_configr$   r   r   r   r6   r   r   r   )rs   tensorboard.plugins.meshrn   Z	as_tensorZcreate_summary_metadatar   r   r   r   r   r   r   r`   Zget_instance_name)rc   display_namedescriptionr1   r   
componentsr  rs   rn   Ztensor_metadataZtensor_summaryr2   r2   r3   _get_tensor_summaryG  s8    


r  c                 C   s   d}| dk	rt j| dd}|S )z6Parses and returns JSON string from python dictionary.z{}NT)	sort_keys)jsondumps)config_dictr  r2   r2   r3   _get_json_config  s    r  c                 C   s   ddl m} ddlm} t|}	g }
||jf||jf||jfg}dd |D }|dd |D }|D ]"\}}|
	t
| ||||||	 qft|
dS )a(  Outputs a merged `Summary` protocol buffer with a mesh/point cloud.

    Args:
      tag: A name for this summary operation.
      vertices: Tensor of shape `[dim_1, ..., dim_n, 3]` representing the 3D
        coordinates of vertices.
      faces: Tensor of shape `[dim_1, ..., dim_n, 3]` containing indices of
        vertices within each triangle.
      colors: Tensor of shape `[dim_1, ..., dim_n, 3]` containing colors for each
        vertex.
      display_name: If set, will be used as the display name in TensorBoard.
        Defaults to `name`.
      description: A longform readable description of the summary data. Markdown
        is supported.
      config_dict: Dictionary with ThreeJS classes names and configuration.

    Returns:
      Merged summary for mesh/point cloud representation.
    r   r  )MeshPluginDatac                 S   s   g | ]}|d  dk	r|qS )r   Nr2   )rY   r1   r2   r2   r3   ra     s      zmesh.<locals>.<listcomp>c                 S   s   g | ]\}}|qS r2   r2   )rY   r1   r   r2   r2   r3   ra     s     ro   )r  rn   Z(tensorboard.plugins.mesh.plugin_data_pb2r  r  ZVERTEXZFACEZCOLORZget_components_bitmaskr}   r  r   )rm   ZverticescolorsZfacesr  r  r  rn   r  r  Z	summariesZtensorsr  r1   r   r2   r2   r3   r"     s2    )r5   r5   r6   )NNN)NFF)N)N)r   r   )r   r   N)N)r   NN)r   )r   )r   N)r   N)NN)NN)Rr  loggingr   typingr   rs   r.   r-   Zgoogle.protobufr   Z$tensorboard.compat.proto.summary_pb2r   r   r   Z#tensorboard.compat.proto.tensor_pb2r   Z)tensorboard.compat.proto.tensor_shape_pb2r   Z!tensorboard.plugins.custom_scalarr	   Z,tensorboard.plugins.pr_curve.plugin_data_pb2r
   Z(tensorboard.plugins.text.plugin_data_pb2r   Z_convert_npr   _utilsr   r   __all__	getLogger__name__ru   ZhalfZfloat16Zbfloat16r   r|   r  doubleZint8r0   Zqint8r   shortr{   r
  Zqint32Zint64Z	complex32ZchalfZ	complex64Zcfloatr   Z
complex128ZcdoubleZquint8Zquint4x2r   r4   rH   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r  r  r"   r2   r2   r2   r3   <module>   s   
                             
*
 =
*3 

7
%     



+
%   


!;
   