OpenCV DNN模块 获取导入模型各层信息

转载请注明作者和出处: http://blog.csdn.net/john_bh/

文章目录


OpenCV DNN模块支持下面框架的预训练模型的前馈网络(预测图)使用:

  • Caffe
  • Tensorflow
  • Torch
  • DLDT
  • Darknet

同时还支持自定义层解析、非最大抑制操作、获取各层的信息等。OpenCV加载模型的通用API为:

Net cv::dnn::readNet(
	const String & 	model,
	const String & 	config = "",
	const String & 	framework = "" 
)

model二进制训练好的网络权重文件,可能来自支持的网络框架,扩展名为如下:

  • .caffemodel (Caffe, http://caffe.berkeleyvision.org/)
  • .pb (TensorFlow, https://www.tensorflow.org/)
  • .t7 | .net (Torch, http://torch.ch/)
  • .weights (Darknet, https://pjreddie.com/darknet/)
  • .bin (DLDT, https://software.intel.com/openvino-toolkit)

config针对模型二进制的描述文件,不同的框架配置文件有不同扩展名:

  • .prototxt (Caffe, http://caffe.berkeleyvision.org/)
  • .pbtxt (TensorFlow, https://www.tensorflow.org/)
  • .cfg (Darknet, https://pjreddie.com/darknet/)
  • .xml (DLDT, https://software.intel.com/openvino-toolkit)

framework显示声明参数,说明模型使用哪个框架训练出来的

C++:

#include 
#include 
#include 

using namespace cv;
using namespace cv::dnn;
using namespace std;

int main(int argc, char** argv) {
	string bin_model = "D:/projects/opencv_tutorial/data/models/googlenet/bvlc_googlenet.caffemodel";
	string protxt = "D:/projects/opencv_tutorial/data/models/googlenet/bvlc_googlenet.prototxt";

	// load CNN model
	Net net = dnn::readNet(bin_model, protxt);

	// 获取各层信息
	vector<String> layer_names = net.getLayerNames();
	for (int i = 0; i < layer_names.size(); i++) {
		int id = net.getLayerId(layer_names[i]);
		auto layer = net.getLayer(id);
		printf("layer id:%d, type: %s, name:%s \n", id, layer->type.c_str(), layer->name.c_str());
	}
	return 0;
}

Python:

"""
DNN模块 获取导入模型各层信息
"""

import cv2 as cv
import numpy as np

bin_model = "bvlc_googlenet.caffemodel"
protxt = "bvlc_googlenet.prototxt"

# load CNN model
net = cv.dnn.readNet(bin_model, protxt)

# 获取各层信息
layer_names = net.getLayerNames()
for name in layer_names:
    id = net.getLayerId(name)
    layer = net.getLayer(id)
    print("layer id : {}, type : {}, name : {}"
          .format(id, layer.type, layer.name))

print("successfully loaded model...")

cv.waitKey(0)
cv.destroyAllWindows()

结果:
OpenCV DNN模块 获取导入模型各层信息_第1张图片

你可能感兴趣的:(OpenCV)