
    BVh                     t   d Z ddlZddlm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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Z*e jV                  jX                  e jV                  jZ                  e jV                  j\                  gZ/d/dZ0d Z1d Z2d Z3d Z4g dZ5d  Z6d! Z7d" Z8	 	 	 d0d#Z9d$ Z:d% Z;	 	 	 	 	 	 	 	 	 d1d&Z<d' Z=ddddd(d) fd*Z>ddddd(d+ dfd,Z?	 	 	 	 	 	 	 	 	 	 	 d2d-Z@	 d3d.ZAy)4z MetaGraph and related functions.    N)version)Any)text_format)attr_value_pb2)	graph_pb2)
op_def_pb2)meta_graph_pb2)	saver_pb2)pywrap_tf_session)context)byte_swap_tensor)error_interpolation)graph_io)importer)op_def_registry)ops)tensor)versions)file_io)
tf_logging)compatz$unbound_inputs_Fc           
         t        j                  |       }t        |j                        D ]  \  }}|r|j                  |   j	                  d      j                  |      s`t        j                  ddt        z   dz   t        j                  |            |j                  |<   |j                  |j                  |          t        j                  ||      |j                  |<    t        j                  t        j                  | j                  |            |_        | j                   j#                         D ]  \  }}|dk(  r|j$                  j&                  D cg c]c  }|r6t        j                  |      j)                  d      d   j                  |      r)t        j                  t        j                  ||            e }	}|j                   |   j+                  t-        j.                  t,        j.                  j1                  |	      	             |j2                  d
v r|dk(  r|r.t        j                  |j&                        j                  |      r3t        j                  t        j                  |j&                  |            }	|j                   |   j+                  t-        j.                  	             |j                   |   j+                  |        |rd|_        |S c c}w )a  Create a `NodeDef` proto with export_scope stripped.

  Args:
    from_node_def: A `node_def_pb2.NodeDef` protocol buffer.
    export_scope: A `string` representing the name scope to remove.
    unbound_inputs: An array of unbound input names if they exist.
    clear_devices: Boolean which controls whether to clear device information
      from node_def. Default false.

  Returns:
    A `node_def_pb2.NodeDef` protocol buffer.
  ^z([\^]|^)(.*)z\1z\2_class@   )s)list)EnterRefEnter
frame_name )copydeepcopy	enumerateinputlstrip
startswithresub_UNBOUND_INPUT_PREFIXr   as_strappendr   strip_name_scopeas_bytesnameattritemsr   r   splitCopyFromr   	AttrValue	ListValueopdevice)
from_node_defexport_scopeunbound_inputsclear_devicesnode_defivkr   new_ss
             V/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/framework/meta_graph.py	_node_defrC   8   sW    ]]=)(' 
@daNN1$$S)44\B &&!&)>!>!F!'q!13hnnQ HNN1-...q,?hnnQ
@ //	=--|<>(-  &&( #daH}:;&&((H56"}}Q%%c*1-88F 


q,
/1 He H mmA 8 8''11E1:!< =	-	-!|2CV]]133/::<H 4 4QSS, GHmmA 8 85 ABmmA"# HO	/!Hs   A(K,c           	         t        j                         }t        j                  |       st	        d|  d      t        j
                  | d      5 }|j                         }ddd       	 |j                         |S # 1 sw Y   xY w# t        $ r Y nw xY w	 t        j                  |       |S # t        j                  $ r }t	        d|  dt        |       d      d}~ww xY w)a	  Reads a file containing `GraphDef` and returns the protocol buffer.

  Args:
    filename: `graph_def` filename including the path.

  Returns:
    A `GraphDef` protocol buffer.

  Raises:
    IOError: If the file doesn't exist, or cannot be successfully parsed.
  zFile z does not exist.rbNCannot parse file : .)r   GraphDefr   file_existsIOErrorFileIOreadParseFromString	Exceptionr   Merge
ParseErrorstr)filename	graph_defffile_contentes        rB   
_read_filerX   h   s       ")			X	&
E(#34
55~~h% 668L	l+	 
 
 		>lI. 
 
		 >
&xj3q6(!<
==>s6   A<)B <B	BBB0 0C#CC#c                 h   i | j                   j                  D ]  }||j                  j                  <    t	               g fdfd}| j
                  D ]
  } ||        r,j                         }|j                  D ]
  } ||        r,D cg c]	  }|vs| c}S c c}w )zCollect the list of ops used by a graph.

  Does not validate that the ops are all registered.

  Args:
    graph_def: A `GraphDef` proto, as from `graph.as_graph_def()`.

  Returns:
    A list of strings, each naming an op used by the graph.
  c                 `    | vr| v rj                  |           j                  |        y N)r-   add)r7   functions_to_processname_to_functionused_opss    rB   mark_op_as_usedz.ops_used_by_graph_def.<locals>.mark_op_as_used   s3    	b$44!!"22"67LL    c                      | j                          | j                   dv r* | j                  d   j                  j                         y y )N)PartitionedCallStatefulPartitionedCallrU   )r7   r1   funcr0   )noder`   s    rB   process_nodez+ops_used_by_graph_def.<locals>.process_node   s>    DGGww@@diin))../ Ara   )libraryfunction	signaturer0   setrf   popr=   )	rT   funrg   rf   r7   r]   r`   r^   r_   s	        @@@@rB   ops_used_by_graph_defrn      s     '' /c+.S]]''(/
 U(
0
 nn d

"
"
$C 4 	
  	>2-=#="	>>	>s   	B/(B/c                     t        |       }g }t        |      D ]+  }t        j                  |      }||j	                  |       - t        j                  |      S )a  Collect the stripped OpDefs for ops used by a graph.

  This function computes the `stripped_op_list` field of `MetaGraphDef` and
  similar protos.  The result can be communicated from the producer to the
  consumer, which can then use the C++ function
  `RemoveNewDefaultAttrsFromGraphDef` to improve forwards compatibility.

  Args:
    graph_def: A `GraphDef` proto, as from `graph.as_graph_def()`.

  Returns:
    An `OpList` of ops used by the graph.
  )r7   )rn   sortedr   getr-   r   OpList)rT   r_   op_defsr7   op_defs        rB   stripped_op_list_for_graphru      s\    " #9-('8 b  $FnnV 
		g	&&ra   c                     t        | t        t        f      rd}|S t        | t              rd}|S t        | t              rd}|S t        | t
              rd}|S d}|S )zReturns the kind name in CollectionDef.

  Args:
    item: A data item.

  Returns:
    The string representation of the kind in CollectionDef.
  
