Tensorflow serving: Unable to base64 decode

1.6k views Asked by At

I use the slim package resnet_v2_152 to train a classification model. Then it is exported to .pb file to provide a service. Because the input is image, so it would be encoded with web-safe base64 encoding. It looks like:

serialized_tf_example = tf.placeholder(dtype=tf.string, name='tf_example') decoded = tf.decode_base64(serialized_tf_example)

I then encode an image with base64 such that:

img_path = '/Users/wuyanxue/Desktop/not_emoji1.jpeg'

img_b64 = base64.b64encode(open(img_path, 'rb').read())

s = str(img_b64, encoding='utf-8')

s = s.replace('+', '-').replace(r'/', '_')

My post data is as structured as follow: post_data = { 'signature_name': 'predict', 'instances':[ { 'inputs': { 'b64': s } }] } Finally, I post a HTTP request to this server:

res = requests.post('server_address', json=post_data)

It gives me:

'{ "error": "Failed to process element: 0 key: inputs of \\\'instances\\\' list. Error: Invalid argument: Unable to base64 decode" }'

I want to know how it could be encountered? And are there some solutions for that?

2

There are 2 answers

0
Roei Bahumi On

I had the same issue when using python3. I solved it by adding a 'b' - a byte-like object instead of the default str to the encode function: b'{"instances" : [{"b64": "%s"}]}' % base64.b64encode( dl_request.content)

Hope that helps, please see this answer for extra info.

0
JavaFresher On

This question is already solved.

post_data = {
'signature_name': 'predict',
'instances':[ { 
  'inputs': 
      { 'b64': s }
  }]
}

We see that inputs is with 'b64' flag, which illustrates that tensorflow serving will decode s with base64 code. It belongs to the tensorflow serving internal method. So, the placeholder:

serialized_tf_example = tf.placeholder(dtype=tf.string, name='tf_example')

will directly receive the binary format of the input data BUT NOT base64 format.

So, finally,

decoded = tf.decode_base64(serialized_tf_example)

is NOT necessary.