如何在C ++中向Caffe模型添加权重的C ++向量

时间:2018-10-24 13:00:56

标签: c++ caffe

在下面的c ++程序中,如何将权重的c ++向量添加到卷积层?

我有一个caffe::ConvolutionParameter对象convParam,如何为它添加权重?我需要使用caffe::BlobProto对象吗?

我需要一遍一遍地循环复制权重吗?

我已经在线搜索了,找不到任何使用C ++进行操作的示例。

完成此操作后,我会将其写入caffemodel文件。我已经将网络写入prototxt文件。

#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;

/*Network object*/
caffe::NetParameter netParam; 

int main()
{
           caffe::LayerParameter *layerParam = netParam.add_layer();

            /*Set name and type of the layer*/
            layerParam->set_name("Test";
            layerParam->set_type("Convolution");

            /*Get the input operation*/
            layerParam->add_bottom("bootom");

            /*Get the output operation*/
            layerParam->add_top("top");

            /*Set layer to have a conv parameter*/
            caffe::ConvolutionParameter *convParam = layerParam->mutable_convolution_param();

            /*Set stride on ConvolutionParameter object*/
            convParam->add_stride(2);

            /*Set kernel on ConvolutionParameter object*/
            convParam->add_kernel_size(3);

            /*Set number of output channels*/
            convParam->set_num_output(64);

             /*weights*/
             vector<int> weights{ 10, 20, 30 }; 

            //How to add weights to the layer??

            //add_blobs()
            caffe::BlobProto *blobProto = layerParam->add_blobs();
            blobProto->has_shape();


            /*create prototxt*/
            std::ofstream ofs;
            ofs.open("test.prototxt", std::ofstream::out | std::ofstream::app);
            ofs << netParam.Utf8DebugString();
            ofs.close();

            return 0;
}

0 个答案:

没有答案