0

I got trouble to re-train frozen graph Due to Const in Graph Actually I checked out many reference codes like this and this Tried everything but no lock for me.

My environment below

python 3.7.7
TensorFlow 2.1.0

My code below

  graph_def = optimize_graph(graph)
  
  graph = tf.Graph()
  with tf.compat.v1.Session(graph=graph):
      tf.graph_util.import_graph_def(graph_def, name='')
  
  g = graph
  to_convert = [op.name for op in g.get_operations() if op.type == "Const"]
  const_var_name_pairs = []

  with g.as_default():
      for name in to_convert:
          tensor = g.get_tensor_by_name('{}:0'.format(name))
          with tf.compat.v1.Session() as sess:
              tensor_as_numpy_array = sess.run(tensor_cast(tensor, tensor.dtype))
          var_name = '{}_turned_var'.format(name)
          var = tf.Variable(name=var_name, dtype=tensor.dtype, shape=tensor.get_shape(),
                      initial_value=tensor_as_numpy_array
                      )
          var.assign(tensor_as_numpy_array)
          const_var_name_pairs.append((name, var_name))
    
  ge_graph = ge.Graph(g.as_graph_def())

  for const_name, var_name in const_var_name_pairs:
      const_op = ge_graph._node_name_to_node[const_name]
      var_reader_op = ge_graph._node_name_to_node[var_name]
      ge.swap_outputs(ge.sgv(const_op), ge.sgv(var_reader_op))
  
  detection_training_graph = ge_graph.to_tf_graph()

The problem is,

/var/folders/2d/pqn3pzcj0qs_t6_wgtmd8p9r0000gn/T/ipykernel_50693/993434432.py in load_graph_model_and_signature_with_swap(model_dir, compat_mode)
     66       #var_reader_op = name_to_op[var_name + '/Read/ReadVariableOp']
     67       #print(var_reader_op)
---> 68       ge.swap_outputs(ge.sgv(const_op), ge.sgv(var_reader_op))
     69 
     70 

ValueError: Dtypes <dtype: 'float32'> and <dtype: 'resource'> of tensors Const:0 and Const_turned_var:0 are not compatible.

When I replace variable by using below code

  for const_name, var_name in const_var_name_pairs:
      const_op = ge_graph._node_name_to_node[const_name]
      var_reader_op = ge_graph._node_name_to_node[var_name + '/Read/ReadVariableOp']
      ge.swap_outputs(ge.sgv(const_op), ge.sgv(var_reader_op))

The model is modified well but, I could not inference by using this model due to initialize problem

FailedPreconditionError:  Error while reading resource variable Const_99_turned_var_load_43352 from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/Const_99_turned_var_load_43352/N10tensorflow3VarE does not exist.
     [[node Const_99_turned_var/Read/ReadVariableOp (defined at var/folders/2d/pqn3pzcj0qs_t6_wgtmd8p9r0000gn/T/ipykernel_50693/3656424118.py:3) ]] [Op:__inference_pruned_44901]

I'm struggling few weeks, I never know how TensorFlow Graph has been working inside. There is no exact document about it also. I read every document on Tensorflow page relevant. Please help me out from the dark

YoungIn Lee
  • 45
  • 1
  • 8

1 Answers1

0

I finally decided to make my own graph model from tfjs model.json and weight. and it works

please visit on it if you are interested in this topic

https://github.com/reiui9/facemesh_face-landmark-detector

YoungIn Lee
  • 45
  • 1
  • 8