Hello,
I am trying to obtain pixel-wise predictions for a single image using an already-trained caffe network.
I got this to work in python, but not in C++. The reason is that the add_input_arrays function in python does not seem to be available in C++. So I changed the input layer into a memory data layer, and reset the layer using the following code, but a forward pass only generates a single integer value rather than a matrix of integer values.
Caffe::set_mode(Caffe::CPU);
/* Load the network. */
net_.reset(new Net<float>(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);
// blob dimensions are in this order: num, channels, height, width
shared_ptr<MemoryDataLayer<float> > dataLayer = boost::dynamic_pointer_cast<MemoryDataLayer<float> >(net_->layers()[0]);
int num_channels_ = net_->blobs()[0]->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< "Input layer should have 1 or 3 channels.";
input_geometry_ = cv::Size(net_->blobs()[0]->width(), net_->blobs()[0]->height());
//float loss;
Blob<float>* blob = new Blob<float>(1, img.channels(), img.rows, img.cols);
const float img_to_net_scale = 0.0039215684; // assuming 0 to 255
TransformationParameter input_xform_param;
input_xform_param.set_scale( img_to_net_scale );
DataTransformer<float> input_xformer( input_xform_param, TEST );
input_xformer.Transform( img, blob );
//net_->add_input_blob(blob);
//net_->Update();
float *data = new float[1 * 3 * input_geometry_.height * input_geometry_.width];
float *labels = new float[1]; //new float[1 * 1 * input_geometry_.height * input_geometry_.width];
labels[0] = 0.0;
for (unsigned int c = 0; c < num_channels_; ++c)
{
for (unsigned int h = 0; h < input_geometry_.height; ++h)
{
for (unsigned int w = 0; w < input_geometry_.width; ++w)
{
// index (n, k, h, w) is physically located at index ((n * C + c) * H + h) * W + w
data[((0 * num_channels_ + c) * input_geometry_.height + h) * input_geometry_.width + w] = blob->data_at(0, c, h, w);
}
}
}
dataLayer->Reset(data, labels, input_geometry_.height * input_geometry_.width);
net_->ForwardFrom(0);
Blob<float>* output_layer = net_->output_blobs()[0];
std::vector<std::vector<int> > predictions;
unsigned int W = output_layer->width(), H = output_layer->height(), C = output_layer->channels();
std::cout<<"***********"<<net_->output_blobs().size()<<" , "<<C<<" , "<<W<<" , "<<H<<std::endl;
Any suggestions are appreciated. Thanks
~Alycia