caffe上手:如何导出caffemodel参数
来源:互联网 发布:阿里云 aws对比 编辑:程序博客网 时间:2024/05/23 00:01
最近在github上看到一个导出mnist的model参数的Matlab和Python的脚本,比较简单,以后可能用得到,记录一下。
load_caffemodel.py: 使用python脚本加载lenet的参数。
conv*.mat: 在blob数据中相同结构定义的层的参数。
parse_param.m: Matlab脚本,将加载到的每层参数保存出来的mat文件整合成一个parsed_param.mat文件。
parsed_param.mat: Matlab保存生成的mat文件。
如果github打不开的话,我这里把这几个脚本的代码贴出来。
================================================================================
load_caffemodel.py
parse_param.m
========================================分割线===================================
基于caffe 的卷积神经网络模型训练后得到的权值是通过Google Protobuf来存储的后缀名为.caffemodel的二进制文件,这类文件一般很难直接打开进行权值的读取和修改。有的时候我们希望直观的看到网络中每个神经元的权值,或者希望更改网络的部分结构来得到新的结构进行finetune。在这样的情况下我们就需要对caffemodel文件进行操作。好在caffe的Python接口提供了针对caffemodel文件的修改方法,用户可以从caffemodel文件中读取参数,并对参数进行修改以得到新的caffemodel文件。
1、读取caffemodel里的权值
首先caffe要先进行pycaffe 的编译并安装好pycaffe的依赖环境。然后用下述python文件可以查看caffemodel文件中各个层的参数,以查看lenet-5权值为例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
需要注意的是,这样的方法只能读取有训练参数层的权值,对于下采样、激活层等无训练参数的层,无法得到其层内权值(因为层内根本没有权值)。
2、修改caffemodel内的权值并保存为新的caffemodel
有的时候我们需要修改原caffemodel来得到新的caffemodel文件用于finetune等工作,可通过下述python文件实现,以修改lenet-5权值为例。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
但是这种方法的弊端也很明显,这种方法只能在原有的结构上进行权值的修改,而不能对原有结构进行修改,比如,删除原有结构中的某一层或增加新的层,或更改原有层的维度等。
在caffe 的官方文档中提供了一种修改caffemodel文件的方法,具体参考http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/net_surgery.ipynb(需翻墙)。这里面的案例是讲caffenet的后三个全连接层(fc6/fc7/fc8)改成全卷基层(fc6-conv/fc7-conv/fc8-conv)以形成新的网络权值文件bvlc_caffenet_full_conv.caffemodel。值得注意的是,这里面的案例也仅仅是将原来后三个全连接层的权值“摊平”(文中的写法为flat)并赋给卷积层,由于全连接层和卷基层的参数个数是相同的,因此这个案例本质上也属于权值的进一步修改。文中最后有这么一段话:
Note that this model isn’t totally appropriate for sliding-window detection since it was trained for whole-image classification. Nevertheless it can work just fine. Sliding-window training and finetuning can be done by defining a sliding-window ground truth and loss such that a loss map is made for every location and solving as usual. (This is an exercise for the reader.)
- caffe上手:如何导出caffemodel参数
- caffe上手:如何导出caffemodel参数
- 如何导出caffemodel参数
- Caffe小玩意(2)-从caffemodel中导出参数
- 从caffemodel中导出参数
- 从caffemodel中导出参数
- Caffe学习:使用pycaffe读取caffemodel参数
- Caffe学习:使用pycaffe读取caffemodel参数
- Caffe学习:使用pycaffe读取caffemodel参数
- Caffe学习:使用pycaffe读取caffemodel参数
- 深度学习caffe框架(1):如何快速上手caffe?
- caffe上手
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe的python接口学习(8):caffemodel中的参数及特征的抽取
- caffe的python接口学习:caffemodel中的参数及特征的抽取(转载)
- pbzip2: error while loading shared libraries: libbz2.so.1.0: cannot open shared object file
- jvm学习记录--04 垃圾回收概念与垃圾回收算法
- 两种方法快速搞定高版本的cad图怎么在低版本中打开
- [P3811][模板]乘法逆元
- Deep Face Recognition读书笔记
- caffe上手:如何导出caffemodel参数
- Map四种获取key和value值的方法,以及对map中的元素排序(转载)
- 导入excel
- Java面向对象习题
- 深入剖析ThreadLocal
- leetcode--Longest Palindromic Subsequence
- 最佳股票交易时机
- Ethernet,TCP,IP协议简介
- 【安全】Android O 中的 Seccomp 过滤器