bytes_list
int64_list
float_listany_list	node_list)
isinstancerR   bytesintfloatr   )itemkinds     rB   _get_kind_namer      sr     sEl#D 
+ $D 
+ $D
 
+	 $D 
+ D	+ra   )
SaveV2Save	SaveSlice
LegacySaveLegacySaveSlice	RestoreV2RestoreRestoreSliceLegacyRestoreLegacyRestoreSlicec                     | st        d|  d      | j                  d      r| dd } d| v r| j                  dd      \  }}|S y)a^  Extract the scope name from a node name.

  The scope name is everything before the final slash,
  not including any ^ prefix denoting a control dependency.

  Args:
    node_name: the full name of an Op or a Tensor in the graph.
  Returns:
    The deepest named scope containing the node.
  Raises:
    ValueError: if tensor_name is None or empty
  z-Node name cannot be empty or None. Received: rH   r   r   N/r"   )
ValueErrorr(   rsplit)	node_namescope_s      rB   
_get_scoper      sc     


7	{!DF F #!"IIQ'HE1L	ra   c                    | j                   D ci c]5  }|j                  t        d |j                  D              |j                  f7 }}d}d}|Zt        j                  |j                        }t        j                  |j                        }t        |      dz   }t        |      dz   }t        d |j                         D              }t        d |D              |z
  }	t        d |	D              }	|	t        ||g      z
  }
t               }|j                         D ]0  \  }}|
D ]&  }|j                  |      s|j                  |        0 2 |S c c}w )a  Identifies any nodes in the graph_def related to unused Savers.

  This approach assumes that each Saver is cleanly isolated in its own name
  scope, so we need only identify the scopes associated with extraneous Savers
  and return all the nodes in those scopes.

  Args:
    graph_def: a GraphDef proto to evaluate.
    saver_def: a SaverDef proto referencing Save/Restore ops to be retained.
  Returns:
    An iterable of node names that may be safely omitted.
  c              3   F   K   | ]  }t        j                  |        y wr[   )r   get_op_name.0xs     rB   	<genexpr>z/_find_extraneous_saver_nodes.<locals>.<genexpr>  s     <f  #<s   !Nr   c              3   >   K   | ]  \  }\  }}|t         v s|  y wr[   )SAVE_AND_RESTORE_OPS)r   r0   r   r7   s       rB   r   z/_find_extraneous_saver_nodes.<locals>.<genexpr>*  s'      MtWa7K1KdMs   c              3   2   K   | ]  }t        |        y wr[   )r   r   s     rB   r   z/_find_extraneous_saver_nodes.<locals>.<genexpr>.  s     	6A*Q-	6s   c              3   &   K   | ]	  }|d z     yw)r   N r   s     rB   r   z/_find_extraneous_saver_nodes.<locals>.<genexpr>/  s     ;QS;s   )rf   r0   rk   r&   r7   r   r   save_tensor_namerestore_op_namer   r2   r(   r\   )rT   	saver_defr=   nodesretain_scope_saveretain_scope_restoresave_op_namer   all_saver_node_namesall_saver_scopesextraneous_scopesextraneous_node_namesr0   r   extraneous_scopes                  rB   _find_extraneous_saver_nodesr     s   ,  nn  mm
