Pieter Van Der Berg
Pieter Van Der Berg

Reputation: 31

How to create a TensorProto in c#?

This is a snipped of the c# client I created to query the tensorflow server I set up using this tutorial: 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);

I found out that most classes needed to be generated from proto files using protoc

The only thing which I cant find is how to construct the TensorProto. The error I keep getting is : Additional information: Status(StatusCode=InvalidArgument, Detail="tensor parsing error: images")

There is a sample client (https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/inception_client.py) byt my Python skills are not sufficient to understand the last bit.

Upvotes: 2

Views: 822

Answers (1)

Kellen
Kellen

Reputation: 611

I also implemented that client in another language (Java).

Try to change

jpgeproto.Dtype = DataType.DtStringRef; 

to

jpgeproto.Dtype = DataType.DtString;

You may also need to add a tensor shape with a dimension to your tensor proto. Here's my working solution in Java, should be similar in 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); 

Upvotes: 2

Related Questions