如何使用Google Protobuf在C ++中将Caffe网络写入Prototxt

时间:2018-10-23 10:56:47

标签: c++ protocol-buffers caffe

我正在使用protobuf和咖啡。我想使用C ++中的caffe API创建网络,然后将其写入protobuf文件。

名称:“ AlexNet” 输入数据” input_shape {   昏暗: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;
}

这是正确的吗?此函数将原始消息作为第一个参数。如何将param对象转换为原始消息?

1 个答案:

答案 0 :(得分:0)

创建NetParameter后,可以使用以下命令将其写入原型:

caffe::NetParameter param;

// ... param initialization ...

std::ofstream ofs; 
ofs.open ("test.prototxt", std::ofstream::out | std::ofstream::app);
ofs << param.Utf8DebugString(); 
ofs.close();
相关问题