【问题标题】:saved_model from AutoML Vision Edge not loading properlyAutoML Vision Edge 中的 saved_model 未正确加载
【发布时间】:2020-01-01 05:31:00
【问题描述】:

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

代码sn-p:

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] 您的 CPU 支持未编译此 TensorFlow 二进制文件以使用的指令:AVX2 FMA 2019-10-18 23:29:08.829017:我 tensorflow/compiler/xla/service/service.cc:168] XLA 服务 0x7ffc2d717510 在平台主机上执行计算。设备: 2019-10-18 23:29:08.829038:I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor 设备(0):主机,默认版本 回溯(最近一次通话最后): 文件“classify_in_out_tf2.py”,第 81 行,在 打印(my_model.summary()) AttributeError: 'AutoTrackable' 对象没有属性 'summary'

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

任何帮助将不胜感激!

【问题讨论】:

  • 为了确定,您是使用升级脚本 [1] 还是手动进行了更改? [1]:tensorflow.org/guide/upgrade
  • @Gurkomal 该模型是使用 Google 的 AutoML 工具生成的,并根据此文档导出为保存的模型:cloud.google.com/vision/automl/docs/export-edge 我对 TF 2 升级过程并不完全熟悉...你知道吗如果可以简单地升级导出的保存模型,还是需要更新实际的模型代码?
  • fwiw,我联系了 AutoML 团队,他们说该服务并非旨在导出在当前设置的 docker 容器之外工作的已保存模型。如果有人知道如何获取导出的保存模型并将其修改为在 docker 容器之外工作,那将非常有帮助。谢谢!

标签: python tensorflow google-cloud-automl


【解决方案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))

【讨论】:

  • 感谢您的彻底回应 - 很高兴听到其他人尝试这样做!两个快速跟进: 1) 在 netron 中查看图表时……它非常笨拙。关于找到正确的输入/输出部分有什么建议吗? 2) 你知道不用tobytes() 转换就可以运行TF 2.0 版本的方法吗?
  • 认为分享模型文件github.com/matt-virgo/TF_saved_model_test 可能会有所帮助,如果你能帮助我理解图中发生的事情,那将是一个巨大的帮助。谢谢!
  • 1) 在 tf 2.0 示例的过程中,我发现一种更简单的方法来查找输入/输出是来自 tensorflow 2.0 调用 print(infer.inputs) print(infer.outputs) - 这在你的情况下作为输入 @987654329 @ 和 Placeholder_1:0。第一个是图像,第二个是键名,只有在您想要键时才需要
  • 2) 我不知道如何在 tf 2.0 中使用 numpy 数组,我还没有弄清楚如何指定使用备用输入,输入应该与 tf 1.14 示例相同- 如果你弄清楚了,请告诉我
猜你喜欢
  • 2019-10-28
  • 2021-02-20
  • 1970-01-01
  • 2019-11-25
  • 2021-10-03
  • 2021-03-01
  • 1970-01-01
  • 2023-03-11
  • 2021-05-25
相关资源
最近更新 更多