为了账号安全,请及时绑定邮箱和手机立即绑定

AutoML Vision Edge 中的 saved_model 未正确加载

AutoML Vision Edge 中的 saved_model 未正确加载

潇湘沐 2022-06-28 11:04:41
在以 TFLite 格式导出模型时,我一直在使用 AutoML Vision Edge 完成一些图像分类任务,并取得了很好的效果。但是,我只是尝试导出 saved_model.pb 文件并使用 Tensorflow 2.0 运行它,似乎遇到了一些问题。代码片段:import numpy as npimport tensorflow as tfimport cv2from tensorflow import kerasmy_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 行,在 print(my_model.summary()) AttributeError: 'AutoTrackable' 对象没有属性 'summary'我不确定这是我如何导出模型的问题,还是我的代码加载模型的问题,或者这些模型是否与 Tensorflow 2.0 或某种组合不兼容。任何帮助将不胜感激!
查看完整描述

1 回答

?
慕娘9325324

TA贡献1783条经验 获得超5个赞

我已经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))


查看完整回答
反对 回复 2022-06-28
  • 1 回答
  • 0 关注
  • 212 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号