如何在Caffe中使用C ++从多个图层获取功能 [英] How to get features from several layers using c++ in caffe

查看:94
本文介绍了如何在Caffe中使用C ++从多个图层获取功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++向前传递一次后,如何在caffe中同时获得4096个昏暗特征层和1000个昏暗类图层?

How can I get both the 4096 dim feature layer and the 1000 dim class layer in caffe after one forward pass using C++?

我试图在extract_features.cpp中查找它,但是它使用了一些奇怪的datum对象,因此我无法真正理解它的工作原理.

I tried to look it up in extract_features.cpp but it uses some weird datum object, so I cannot really understand how it works.

到目前为止,我只是将prototxt文件裁剪到我要提取和使用的层上

So far I was simply cropping my prototxt files up to the layer that I wanted to extract and used

[...]
net->ForwardPrefilled();
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);

但是如果我要同时提取两个特定的层(例如"prob"和"fc7"),那将不起作用.

but that does not work if I want to extract two specific layers (eg "prob" and "fc7") simultaneously.

推荐答案

更新

extract_feature.cpp的简单工作流程(假设您在c ++中有一个shared_ptr<Net<float> > net对象):

Update

The simple work flow of extract_feature.cpp(suppose you have a shared_ptr<Net<float> > net object in c++):

  1. 执行网络正向处理输入:net->Forward(). 在此步骤中,net中有一个Data层以读取输入图像.因此,如果您想在自己的应用程序/代码中将图像读取到cv::Mat image并将其输入到net中,则可以编写类似以下代码:

  1. perform net forward to process input: net->Forward(). In this step, there is a Data layer in the net to read the input images. So if in your own app/code you want read an image to cv::Mat image and feed it into net, you can write a code like:

// for data preprocess
shared_ptr<caffe::DataTransformer<float> > data_transformer;
caffe::TransformationParameter trans_para;
// set mean
trans_para.set_mean_file("/path/to/image_mean.binaryproto");
// set crop size, e.g.here is cropping 227x227
trans_para.set_crop_size(227);
// instantiate a DataTransformer using trans_para for image preprocess
data_transformer.reset(new caffe::DataTransformer<float>(trans_para, caffe::TEST));
const std::vector<caffe::Blob<float> *> net_input = net->input_blobs();
// maybe you need to resize image before this step
data_transformer->Transform(image, *net_input[0]);
net->Forward();

net.prototxt的第一层应为Input层,例如此 deploy.prototxt .

And the net.prototxt should have a Input layer as the first layer, e.g. this deploy.prototxt.

从您进入所需结构的Blob中提取特征数据,例如arry,一个简单的示例代码可以是:

extract the feature data from the blob you get into a structure you want, e.g. an arry, a simple sample code can be:

count = feature_blob->channels() * feature_blob->height() * 
    feature_blob->width();
float* feature_array = new float[count]; 
const float* feature_blob_data = feature_blob->cpu_data() +
    feature_blob->offset(n); // feature data generated from 
                             // the nth input image within a batch 
memcpy(feature_array, feature_blob_data, count * sizeof(float)); 
...// other operations
delete [] feature_array;        

请注意,从feature_blob_data存储的数据按行主要顺序.

Note that the data stored from feature_blob_data is in row-major order.

extract_feature.cpp的用法应类似于您的任务:

The extract_feature.cpp's usage should be like this for your task:

path/to/extract_features your_pretrained_model.caffemodel \
    net.prototxt 4096_dim_feature_blob_name,1000_dim_class_feature_blob_name \
    saved_4096_dim_feature_database,saved_1000_dim_class_feature_database \
    num_mini_batches(times for forward pass) lmdb(or leveldb) GPU(or CPU)

net.prototxt应该包含一个可以读取输入图像数据的数据层.

The net.prototxt should contain a data layer that can read the input image data.

在运行时,它将首先从net.prototxt内的数据层读取图像数据并执行num_mini_batches次正向通过,并将2个两个特征blob 4096_dim_feature_blob_name1000_dim_class_feature_blob_name的数据提取到一个类型为Datum的结构,然后将它们序列化以保存在类型为lmdbleveldb的数据库saved_4096_dim_feature_databasesaved_1000_dim_class_feature_database中.

And when running, it will first the read image data from the data layer within net.prototxt and perform num_mini_batches times of forward pass and extract the 2 two feature blob 4096_dim_feature_blob_name, 1000_dim_class_feature_blob_name's data into a structure typed of Datum and then serialize them to save in the database saved_4096_dim_feature_database, saved_1000_dim_class_feature_database which are typed of lmdb or leveldb.

完成后,您可以分别使用net.prototxt中的数据层从saved_4096_dim_feature_databasesaved_1000_dim_class_feature_database中读取保存的要素数据.

When finished, you can read the saved feature data from saved_4096_dim_feature_database, saved_1000_dim_class_feature_database using a data layer in net.prototxt respectively.

BTW,datum是一种结构,最多可以存储4D数据以及数据的形状和标签信息等.它在caffe.proto中定义,并使用

BTW, datum is a structure that can store at most 4D data as well as the data's shape and label information etc. It is defined in caffe.proto, generated using google protobuf and is convenient for data interchange between caffe and database like LMDB and LEVELDB.

这篇关于如何在Caffe中使用C ++从多个图层获取功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