每个人。
我有一个 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/