<X^^<
<hkkK K%  %%i&@&@AL(()B)BCO &o6<"<036 M %M M 
	6!5	669MM ;*:;;&.?.B.D *E E % gdA- 	)	*!!$' 
Gs   :Ec                     t        | t              s	 | j                  }n| }|r	| |v s||v ry|j	                  t
              xs | xs |j	                  |      S # t        $ r Y yw xY w)aA  Returns `True` if a node should be included.

  Args:
    node_or_node_name: A node or `string` node name.
    export_scope: `string`. Name scope under which to extract the subgraph. The
      scope name will be stripped from the node definitions for easy import
      later into new name scopes.
    exclude_nodes: An iterable of nodes or `string` node names to omit from the
      export, or None.  Note no sanity-checking is done, so this list must be
      carefully constructed to avoid producing an invalid graph.

  Returns:
    `True` if the node should be included.
  TF)r|   rR   r0   AttributeErrorr(   r+   )node_or_node_namer:   exclude_nodesr   s       rB   _should_include_noder   >  s     
%s	+#((i
 "I)]:$5


4
5 CAy33LAD  s   A 	A%$A%c                 d   |r2t        |t        j                        st        dt	        |       d      t        |t
              s0t        |t              s t        j                  dt	        |             y|xs t        j                         }|r|}n|j                  |      }|D cg c]  }t        |||      r| }}|sy	 | j                  |   }t        j                  |      }	t        j                  |      }
|	rXd}|D ]P  } |	||      }|st        ||
      sJ t        ||      j                   j#                  |j%                                R yt'        |d         }|dk(  ri|D ]c  }|r|j(                  j+                  |      s!t        ||      j                   j#                  t        j,                  |j(                  |             e y|dk(  rGt        ||      j                   j/                  |D cg c]  }t1        j2                  |       c}       yt        ||      j                   j/                  |D cg c]  }| c}       yc c}w c c}w c c}w # t4        $ rE}t        j                  d	|t        |             || j                  v r| j                  |= Y d}~yd}~ww xY w)
a  Adds a collection to MetaGraphDef protocol buffer.

  Args:
    meta_graph_def: MetaGraphDef protocol buffer.
    key: One of the GraphKeys or user-defined string.
    graph: The `Graph` from which to get collections.
    export_scope: Optional `string`. Name scope to remove.
    exclude_nodes: An iterable of nodes or `string` node names to omit from the
      collection, or None.
    override_contents: An iterable of values to place in the collection,
      ignoring the current values (if set).
  ,graph must be of type Graph. Received type: rH   zJOnly collections with string type keys will be serialized. This key has %sNrw   )r:   r   r{   zIssue encountered when serializing %s.
Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore.
%s)r|   r   Graph	TypeErrortyperR   r}   loggingwarningget_default_graphget_collectionr   collection_defget_to_proto_functionget_collection_proto_typegetattrvaluer-   SerializeToStringr   r0   r(   r.   extendr   r/   rO   )meta_graph_defkeygraphr:   r   override_contentscollection_listr   col_defto_proto
proto_typer   protorW   s                 rB   add_collection_defr   ^  s    :eSYY/

