All probabilities 0 on predict with C++. Works in Python.

451 views
Skip to first unread message

Steph van Schalkwyk

unread,
Feb 6, 2015, 1:11:07 AM2/6/15
to caffe...@googlegroups.com
Hi
I have a net trained on gray scale jpgs. As they are jpgs, Caffe sees them as 3 channel. No problems there with Python.
For C++, I changed the predict.prototxt file as follows:

name: "CaffeNet"
#input: "data"  --- this is needed as per the Python tutorial
#input_dim: 1
#input_dim: 3
#input_dim: 100
#input_dim: 100
layers {           # this layer is added for c++
  name: "data"
  type: MEMORY_DATA
  top: "data"
  top: "label"
  memory_data_param {
     channels: 3
     height: 100
     width: 100
     batch_size: 1
  }
  transform_param {
    crop_size: 100
    mean_file: "/xxx/mean.binaryproto"
    mirror: false
  }
}
.....               # all layers same as train_val.prototxt with all training parameters removed
layers {
  name: "prob"
  type: SOFTMAX
  bottom: "fc8"
  top: "prob"
}
layers {
  name: "argmax"
  type: ARGMAX
  bottom: "prob"
  top: "argmax"
  argmax_param {
    top_k: 100    # I need to see probabilities for all classes. Is this correct?
  }
}
The relevant C++ is as follows:

Net<float> caffe_net(argv[1]);
caffe_net.CopyTrainedLayersFrom(argv[2]);
Datum datum;
boost::shared_ptr<MemoryDataLayer<float>> memory_data_layer;
memory_data_layer = boost::static_pointer_cast<MemoryDataLayer<float> >(caffe_net.layer_by_name("data"));
cv::Mat input_image;
input_image = cv::imread(argv[6]);
cv::resize(input_image, input_image, cv::Size(memory_data_layer->width(), memory_data_layer->height()));
CVMatToDatum(input_image, &datum);
std::vector<Datum> datumVector;
datumVector.push_back(datum);
memory_data_layer->AddDatumVector(datumVector);
float loss;
std::vector<Blob<float>*> results = caffe_net.ForwardPrefilled(&loss);
const float* argmaxs = results[1]->cpu_data();
for (int i = 0; i < results[0]->num(); i++) {
   for (int j = 0; j < results[0]->height(); j++) {
cout << "Image: " << i << " prob: " << argmaxs[i*results[0]->height() + j] << endl;
}
   }
   for (int i = 0; i < results[1]->num(); i++) {
for (int j = 0; j < results[1]->height(); j++) {
cout << "Image: " << i << " class:" << argmaxs[i*results[1]->height() + j] << endl;
}
}

I'm always getting probabilities of 0 or exceedingly small numbers (e.g. prob: -1.23558e+33). 
What am I not seeing? 
Thanks
Steph

Steph van Schalkwyk

unread,
Feb 6, 2015, 2:38:35 AM2/6/15
to caffe...@googlegroups.com
I am now using this and it does seem to work:
// Get probabilities
const boost::shared_ptr<Blob<float> >& probLayer = caffe_net.blob_by_name("prob");
const float* probs_out = probLayer->cpu_data();
// Get argmax results
const boost::shared_ptr<Blob<float> >& argmaxLayer = caffe_net.blob_by_name("argmax");
The "prob" layer does not always seem to return all the channels that the "fc8" layer propagates to it. Has anyone seen this? I can force it to return all channels by indexing into probs_out but most are 0.

lavaniac

unread,
Jul 8, 2015, 9:33:17 PM7/8/15
to caffe...@googlegroups.com
Hi. 

Were you able to figure out the issue ? I am facing the same issue when I set Caffe::set_mode(Caffe::GPU). But when I set mode to CPU using Caffe::set_mode(Caffe::CPU), I get reasonable results.

Thanks

Reply all
Reply to author
Forward
0 new messages