简体   繁体   中英

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.

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

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.

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']

{'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'>}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM