
    i                     j   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 g dZ ej<                  e      Z de!de"fdZ#de"de!fdZ$dejJ                  de&e"   fdZ'dejJ                  de&e!   fdZ(dejJ                  de&e   fdZ)i ejT                  dde'fejV                  dde'fejX                  dde'fejZ                  dde)fejB                  dde)fej\                  dde)fej^                  dde)fej`                  dde)fejb                  d de)fejd                  d de)fejf                  d!de)fejh                  d!de)fejD                  d"de)fejj                  d"de)fejl                  d"de)fejn                  d#d$e)fejp                  d%d&e(fejr                  d%d&e(fejt                  d'd&e(fejv                  d'd&e(fejx                  d(d)e)fejz                  d*d+e(fej|                  d*d+e(fejb                  d d,e)fej~                  d d,e)fej                  d d,e)fi	ZAde"fd-ZB	 	 	 dDd.ZCdEd/ZDdFd0ZEd1 ZFd2 ZGdGd3ZHdGd4ZIdHd5ZJ	 dId6ZKdGd7ZLdJd8ZMdKd9ZNd: ZOdLd;ZPd< ZQd= ZR	 dMd>ZSdMd?ZTdNd@ZUdA ZVdB ZW	 dNdCZXy)O    N)Any)
struct_pb2)HistogramProtoSummarySummaryMetadata)TensorProto)TensorShapeProto)
layout_pb2)PrCurvePluginData)TextPluginData   )make_np)_prepare_videoconvert_to_HWC)half_to_intint_to_half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freturnc                 `    t        j                  d|       }t        j                  d|      d   S )aA  Casts a half-precision float value into an integer.

    Converts a half precision floating point value, such as `torch.half` or
    `torch.bfloat16`, into an integer value which can be written into the
    half_val field of a TensorProto for storage.

    To undo the effects of this conversion, use int_to_half().

    r&   ir   structpackunpack)r&   bufs     Y/var/www/html/engine/venv/lib/python3.12/site-packages/torch/utils/tensorboard/summary.pyr   r   6   s*     ++c1
C==c"1%%    r)   c                 `    t        j                  d|       }t        j                  d|      d   S )zCasts an integer value to a half-precision float.

    Converts an integer value obtained from half_to_int back into a floating
    point value.

    r)   r&   r   r*   )r)   r.   s     r/   r   r   C   s*     ++c1
C==c"1%%r0   tc                 v    | j                         j                         D cg c]  }t        |       c}S c c}w N)flattentolistr   )r2   xs     r/   _tensor_to_half_valr8   M   s)    $%IIK$6$6$89qKN999s   6c                 d    t        j                  |       j                         j                         S r4   )torchview_as_realr5   r6   r2   s    r/   _tensor_to_complex_valr=   P   s%    a ((*1133r0   c                 >    | j                         j                         S r4   )r5   r6   r<   s    r/   _tensor_to_listr?   S   s    99;r0   DT_HALFhalf_valDT_BFLOAT16DT_FLOAT	float_val	DT_DOUBLE
double_valDT_INT8int_valDT_UINT8DT_INT16DT_INT32DT_INT64	int64_valDT_COMPLEX32scomplex_valDT_COMPLEX64DT_BOOLbool_valDT_COMPLEX128dcomplex_val
uint32_valc                     t        | t        j                        s| j                         n| }|j                  t        j
                  k(  rdS dS )Nr      )
