小编典典

AutoML Vision Edge中的saved_model无法正确加载

python

以TFLite格式导出模型时,我一直在使用AutoML Vision
Edge进行某些图像分类任务,并获得了不错的效果。但是,我只是尝试导出saved_model.pb文件并在Tensorflow
2.0中运行它,似乎遇到了一些问题。

程式码片段:

import numpy as np
import tensorflow as tf
import cv2

from tensorflow import keras

my_model = tf.keras.models.load_model('saved_model')
print(my_model)
print(my_model.summary())

“ saved_model”是包含我下载的save_model.pb文件的目录。这是我所看到的:

2019-10-18 23:29:08.801647:I tensorflow / core / platform /
cpu_feature_guard.cc:142]您的CPU支持该TensorFlow二进制文件未编译为使用的指令:AVX2 FMA
2019-10-18 23:29:08.829017 :我tensorflow / compiler / xla / service /
service.cc:168] XLA服务0x7ffc2d717510在平台Host上执行计算。设备:2019-10-18
23:29:08.829038:I tensorflow / compiler / xla / service / service.cc:175]
StreamExecutor设备(0):主机,默认版本回溯(最近一次调用为最新):文件“ classify_in_out_tf2。
py”,第81行,在print(my_model.summary())中AttributeError:“ AutoTrackable”对象没有属性“
summary”

我不确定这与我如何导出模型,代码与加载模型有关,或者这些模型与Tensorflow 2.0或某些组合不兼容是否存在问题。

任何帮助将不胜感激!


阅读 212

收藏
2021-01-20

共1个答案

小编典典

我已经saved_model.pb在docker容器之外进行了工作(用于对象检测,而不是分类-但它们应该是相似的,更改的输出以及输入tf 1.14),这是如何做的:

张量流1.14.0:

图像编码为字节

import cv2
import tensorflow as tf
cv2.imread(filepath)
flag, bts = cv.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
with tf.Session(graph=tf.Graph()) as sess:
    tf.saved_model.loader.load(sess, ['serve'], 'directory_of_saved_model')
    graph = tf.get_default_graph()
    out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
            sess.graph.get_tensor_by_name('detection_scores:0'),
            sess.graph.get_tensor_by_name('detection_boxes:0'),
            sess.graph.get_tensor_by_name('detection_classes:0')],
           feed_dict={'encoded_image_string_tensor:0': inp})

图像作为numpy数组

import cv2
import tensorflow as tf
import numpy as np
with tf.Session(graph=tf.Graph()) as sess:
    tf.saved_model.loader.load(sess, ['serve'], 'directory_of_saved_model')
    graph = tf.get_default_graph()
    # Read and preprocess an image.
    img = cv2.imread(filepath)
    # Run the model
    out = sess.run([sess.graph.get_tensor_by_name('num_detections:0'),
                    sess.graph.get_tensor_by_name('detection_scores:0'),
                    sess.graph.get_tensor_by_name('detection_boxes:0'),
                    sess.graph.get_tensor_by_name('detection_classes:0')],
                   feed_dict={'map/TensorArrayStack/TensorArrayGatherV3:0': img[np.newaxis, :, :, :]})

我使用netron查找我的输入。

张量流2.0:

import cv2
import tensorflow as tf
img = cv2.imread('path_to_image_file')
flag, bts = cv2.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
loaded = tf.saved_model.load(export_dir='directory_of_saved_model')
infer = loaded.signatures["serving_default"]
out = infer(key=tf.constant('something_unique'), image_bytes=tf.constant(inp))
2021-01-20