6tE{m1EG G 
C	je&<OO 2379>
 
*3((*%'O**3/O !0 K1)!\=I  K/ K	
"++C0G((-H..s3Jd I! 6E:.
..
'4
 
&
&
-
-e.E.E.G
HI OA./d		  	<A!2!2<!@GT"((//$$QVV\:<	< < 	$$++)89AV__Q9	; 	$$++,H1Q,HI?K: :,H	 OO 7 9<SVE n+++

'
'
,
s]   'IAI! AI! 5I! AI! (I! :II! #I! 	I
I! 
I! !	J/*;J**J/c                     | j                   D ]c  }|j                  |k(  s|j                  d      s yt        j                  |j                         |j                  j                                c S  y)zBChecks if given attribute matches the default value in the op def.default_valueF)r1   r0   HasFieldc_apiEqualAttrValueWrapperr   r   )rt   	attr_name
attr_valueattr_defs       rB   _is_default_attr_valuer     so    ++ 6h}}	!/ ,,

&
&
(

 
 
2
2
46 6 66 
ra   c                 t   i | j                   j                  j                  D ]  }||j                  j                  <    fd}| j                   j
                  D ]
  } ||        | j                   j                  j                  D ]  }|j                  D ]
  } ||         d| j                  _        y)a  Strips default valued attributes for node defs in given MetaGraphDef.

  This method also sets `meta_info_def.stripped_default_attrs` in the given
  `MetaGraphDef` proto to True.

  Args:
    meta_graph_def: `MetaGraphDef` protocol buffer

  Returns:
    None.
  c                 &   | j                   v ryt        j                  | j                         }|yt               }| j                  j                         D ]$  \  }}t        |||      s|j                  |       & |D ]  }| j                  |=  y)z9Removes default valued attributes from a single node def.N)r7   r   rq   rk   r1   r2   r   r\   )r=   rt   attrs_to_stripr   r   r1   op_name_to_functions         rB    _strip_node_default_valued_attrszJstrip_graph_default_valued_attrs.<locals>._strip_node_default_valued_attrs  s    {{))  -F~UN!)!4!4!6 &	:		:	>9%&  
--
ra   TN)	rT   rh   ri   rj   r0   rf   r=   meta_info_defstripped_default_attrs)r   function_defr   r=   function_node_defr   s        @rB    strip_graph_default_valued_attrsr     s     $..66?? Dl7C..334D$ !**// /h$X./ %..66?? :l)22 :&'89::
 9=.5ra   c	                 |   |r2t        |t        j                        st        dt	        |       d      | r<t        | t
        j                  j                        st        dt	        |        d      |r2t        |t        j                        st        dt	        |       d      |r2t        |t        j                        st        dt	        |       d      |xs t        j                         }t        j                         }	| st
        j                  j                         } t        j                  | _        t        j                   | _        |	j$                  j'                  |        |s,|	j(                  j'                  |j+                  d             n|	j(                  j'                  |       t-        |	j$                  j.                  j0                        dk(  r8|	j$                  j.                  j'                  t3        |	j(                               |rt5        |	       |r|	j6                  j'                  |       ||}
n|j9                         }
|
D ]_  }|rK|t        j:                  j<                  k(  r.t        j>                  |      }tA        |	|||| ||      g	       PtA        |	||||
       a |	S )a  Construct and returns a `MetaGraphDef` protocol buffer.

  Args:
    meta_info_def: `MetaInfoDef` protocol buffer.
    graph_def: `GraphDef` protocol buffer.
    saver_def: `SaverDef` protocol buffer.
    collection_list: List of string keys to collect.
    graph: The `Graph` to create `MetaGraphDef` out of.
    export_scope: Optional `string`. Name scope to remove.
    exclude_nodes: An iterable of nodes or `string` node names to omit from all
      collection, or None.
    clear_extraneous_savers: Remove any preexisting SaverDefs from the SAVERS
        collection.  Note this method does not alter the graph, so any
        extraneous Save/Restore ops should have been removed already, as needed.
    strip_default_attrs: Boolean. If `True`, default-valued attributes will be
        removed from the NodeDefs. For a detailed guide, see
        [Stripping Default-Valued Attributes](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md#stripping-default-valued-attributes).

  Returns:
    MetaGraphDef protocol buffer.

  Raises:
    TypeError: If the arguments are not of the correct proto buffer type.
  r   rH   z:meta_info_def must be of type MetaInfoDef. Received type: z3graph_def must be of type GraphDef. Received type: z3saver_def must be of type SaverDef. Received type: T)
add_shapesr   )r   r:   r   r   )r   r:   r   )!r|   r   r   r   r   r	   MetaGraphDefMetaInfoDefr   rI   r
   SaverDefr   r   __version__tensorflow_version__git_version__tensorflow_git_versionr   	MergeFromrT   as_graph_deflenstripped_op_listr7   ru   r   r   get_all_collection_keys	GraphKeysSAVERSget_from_proto_functionr   )r   rT   r   r   r   r:   r   clear_extraneous_saversstrip_default_attrsr   clistctype
from_protos                rB   create_meta_graph_defr     s   H :eSYY/