isinstancenpndarraynumpydtypeuint8)tensor	converteds     r/   _calc_scale_factorr`   u   s6    &0&D&I288+144r0   c	                    ddl m}	m}
 |
j                         }|	j	                  |       }||||f\  }}}}|j                  ||f||f||f||f||fg||       |r|}|j                  |      \  }}}}||z
  ||z
  }}t        j                  d|z        }|j                  |||z
  d|z  z
  f||z   |fg|       |j                  ||z   ||z
  |z
  f|||       | S )Nr   )	ImageDraw	ImageFont)widthfillg?   )re   )re   font)PILrb   rc   load_defaultDrawlinegetbboxrY   ceil	rectangler    )r   xminyminxmaxymaxdisplay_strcolor
color_text	thicknessrb   rc   rg   drawleftrighttopbottomtext_bottom_left_top_right_bottom
text_widthtext_heightmargins                            r/   _draw_single_boxr   z   s/    )!!#D>>% D"&dD!9T5#vII
tVnufos|dC[Q  
 '+||K'@$tVW"(5.'D.K
+,{[01v:=>
"K0  	 	
 			F]K+5>?	 	 	
 Lr0   c           	      j	    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&              s t(        j+                  d       t-        d      t%        |t&              s t(        j+                  d       t-        d      |xs i }t%        |t&              st-        d      |j/                         D ]@  \  } vs&t%        |t0              rt3         fd	|D              r0t-        d
 d d       g } |       } j/                         D ],  \  }|
t%        |t4        t6        f      r||j8                     _        |v r<t=        j>                  |   D cg c]  }t=        j@                  |       c}      }nd}|jC                   ||jA                  d      |             t%        |tD              r||j8                     _#        |v r<t=        j>                  |   D cg c]  }t=        j@                  |       c}      }nd}|jC                   ||jA                  d      |             1t%        |tH              r||j8                     _%        |v r<t=        j>                  |   D cg c]  }t=        j@                  |       c}      }nd}|jC                   ||jA                  d      |             t%        ||jL                        rLtO        |      d   }||j8                     _        |jC                   ||jA                  d                   $tQ        d        |||      }tS        tS        jT                  ||jW                                     }tY        tY        j@                  ||      g      }|D cg c]  } | ||             }} |||      } |||      }tS        tS        jT                  ||jW                                     }tY        tY        j@                  |
|      g      } ||	jA                  d       !      } |||"      }tS        tS        jT                  ||jW                                     }tY        tY        j@                  ||      g      }|||fS c c}w c c}w c c}w c c}w )#a2  Output 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   N   K   | ]  }t        |t                         y wr4   )rX   type).0dhparam_dictks     r/   	<genexpr>zhparams.<locals>.<genexpr>   s!     Fqz!T+a.%9:Fs   "%z"parameter: hparam_domain_discrete[z/] should be a list of same type as hparam_dict[z].)number_value)valuesDATA_TYPE_FLOAT64)namer   domain_discrete)string_valueDATA_TYPE_STRING)
bool_valueDATA_TYPE_BOOL)r   r   z=value should be one of int, float, str, bool, or torch.Tensor)session_start_infoversionplugin_namecontentplugin_data)tagmetadatavaluer   )r   )hparam_infosmetric_infos)
experimentr   STATUS_SUCCESS)status)session_end_infor   )-r:   #tensorboard.plugins.hparams.api_pb2r   r   r   r   r   r   $tensorboard.plugins.hparams.metadatar   r   r   r   r   +tensorboard.plugins.hparams.plugin_data_pb2r   r   r   rX   dictloggerwarning	TypeErroritemslistallintfloatr   r   r   	ListValueValueappendstrr   boolr   Tensorr   
ValueErrorr   
PluginDataSerializeToStringr   )r   metric_dicthparam_domain_discreter:   r   r   r   r   r   r   r   r   r   r   r   r   r   r   vhpsssir   r   r   smdr   mtsexpseis   `                        `   r/   r   r      s   (     k4(WXL
 	
 k4(WXL
 	
 49r,d3W
 	
 ',,. 1[ a&FAFF4QC7fghfiikl  C

C!!# P
19a#u&*+CKKN'**?I?S?S "8!: #((a8@ #'JJ!(;<$3	 a*+CKKN'**","6"6 "8!: #((a8# #'JJ!(:;$3	 a()CKKN%**","6"6 "8!: #((A6# #'JJ!(89$3	 a&
1A*+CKKN'JJzqx~~>Q/RSTK
 	
]P
d  3@STG
#..#W-F-F-H
C +ACPQ
RC7B
C!::!,-
CC
C
#C
8C38KLG
#..#W-F-F-H
C >CHI
JC -= >
?C>QRG
#..#W-F-F-H
C +?#NO
PCS=]..L Ds   *R!
:R&
R+
+R0c                    t        |      j                         }|j                  dk7  r&t        d|j                   d|j                   d      t        |      }|rct        |gd      }|rt        |gd      }t        j                  d	
      }t        |      }t        t        j                  | ||      g      S t        t        j                  | |      g      S )a  Output 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   zNTensor should contain one element (0 dimensions).             Was given size: z and z dimensions.rC   )rD   r\   rE   )rF   r\   scalarsr   r   r   r^   r   r   )r   simple_value)r   squeezendimAssertionErrorsizer   r   r   r   r   r   )	r   r^   collections	new_styledouble_precisionr   r!   r   r   s	            r/   r   r   m  s    " V_$$&F{{a  #[[Mv{{m<I J 	J 6]F"fXZH&6(+NL &00YG+6 ' 	
 		
 gmm6JKLLr0   c                    |j                         |j                  z  dk\  rt        d      |j                  t        v ret        |j                     \  }}}t        di d|dt        |j                  D cg c]  }t        j                  |       c}      | ||      i}nt        |  d|j                         t        j                  d	      }t        |
      }t        t        j                  | ||      g      S c c}w )a  Outputs a `Summary` protocol buffer containing the full tensor.
    The generated Summary has a Tensor.proto containing the input Tensor.
    Args:
      tag: 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\   tensor_shaper   dimz has unsupported tensor dtype r^   r   r   r   r   r^   r    )numelitemsizer   r\   _TENSOR_TYPE_MAPr   r	   shapeDimr   r   r   r   )	r   r^   r\   
field_nameconversion_fnr7   r!   r   r   s	            r/   r!   r!     s     ||~'G4O
 	
 ||''+;FLL+I(z=" 	
 0?E||L!)--15L! M&1	
 C5 >v||nMNN ",,BK
k
2C'--C#lSTUU Ms   2Dc           	      j    t        |||||||      }t        t        j                  | |      g      S )a  Output 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_squaresbucket_limitbucketr   histor   )r   r   r   )	r   r   r   r   r   r   bucket_limitsbucket_countshists	            r/   r   r     s>    * "D '--D=>??r0   c                     t        |      }t        |j                  t              ||      }t	        t	        j
                  | |      g      S )ad  Output 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   r   )r   r   astyper   r   r   )r   r   binsmax_binsr   s        r/   r   r     s=    " V_F&--.h?D'--D=>??r0   c           
      8   | j                   dk(  rt        d      | j                  d      } t        j                  | |      \  }}t        |      }|||kD  r||z  }||z  }|dk7  rt        j                  |d||z
  ggdd      }|j                  d|      j                  d      }t        j                  |j                   d	z   f|j                        }|dd|   |dd |d   |d<   |}t        j                  t        j                  |d            }	t        j                  |	d|	d   d	z
  gd
      \  }
}t        |
      }
t        |      d	z   }~	|
dkD  r||
d	z
  | nt        j                  dg|d| g      }||
|d	z    }|j                   dk(  s|j                   dk(  rt        d      | j                  |       }t!        | j#                         | j%                         t        |       | j                         ||j'                         |j'                               S )zDConvert values into a histogram proto using logic from histogram.cc.r   zThe input has no element.)r   Nconstant)	pad_widthmodeconstant_values)axisr   ry   )sidez1The histogram is empty, please file a bug report.r   )r   r   reshaperY   r   lenpadr   emptyr\   cumsumgreatersearchsortedr   concatenatedotr   r   r   r6   )r   r   r   countslimitsnum_binssubsamplingsubsampling_remainder
new_limits
cum_countsstartendsum_sqs                r/   r   r     s   {{a455^^BF\\&t4NFF6{H8 3(* (; 6 A%VV{-BBCD !	F K044"4=XXv{{Q0&,,?
 "[1
3B
2 2::fa01JaB!1C-D7SJE3JE
c(Q,C $)19uqy3"..1#vds|AT2U  EC!G$F{{a6;;!+LMMZZFJJLJJLKJJL]]_}} r0   c                 F   t        |      }t        ||      }t        |      }|j                  t        j
                        }||z  j                  dd      j                  t        j                        }t        ||      }t        t        j                  | |      g      S )aJ  Output 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   rW   )rescaler   r   r   )r   r   r`   r   rY   float32clipr]   r   r   r   )r   r^   r  dataformatsscale_factorr   s         r/   r   r   4  s    6 V_FFK0F%f-L]]2::&F|#))!S188BFvw/E'--Cu=>??r0   c                 X   t        |      }t        ||      }t        |      }|j                  t        j                        t        |      z  }t        |j                  dd      j                  t        j                        |||      }t        t        j                  | |      g      S )z/Output a `Summary` protocol buffer with images.r   rW   )r  roislabelsr  r   )r   r   r   rY   r  r`   r   r  r]   r   r   )r   tensor_imagetensor_boxesr  r   r$  r   s          r/   r   r   Z  s     <(L!,<L<(L&&rzz25G5UUL!S!((2	E '--Cu=>??r0   c           
          |j                   d   }t        |      }|D ]/  }t        | ||df   ||df   ||df   ||df   |d n||   d      } 1 | S )Nr   r   rf      Red)rs   rt   )r   ranger   )
