【发布时间】:2019-03-27 14:10:37
【问题描述】:
我正在使用 protobuf 和 caffe。我想使用 C++ 中的 caffe API 创建一个网络,然后将其写入 protobuf 文件。
名称:“AlexNet” 输入数据” 输入形状{ 昏暗:1 昏暗:3 昏暗:227 昏暗:227 }
我有一个 C++ 程序正在创建一个 NetParameter 对象 param。我的问题是我应该调用哪个函数将其写入 prototxt 文件。
我想我应该使用位于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;
}
这是正确的吗?此函数将 proto 消息作为第一个参数。如何将param 对象转换为原始消息?
【问题讨论】:
标签: c++ protocol-buffers caffe