Dmi Dev
Dmi Dev

Reputation: 11

TensorflowSharp and model from hub without input

I'm trying to port a python code to TensorflowSharp. It loads a TF2 saved model from here which gets Feature vectors of images. Python code:

# This is required for the mobilenet model we are using
img  = tf.image.convert_image_dtype(img, tf.float32)[tf.newaxis, ...]

module = tf.saved_model.load(path)
# Calculate the image feature vector of the img
features = module(img)

My C# code looks differently and I don't have "module" function that does it automatically. I get NullReferenceException here: graph["input"][0]

TFGraph graph = new TFGraph();
TFTensor tensor;
tensor = TFTensor.CreateString(File.ReadAllBytes(filepath));

TFOutput input, output;
var tfSessionOptions = new TFSessionOptions();
var metaGraphUnused = new TFBuffer();
var session = TFSession.FromSavedModel(tfSessionOptions, null, dir, new[] { "serve" }, graph, metaGraphUnused);

ConstructGraphToNormalizeImage(out graph, out input, out output, 96, 96, 3);

// Execute that graph to normalize this one image
using (var session2 = new TFSession(graph))
{
    var normalized = session2.Run(
        inputs: new[] { input },
        inputValues: new[] { tensor },
        outputs: new[] { output });

    tensor = normalized[0];
}


var runner = session.GetRunner();
runner.AddInput(graph["input"][0], tensor);
runner.Fetch(graph["output"][0]);

// Run the model
var res_output = runner.Run();
var nums = (float[])res_output[0].GetValue(jagged: false);

The model seems to have no input and looks differently to sample models from tutorials. What should I do to be able to use it? How to find the names of the input and output tensors?

Model visualization with Netron

Thanks in advance.

Upvotes: 1

Views: 284

Answers (0)

Related Questions