3

Trying to save a Keras .h5 file containing weights to Tensorflow .pb file

# I keep getting the error:  ValueError: Cannot find the variable that is an input to the ReadVariableOp.
frozen_graph = freeze_session(K.get_session(),
                              output_names=[out.op.name for out in model.keras_model.output])

0

frozen_graph = freeze_session(K.get_session(), output_names=[out.op.name for out in model.keras_model.output])

I got an error:

ValueError Traceback (most recent call last) in 1 frozen_graph = freeze_session(K.get_session(), ----> 2 output_names=[out.op.name for out in model.keras_model.output])

in freeze_session(session, keep_var_names, output_names, clear_devices) 26 node.device = "" 27 frozen_graph = tf.graph_util.convert_variables_to_constants( ---> 28 session, input_graph_def, output_names, freeze_var_names) 29 return frozen_graph

~/anaconda3/envs/env_name/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs) 322 'in a future version' if date is None else ('after %s' % date), 323 instructions) --> 324 return func(*args, **kwargs) 325 return tf_decorator.make_decorator( 326 func, new_func, 'deprecated',

~/anaconda3/envs/env_name/lib/python3.6/site-packages/tensorflow/python/framework/graph_util_impl.py in convert_variables_to_constants(sess, input_graph_def, output_node_names, variable_names_whitelist, variable_names_blacklist) 300 source_op_name = get_input_name(map_name_to_node[source_op_name]) 301 if map_name_to_node[source_op_name].op != "VarHandleOp": --> 302 raise ValueError("Cannot find the variable that is an input " 303 "to the ReadVariableOp.") 304

ValueError: Cannot find the variable that is an input to the ReadVariableOp.

JohnnyUtah
  • 350
  • 4
  • 11
  • I assume `freeze_session` is the function posted [here](https://stackoverflow.com/a/45466355/1782792)? I'm not sure where that error comes from... Are you using TensorFlow 2.x? [This issue](https://github.com/tensorflow/tensorflow/issues/30864) suggests that `convert_variables_to_constants` may not work as expected in 2.x... – jdehesa Oct 18 '19 at 17:42

1 Answers1

3

I just ran into this same issue, adding

import keras.backend as K
k.set_learning_phase(0)

which sets the learning phase to testing mode, was the solution.