[英]Use saved_model.pb from Google Cloud automl to Predict in Python Locally
I have trained model using google cloud vision and downloaded saved_mode.pb
file.我已经使用谷歌云视觉训练了 model 并下载
saved_mode.pb
文件。
Instead of using the Google Cloud AutoML Containers' Tutorial to start a docker container, I am interested in detecting objects in my own python code without any docker machine.我没有使用Google Cloud AutoML 容器的教程来启动 docker 容器,而是有兴趣在我自己的 python 代码中检测对象,而无需任何 docker 机器。 The signature is:
签名是:
inputs['image_bytes'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: encoded_image_string_tensor:0
inputs['key'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: key:0
Which generated the error below: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]
这产生了以下错误:
[Unable to decode bytes as JPEG, PNG, GIF, or BMP]
2020-04-22 22:11:41.785038: W tensorflow/core/common_runtime/base_collective_executor.cc:216] BaseCollectiveExecutor::StartAbort Invalid argument: assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP]
[[{{node map/while/decode_image/cond_jpeg/cond_png/cond_gif/Assert_1/Assert}}]]
Traceback (most recent call last):
File "predict.py", line 29, in <module>
print(infer(image_bytes = tf.convert_to_tensor([str(image_bytes)]), key = tf.convert_to_tensor(["123"])))
File "C:\Users\DiYing\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1081, in __call__
return self._call_impl(args, kwargs)
File "C:\Users\DiYing\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1121, in _call_impl
return self._call_flat(args, self.captured_inputs, cancellation_manager)
File "C:\Users\DiYing\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 1224, in _call_flat
ctx, args, cancellation_manager=cancellation_manager)
File "C:\Users\DiYing\anaconda3\lib\site-packages\tensorflow_core\python\eager\function.py", line 511, in call
ctx=ctx)
File "C:\Users\DiYing\anaconda3\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
six.raise_from(core._status_to_exception(e.code, message), None)
File "<string>", line 3, in raise_from
My code is below:我的代码如下:
import tensorflow as tf
import io
import base64
export_path = '.'
loaded = tf.saved_model.load(".")
infer = loaded.signatures["serving_default"]
path = './57.png'
with io.open(path, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
print(infer(image_bytes = tf.convert_to_tensor([str(encoded_image)]), key = tf.convert_to_tensor(["123"])))
I have also tried using the code below, with no luck.我也尝试过使用下面的代码,但没有成功。
with io.open(path, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
image_bytes2 = {'b64': str(encoded_image)}
print(infer(image_bytes = tf.convert_to_tensor([str(image_bytes2)]), key = tf.convert_to_tensor(["123"])))
inputs['image_bytes'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: Placeholder:0
inputs['key'] tensor_info:
dtype: DT_STRING
shape: (-1)
name: Placeholder_1:0
image_bytes should be a byte sequence. image_bytes 应该是一个字节序列。
import cv2
import tensorflow as tf
loaded = tf.saved_model.load(export_dir='./')
print(list(loaded.signatures.keys()))
infer = loaded.signatures["serving_default"]
img = cv2.imread('./doragon6.png')
rst,bts = cv2.imencode('.png', img)
bimg = [bts.tobytes()]
out = infer(key=tf.constant('123'), image_bytes=tf.constant(bimg))
print(out)
['serving_default']
['serving_default']
{'scores': <tf.Tensor: shape=(1, 14), dtype=float32, numpy= array([[0.05823032, 0.05273063, 0.60351413, 0.04105317, 0.06426468, 0.0380617, 0.05016823, 0.05680692, 0.5262339, 0.03903484, 0.05968711, 0.06426468, 0.05968711, 0.8394657 ]], dtype=float32)>, 'labels': <tf.Tensor: shape=(1, 14), dtype=string, numpy= array([[b'orange', b'yellow', b'purplish_red', b'green', b'gray', b'yellow_green', b'purple', b'pink', b'red', b'black', b'white', b'blue_green', b'blue', b'brown']], dtype=object)>, 'key': <tf.Tensor: shape=(), dtype=string, numpy=b'123'>}
{'scores': <tf.Tensor: shape=(1, 14), dtype=float32, numpy= array([[0.05823032, 0.05273063, 0.60351413, 0.04105317, 0.06426468, 0.0380617, 0.05016823, 0.05680692, 0.5262339, 0.03903484, 0.05968711, 0.06426468, 0.05968711, 0.8394657 ]], dtype=float32)>, 'labels': <tf.Tensor: shape=(1, 14), dtype=string, numpy= array([[b'orange', b'yellow' , b'purplish_red', b'green', b'gray', b'yellow_green', b'purple', b'pink', b'red', b'black', b'white', b'blue_green' , b'blue', b'brown']], dtype=object)>, 'key': <tf.Tensor: shape=(), dtype=string, numpy=b'123'>}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.