migueldeicaza / TensorFlowSharp

TensorFlow API for .NET languages
MIT License
3.14k stars 578 forks source link

Invalid GraphDef Importing from Python Saved Model #374

Open jessebett opened 5 years ago

jessebett commented 5 years ago

I am trying to import the Glow demo from https://github.com/openai/glow/tree/master/demo

This model has is saved and downloadable according to their install script:

# Get model weights
curl https://storage.googleapis.com/glow-demo/large3/graph_optimized.pb > graph_optimized.pb

This provides the Graph Def, and in python is loaded in the usual way as seen in https://github.com/openai/glow/blob/master/demo/model.py

with tf.gfile.GFile(graph_path, 'rb') as f:
    graph_def_optimized = tf.GraphDef()
    graph_def_optimized.ParseFromString(f.read())

sess = tensorflow_session()
tf.import_graph_def(graph_def_optimized)

where graph_path points to the graph_optimized.pb from above.

However, trying to import from graph_optimized.pb or even opening it in model.py and saving it as a .bytes graph causes TFException: Invalid GraphDef in C#.

Can you let me know if there is any way to import this graph?

I am confused why python is able to read the graphdef and perform the functions inmodel.py from the information saved in this .pb file, but TensorFlowSharp cannot read this as a valid GraphDef.

csharpner commented 5 years ago

I'm having the same problem with an 86KB .pb file.

jeremy7710 commented 5 years ago

I freeze graph first, and save again. And it works. Link is below.

def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
    """
    Freezes the state of a session into a pruned computation graph.
    Creates a new computation graph where variable nodes are replaced by
    constants taking their current value in the session. The new graph will be
    pruned so subgraphs that are not necessary to compute the requested
    outputs are removed.
    @param session The TensorFlow session to be frozen.
    @param keep_var_names A list of variable names that should not be frozen,
                          or None to freeze all the variables in the graph.
    @param output_names Names of the relevant graph outputs.
    @param clear_devices Remove the device directives from the graph for better portability.
    @return The frozen graph definition.
    """
    graph = session.graph
    with graph.as_default():
        freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
        output_names = output_names or []
        output_names += [v.op.name for v in tf.global_variables()]
        input_graph_def = graph.as_graph_def()
        if clear_devices:
            for node in input_graph_def.node:
                node.device = ""
        frozen_graph = tf.graph_util.convert_variables_to_constants(
            session, input_graph_def, output_names, freeze_var_names)
        return frozen_graph

tf.train.write_graph(frozen_graph, "some_directory", "my_model.pb", as_text=False)

Link is here. https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb