我正在使用Google AutoML Vision导出的分类模型,因此我只有一个saved_model.pb
变量,没有检查点等。我想将此模型图加载到本地TensorFlow安装中,以进行推理并继续训练更多图片。
主要问题:
此计划是否可行,即使用saved_model.pb
不带变量,检查点等的单个对象,并使用新数据训练结果图?
如果是:如何将输入的形状(?,)
编码为字符串的图像变成?
理想情况下,展望未来:培训部分需要考虑什么重要事项?
有关代码的背景信息:
为了读取图像,我使用与使用Docker容器进行推理时使用的相同方法,因此使用base64编码的图像。
要加载图形,我通过CLI(saved_model_cli show --dir input/model
)检查了需要设置图形的什么标签serve
。
为了让我使用输入张量的名字graph.get_operations()
,这让我Placeholder:0
对image_bytes并Placeholder:1_0
为关键(任意的字符串识别图像)。两者都有尺寸dim -1
import tensorflow as tf
import numpy as np
import base64
path_img = "input/testimage.jpg"
path_mdl = "input/model"
# input to network expected to be base64 encoded image
with io.open(path_img, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
# reshaping to (1,) as the expecte dimension is (?,)
feed_dict_option1 = {
"Placeholder:0": { np.array(str(encoded_image)).reshape(1,) },
"Placeholder_1:0" : "image_key"
}
# reshaping to (1,1) as the expecte dimension is (?,)
feed_dict_option2 = {
"Placeholder:0": np.array(str(encoded_image)).reshape(1,1),
"Placeholder_1:0" : "image_key"
}
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, ["serve"], path_mdl)
graph = tf.get_default_graph()
sess.run('scores:0',
feed_dict=feed_dict_option1)
sess.run('scores:0',
feed_dict=feed_dict_option2)
输出:
# for input reshaped to (1,)
ValueError: Cannot feed value of shape (1,) for Tensor 'Placeholder:0', which has shape '(?,)'
# for input reshaped to (1,1)
ValueError: Cannot feed value of shape (1, 1) for Tensor 'Placeholder:0', which has shape '(?,)'
如何得到输入形状(?,)
?
非常感谢。
是! 有可能我有一个应该相似的对象检测模型,我可以在tensorflow 1.14.0中如下运行它:
import cv2
cv2.imread(filepath)
flag, bts = cv.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
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})
我使用netron查找输入内容。
在tensorflow 2.0中甚至更容易:
import cv2
cv2.imread(filepath)
flag, bts = cv.imencode('.jpg', img)
inp = [bts[:,0].tobytes()]
saved_model_dir = '.'
loaded = tf.saved_model.load(export_dir=saved_model_dir)
infer = loaded.signatures["serving_default"]
out = infer(key=tf.constant('something_unique'), image_bytes=tf.constant(inp))
另外saved_model.pb
是不是frozen_inference_graph.pb
,请参阅:什么是差异frozen_inference_graph.pb和saved_model.pb?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句