深度学习:查看修改caffemodel中的数据

参考文章:

https://blog.csdn.net/jiongnima/article/details/72904526

https://stackoverflow.com/questions/37159655/set-caffe-net-parameters-via-string/38845573

 

读取


bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
  int fd = open(filename, O_RDONLY);
  CHECK_NE(fd, -1) << "File not found: " << filename;
  ZeroCopyInputStream* raw_input = new FileInputStream(fd);
  CodedInputStream* coded_input = new CodedInputStream(raw_input);
  coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
 
  bool success = proto->ParseFromCodedStream(coded_input);
 
  delete coded_input;
  delete raw_input;
  close(fd);
  return success;
}

保存到文件

void WriteProtoToTextFile(const Message& proto, const char* filename) {
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  FileOutputStream* output = new FileOutputStream(fd);
  CHECK(google::protobuf::TextFormat::Print(proto, output));
  delete output;
  close(fd);
}

既然能保存到文件,那一定能保存到string变量,后续的加密还是其他操作都很方便

	caffe::NetParameter proto;
	ReadProtoFromBinaryFile(caffemodelFile, &proto);
	std::string str11;
	google::protobuf::TextFormat::PrintToString(proto, &str11);

都能查看了,修改就不是什么问题了。

 

完整代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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/cvlab/files/caffe-master/data/mnist/lenet_iter_10000.caffemodel", &proto);
	WriteProtoToTextFile(proto, "/home/cvlab/files/caffe-master/data/mnist/test.txt");
	return 0;

}

 

model比较大,就给个截取的简单的示例吧

name: "PVANET"
layer {
  name: "input"
  type: "Input"
  top: "data"
  top: "im_info"
  phase: TEST
  input_param {
    shape {
      dim: 1
      dim: 3
      dim: 608
      dim: 732
    }
    shape {
      dim: 1
      dim: 6
    }
  }
}
layer {
  name: "conv1"
  type: "Convolution"
  bottom: "data"
  top: "conv1"
  param {
    lr_mult: 0.1
    decay_mult: 0.1
  }
  param {
    lr_mult: 0.2
    decay_mult: 0
  }
  blobs {
    data: 0.00058896514
    data: -0.00520326
    data: -0.011873015
    data: -0.0021785062
    data: 0.00010243683
    data: -0.011511126
    data: -0.031244002
    data: -0.008328313
    data: -0.0020084055
    data: -0.021817807
    data: -0.044573765
    data: -0.0086042108
    data: 0.0026786365
    data: -0.003945041
    data: -0.021285834
    data: -0.006990117
    data: 0.013294745
    data: 0.0011025955
    data: 0.001851631
    data: 0.0079139434
    data: 0.017405713
    data: 0.0089700241
    data: -0.048809346
    data: 0.017718213
    data: 0.0064394563
    data: -0.010589546
    data: -0.10081001
    data: 0.022317449
    data: 0.024665838
    data: 0.035022043
    data: -0.025070893
    data: 0.02545714
    data: 0.018829314
    data: 0.013582095
    data: 0.0065317904
    data: 0.016990315
    data: 0.022721361
    data: 0.014547619
    data: -0.017411726
    data: 0.02053489
    data: 0.018707082
    data: -0.0006599765
    data: -0.042962622
    data: 0.020812372
    data: 0.027259395
    data: 0.02901859
    data: -0.0030232966
    data: 0.025934035
    data: -0.00081500213
    data: 0.00037967053
    data: 0.00030335307
    data: -0.00040273982
    data: 0.00036216722
    data: 0.0013306515
    data: 0.0014849391
    data: 0.00091943826
    data: 0.0011426941
    data: 0.00252376
    data: 0.0023100565
    data: 0.00264704
    data: 0.00020415679
    data: 0.0016482215
    data: 0.0020868273
    data: 0.0018382128
    data: -0.0022172243
    data: -0.00078450493
    data: -0.00090912695
    data: -0.0011012228
    data: -0.0010559601
    data: 0.00019144831
    data: 0.0001453523
    data: 0.00021092396
    data: -0.00031483092
    data: 0.0012122762
    data: 0.00080809108
    data: 0.0018300327
    data: -0.00082994654
    data: 0.00098395755
    data: 0.0012179097
    data: 0.0016004875
    data: -0.0029145908
    data: -0.0013600575
    data: -0.0015429525
    data: -0.0016674764
    data: -0.0017289644
    data: -0.00027367173
    data: -0.00033747757
    data: -0.00019530482
    data: -0.00081708335
    data: 0.00091378705
    data: 0.00055219373
    data: 0.0015644724
    data: -0.0012227627
    data: 0.00084487966
    data: 0.0011236003
    data: 0.0015639245
    data: 0.003437219
    data: 0.0061262706
    data: 0.0091402764
    data: 0.0067112911
    data: 0.0009827906
    data: 0.0049642297
    data: 0.013254238
    data: 0.0098424153
    data: -0.0046346537
    data: -0.0038782367
    data: -0.0038529609
    data: -0.0015706874

 

你可能感兴趣的:(深度学习)