Skip to content

Latest commit

 

History

History
 
 

code_121

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

读取DNN模型各层信息

概述

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

  • Caffe
  • Tensorflow
  • Torch
  • DLDT
  • Darknet

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

cv2.dnn.readNet(model,  # 模型
            	config = "", 
            	framework = "" )

其中:

代码

import cv2
import numpy as np

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

# load CNN model
net = cv2.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 : %d, type : %s, name: %s"%(id, layer.type, layer.name))

print("successfully")

输出

layer id : 1, type : Convolution, name: conv1/7x7_s2
layer id : 2, type : ReLU, name: conv1/relu_7x7
layer id : 3, type : Pooling, name: pool1/3x3_s2
layer id : 4, type : LRN, name: pool1/norm1
layer id : 5, type : Convolution, name: conv2/3x3_reduce
layer id : 6, type : ReLU, name: conv2/relu_3x3_reduce
layer id : 7, type : Convolution, name: conv2/3x3
layer id : 8, type : ReLU, name: conv2/relu_3x3
layer id : 9, type : LRN, name: conv2/norm2
layer id : 10, type : Pooling, name: pool2/3x3_s2
...
successfully