c# - 如何在 C# 中创建 TensorProto?

标签 c# python tensorflow

这是我创建的 C# 客户端的一个片段,用于查询我使用本教程设置的 tensorflow 服务器:https://tensorflow.github.io/serving/serving_inception.html

        var channel = new Channel("TFServer:9000", ChannelCredentials.Insecure);

        var request = new PredictRequest();

        request.ModelSpec = new ModelSpec();
        request.ModelSpec.Name = "inception";

        var imgBuffer = File.ReadAllBytes(@"sample.jpg");

        ByteString jpeg = ByteString.CopyFrom(imgBuffer, 0, imgBuffer.Length);

        var jpgeproto = new TensorProto();
        jpgeproto.StringVal.Add(jpeg);
        jpgeproto.Dtype = DataType.DtStringRef;


        request.Inputs.Add("images", jpgeproto); // new TensorProto{TensorContent = jpeg});

        PredictionClient client = new PredictionClient(channel);

我发现大多数类需要使用protoc从proto文件生成

我唯一找不到的是如何构建 TensorProto。我不断收到的错误是:附加信息:Status(StatusCode=InvalidArgument, Detail="张量解析错误:图像")

有一个示例客户端 ( https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/inception_client.py ),但我的 Python 技能不足以理解最后一点。

最佳答案

我还用另一种语言(Java)实现了该客户端。

尝试改变

jpgeproto.Dtype = DataType.DtStringRef; 

jpgeproto.Dtype = DataType.DtString;

您可能还需要向张量原型(prototype)添加具有尺寸的张量形状。这是我在 Java 中的工作解决方案,在 C# 中应该类似:

TensorShapeProto.Dim dim = TensorShapeProto.Dim.newBuilder().setSize(1).build();
TensorShapeProto shape = TensorShapeProto.newBuilder().addDim(dim).build();
TensorProto proto = TensorProto.newBuilder()
                .addStringVal(ByteString.copyFrom(imageBytes))
                .setTensorShape(shape)
                .setDtype(DataType.DT_STRING)
                .build();
ModelSpec spec = ModelSpec.newBuilder().setName("inception").build();
PredictRequest r = PredictRequest.newBuilder()
                .setModelSpec(spec)
                .putInputs("images", proto).build();
PredictResponse response = blockingStub.predict(r); 

关于c# - 如何在 C# 中创建 TensorProto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911330/

相关文章:

c# - global.asax 中的 Application_OnStart 是否适用于缓存页面?

tensorflow:可微分索引可能吗?

machine-learning - 我可以使用 `tf.nn.dropout`来实现DropConnect吗?

machine-learning - 为什么我的带有 ReLU 的 1 隐藏层神经网络在 notMNIST 数据集上的准确率没有超过 18%?

c# - 为什么有些键一旦转换为字符串,就不会转换回其原始键码?

C# PDF 库来操作 PDF

c# - 如果填充文本框则启用按钮

python manage.py build_solr_schema 给出 ImportError : No module named markup

Java 等价于 Python repr()?

python - 如何检查 python 函数是否是 Tornado 生成器