Caffe模型简单读写及修改

                                                   Caffe模型简单读写及修改

   Caffe模型训练好以后,有时候我们只需要提取特征,而不需要最后的分类全连接层,只需要前面一层的特征层的参数。人脸识别模型就是典型的例子,训练的时候最后一个全连接为人的身份类别全连接,实际应用部署只需要前一层提特征,然后人脸特征比对,因此最后一层并不需要,而最后一层全连接又占据了大量的参数,模型.caffemodel非常大,因此需要去掉最后一层参数再部署。

Code:

#include <caffe/caffe.hpp> 

/*这些包含文件在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 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;

 

using namespace caffe;

using namespace std;

int main()

{

    std::string model_dir = "your .caffemodel path";

    std::string save_dir ="your .txt path";

    std::string finalsave_dir = "your  new caffemodel path";

 

    //声明网络参数对象,即NetParameter对象

    NetParameter proto;

 

//1、读取caffemodel参数到NetParameter对象

    ReadProtoFromBinaryFile(model_dir, &proto);

 

//2、以proto格式写入txt

    WriteProtoToTextFile(proto, save_dir);     

 

/*3、写完之后,可以打开txt手动剔除最后一层参数,然后存储到.caffemodel */

 

    //NetParameter proto2; 

//4、从txt读取修改过的参数到Netparameter

    //ReadProtoFromTextFileOrDie(save_dir, &proto2);

 

     //5、重新将新修改过的proto写入.caffemodel

    //WriteProtoToBinaryFile(proto2, finalsave_dir); 

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值