
    BVh,                         d Z 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Zd
 Zd Zd Zedk(  r0 e       \  ZZ ej.                  eej0                  d   gez          yy)a  Removes parts of a graph that are only needed for training.

There are several common transformations that can be applied to GraphDefs
created to train a model, that help reduce the amount of computation needed when
the network is used only for inference. These include:

 - Convert given PlaceholderWithDefault or Placeholder nodes to Constant

 - Removing training-only operations like checkpoint saving.

 - Stripping out parts of the graph that are never reached.

 - Removing debug operations like CheckNumerics.

 - Fusing a group of primitive ops for batch normalization to FusedBatchNorm op.

 - Folding batch normalization ops into the pre-calculated weights.

 - Fusing common operations into unified versions.

This script takes either a frozen binary GraphDef file (where the weight
variables have been converted into constants by the freeze_graph script), or a
text GraphDef proto file (the weight variables are stored in a separate
checkpoint file), and outputs a new GraphDef with the optimizations applied.

If the input graph is a text graph file, make sure to include the node that
restores the variable weights in output_names. That node is usually named
"restore_all".

An example of command-line usage is:

bazel build tensorflow/python/tools:optimize_for_inference && \
bazel-bin/tensorflow/python/tools/optimize_for_inference \
--input=frozen_inception_graph.pb \
--output=optimized_inception_graph.pb \
--frozen_graph=True \
--input_names=Mul \
--output_names=softmax


    N)app)text_format)	graph_pb2)dtypes)graph_io)gfile)optimize_for_inference_libc           
         t        j                  t        j                        s t	        dt        j                  z   dz          yt        j                         }t        j                  t        j                  d      5 }|j                         }t        j                  r|j                  |       n%t        j                  |j                  d      |       d d d        t        j                  |t        j                   j#                  d      t        j$                  j#                  d      t'        t        j(                        t        j*                  t        j,                  j#                  d            }t        j                  rDt        j.                  t        j0                  d      }|j3                  |j5                                yt7        j8                  |t:        j<                  j?                  t        j0                        t:        j<                  jA                  t        j0                               y# 1 sw Y   bxY w)	NzInput graph file 'z' does not exist!rbzutf-8,wr   )!r   ExistsFLAGSinputprintr   GraphDefOpenreadfrozen_graphParseFromStringr   Mergedecoder	   optimize_for_inferenceinput_namessplitoutput_names_parse_placeholder_typesplaceholder_type_enumtoco_compatibleplaceholder_to_const_namesGFileoutputwriteSerializeToStringr   write_graphospathdirnamebasename)unused_argsinput_graph_deffdataoutput_graph_defs        ^/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tools/optimize_for_inference.pymainr1   J   s   	ekk	"	

,/B
BC&&(/zz%++t$ ?668D%%d+G,o>? 0FFc"s#u::;&&,,S1 ELL#&AGG..01
 
 )6))%,,79 
/? ?s   <AH44H>c                     | j                  d      D cg c]  }t        |       } }t        |       dkD  r| S | d   S c c}w )z6Extracts placeholder types from a comma separate list.r      r   )r   intlen)valuesvalues     r0   r   r   j   s@    $*LL$565CJ6&6v;?1q	1 7s   =c                  @   t        j                         } | j                  ddd        | j                  dt        dd       | j                  dt        dd	       | j                  d
t        dd       | j                  dt        dd       | j                  dddddd       | j                  dt        t	        t
        j                  j                        d       | j                  dt        dd       | j                  dt        dd       | j                         S )zParses command line arguments.typeboolc                 (    | j                         dk(  S )Ntrue)lower)vs    r0   <lambda>zparse_args.<locals>.<lambda>s   s    AGGI,?     z--input z#TensorFlow 'GraphDef' file to load.)r9   defaulthelpz--outputz!File to save the output graph to.z--input_namesz"Input node names, comma separated.z--output_namesz#Output node names, comma separated.z--frozen_graph?Tzx      If true, the input graph is a binary frozen GraphDef
      file; if false, it is a text GraphDef proto file.      )nargsconstr9   rB   rC   z--placeholder_type_enumzx      The AttrValue enum to use for placeholders.
      Or a comma separated list, one value for each placeholder.      z--toco_compatibleFz]      If true, only use ops compatible with Tensorflow
      Lite Optimizing Converter.      z--placeholder_to_const_namesz      List of PlaceholderWithDefault or Placeholder node names and
      their new value to be converted to Constant node, comma separated.
      eg: --placeholder_to_const_names=phase_train=False      )
argparseArgumentParserregisteradd_argumentstrr   float32as_datatype_enumr:   parse_known_args)parsers    r0   
parse_argsrP   p   sU   ""$&//&&"?@2	  4
 	.	  0
 	/	  1
 	0	  2
 	
  	 	&..112
	   	
	   	$
	  	 
	 	 	""r@   __main__)r1   argv)__doc__rG   r'   sysabslr   google.protobufr   tensorflow.core.frameworkr   tensorflow.python.frameworkr   r   tensorflow.python.platformr   tensorflow.python.toolsr	   r   r1   r   rP   __name__unparsedrunrR    r@   r0   <module>r_      sz    (T  	 
  ' / . 0 , >@2<#~ zL/%	#''t388A;-(23 r@   