Opencv中由 preds = net.forward() cv2.error: 引起的’cv::dnn::dnn4_v20191202::DataLayer::getMemoryShapes’错误

File "E:/python_user/dnn_blob/blob_from_images.py", line 27, in <module>
    preds = net.forward()
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:730: error: (-215:Assertion failed) inputs.size() == requiredOutputs in function 'cv::dnn::dnn4_v20191202::DataLayer::getMemoryShapes'

在使用Opencv做一个DNN 的项目中,原码是由Caffe框架编写,然后在此基础上将Caffe转为Tensorflow框架。
部分代码为:

# Caffe所需配置文件
net = cv2.dnn.readNetFromCaffe("bvlc_googlenet.prototxt",
	"bvlc_googlenet.caffemodel")
	...
# 图像数据预处理
image = cv2.imread(imagePaths[0])
resized = cv2.resize(image, (224, 224))
# image scalefactor size mean swapRB 
blob = cv2.dnn.blobFromImage(resized, 1, (224, 224), (104, 117, 123))
net.setInput(blob)
preds = net.forward()

改为:

net = cv2.dnn.readNetFromTensorflow(model= "best_vgg16.pb",
	config=None)
	...
# 图像数据预处理
image = cv2.imread(imagePaths[0])
resized = cv2.resize(image, (224, 224))
# image scalefactor size mean swapRB 
blob = cv2.dnn.blobFromImage(resized, 1, (224, 224), (104, 117, 123))
#blob = blob.astype(np.float32)
net.setInput(blob)
preds = net.forward()

后续更改适合tensorflow框架的数据类型。
参考链接:
OpenCV DNN(二)——Net

opencv dnn分类网络调用-python版
eltwise_layer.cpp:170: error: (-215:Assertion failed) inputs.size() >= 2 in function 'cv::dnn::Eltwi
opencv 加载tensorflow pb模型
https://stackoverflow.com/questions/65127290/cvnet-forward-error-215assertion-failed-inputs-size-requiredoutputs-i

GitHub 加速计划 / opencv31 / opencv
77.39 K
55.71 K
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:2 个月前 )
7be5181b Fixed KLEIDICV_SOURCE_PATH handling for external KleidiCV 1 天前
c3ca3f4f - 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