gpt4 book ai didi

python - 来自 AutoML Vision Edge 的 saved_model 未正确加载

转载 作者:行者123 更新时间:2023-12-03 23:28:00 27 4
gpt4 key购买 nike

我一直在使用 AutoML Vision Edge 执行一些图像分类任务,在以 TFLite 格式导出模型时取得了很好的效果。但是,我只是尝试导出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”是包含我下载的saved_model.pb 文件的目录。这是我所看到的:

2019-10-18 23:29:08.801647: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-10-18 23:29:08.829017: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7ffc2d717510 executing computations on platform Host. Devices: 2019-10-18 23:29:08.829038: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): Host, Default Version Traceback (most recent call last): File "classify_in_out_tf2.py", line 81, in print(my_model.summary()) AttributeError: 'AutoTrackable' object has no attribute 'summary'



我不确定这是否与我导出模型的方式或加载模型的代码有关,或者这些模型是否与 Tensorflow 2.0 或某些组合不兼容。

任何帮助将不胜感激!

最佳答案

我有我的 saved_model.pb在 docker 容器之外工作(用于对象检测,而不是分类 - 但它们应该是相似的,更改输出,也许是 tf 1.14 的输入),方法如下:

tensorflow 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 来查找我的输入。

tensorflow 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))

关于python - 来自 AutoML Vision Edge 的 saved_model 未正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58461211/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com