6tE{m1EG G:m&4&A&A&M&MO
	}-.a	12 2 z)Y-?-?@
	y/*!	-. . z)Y-?-?@
y/*!	-. .
 
*3((*% "..0.	"//;;=M &.%9%9-")1)A)A-&((7 
&&u'9'9T'9'JK&&y1 		%	%	6	6	9	9:a?  11;;">#;#;<>
 $^4 &&y1  E))+E 6e5CMM,@,@#@..u5j$&2'4,6y,A+B	D $&2'466 
ra   c           	         t        j                         }t        j                  |       st	        d|  d      t        j
                  | d      5 }|j                         }ddd       	 |j                         t        j                  dk(  rt        j                  |dd       |S # 1 sw Y   GxY w# t        $ r Y nw xY w	 t        j                  j                  d      |       t        j                  dk(  rt        j                  |dd       |S # t        j                   $ r }t	        d|  d	t#        |       d      d}~ww xY w)
a  Reads a file containing `MetaGraphDef` and returns the protocol buffer.

  Args:
    filename: `meta_graph_def` filename including the path.

  Returns:
    A `MetaGraphDef` protocol buffer.

  Raises:
    IOError: If the file doesn't exist, or cannot be successfully parsed.
  zFile does not exist. Received: rH   rE   Nbiglittlezutf-8rF   rG   )r	   r   r   rJ   rK   rL   rM   rN   sys	byteorderbst%swap_tensor_content_in_graph_functionrO   r   rP   decoderQ   rR   )rS   r   rU   rV   rW   s        rB   read_meta_graph_filer   U  s,    "..0.			X	&
3H:Q?
@@~~h% 668L	""<0
}}	//%P  
 		>l))'2NC
}}	//%P 
 
		 >
&xj3q6(!<
==>s7   B&)<B2 &B/2	B>=B>AD E&EEr;   c                      yNTr   r   s    rB   <lambda>r        ra   c           	      *    t        | ||||||      d   S )a|  Recreates a `Graph` saved in a `MetaGraphDef` proto.

  This function takes a `MetaGraphDef` protocol buffer as input. If
  the argument is a file containing a `MetaGraphDef` protocol buffer ,
  it constructs a protocol buffer from the file content. The function
  then adds all the nodes from the `graph_def` field to the
  current graph, recreates the desired collections, and returns a dictionary of
  all the Variables imported into the name scope.

  In combination with `export_scoped_meta_graph()`, this function can be used to

  * Serialize a graph along with other Python objects such as `QueueRunner`,
    `Variable` into a `MetaGraphDef`.

  * Restart training from a saved graph and checkpoints.

  * Run inference from a saved graph and checkpoints.

  Args:
    meta_graph_or_file: `MetaGraphDef` protocol buffer or filename (including
      the path) containing a `MetaGraphDef`.
    clear_devices: Boolean which controls whether to clear device information
      from graph_def. Default false.
    graph: The `Graph` to import into. If `None`, use the default graph.
    import_scope: Optional `string`. Name scope into which to import the
      subgraph. If `None`, the graph is imported to the root name scope.
    input_map: A dictionary mapping input names (as strings) in `graph_def` to
      `Tensor` objects. The values of the named input tensors in the imported
      graph will be re-mapped to the respective `Tensor` values.
    unbound_inputs_col_name: Collection name for looking up unbound inputs.
    restore_collections_predicate: a predicate on collection names. A collection
      named c (i.e whose key is c) will be restored iff
      1) `restore_collections_predicate(c)` is True, and
      2) `c != unbound_inputs_col_name`.

  Returns:
    A dictionary of all the `Variables` imported into the name scope.

  Raises:
    ValueError: If the graph_def contains unbound inputs.
  r   )-import_scoped_meta_graph_with_return_elements)meta_graph_or_filer<   r   import_scope	input_mapunbound_inputs_col_namerestore_collections_predicates          rB   import_scoped_meta_graphr  z  s-    ` 
7-i<
>>?
A Ara   c                      yr  r   r  s    rB   r  r    r  ra   c           
         t        j                         rt        d      t        | t        j
                        r| }nt        |       }|r|j                  j                         D ]  \  }	}
|	|k(  s|
j                  d      }t        |
|      }|j                  rtrBt        |j                  D cg c]  }t        j                  |       c}      t              k7  r0t        ddj                  fd|j                  D              z         n |xs t!        j"                         }|j%                         5  d}|j&                  j)                  d      r|j&                  j*                  }|j,                  }|r|j.                  D ]	  }d|_         |j3                  |xs dd	
      }t5        j6                  ||xs |||      }|j&                  j8                  }|sd}n+t;        j<                  |      t;        j<                  d      k\  }g }t         j>                  j@                  |j                  v rP|jC                  t         j>                  j@                  |j                  t         j>                  j@                     f       t        |j                  j                               D ]6  \  }	}|	t         j>                  j@                  k7  s$|jC                  |	|f       8 i }|D ]  \  }	}
|	|k(  r ||	      s|
j                  d      }|tE        jF                  d|	       @t!        jH                  |	      }|	t         j>                  jJ                  k(  r-t!        jH                  t         j>                  jL                        }|r|dk(  rt!        jN                  |	      }|	t         j>                  jP                  v r|
jR                  j                  D ]s  }|jU                  |d      }|K |       }|jW                  |       |s"|	t         j>                  j@                  k(  |_,         |||      }|||<   |j[                  |	|       u g|
jR                  j                  D ]4  } |       }|jW                  |       |j[                  |	 |||             6 t        |
|      }|	t\        v rtE        j^                  d|	|       |dk(  rJ|j                  D ]9  }|ja                  t!        jb                  ||            }|j[                  |	|       ; 0|dk(  r.|j                  D ]  }|j[                  |	te        |              c|j                  D ](  }|j[                  |	t!        jb                  ||             *  i }|jg                  t         j>                  jL                  |      }|D ]%  }||t!        jh                  |jj                  |      <   ' 	 ddd       ||fS c c}w # 1 sw Y   fS xY w)a  Imports graph from `MetaGraphDef` and returns vars and return elements.

  This function takes a `MetaGraphDef` protocol buffer as input. If
  the argument is a file containing a `MetaGraphDef` protocol buffer ,
  it constructs a protocol buffer from the file content. The function
  then adds all the nodes from the `graph_def` field to the
  current graph, recreates the desired collections, and returns a dictionary of
  all the Variables imported into the name scope.

  In combination with `export_scoped_meta_graph()`, this function can be used to

  * Serialize a graph along with other Python objects such as `QueueRunner`,
    `Variable` into a `MetaGraphDef`.

  * Restart training from a saved graph and checkpoints.

  * Run inference from a saved graph and checkpoints.

  Args:
    meta_graph_or_file: `MetaGraphDef` protocol buffer or filename (including
      the path) containing a `MetaGraphDef`.
    clear_devices: Boolean which controls whether to clear device information
      from graph_def. Default false.
    graph: The `Graph` to import into. If `None`, use the default graph.
    import_scope: Optional `string`. Name scope into which to import the
      subgraph. If `None`, the graph is imported to the root name scope.
    input_map: A dictionary mapping input names (as strings) in `graph_def` to
      `Tensor` objects. The values of the named input tensors in the imported
      graph will be re-mapped to the respective `Tensor` values.
    unbound_inputs_col_name: Collection name for looking up unbound inputs.
    restore_collections_predicate: a predicate on collection names. A collection
      named c (i.e whose key is c) will be restored iff
      1) `restore_collections_predicate(c)` is True, and
      2) `c != unbound_inputs_col_name`.
    return_elements:  A list of strings containing operation names in the
      `MetaGraphDef` that will be returned as `Operation` objects; and/or
      tensor names in `MetaGraphDef` that will be returned as `Tensor` objects.

  Returns:
    A tuple of (
      dictionary of all the `Variables` imported into the name scope,
      list of `Operation` or `Tensor` objects from the `return_elements` list).

  Raises:
    ValueError: If the graph_def contains unbound inputs.

  zQExporting/importing meta graphs is not supported when eager execution is enabled.r   zOGraph contains unbound inputs: %s. Must provide these inputs through input_map.,c              3   T   K   | ]  }r|vrt        j                  |       ! y wr[   )r   r,   )r   r?   r	  s     rB   r   z@import_scoped_meta_graph_with_return_elements.<locals>.<genexpr>  s0      PG#$&/1I3E  &}}Q/PGs   %(Nr   r"   F)mark_as_used)r0   r	  producer_op_listreturn_elementsTz1.9z6Cannot identify data type for collection %s. Skipping.rw   )r  zThe saved meta_graph is possibly from an older release:
'%s' collection should be of type 'byte_list', but instead is of type '%s'.r{   rx   r   )6r   executing_eagerlyr   r|   r	   r   r   r   r2   
WhichOneofr   r   rp   r   r,   joinr   r   
as_defaultr   r   r   rT   rf   r8   unique_namer   import_graph_defr   packaging_versionparser   TRAINABLE_VARIABLESr-   r   errorr   METRIC_VARIABLESGLOBAL_VARIABLESr   _VARIABLE_COLLECTIONSrw   rq   rN   	trainableadd_to_collection_COMPAT_COLLECTION_LISTr   as_graph_elementprepend_name_scoper~   r   r.   r0   )r  r<   r   r  r	  r
  r  r  r   r   r   r   fieldr?   r  input_graph_defrf   scope_to_prepend_to_namesimported_return_elements
tf_versionvariables_have_trainablesorted_collectionsr   variable_objectsr   r   variabler   col_opvar_list	variabless       `                          rB   r  r    s   p  
 3 4 4"N$?$?@'N)*<=N&55;;= W	'	'!!&)&;;ekk:FMM!$:;9 EGJxx PG(-PG HGG H H
 	  
*3((*%  uL##,,-?@'55FF$..O !&& $ !& 1 1 !2 !0  (8877)' )  --@@J!% 
!
!*
-1B1H1H1O
O 
 
}}((N,I,II==,,(()J)JKMN ^::@@BC 0
U	11	1!!3,/0
 * DOW	'	'*3/'d	N	..s3j
 
..	.001O1OP
	,2237
#--555))// 3e'++E48H le##E*- $'#--*K*K#K#&?Ah(0u%##C2!3$ ))// DeLE!!%(##Z(ACDD &))
//!"%t- ;{{ 1e++&&u.GHJF##C01 \! {{ 5e##CU45 {{ Oe##S++E3LMOOEDOL H$$S]]%C%C+D % FI LJKhs##AFF,EFGLiuLn 
+	++I ;uLn 
+	++s   /W'
9FW,LW,,W8c                 	   t        j                         r||t        d      |xs t        j                         }d}g }|s|s|r|rt        j                         }|j                  j                  |j                         |j                  j                  |j                         |rt        ||      }|j                  D ]E  }t        |j                  ||      st        ||||      }|j                  j                  |g       G |}nt        j                         }|j                  j                  |j                          d}|rt        |j#                         |      }t%        |j&                        D ]>  }t        |j&                  |   j                  ||      s)|j&                  |   }t        |j(                  |||      }|j                  j                  |g       |j*                  rd|j                  d   j,                  vsJ |j                  d   j,                  d   j.                  j0                  j                  |j*                  D cg c]   }|j3                         j5                         " c}       ||j(                  j7                         z  }|dk\  s|dk  s2t        d| d	       |j9                  ||       |r*|j;                  |       |D ]  }|j=                  ||        i }|j?                  t        j@                  jB                  |
      }|D ]3  }t        |||      s||t        jD                  |j                  |      <   5 tG        d|||||||	d|}| rQtI        jJ                  |tL        jN                  jQ                  |       tL        jN                  jS                  |       |       |
rtL        jN                  jU                  |       \  }}djW                  |d      }g }|jX                  j                  D ]D  }t        jZ                  |j                  |      }|j]                  d|j_                  |      f       F ta        jb                  |      } tI        jJ                  | tL        jN                  jQ                  |      tL        jN                  jS                  |      |       ||fS c c}w )a2  Returns `MetaGraphDef` proto. Optionally writes it to filename.

  This function exports the graph, saver, and collection objects into
  `MetaGraphDef` protocol buffer with the intention of it being imported
  at a later time or location to restart training, run inference, or be
  a subgraph.

  Args:
    filename: Optional filename including the path for writing the
      generated `MetaGraphDef` protocol buffer.
    graph_def: `GraphDef` protocol buffer.
    graph: The `Graph` to export. If `None`, use the default graph.
    export_scope: Optional `string`. Name scope under which to extract
      the subgraph. The scope name will be stripped from the node definitions
      for easy import later into new name scopes. If `None`, the whole graph
      is exported.
    as_text: If `True`, writes the `MetaGraphDef` as an ASCII proto.
    unbound_inputs_col_name: Optional `string`. If provided, a string collection
      with the given name will be added to the returned `MetaGraphDef`,
      containing the names of tensors that must be remapped when importing the
      `MetaGraphDef`.
    clear_devices: Boolean which controls whether to clear device information
      before exporting the graph.
    saver_def: `SaverDef` protocol buffer.
    clear_extraneous_savers: Remove any Saver-related information from the
        graph (both Save/Restore ops and SaverDefs) that are not associated
        with the provided SaverDef.
    strip_default_attrs: Set to true if default valued attributes must be
      removed while exporting the GraphDef.
    save_debug_info: If `True`, save the GraphDebugInfo to a separate file,
      which in the same directory of filename and with `_debug` added before the
      file extension.
    **kwargs: Optional keyed arguments, including meta_info_def and
        collection_list.

  Returns:
    A `MetaGraphDef` proto and dictionary of `Variables` in the exported
    name scope.

  Raises:
    ValueError: When the `GraphDef` is larger than 2GB.
    ValueError: When executing in Eager mode and either `graph_def` or `graph`
      is undefined.
  NzQExporting/importing meta graphs is not supported when Eager Execution is enabled.)r<   r   _output_shapesl        z3GraphDef cannot be larger than 2GB. Received size: rH   r  )rT   r   r:   r   r   r   r   )as_textz{name}{ext}z.debug)r0   extr"   r   )2r   r  r   r   r   r   rI   r   r4   rh   r   rf   r   r0   rC   r   graph_def_versionsr   rp   _nodes_by_idr=   outputsr1   r   shape	get_shapeas_protoByteSize_copy_functions_to_graph_defclear_collectionr#  r   r   r   r.   r   r   write_graphospathdirnamebasenamesplitextformatrT   r&  r-   get_operation_by_namer   create_graph_debug_info_def)!rS   rT   r   r:   r6  r
  r<   r   r   r   save_debug_infokwargsr   r;   new_graph_defr=   new_node_defbytesizer   r   outputr@   r1  r2  r?   scoped_meta_graph_defr0   r   debug_filenameops_to_exportrf   scoped_op_namegraph_debug_infos!                                    rB   export_scoped_meta_graphrU  }  s   p  )*?*/*;
 3 4 4

