简体   繁体   English

使用 Google Cloud automl 中的 saved_model.pb 本地预测 Python

[英]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.

相关问题 在本地测试 (Python) Google Cloud Function 时出现应用程序上下文错误 - Application context errors when locally testing a (Python) Google Cloud Function Google Cloud AutoML Vision 支持哪些图像位类型? - What image bit types are supported by Google Cloud AutoML Vision? 如何在 App Engine Flex (Python 3.7) 中本地模拟 Google 云任务服务? - How to emulate Google cloud tasks service locally in app engine flex (Python 3.7)? 需要使用Python 2.7版本中的“Google-cloud-ndb”库 - Need to use the "Google-cloud-ndb" library in Python 2.7 version 将使用 google-api-python-client 库的 Python 脚本部署为 Google Cloud Function - Deploying Python script that use google-api-python-client library as Google Cloud Function 使用 Google Kubernetes 引擎在本地运行项目时,VS Code 使用了错误的 Google Cloud 帐户 - VS Code using wrong Google Cloud account when running project locally with Google Kubernetes Engine 谷歌云存储从 Python 生成器上传 - Google Cloud Storage streaming upload from Python generator 在 Python/Django 中从 Google Cloud Storage/ Buckets 上传和检索文件 - Upload and retrieve files from Google Cloud Storage/ Buckets in Python/Django 您可以将 Google Cloud 模拟器与 Terraform 一起使用吗? - Can you use the Google Cloud emulator with Terraform? Google Cloud Speech 使用自定义词汇表 - Google Cloud Speech use custom vocabulary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM