gpt4 book ai didi

python - OpenCV DNN因keras DenseNet121而失败

转载 作者:行者123 更新时间:2023-12-02 16:37:27 25 4
gpt4 key购买 nike

http://answers.opencv.org/question/183507/opencv-dnn-import-error-for-keras-pretrained-vgg16-model/之后,我正在尝试让密集网络在openCV DNN上工作,但得到:

"error: OpenCV(3.4.2) /io/opencv/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp:712: error: (-2:Unspecified error) Tensor's data type is not supported in function 'getTensorContent'"


import numpy as np
from keras import applications
from keras import backend as K
import cv2 as cv
import tensorflow as tf

model = applications.densenet.DenseNet121(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
sess = K.get_session()

print(model.input, model.outputs)
## Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) [<tf.Tensor 'fc1000/Softmax:0' shape=(?, 1000) dtype=float32>]

from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib

MODEL_PATH = 'out'
MODEL_NAME = 'test'
input_node_name = 'input_1'
output_node_name = 'fc1000/Softmax'
!rm -rf {MODEL_PATH}/

tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pb', as_text=False)
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pbtxt')
tf.train.Saver().save(sess, f'{MODEL_PATH}/{MODEL_NAME}.chkp')

freeze_graph.freeze_graph(f'{MODEL_PATH}/{MODEL_NAME}_graph.pbtxt',
None, False,
f'{MODEL_PATH}/{MODEL_NAME}.chkp',
output_node_name,
"save/restore_all",
"save/Const:0",
f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb',
True, "")

graph_def = tf.GraphDef()
with tf.gfile.Open(f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb', "rb") as f:
graph_def.ParseFromString(f.read())

output_graph_def = optimize_for_inference_lib.optimize_for_inference(
graph_def, [input_node_name], [output_node_name], tf.float32.as_datatype_enum)

with tf.gfile.GFile(f'{MODEL_PATH}/opt_{MODEL_NAME}.pb', "wb") as f:
f.write(output_graph_def.SerializeToString())

# Strip Const nodes.
for i in reversed(range(len(graph_def.node))):
if graph_def.node[i].op == 'Const':
del graph_def.node[i]
# for attr in ['T', 'data_format', 'Tshape', 'N', 'Tidx', 'Tdim',
# 'use_cudnn_on_gpu', 'Index', 'Tperm', 'is_training',
# 'Tpaddings']:
# if attr in graph_def.node[i].attr:
# del graph_def.node[i].attr[attr]

# Save stripped model.
tf.train.write_graph(graph_def, "", f'{MODEL_PATH}/stripped_{MODEL_NAME}.pbtxt', as_text=True)

net = cv.dnn.readNetFromTensorflow(f'{MODEL_PATH}/opt_{MODEL_NAME}.pb', f'{MODEL_PATH}/stripped_{MODEL_NAME}.pbtxt')
## error: OpenCV(3.4.2) /io/opencv/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp:712: error: (-2:Unspecified error) Tensor's data type is not supported in function 'getTensorContent'

很难说出是哪个节点导致了错误。
有什么想法吗?
干杯

tensorflow 1.12.0
opencv 3.4.3

最佳答案

尽管由于FusedBatchNorm,我仍然无法使optimize_for_inference正常工作,但感谢@dkurt和https://github.com/keras-team/keras/issues/6775的反馈,它们解释了keras学习阶段。 您必须在加载模型之前设置学习阶段!

使用set_learning_phase(0)加载模型:

import numpy as np
from keras import applications
from keras import backend as K
import tensorflow as tf

K.set_learning_phase(0) ##
model = applications.densenet.DenseNet121(input_shape=(224, 224, 3), weights='imagenet', include_top=True)
sess = K.get_session()

print(model.input, model.outputs)
## Tensor("input_1:0", shape=(?, 224, 224, 3), dtype=float32) [<tf.Tensor 'fc1000/Softmax:0' shape=(?, 1000) dtype=float32>]

冻结它:
from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib

MODEL_PATH = 'out'
MODEL_NAME = 'test'
input_node_name = 'input_1'
output_node_name = 'fc1000/Softmax'
!rm -rf {MODEL_PATH}/

tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pb', as_text=False)
tf.train.write_graph(sess.graph_def, MODEL_PATH, f'{MODEL_NAME}_graph.pbtxt')
tf.train.Saver().save(sess, f'{MODEL_PATH}/{MODEL_NAME}.chkp')

freeze_graph.freeze_graph(f'{MODEL_PATH}/{MODEL_NAME}_graph.pbtxt',
None, False,
f'{MODEL_PATH}/{MODEL_NAME}.chkp',
output_node_name,
"save/restore_all",
"save/Const:0",
f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb',
True, "")

然后用dnn加载它:
import cv2 as cv
net = cv.dnn.readNetFromTensorflow(f'{MODEL_PATH}/frozen_{MODEL_NAME}.pb')

# Smoke test
inp = np.ones([1, 3, 224, 224]).astype(np.float32)
net.setInput(inp)
dnn_out = net.forward()
print(dnn_out.shape, dnn_out[0,:5])
## (1, 1000) [2.0760612e-04 2.6876197e-04 5.9680151e-05 5.5908626e-05 1.4762023e-04]

如前所述,由于FusedBatchNorm,我无法使optimize_for_inference正常工作: WARNING:tensorflow:Didn't find expected Conv2D input to 'conv2_block1_0_bn/FusedBatchNorm_1'
opencv-4.0.0/modules/dnn/src/tensorflow/tf_importer.cpp:497: error: (-2:Unspecified error) Input layer not found: conv2_block1_1_bn/FusedBatchNorm_1 in function 'connect'
因此,如果您知道解决方案,请告诉我。谢谢

关于python - OpenCV DNN因keras DenseNet121而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53493296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com