0

我使用 slim 包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" }'

我想知道怎么会遇到?是否有一些解决方案?

4

2 回答 2

0

这个问题已经解决了。

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

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

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

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

所以,最后,

decoded = tf.decode_base64(serialized_tf_example)

没有必要。

于 2018-12-29T09:01:26.033 回答
0

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

希望对您有所帮助,请参阅此答案以获取更多信息。

于 2018-12-27T09:32:23.387 回答