*3((*%-.,((*m%%i&8&89$$Y%6%67	 4Y	Jnn 4(|]K"8\>1>@,



#
#\N
3	4
  i $$&i!!%":":;h	 4U5G5G5I5>@ **+ /# 2 23 7 < < , -/ $$S)%u~~|^-:<(
..



+]]#9>>"+=+B+BBBBNN2##$45::@@GG<AMMIK28  "++-IK L
enn--/
/('"hl""*1./ //& ((H=
 45 <! 7;< (""3==#A#A)5 # 7) ?aA|];=>hs##AFFL9:? 0 !5- 	 
!
"	
   *gdA$++8+Dn
 m'1166 P$//		<Hb%"="=n"MNOP -HH
 

''//.
)
''

>
*	 
	((IKs   %S	
c                     |xs t        j                         }|xs t        j                         }||k(  r| |k(  rt        d| d| d|  d| d	      t        | |      \  }}t	        |||      }|S )a  Copies a sub-meta_graph from one scope to another.

  Args:
    from_scope: `String` name scope containing the subgraph to be copied.
    to_scope: `String` name scope under which the copied subgraph will reside.
    from_graph: Optional `Graph` from which to copy the subgraph. If `None`, the
      default graph is use.
    to_graph: Optional `Graph` to which to copy the subgraph. If `None`, the
      default graph is used.

  Returns:
    A dictionary of `Variables` that has been copied into `to_scope`.

  Raises:
    ValueError: If `from_scope` and `to_scope` are the same while
      `from_graph` and `to_graph` are also the same.
  zq'from_scope' and 'to_scope' need to be different when performing copy in the same graph. Received: 'from_graph': z, 'to_graph': z, 'from_scope': z, 'to_scope': rH   )r:   r   )r   r  )r   r   r   rU  r  )
