要想了解caffe生成的caffemodel文件里的内容,我们就需要解析.caffemodel文件(caffemodel里不仅存储了权重和偏置等信息,还存储了整个训练网络的结构信息,即.prototxt信息,当然solver.prototxt信息是看不见的)。
1.单独查看
要是只是想看看权重信息正不正确(有时候学习率设置的太大,可能会导致梯度爆炸,但是又不确定的情况下,可以查看下caffemodel文件中的权重是否是nan,是的话说明梯度爆炸了),可以通过caffe中的python接口来快速查看,基本代码如下:
import caffe
import numpy as np
deploy='/home/b622/caffe-action_recog/evaluation/prototxt/pspnet101_VOC2012_deploy.prototxt'
model='/media/b622/My Passport/pspnet101resultbn/train_iter_100.caffemodel'
net = caffe.Net(deploy,model,caffe.TEST)
w=net.params['conv1_1_3x3_s2/bn'][0].data
print w
其中deploy后面的是你自己的网络的deploy.prototxt文件路径;model是训练好的caffemodel文件的路径;net.params中的'conv1_1_3x3_s2/bn'是deploy.prototxt文件中某一层的名字,即该层的'name:'后面的内容,caffe是根据这个名字来索引该层的权重信息的(我这里用的是PSPNet网络中的某一层)。
2.整体输出
如果想输出caffemodel中所有的信息,可以通过以下的小程序来解析:
(1)定义主函数文件
打开终端输入以下命令,创建一个.cpp文件:
gedit test.cpp
在弹出的文件中写入以下代码,并保存:
#include <caffe/caffe.hpp>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <iostream>
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
using namespace caffe;
using namespace std;
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::Message;
int main()
{
NetParameter proto;
ReadProtoFromBinaryFile("/home/b622/caffe-action_recog/evaluation/pspnet101_VOC2012.caffemodel", &proto);
WriteProtoToTextFile(proto, "test.txt");
return 0;
}
其中ReadProtoFromBinaryFile()为读取函数,WriteProtoToTextFile()为写入函数。
(2)定义CMakeLists.txt文件(注意不要改文件名)
cmake_minimum_required (VERSION 2.8)
project (test)
add_executable(test test.cpp)
include_directories ( /home/b622/caffe-action_recog/include
/usr/local/include
/usr/local/cuda/include
/usr/include )
target_link_libraries(test
/home/b622/caffe-action_recog/build/lib/libcaffe.so
/usr/lib/x86_64-linux-gnu/libglog.so
/usr/lib/x86_64-linux-gnu/libboost_system.so
)
只需要将上面的/home/b622/PSPNet-master/include和/home/b622/PSPNet-master/build/lib/libcaffe.so替换成你自己的caffe中的include和libcaffe.so的路径即可。
将上述两个文件放在同一文件夹下,然后依次输入以下命令进行编译:
cmake .
make
如下图所示:
如果在make过程中出现以下错误:
/home/b622/caffe-action_recog/include/caffe/blob.hpp:9:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory
compilation terminated.
CMakeFiles/test.dir/build.make:62: recipe for target 'CMakeFiles/test.dir/test.cpp.o' failed
make[2]: *** [CMakeFiles/test.dir/test.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/test.dir/all' failed
make[1]: *** [CMakeFiles/test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
则需要手动编译出caffe.pb.h文件,打开终端输入以下命令:
protoc --cpp_out=/home/b622/caffe-action_recog/include/caffe/proto caffe.proto
如下图所示:
成功后会在caffe-action_recog/include/caffe/proto文件夹下生成caffe.pb.h文件,如下图
完成后再次make,即可通过,如果再出现其他错误,请自行百度。
生成的文件如下:
在终端输入以下命令,生成解析出的test.txt文档:
./test
注:先cd到test执行文件的根目录下,在输入上述语句。
成功后,可以查看tets.txt文件中的内容,我解析的是官方给的PSPNet网络模型,如下(部分)所示:
name: ""
layer {
name: "data"
type: "ImageSegData"
top: "data"
top: "label"
include {
phase: TRAIN
}
phase: TRAIN
transform_param {
mirror: true
crop_size: 473
mean_value: 103.939
mean_value: 116.779
mean_value: 123.68
8: 0x3f000000
8: 0x40000000
9: 0xc1200000
9: 0x41200000
10: 1
}
image_data_param {
source: "/mnt/lustre/zhaohengshuang/dataset/VOC2012/list/trainval.txt"
batch_size: 16
shuffle: true
root_folder: "/mnt/lustre/zhaohengshuang/dataset/VOC2012"
16: 2
}
}
layer {
name: "label_gather"
type: "Gather"
bottom: "label"
top: "label_gather"
phase: TRAIN
}
layer {
name: "conv1_1_3x3_s2"
type: "ConvolutionData"
bottom: "data"
top: "conv1_1_3x3_s2"
param {
lr_mult: 1
decay_mult: 1
}
blobs {
data: 0.0022265632
data: 0.0063848812
data: -0.021314347
data: 0.0087224049
data: -0.0017450363
data: -0.040702991
data: 0.040957987
data: 0.030024877
data: -0.00040234922
data: 0.035385851
data: 0.03061888
data: 0.00975236
data: 0.029203681
data: 0.027859332
data: -0.02282569
data: 0.051132251
data: 0.050982021
data: 0.028273048
data: 0.00045456921
data: 0.0046192827
data: -0.014982899
data: 0.0010559497
data: -0.01121246
data: -0.043569062
data: 0.018522989
data: 0.013444615
data: -0.012451748
data: -0.0035903456
data: -0.0036976787
data: -0.0086726872
data: 0.013544118
data: 0.039940353
data: 0.0332526
data: 0.0174512
data: 0.030373706
data: 0.015946979
data: -0.03056127
data: -0.077426851
data: -0.064679071
data: -0.049140476
data: -0.12691125
data: -0.15443584
data: -0.067412965
data: -0.13174506
data: -0.13033487
data: -0.04345879
data: -0.022715034
data: 0.033402845
data: -0.0118775
data: 0.032909606
data: 0.031529181
data: 0.0028268516
data: 0.030321535
data: 0.055880059
data: -0.012543317
data: 0.29960331
data: -0.26908255
data: -0.060308661
data: 0.053303137
data: -0.035357445
data: -0.027739143
data: 0.013819358
data: 0.028950721
data: 0.0096227145
data: 0.600619
data: -0.47760749
data: -0.085985318
data: 0.12866144
data: -0.085491486
data: -0.069523379
data: 0.0065062055
data: 0.02735726
data: -0.011153187
data: 0.43719363
data: -0.38543427
data: -0.082773171
data: 0.07562118
data: -0.070219189
data: -0.041084472
data: 0.01311215
data: 0.036830757
data: 0.024847485
data: -0.0090860045
data: 0.051152926
data: 0.03113804
data: 0.022111731
data: 0.056659479
data: 0.087109923
data: 0.069432892
data: -0.004762331
data: 0.0077367546
data: -0.11521299
data: -0.12150885
原文链接:https://blog.csdn.net/qq_21368481/article/details/81274506