如何在c#中创建TensorProto?

时间:2016-10-07 07:03:20

标签: c# python tensorflow

这是我为查询使用本教程设置的tensorflow服务器创建的c#客户端的剪辑: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。我一直得到的错误是:附加信息:状态(StatusCode = InvalidArgument,Detail =“tensor parsing error:images”)

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

1 个答案:

答案 0 :(得分:1)

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

尝试更改

jpgeproto.Dtype = DataType.DtStringRef; 

jpgeproto.Dtype = DataType.DtString;

您可能还需要为张量原型添加尺寸张量形状。这是我在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);