Serving: PredictResponse as JSON

Created on 27 Jun 2017  Â·  2Comments  Â·  Source: tensorflow/serving

Hello folks,
does anybody know how to turn a PredictResponse to JSON/Dictionary?

The response looks like this:

outputs { key: "classes" value { dtype: DT_STRING tensor_shape { dim { size: 1 } dim { size: 5 } } string_val: "giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca" string_val: "indri, indris, Indri indri, Indri brevicaudatus" string_val: "lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens" string_val: "gibbon, Hylobates lar" string_val: "sloth bear, Melursus ursinus, Ursus ursinus" } } outputs { key: "scores" value { dtype: DT_FLOAT tensor_shape { dim { size: 1 } dim { size: 5 } } float_val: 8.986181259155273 float_val: 5.365548610687256 float_val: 4.926231861114502 float_val: 2.988001823425293 float_val: 2.765580892562866 } }

Most helpful comment

In C++, Protocol Buffers has built-in support for Json serialization.

Here are a few lines of code from a project I wrote.

………..
………..
Status status = stub_->Predict(&context, predictRequest, &response);

if (status.ok()) {
………….
………….
google::protobuf::util::Status response_serialize_status =
google::protobuf::util::MessageToJsonString(
response, &serialized_result_object, jprint_options);

if (!response_serialize_status.ok()) {

………..
………..

The nice thing is it works directly on the response object returned by gRPC. You have to like the format it gives you, but for many things it is perfectly OK. Strings will be base64 encoded, so you will need to encode/decode them.

It looks like there may be something similar for Python.
https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.json_format-module

All 2 comments

In C++, Protocol Buffers has built-in support for Json serialization.

Here are a few lines of code from a project I wrote.

………..
………..
Status status = stub_->Predict(&context, predictRequest, &response);

if (status.ok()) {
………….
………….
google::protobuf::util::Status response_serialize_status =
google::protobuf::util::MessageToJsonString(
response, &serialized_result_object, jprint_options);

if (!response_serialize_status.ok()) {

………..
………..

The nice thing is it works directly on the response object returned by gRPC. You have to like the format it gives you, but for many things it is perfectly OK. Strings will be base64 encoded, so you will need to encode/decode them.

It looks like there may be something similar for Python.
https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.json_format-module

Great, thanks for the hint!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

daikankan picture daikankan  Â·  4Comments

OmriShiv picture OmriShiv  Â·  3Comments

sskgit picture sskgit  Â·  4Comments

brianschardt picture brianschardt  Â·  3Comments

dylanrandle picture dylanrandle  Â·  3Comments