簡體   English   中英

如何使用Google Protobuf在C ++中將Caffe網絡寫入Prototxt

[英]How to write a caffe network to prototxt in C++ using google protobuf

我正在使用protobuf和caffe。 我想使用C ++中的caffe API創建網絡,然后將其寫入protobuf文件。

名稱:“ AlexNet”輸入:“ data” input_shape {昏暗:1昏暗:3昏暗:227昏暗:227}

我有一個正在創建NetParameter對象param的C ++程序。 我的問題是應該調用哪個函數將其寫入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對象轉換為原始消息?

創建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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM