Skip to content

Where graph.Import() method imports graph? #483

Closed

Description

@aFewThings

I have tried GraphDef.Parser.ParseFrom(File.ReadAllBytes(model_path)) and tf.import_graph_def(). It works fine to import .pb file but, at some reason, I want to use graph.Import() method.
When I use import method shown as below, there is no imported graph.
It's not imported to default graph either. I checked the frozen graph model that has operations also.

// just simple logistic graph.
var model_path = Path.Combine(model_folder, "frozen_graph_logistic.pb"); 

//var graph_def = GraphDef.Parser.ParseFrom(File.ReadAllBytes(model_path));
//tf.import_graph_def(graph_def);

var graph = tf.Graph();
bool check = graph.Import(model_path);
Debug.Log("" + check); // print true

foreach (var op in graph.get_operations())
    Debug.Log("" + op.name); // print nothing
Debug.Log("Done1");

foreach (var op in tf.get_default_graph().get_operations())
    Debug.Log("" + op.name); // print nothing also
Debug.Log("Done2");

I used Tensorflow 1.13 (python) to build frozen graph and Tensorflow.NET 0.12 to run.
I tested in Visual Studio 2017.

Activity

  1. HanClinto commented on Jan 23, 2020

    @HanClinto

    I ran into the same issue. I eventually worked around it by loading my graph this way:

    var model_path = Path.Combine(model_folder, "frozen_graph_logistic.pb"); 
    var graph_def = GraphDef.Parser.ParseFrom(File.ReadAllBytes(model_path));
    
    var graph = new Graph().as_default();
    tf.import_graph_def(graph_def);
    
  2. added a commit that references this issue on Jan 25, 2020
  3. arthurvb commented on May 7, 2020

    @arthurvb

    @HanClinto Solved my issue as well.
    Tried to run the example on https://github.com/SciSharp/SciSharp-Stack-Examples/blob/tf.net-1.15/src/TensorFlowNET.Examples/ImageProcessing/InceptionArchGoogLeNet.cs but got a KeyError when doing get_operation_by_name("import/input");

  4. caj-github commented on Nov 11, 2020

    @caj-github

    graph.get_operations() return nothing when using graph.Import to load pb model, but it work well for inference, tf.import_graph_def() throw an exception "Attempt to read or write to protected memory.This usually indicates that other memory is corrupted."

    at last, I solved the problem like this:

    using System.Linq;

    var model_path = Path.Combine(model_folder, *.pb");
    var graph = new Graph();
    graph.Import(model_path);
    Operation[] ops = graph.ToArray();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions