B
    лdQ                 @   s>  d Z ddlZddlZddlZddlmZ ddlZddlm	Z	 ddl
mZ ddlmZ dZd)dd	Zd*ddZdd Zdd Zedkr:e Zedddd  ejdeddd ejdeddd ejdeddd ejddddd
d d! ejd"dddd
d#d! ejd$d%ed&d' e \ZZejeejd ge d( dS )+z-A simple script for inspect checkpoint files.    N)app)errors_impl)flags)py_checkpoint_reader c                s   |    |rxt|}i }d}d}x> D ]6}||rR|d7 }|t | 7 }q( | ||< q(W | td|||f   fdd D }tj|tdS )z Count total number of variables.r      z?# Excluding %d tensors (%d params) that match %s when counting.c                s   g | ]}t  | qS  )npprod).0v)var_to_shape_mapr   \/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/tools/inspect_checkpoint.py
<listcomp>2   s    z'_count_total_params.<locals>.<listcomp>)Zdtype)	get_variable_to_shape_maprecompilesearchr	   r
   printsumint)readercount_exclude_patternZregex_patternZnew_var_to_shape_mapZexclude_num_tensorsZexclude_num_paramsr   Z	var_sizesr   )r   r   _count_total_params   s     


r   Fc          
      s  yt  }|s|r| }| }xt| D ]V\}}	td||| j|	f  |r4yt|| W q4 t	j
k
r   td Y q4X q4W np|st| jddd nT||std|  dS | }| }td||| j|| f  t|| tdt||d	  W n tk
r }
 zvtt|
 d
t|
krLtd dt|
krt fdddD rd ddd }d}t|| W dd}
~
X Y nX dS )a#  Prints tensors in a checkpoint file.

  If no `tensor_name` is provided, prints the tensor names and shapes
  in the checkpoint file.

  If `tensor_name` is provided, prints the content of the tensor.

  Args:
    file_name: Name of the checkpoint file.
    tensor_name: Name of the tensor in the checkpoint file to print.
    all_tensors: Boolean indicating whether to print all tensors.
    all_tensor_names: Boolean indicating whether to print all tensor names.
    count_exclude_pattern: Regex string, pattern to exclude tensors when count.
  ztensor: %s (%s) %sz"<not convertible to a numpy dtype>zutf-8ignore)errorsz!Tensor %s not found in checkpointNz# Total number of params: %d)r   z#corrupted compressed block contentszFIt's likely that your checkpoint file has been compressed with SNAPPY.z	Data lossc             3   s   | ]}| kV  qd S )Nr   )r   e)	file_namer   r   	<genexpr>j   s    z3print_tensors_in_checkpoint_file.<locals>.<genexpr>)z.indexz.metaz.data.r   z
It's likely that this is a V2 checkpoint and you need to provide the filename
*prefix*.  Try removing the '.' and extension.  Try:
inspect checkpoint --file_name = {})r   ZNewCheckpointReaderr   Zget_variable_to_dtype_mapsorteditemsr   nameZ
get_tensorr   ZInternalErrorZdebug_stringdecodeZ
has_tensorr   	Exceptionstranyjoinsplitformat)r   tensor_nameall_tensorsall_tensor_namesr   r   r   Zvar_to_dtype_mapkeyvaluer   Zproposed_fileZv2_file_error_templater   )r   r    print_tensors_in_checkpoint_file6   sD    


r0   c          
   C   s   |  dd}t|dks |d s.td|  |\}}t }||krTtd| t|| }|tdkrztd| y"|tk	r||nt	 
|}W n. tk
r } zt|jW dd}~X Y nX tjf ||i dS )	a  Sets a single numpy printoption from a string of the form 'x=y'.

  See documentation on numpy.set_printoptions() for details about what values
  x and y can take. x can be any option listed there other than 'formatter'.

  Args:
    kv_str: A string of the form 'x=y', such as 'threshold=100000'

  Raises:
    argparse.ArgumentTypeError: If the string couldn't be used to set any
        nump printoption.
  =r      r   z'%s' is not in the form k=v.z '%s' is not a valid printoption.Nz4Setting '%s' from the command line is not supported.)r)   lenargparseArgumentTypeErrorr	   Zget_printoptionstypeboolr   ZBooleanParserparse
ValueErrormessageZset_printoptions)Zkv_strZk_v_strkZv_strZprintoptionsZv_typer   r   r   r   r   parse_numpy_printoptions   s     
"r<   c             C   s:   t jstd td ntt jt jt jt jt j	d d S )NzUsage: inspect_checkpoint --file_name=checkpoint_file_name [--tensor_name=tensor_to_print] [--all_tensors] [--all_tensor_names] [--printoptions]r   )r   )
FLAGSr   r   sysexitr0   r+   r,   r-   r   )Zunused_argvr   r   r   main   s    r@   __main__r6   r7   c             C   s   |   dkS )Ntrue)lower)r   r   r   r   <lambda>       rD   z--file_namez}Checkpoint filename. Note, if using Checkpoint V2 format, file_name is the shared prefix between all files in the checkpoint.)r6   defaulthelpz--tensor_namezName of the tensor to inspectz--count_exclude_patternzAPattern to exclude tensors, e.g., from optimizers, when counting.z--all_tensors?Tz7If True, print the names and values of all the tensors.)nargsconstr6   rF   rG   z--all_tensor_namesz,If True, print the names of all the tensors.z--printoptions*z9Argument for numpy.set_printoptions(), in the form 'k=v'.)rI   r6   rG   )r@   argv)r   )Fr   )__doc__r4   r   r>   Zabslr   numpyr	   Ztensorflow.python.frameworkr   Ztensorflow.python.platformr   Ztensorflow.python.trainingr   r=   r   r0   r<   r@   __name__ArgumentParserparserregisteradd_argumentr&   parse_known_argsZunparsedrunrL   r   r   r   r   <module>   sj   
 
;!
