c++ - 如何解码caffemodel中的权重并转发图像?

标签 c++ neural-network protocol-buffers deep-learning caffe

每个人。

我有一个 prototxt 文件、caffemodel 文件和一个图像。 我想提取caffemodel文件中的权重并转发图像 获取关于 prototxt 文件的输出 vector (例如用于分类的 1000 维 vector )。

我知道 $(CAFFE_ROOT)/src/caffe/proto/caffe.proto 中的文件定义 protobuf消息结构,caffemodel文件根据序列化 到这些结构。

我还研究了如何对消息结构进行编码。 例如,如果我有一个消息结构:

message Test2 {
    required string b = 2;
}

并将b的值设置为“testing”,然后编码后, 我会得到“12 07 74 65 73 74 69 6e 67”

但我仍然不知道如何仅通过 C 和 C++ 编程使用 caffemodel 文件中的权重转发图像。 我想解码 caffemodel 文件并通过我的 C 和转发图像 C++ 代码,而不是使用 Caffe 或 Protobuf 提供的 API。

下一步我有什么想法吗? 例如,研究其他 Material 或其他。 非常感谢。

最佳答案

Caffe 的代码非常清晰易读。开始研究$CAFFE_DIR/tools/caffe.cpp文件,具体就是int test()函数。

重要行:

Net<float> caffe_net(FLAGS_model, caffe::TEST);
caffe_net.CopyTrainedLayersFrom(FLAGS_weights);
const vector<Blob<float>*>& result = caffe_net.Forward(&iter_loss);

$CAFFE_DIR/examples/cpp_classification/classification.cpp 也有有用的代码。

vector<float> Classifier::Predict(const cv::Mat& img) {
    Blob<float>* input_layer = net_->input_blobs()[0];
    input_layer->Reshape(1, num_channels_,
            input_geometry_.height, input_geometry_.width);
    /* Forward dimension change to all layers. */
    net_->Reshape();

    vector<cv::Mat> input_channels;
    WrapInputLayer(&input_channels);

    Preprocess(img, &input_channels);

    net_->Forward();

    /* Copy the output layer to a vector */
    Blob<float>* output_layer = net_->output_blobs()[0];
    const float* begin = output_layer->cpu_data();
    const float* end = begin + output_layer->channels();
    return vector<float>(begin, end);
}

关于c++ - 如何解码caffemodel中的权重并转发图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38455117/

相关文章:

c++ - 为什么这段代码可以省略一个拷贝?

c++ - 在 qt 应用程序内执行 qt 应用程序

c++ - 将单独的 header 和类定义文件链接到主函数文件-G++返回 “undefined reference to”重载的构造函数

python - Apache Beam Google Datastore ReadFromDatastore 实体 protobuf

javascript - ProtoBuf.js 如何在编码对象时过滤额外字段

c++ - MySQL 段错误,间歇性

python - 数据嘈杂时如何有效衡量神经网络的性能

python - 计算 CNN 层数

machine-learning - 神经网络梯度下降中的反向传播与线性回归

c# - ProtoInclude 属性是什么意思(在 protobuf-net 中)