1

我正在尝试将 python 代码移植到 TensorflowSharp。它从这里加载一个 TF2 保存的模型,该模型获取图像的特征向量。Python代码:

# 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)

我的 C# 代码看起来不同,我没有自动执行它的“模块”功能。我在这里得到 NullReferenceException: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);

该模型似乎没有输入,并且与教程中的示例模型看起来不同。我应该怎么做才能使用它?如何找到输入和输出张量的名称?

使用 Netron 进行模型可视化

提前致谢。

4

0 回答 0