gwe*_*e14 5 python opencv keras tensorflow
我正在编写教程https://www.pyimagesearch.com/2019/03/11/liveness-detection-with-opencv/。本教程完美运行,我想使用 .pb 和 .pbtxt 文件将 keras 模型转换为 tensorflow 模型。我搜索了许多教程如何在 Python 中将 keras 模型转换为 .pb 和 pb.txt。我创建了一个函数“freeze_session”,它可以在这些文件中进行转换。无论如何,当我尝试使用 OpenCV 导入这些文件时,它会触发几个错误。
我第一次导入文件 pb 和 pbtxt 时收到此错误:
错误:(-215:断言失败)const_layers.insert(std::make_pair(name, li)).second in function 'cv::dnn::dnn4_v20190122::`anonymous-namespace'::addConstNodes'
我在论坛上创建过,他们说我们必须在导出图形之前使用此行代码将学习阶段设置为使用值 0 进行测试:
K.set_learning_phase(0)
在此之后,我成功地仅使用 pb 文件导入了 TensorFlow:
cvNet = cv.dnn.readNetFromTensorflow('./model/tf_model.pb')
在这些之后,当我想使用以下命令测试 TensorFlow 神经网络时,会触发一个新错误:
blob2 = cv2.dnn.blobFromImage(cv2.resize(face, (32, 32)), 1.0)
cvNet.setInput(blob2)
detections2 = cvNet.forward()
Run Code Online (Sandbox Code Playgroud)
错误:
detections2 = cvNet.forward() cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) 可以'不在函数'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'中创建“Shape”类型的层“flatten_1/Shape”
我在几个论坛上看到我们必须删除节点和优化图,但它对我不起作用:
https://answers.opencv.org/question/183507/opencv-dnn-import-error-for-keras-pretrained-vgg16-model/
事实上,我无法使用两个文件导入模型,导入仅适用于 pb 文件,但不适用于检测。
我如何轻松地进行这种转换?为什么有时用户只用 pb 文件导入,有时用 pb 和 pbtxt 导入?
import tensorflow as tf
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
"""
Freezes the state of a session into a pruned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
pruned so subgraphs that are not necessary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
from tensorflow.python.framework.graph_util import convert_variables_to_constants
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
# Graph -> GraphDef ProtoBuf
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph
from keras import backend as K
K.set_learning_phase(0) # all new operations will be in test mode from now on
sess = K.get_session()
model = load_model("liveness2.model")
frozen_graph = freeze_session(K.get_session(), output_names=[out.op.name for out in model.outputs])
# inputs: ['conv2d_1_input']
print('inputs: ', [input.op.name for input in model.inputs])
# outputs: ['activation_6/Softmax']
print('outputs: ', [output.op.name for output in model.outputs])
tf.train.write_graph(frozen_graph, "model", "tf_model.pb", as_text=False)
tf.train.write_graph(frozen_graph, "model", "tf_model.pbtxt", as_text=True)
net = cv2.dnn.readNetFromTensorflow("model/tf_model.pb");
Run Code Online (Sandbox Code Playgroud)
detections2 = cvNet.forward() cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:524: error: (-2:Unspecified error) 可以'不在函数'cv::dnn::dnn4_v20190122::LayerData::getLayerInstance'中创建“Shape”类型的层“flatten_1/Shape”
| 归档时间: |
|
| 查看次数: |
1423 次 |
| 最近记录: |