我是 Tensorflow 的新手,并且一直在使用 Tensorflow 对象检测 API 对 SSD 进行实验。我可以成功训练一个模型,但默认情况下,它只保存最后 n 个检查点。我想改为保存损失最低的最后 n 个检查点(我假设这是最好的指标)。
我找到了 tf.estimator.BestExporter,它导出了一个 saved_model.pb 和变量。但是,我还没有弄清楚如何加载保存的模型并对其进行推理。在 checkpoiont 上运行 models/research/object_detection/export_inference_graph.py 后,我可以轻松加载检查点并使用对象检测 jupyter notebook 对其运行推理:https : //github.com/tensorflow/models/blob/master/research /object_detection/object_detection_tutorial.ipynb
我找到了有关加载保存模型的文档,并且可以加载这样的图表:
with tf.Session(graph=tf.Graph()) as sess:
tags = [tag_constants.SERVING]
meta_graph = tf.saved_model.loader.load(sess, tags, PATH_TO_SAVED_MODEL)
detection_graph = tf.get_default_graph()
Run Code Online (Sandbox Code Playgroud)
但是,当我将该图与上述 jupyter 笔记本一起使用时,出现错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-17-9e48f0d04df2> in <module>
7 image_np_expanded = np.expand_dims(image_np, axis=0)
8 # Actual detection.
----> 9 output_dict = run_inference_for_single_image(image_np, detection_graph)
10 # Visualization of the results of a detection.
11 vis_util.visualize_boxes_and_labels_on_image_array(
<ipython-input-16-0df86999596e> in run_inference_for_single_image(image, graph)
31 detection_masks_reframed, 0)
32
---> 33 image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
34 # image_tensor = tf.get_default_graph().get_tensor_by_name('serialized_example')
35
~/anaconda3/envs/sb/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in get_tensor_by_name(self, name)
3664 raise TypeError("Tensor names are strings (or similar), not %s." %
3665 type(name).__name__)
-> 3666 return self.as_graph_element(name, allow_tensor=True, allow_operation=False)
3667
3668 def _get_tensor_by_tf_output(self, tf_output):
~/anaconda3/envs/sb/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
3488
3489 with self._lock:
-> 3490 return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
3491
3492 def _as_graph_element_locked(self, obj, allow_tensor, allow_operation):
~/anaconda3/envs/sb/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
3530 raise KeyError("The name %s refers to a Tensor which does not "
3531 "exist. The operation, %s, does not exist in the "
-> 3532 "graph." % (repr(name), repr(op_name)))
3533 try:
3534 return op.outputs[out_n]
KeyError: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来加载保存的模型或将其转换为推理图?
谢谢!
Tensorflow 检测 API 在导出过程中支持不同的输入格式,如文件export_inference_graph.py 的文档中所述:
image_tensor:接受形状为 [None, None, None, 3] 的 uint8 4-D 张量encoded_image_string_tensor:接受形状为 [None] 的一维字符串张量,其中包含编码的 PNG 或 JPEG 图像。如果提供 1 张以上的图像,则图像分辨率应相同。tf_example:接受形状为 [None] 的一维字符串张量,其中包含序列化的 TFExample 原型。如果提供 1 张以上的图像,则图像分辨率应相同。所以你应该检查你是否使用了image_tensorinput_type。所选输入节点将在导出模型中命名为“输入”。所以我想image_tensor:0用inputs(或者也许inputs:0)替换会解决你的问题。
另外我想推荐一个有用的工具来运行带有几行代码的导出模型:tf.contrib.predictor.from_saved_model. 以下是如何使用它的示例:
import tensorflow as tf
import cv2
img = cv2.imread("test.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_rgb = np.expand_dims(img, 0)
predict_fn = tf.contrib.predictor.from_saved_model("./saved_model")
output_data = predict_fn({"inputs": img_rgb})
print(output_data) # detector output dictionary
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4123 次 |
| 最近记录: |