Antoine
Antoine

Reputation: 33

Caffe C++ set data in input layer

For one of my project, I took interest in Caffe, and more generaly in deep learning. After several hours, I managed to get Caffe installed on my computer. I am now trying to make us of it.

So I have already loaded the network as follow :

std::string model_file = "/home/CXX/Desktop/caffemodel/deploy.prototxt";
std::string trained_file = "/home/CXX/Desktop/caffemodel/modelWeights.caffemodel";

Caffe::set_mode(Caffe::CPU);
boost::shared_ptr<Net<float>> net_;
net_.reset(new Net<float>(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);

The loaded network and weights are not mine. Please find below the structure of the input and output layers:

name: "simple_conv-dense"
input: "data"
input_dim: 1
input_dim: 1
input_dim: 250
input_dim: 250
layer {
  name: "conv1"
  bottom: "data"
  type: "Convolution"
  top: "conv1"
  param {
    lr_mult: 1
    decay_mult: 1
  }
  param {
    lr_mult: 2
    decay_mult: 0
  }

The input is a single 250*250 depth (values normalized between 0 and 1) "image". The pre-processing is already handled, and my data is stored in a Matrix (personnal library, pointers to std::vector elements), so that you can access it this way like a 2D array (data[i][j])

The output of the network is organized in this order: [NbBlob][NbClass][outHeight][outWidth], giving in my case [1][46][250][250]

I have already written the code to retrieve the output:

Blob<float>* output_layer = net_->output_blobs()[0];
const float* begin = output_layer->cpu_data();
for (int k = 0; k < 46; k++)
    for (int h = 0; h < 250; h++)
        for (int w = 0; w < 250; w++){
            currentprob =  *(begin + ((k * 250 + h) * 250 + w));

This code has been checked, by summing the pixel-wise 46 class predections, giving obviously 1 as a result for a single pixel.

My problem is that I do not know how to feed my data in the network. I first retrieve the input layer by this method:

Blob<float>* input_layer = net_->input_blobs()[0];

From the debugger, I do know that input_layer has an attribute named capacity_ which has the expected value (62 500, being 250*250).

So here is my question: How can one feed his data into the input layer? I have spend quite some time looking by myself, but I have no idea where to look anymore.

Please note that I am not using OpenCV, and that I have barely any background on Deep learning (Bachelor student).

Thank you for the time you might spend helping me. Any kind of help (documentation, pseudo-code, code, explanations) is very welcome.

PS: using namespace caffe;

EDIT: added more input layer info. Typos.

Upvotes: 3

Views: 1737

Answers (1)

Shai
Shai

Reputation: 114786

I would try directly push the data to the net:

Blob<float>* input_layer = net_->input_blobs()[0];
float* input_data = input_layer->mutable_cpu_data();  // get pointer to Blob's data storage
for ( int i=0; i < 250; i++ ) {
    for ( int j=0; j < 250; j++ ) {
        input_data[i*250 + j] = data[i][j];  // I hope I did not filp anything here...
    }
}
net_->forward();  // do forward pass

Depending on how your data is arranged, you might be able to replace the nested loop with a more elegant memcpy...

Upvotes: 1

Related Questions