【问题标题】:How to write a caffe network to prototxt in C++ using google protobuf如何使用 google protobuf 在 C++ 中将 caffe 网络写入 prototxt
【发布时间】: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


    【解决方案1】:

    创建 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 2018-05-21
      • 2017-02-06
      • 2015-08-17
      • 2016-05-27
      • 2018-09-17
      • 2017-09-09
      • 1970-01-01
      相关资源
      最近更新 更多