model无法正确加载

model无法正确加载

本文介绍了AutoML Vision Edge中的saved_model无法正确加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been using AutoML Vision Edge for some image classification tasks with great results when exporting the models in TFLite format. However, I just tried exporting the saved_model.pb file and running it with Tensorflow 2.0 and seem to be running into some issues.

代码段:

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文件的目录.这是我所看到的:

'saved_model' is the directory containing my downloaded saved_model.pb file. Here's what I'm seeing:

我不确定这与我如何导出模型,代码与加载模型有关,或者这些模型与Tensorflow 2.0或某些组合不兼容.

I'm not sure if it's an issue with how I'm exporting the model, or with my code to load the model, or if these models aren't compatible with Tensorflow 2.0, or some combination.

任何帮助将不胜感激!

推荐答案

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

I've got my saved_model.pb working outside of the docker container (for object detection, not classification - but they should be similar, change the outputs and maybe the inputs for tf 1.14), here is how:

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查找输入内容.

I used netron to find my input.

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

这篇关于AutoML Vision Edge中的saved_model无法正确加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:58