C++ Net Output differs from Matlab

33 views
Skip to first unread message

Andreas ten Pas

unread,
Dec 26, 2016, 2:53:03 PM12/26/16
to Caffe Users
I'm having trouble finding a C++ example that directly uses Caffe's net interface to make a prediction for a net input with more than the usual three channels. Currently, I'm using the code beniz posted in https://github.com/BVLC/caffe/issues/1443. My C++ code looks like this:

caffe::Net<float> net(model_file, caffe::TEST);
net
.CopyTrainedLayersFrom(weights_file);

float loss = 0.0;
std
::vector<cv::Mat> dv; // = images; // image is a cv::Mat, as I'm using #1416
cv
::Mat img;
images
[1].convertTo(img, CV_8U);
dv
.push_back(img);
std
::vector<int> dvl;
dvl
.push_back(0);

boost
::dynamic_pointer_cast< caffe::MemoryDataLayer<float> >(net.layers()[0])->AddMatVector(dv,dvl);
std
::vector<Blob<float>*> results = net.Forward(&loss);

I also have the following Matlab code:

net = caffe.Net(deployFile, weightsFile, 'test');
dataShape
= net.blobs('data').shape;
image
= images{1};
inputImage
= zeros(dataShape);
inputImage
(:,:,:,1) = image;
probs
= net.forward({inputImage});

I'm wondering why the values contained in the variable results are very different from the ones contained in probs for the same image. For example: probs = (107.5973, -110.9885), results = (1.30978,
1: -1.3908). Is this typical?

Andreas ten Pas

unread,
Dec 28, 2016, 12:02:56 PM12/28/16
to Caffe Users
To answer my own question: when converting from float to uchar, it's important to apply the correct scaling factor. This line

images[1].convertTo(img, CV_8U);

should be


images
[1].convertTo(img, CV_8U, scale);
Reply all
Reply to author
Forward
0 new messages