【发布时间】:2019-03-12 14:08:54
【问题描述】:
我目前正在尝试使用 OpenCV 加载类似 tensorflow 的 U-net 模型。该模型是使用 Tensorflow 1.12.0 使用 Python 3.6.8 创建、训练和冻结的,我正在尝试使用 C++ 中的 OpenCV (3.4.2) 加载它(现在我正在测试在 Python 中使用 OpenCV 加载模型类似于我将在 C++ 中执行的操作)。
我正在使用 U-net 的这个 (https://github.com/jakeret/tf_unet) 实现。我的训练模型图如下所示:
在我拥有一个训练有素的模型后,我使用以下代码将其冻结:
import tensorflow as tf
import numpy as np
from tensorflow.python.tools import freeze_graph
# Freeze the graph
save_path=".../Unet/unet_trained/run_016/" #directory to model files
input_graph_path = save_path+'graph.pbtxt'#complete path to the input graph
checkpoint_path = save_path+'model_epoch99.ckpt' #complete path to the model's checkpoint file
input_saver_def_path = ""
input_binary = False
output_node_names = "output_map/output" #output node's name
restore_op_name = "save/restore_all"
filename_tensor_name = "save/Const:0"
output_frozen_graph_name = save_path+'frozen_model_.pb' # the name of .pb file you would like to give
clear_devices = False
freeze_graph.freeze_graph(input_graph_path, input_saver_def_path,
input_binary, checkpoint_path, output_node_names,
restore_op_name, filename_tensor_name,
output_frozen_graph_name, clear_devices, "")
下一步我要在终端上运行以下命令:
python3 strip_unused.py --input_graph frozen_model_run16.pb
--output_graph strip_model_run16.pb --input_node_names x
--output_node_names output_map/output --input_binary True --output_binary True
和
python3 optimize_for_inference.py --input strip_model_run16.pb
--output optimized_model_run16.pb --frozen_graph True --input_names x
--output_names output_map/output
(可选)最后,为了在 tensorboard 中可视化图形,我运行:
python3 import_pb_to_tensorboa.py --model_dir optimized_model_run16.pb
--log_dir unet_trained/frozenmodel/
优化后的模型图如下:
在这一切之后,我尝试通过以下方式使用 OpenCV 加载此模型:
import cv2
tensorflowNet = cv2.dnn.readNetFromTensorflow("/path/to/model/frozenmodel/
optimized_model_run16.pb")
但是我得到了这个错误
cv2.error: OpenCV(3.4.2) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:520: error: (-2:Unspecified error) More than one input is Const op in function 'getConstBlob'
我找不到解决方案或解释来解决它。
提前感谢您的帮助!
编辑:
如果我以这种方式加载模型(添加 .pbtxt 文件)
tensorflowNet = cv2.dnn.readNetFromTensorflow("/home/dev/optimized_model_run16.pb", "/home/dev/graph.pbtxt")
我得到一个不同的错误:
cv2.error: OpenCV(3.4.2) /io/opencv/modules/dnn/src/tensorflow/tf_importer.cpp:613: error: (-215:Assertion failed) const_layers.insert(std::make_pair(name, li)).second in function 'addConstNodes'
【问题讨论】:
-
预处理块有问题。在 optimize_for_inference.py 之后它必须是线性的
-
有什么问题?你能详细一点吗? @Nuzny
-
我猜你正在传递一个
layer,而你应该传递一个tensor。 -
我只是尝试在没有预处理块的情况下在 OpenCV 中加载模型(我使用 numpy 创建批处理数组并将其提供给第一个卷积块)并且错误仍然存在@SusmitAgrawal
标签: python c++ opencv tensorflow