Import graphDef files in Python and run


Is there any way to import graphdef in Python and run it?

I am implementing a graph optimizer, similar to a grappler pass but is implemented in the Python frontend. Thus, the python optimizer would be:

graph_to_optimize.pbtxt  ->  Python graph optimizing -> optimized_graph.pbtxt

Thus, in the C++ grappler pass, the above process could be implemented as :

// Dump graph
    DumpGraphDefToFile("graph_to_optimize", item.graph);
// Optimizing in Python
//     python_opt_process()
// Read optimized graph
    GrapplerItem tempitem;
    ReadGraphDefFromFile("optimized_graph.pbtxt", &tempitem.graph);
// Copy to optimized one
    CopyGraph(tempitem, optimized_graph);

The above code requires building TF from the source, I wish to avoid the building from the source. I want to ask if there is a pure Python way, to let users use the optimization process from stock TF. I.e., the user could directly import the graphdef from the file and replace the previous graph.

I found a way of import_graph_def. If this is the answer, could you offer an example of how could I import graphDef and let it run the training task?

Thanks in advance!

Let see if you could find this Python Grappler pass reference useful: