Command line prediction

2,019 views
Skip to first unread message

eran paz

unread,
Mar 8, 2015, 3:43:48 AM3/8/15
to caffe...@googlegroups.com
Hi everybody
I'm new to this group & caffe and I have a newbe question.
I was unable to find a good example on how to use a trained network for predicting new instances from command line (I don't want to use python for the moment).
The only one I found used test_net which is deprecated.

I have a trained network, I have the test.prototxt file (which I believe is correct) but I can't find anywhere how to use the build/tools/caffe test command.
I figured I need to pass the test.prototxt as a model, and somehow I need to pass the trained network weights (not sure how), but do I also need to define a solver? where to I define the test data (it's lmdb format).

I will really appreciate a link to an example or detailed documentation.

Thanks in advance
Eran

Message has been deleted

Evan Shelhamer

unread,
Mar 10, 2015, 4:34:53 PM3/10/15
to eran paz, caffe...@googlegroups.com

Evan Shelhamer

--
You received this message because you are subscribed to the Google Groups "Caffe Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to caffe-users...@googlegroups.com.
To post to this group, send email to caffe...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/caffe-users/1d2b74ee-211b-4de9-bce1-107dd5cf3fe8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eran paz

unread,
Mar 11, 2015, 5:00:32 AM3/11/15
to caffe...@googlegroups.com, era...@gmail.com
Hi Evan
Thanks for the link, I've noticed it and progressed but I'm still stuck.
This is my problem:
I've trained the network, and now I want to test it on a separate test dataset.
I've created the deploy.prototxt file, even figure how to call the caffe test, but what I can't figure is how to pass it the data I want it to classify.

What i'm starting to think is that deploy can only be used interactively and the data has to be fed by a c++\python\matlab program.

I think the solution to what I want to do is create a test.prototxt file which is similar to my train_val.prototxt, but it includes only the TEST phase parts of input & output layers.
It will specify a path to my test lmdb as source.
on this text.prototxt file I can run caffe test.

If what I'm suggesting is correct, I think it might be a good idea to add a VAL phase to the network so the training (using validation dataset) and testing (a new dataset) can use the same net definition.

Again, thanks for the help

Bisher Tarakji

unread,
May 10, 2016, 11:49:16 PM5/10/16
to Caffe Users, era...@gmail.com
Any updates on this? I'd also like to classify/label images from the command line without using python at the moment. Training and testing of model is sucessful but I'm stuck on how to use this model now.

Thanks,
Bisher

SRQ

unread,
May 11, 2016, 4:14:20 AM5/11/16
to Caffe Users, era...@gmail.com
Use this:

cd caffe
./build/examples/cpp_classification/classification.bin path/to/deploy.prototxt path/to/caffemodel path/to/mean.binaryproto path/to/labels.txt path/to/image.jpg

I have colored the individual parts of the command to make it simple.

Bisher Tarakji

unread,
May 11, 2016, 2:54:00 PM5/11/16
to Caffe Users, era...@gmail.com
Thank you. It worked well

beau norgeot

unread,
Jul 27, 2016, 4:00:03 PM7/27/16
to Caffe Users, era...@gmail.com
Hi SRQ,
This method only works for making predictions on a single image. Does anyone know how to pass a list of images to make predictions on?
Thanks!

Thinh Nguyen

unread,
Sep 14, 2016, 7:14:09 AM9/14/16
to Caffe Users, era...@gmail.com
You can put these lines into the main function of classification.cpp

//Read text file for test list here
 
int sample_count = 0;
 
int error_count = 0;
 
string true_label;
 
string file_list = argv[5];
  std
::ifstream input_file(file_list);
  std
::string file;
  std
::getline(input_file, true_label);
 
while (std::getline(input_file, file))
 
{
  cv
::Mat img = cv::imread(file, -1);
  CHECK
(!img.empty()) << "Unable to decode image " << file;
  std
::vector<Prediction> predictions = classifier.Classify(img);
 
 
Prediction high_prop = predictions[0];
 
string label = high_prop.first;
  sample_count
+= 1;
 
if (label.compare(true_label) != 0)
 
{
  error_count
+= 1;
  std
::cout << "---------- Prediction for "
 
<< file << " ----------" << std::endl;
 
/* Print the top N predictions. */
 
for (size_t i = 0; i < predictions.size(); ++i) {
 
Prediction p = predictions[i];
  std
::cout << std::fixed << std::setprecision(4) << p.second << " - \""
 
<< p.first << "\"" << std::endl;
 
}
 
}  
 
}


  duration
= (std::clock() - start) / (double)CLOCKS_PER_SEC;
 
 
float accuracy = 100*(1.0 - (error_count / float(sample_count)));
  std
::cout << "\n Class: " << true_label << " Total: "
 
<< sample_count << " Error: " << error_count << " Accuracy: "
 
<< accuracy << "%" << " Time: " << duration <<"s"<< std::endl;

Text file format:

<class_num> <class_name>
<image_1_path>
<image_2_path>
...

Felix Abecassis

unread,
Sep 20, 2016, 1:26:04 PM9/20/16
to Caffe Users, era...@gmail.com
This approach works but is not great for performance, it's better to batch all your images and then classify them simultaneously. It's not to difficult to modify the classification sample to work on a batch of image, it simply requires adding vectors all over the place, but it makes the code a bit ugly:
https://github.com/flx42/caffe/tree/batched_inference

Jayshree Jadhav

unread,
Feb 13, 2017, 6:06:22 AM2/13/17
to Caffe Users, era...@gmail.com
How can I use caffe testfor  the trained MNIST model ..? Please help!!

ruby

unread,
Oct 12, 2017, 6:01:20 AM10/12/17
to Caffe Users
Hi SRQ, is the method only support jpg format but not png format?
If I want to predict a single png image, what should I do? 
Thank you.

eran paz於 2015年3月8日星期日 UTC+8下午3時43分48秒寫道:
Reply all
Reply to author
Forward
0 new messages