from_scopeto_scope
from_graphto_graphorig_meta_graphr1  s         rB   copy_scoped_meta_graphr\  $  s    & 4S224*0..0(8
h 6
 00:| <$$,: .&&0\z	L M M 7Z1/8%o,43;=( 
/ra   )F)NNNN)	NNNNNNNFF)NNNNFr;   FNFFF)NN)B__doc__r#   	packagingr   r  os.pathrB  r)   r   google.protobuf.any_pb2r   google.protobufr   tensorflow.core.frameworkr   r   r   tensorflow.core.protobufr	   r
   tensorflow.python.clientr   r   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r   r   r   r   r   r   tensorflow.python.lib.ior   tensorflow.python.platformr   r   tensorflow.python.utilr   r+   r   LOCAL_VARIABLESMODEL_VARIABLESr  r$  rC   rX   rn   ru   r   r   r   r   r   r   r   r   r   r   r  r  rU  r\  r   ra   rB   <module>rl     sx    '  2  	 
 ' ' 4 / 0 3 . ? + ? ; 0 0 7 + . 0 , < ) +  ==88==88==99; 
-`B&?R'4,? 66rD@ 378<)-HV,=^ )-$($(*. $'+(,27.3l^"L ,1#'*.'+5E<L2An 
,#3K,\ '+'+#'*.%*5E+0'+5:16-2d)P 6:"ra   