Tensorflow服务:无法进行base64解码

时间:2018-11-29 13:00:40

标签: tensorflow base64 serving

我使用瘦包resnet_v2_152来训练分类模型。 然后将其导出到.pb文件以提供服务。 由于输入是图像,因此将使用网络安全的base64编码对其进行编码。看起来像:

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

然后我使用base64对图像进行编码,使得:

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'/', '_')

我的帖子数据结构如下: post_data = { 'signature_name': 'predict', 'instances':[ { 'inputs': { 'b64': s } }] } 最后,我将HTTP请求发布到此服务器:

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

它给了我

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

我想知道怎么会遇到?对此有解决方案吗?

2 个答案:

答案 0 :(得分:0)

使用python3时,我遇到了同样的问题。我通过在编码函数中添加“ b”(类似于字节的对象)而不是默认str来解决此问题: b'{"instances" : [{"b64": "%s"}]}' % base64.b64encode( dl_request.content)

希望有帮助,请参见this answer,以获取更多信息。

答案 1 :(得分:0)

这个问题已经解决了。

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

我们看到输入带有'b64'标志,这表明tensorflow服务将使用base64代码解码。 它属于tensorflow服务内部方法。 因此,占位符:

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

将直接接收输入数据的二进制格式,而不是base64格式。

最后,

decoded = tf.decode_base64(serialized_tf_example)

不是必需的。