disp_imageboxesr$  	num_boxeslist_gtr)   s         r/   r   r   l  sy    AIIG 	
%!Q$K!Q$K!Q$K!Q$K &F1I

	
 r0   c                    ddl m} | j                  \  }}}t        ||z        }t        ||z        }	|j	                  |       }
|t        |
||      }
|j                  j                  }|
j                  |	|f|      }
ddl	}|j                         }|
j                  |d       |j                         }|j                          t        j                  ||||      S )z=Convert a numpy representation of an image to Image protobuf.r   )ImageN)r$  PNG)formatheightrd   
colorspaceencoded_image_string)rh   r0  r   r   	fromarrayr   
ResamplingLANCZOSresizeioBytesIOsavegetvaluecloser   )r^   r  r#  r$  r0  r4  rd   channelscaled_heightscaled_widthr   	ANTIALIASr;  outputimage_strings                  r/   r   r   }  s    #\\FE7()Muw'LOOF#E5$v6  ((ILL,6	BEZZ\F	JJveJ$??$L
LLN==)	 r0   c                 B   t        |      }t        |      }t        |      }|j                  t        j
                        }||z  j                  dd      j                  t        j                        }t        ||      }t        t        j                  | |      g      S )Nr   rW   r  r   )r   r   r`   r   rY   r  r  r]   r   r   r   )r   r^   fpsr!  r   s        r/   r   r     s|    V_FF#F%f-L]]2::&F|#))!S188BFvs#E'--Cu=>??r0   c                 z   	 dd l }	 ddl m} dd l}| j
                  \  }}}}|j                  t        |       |      }	|j                  d      5 }
|
j                  }	 |	j                  |d	d 
       |
j                  d       |
j                         }t        j                  ||||      cd d d        S # t        $ r t        d       Y y w xY w# t        $ r t        dd       Y y w xY w# t        $ r; 	 |	j                  |d	d	       n"# t        $ r |	j                  |d	       Y nw xY wY w xY w# 1 sw Y   y xY w)Nr   zadd_video needs package moviepy)editorz6moviepy is installed, but can't import moviepy.editor.z2Some packages could be missing [imageio, requests])rG  z.gif)suffixF)verboser   )rK  progress_bar)rK  r3  )moviepyImportErrorprintrI  tempfiler   ImageSequenceClipr   NamedTemporaryFiler   	write_gifr   seekreadr   r0  )r^   rG  rM  mpyrP  _thwcr  r&   filenametensor_strings                r/   r   r     sE   ) ,,KB1a   f3 7D		$	$F	$	3 
q66	8NN8U4N@ 	
q	 }}A!-

 
%  /0
  D@	
 	"  	88xUK 8x78	8	
 
sv   B5 C D1C*29D15CCC'&C'*	D.4D	D.	D(%D.'D((D.+D1-D..D11D:c                 H   t        |      }|j                         }t        |      j                         dkD  rt	        d       |j                  dd      }|j                  dk7  rt        d      |t        j                  t        j                        j                  z  j                  d      }dd l}dd l}|j                         }|j                  |d      5 }|j!                  d       |j#                  d       |j%                  |       |j'                  |j(                         d d d        |j+                         }|j-                          t/        j0                  |d|j2                  d   |d	
      }	t/        t/        j4                  | |	      g      S # 1 sw Y   rxY w)Nr   z4warning: audio amplitude out of range, auto clipped.r  z%input tensor should be 1 dimensional.z<i2r   wbrf   z	audio/wav)sample_ratenum_channelslength_framesencoded_audio_stringcontent_type)r   r   r   )r   r   absr   rO  r  r   r   rY   iinfoint16r   r;  waver<  opensetnchannelssetsampwidthsetframeratewriteframesdatar>  r?  r   Audior   r   )
r   r^   r_  arrayr;  rg  fio
wave_writeaudio_stringr   s
             r/   r   r     sI   FOEMMOE
5z~~!DE

2q!zzQDEERXXbhh'+++33E:E
**,C	3	 +"",uzz*	+
 <<>LIIKMMkk"o) E '--Cu=>??!+ +s   AFF!c           
      2   g }| j                         D ]  \  }}g }|j                         D ]  \  }}|d   }|d   dk(  rqt        |      dk7  rt        d      t        j                  t        j                  j                  |d   |d   |d         g      }t        j                  ||	      }	n-t        j                  |
      }
t        j                  ||
      }	|j                  |	        |j                  t        j                  ||              t        j                  |      } t        j                  d      }t        |      }t        d| j                         gt                     }t!        t!        j"                  d||      g      S )Nr   r   Marginr(  zlen(tags) != 3rf   )r   lowerupper)series)titler   r   )rx  	multiline)rx  chart)categoryr   r   r   	DT_STRINGr\   
string_valr   custom_scalars__config__r   r   )r   r	  r   r
   MarginChartContentSeriesChartMultilineChartContentr   CategoryLayoutr   r   r   r   r	   r   r   )layout
categoriesr   r   charts
chart_namechart_metadatatagsmgccrz  mlccr   r   r^   s                 r/   r   r     s   J F1*+'') 	!&J!!$Da H,t9>()9::!44 #55<<"&q'aQ =  #((z$G!77DA"((zTJMM% #	!$ 	*--AVDE)F, 
3F!,,9IJK
k
2C,,./%'F
  MM8RUV
 r0   c           	      @   t        j                  dt        d      j                               }t        |      }t	        d|j                  d      gt        t        j                  d	
      g            }t        t        j                  | dz   ||      g      S )Nr    r   )r   r   r   r|  utf_8)encodingr   r   r   r}  z/text_summaryr   r   )
r   r   r   r   r   encoder	   r   r   r   )r   r    r   r   r^   s        r/   r    r      s    !,,N1$=$O$O$QK k
2CKKK12%+;+?+?Q+G*HI	F }}!6VTU r0   c	                    |dkD  rd}t        j                  ||||||f      }	t        d|      j                         }
t	        j
                  d|
      }t	        |      }t        d|	j                  d      j                         t        t        j                  |	j                  d   	      t        j                  |	j                  d
   	      g            }t        t        j                  | ||      g      S )N   r   r   num_thresholds	pr_curvesr   r   rC   r  r   r   r   r\   rD   r   r   r   )rY   stackr   r   r   r   r   r  r6   r	   r   r   r   r   )r   tpfptnfn	precisionrecallr  weightsrm  pr_curve_plugin_datar   r   r^   s                 r/   r"   r"   (  s     88RRY78D,.  ",,)=K k
2C,,r"))+% !$$$**Q-8 $$$**Q-8	
F '--C#fMNOOr0   c                    t        |d      }t        ||||      }t        d|      j                         }t	        j
                  d|      }t	        |      }t        d|j                  d	      j                         t        t        j                  |j                  d   
      t        j                  |j                  d   
      g            }	t        t        j                  | ||	      g      S )Nr  )r  r  r   r  r  r   r   rC   r  r   r   r   r  r   r   )r   r$   r   r   r   r   r   r  r6   r	   r   r   r   r   )
r   r$  predictionsr  r  rm  r  r   r   r^   s
             r/   r#   r#   F  s    -NNGD -.  ",,)=K k
2C,,r"))+% !$$$**Q-8 $$$**Q-8	
F '--C#fMNOOr0   c                    d}|d}t        j                  t        j                  ||dz
  z              }| j                  t         j                        }d|dz
  f}t        j
                  |||||z        \  }}	t        j
                  |||d|z
  |z        \  }
}	t        j                  |d d d         d d d   }t        j                  |
d d d         d d d   }|d   |z
  }|d   |z
  }|t        j                  |||z         z  }|t        j                  |||z         z  }t        j                  ||||||f      S )NgHz>g      ?r   r   )r   r*  r  r  )	rY   int32floorr   float64r   r  maximumr  )r$  r  r  r  _MINIMUM_COUNTbucket_indicesfloat_labelshistogram_range
tp_buckets_
fp_bucketsr  r  r  r  r  r  s                    r/   r$   r$   e  sL   N XXbhh{nq6H'IJKN==,L.1,-OLLw&MJ LL|#w.MJ 
:dd#	$TrT	*B	:dd#	$TrT	*B	AB	ABRZZR88I"**^R"W55F88RRY788r0   c                    ddl }ddlm}  |j                  |      }|j	                  | ||||j
                  ||      }	t        d|j                  d      j                         t        t        j                  |j
                  d         t        j                  |j
                  d         t        j                  |j
                  d	         g
            }t        j                  |j                  | |      ||	      }
|
S )a  Create 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r   )json_configrC   r  r   r   rf   r   r  r   )r:   tensorboard.plugins.meshr   	as_tensorcreate_summary_metadatar   r   r  r6   r	   r   r   r   get_instance_name)r   display_namedescriptionr^   rc  
componentsr  r:   r   tensor_metadatatensor_summarys              r/   _get_tensor_summaryr    s    . 1U__V$F66 7 O ..$++-% !$$&,,q/: $$&,,q/: $$&,,q/:	
F  ]]&&t\: N r0   c                 <    d}| t        j                  | d      }|S )z5Parse and returns JSON string from python dictionary.z{}T)	sort_keys)jsondumps)config_dictr  s     r/   _get_json_configr    s#    Kjj=r0   c                 z   ddl m} ddlm} t	        |      }	g }
||j
                  f||j                  f||j                  fg}|D cg c]
  }|d   	| }}|j                  |D cg c]  \  }}|	 c}}      }|D ]%  \  }}|
j                  t        | ||||||	             ' t        |
      S c c}w c c}}w )a'  Output 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  )MeshPluginDatar   )r  r   (tensorboard.plugins.mesh.plugin_data_pb2r  r  VERTEXFACECOLORget_components_bitmaskr   r  r   )r   verticescolorsfacesr  r  r  r   r  r  	summariestensorsr^   rc  r  s                  r/   r%   r%     s    , 2G";/KI 
>(()	##$	%%&G %,E&vay/DvEGE004;<06<<J !( 

	

 ##% F<s   
B2B2(B7
)blackr  rf   )NNN)NFFr4   )r   NCHW)r   CHWN)r   NN)   )iD  )r  N)NN)Yr  loggingr+   typingr   r:   r[   rY   google.protobufr   $tensorboard.compat.proto.summary_pb2r   r   r   #tensorboard.compat.proto.tensor_pb2r   )tensorboard.compat.proto.tensor_shape_pb2r	   !tensorboard.plugins.custom_scalarr
   ,tensorboard.plugins.pr_curve.plugin_data_pb2r   (tensorboard.plugins.text.plugin_data_pb2r   _convert_npr   _utilsr   r   __all__	getLogger__name__r   r   r   r   r   r   r   r8   r=   r?   halffloat16bfloat16r  r  doubleint8r]   qint8rf  shortr  qint32int64	complex32chalf	complex64cfloatr   
complex128cdoublequint8quint4x2r   r`   r   r   r   r!   r   r   r   r   r   r   r   r   r   r   r   r    r"   r#   r$   r  r  r%   r   r0   r/   <module>r     s         ' 
 < F 8 J C   20 
		8	$&5 &S &&3 &5 &:5<< :DI :4ell 4tE{ 4 u||  S	  	JJJ(;<	MMIz+>? 
NN]J0CD 
MMJ_=	
 
KK*k?; 
MMK? 
LL;o> 
JJI7 
KK*i9 
KK*i9 
KK*i9 
KK*i9 
II
I7 
KK*i9 
LL:y/:  
KK*k?;!" 
OOnn6LM#$ 
KK..2HI	OOnn6LM	LL>>3IJ	JJJ8	8NO	MMO^5KL	KK*lO<	LL:|_=	NNZ?5 <5# 5 (VEP+M\%VP@D@.5p#@N KO@$"6	@%
P@B&R& IMP<P>"9J<~ OS6$r0   