[英]How to write a caffe network to prototxt in C++ using google protobuf
I am using protobuf and caffe. 我正在使用protobuf和caffe。 I want to create a network using the caffe API in C++ and then write this to a protobuf file.
我想使用C ++中的caffe API创建网络,然后将其写入protobuf文件。
name: "AlexNet" input: "data" input_shape { dim: 1 dim: 3 dim: 227 dim: 227 } 名称:“ AlexNet”输入:“ data” input_shape {昏暗:1昏暗:3昏暗:227昏暗:227}
I have a C++ program that is creating a NetParameter
object param
. 我有一个正在创建
NetParameter
对象param
的C ++程序。 My question is which function should I call to write this to a prototxt file. 我的问题是应该调用哪个函数将其写入prototxt文件。
I think I should be using WriteProtoToTextFile(const Message& proto, const char* filename)
located here in src/caffe/util/io.cpp: 我认为我应该使用位于src / caffe / util / io.cpp中的
WriteProtoToTextFile(const Message& proto, const char* filename)
:
#include <fcntl.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <stdint.h>
#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
#include <string>
#include <vector>
#include <string>
#include "caffe.pb.h"
#include <iostream>
#include <caffe/caffe.hpp>
using namespace std;
using google::protobuf::Message;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
int main()
{
caffe::NetParameter param;
const char *filename = "/test.prototxt";
int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1)
cout << "File not found: " << filename;
FileOutputStream *output = new FileOutputStream(fd);
param.set_name("AlexNet");
//How to convert param to a proto message
// Call WriteProtoToTextFile(const Message& proto, const char* filename) ??
delete outputput;
close(fd);
return 0;
}
Is this correct? 这个对吗? This function take a proto message as the first argument.
此函数将原始消息作为第一个参数。 How do I convert the
param
object to a proto message? 如何将
param
对象转换为原始消息?
After you've created the NetParameter, you can write it to a prototxt by using: 创建NetParameter后,可以使用以下命令将其写入prototxt:
caffe::NetParameter param;
// ... param initialization ...
std::ofstream ofs;
ofs.open ("test.prototxt", std::ofstream::out | std::ofstream::app);
ofs << param.Utf8DebugString();
ofs.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.