Using C++ Example Classifier in Windows Caffe

1,731 views
Skip to first unread message

Kyle Glowacki

unread,
Jan 14, 2016, 6:03:15 PM1/14/16
to Caffe Users
I'm using this windows port of caffe : https://github.com/happynear/caffe-windows

I am able to run iterations of googlenet's training and get some saved states(eg kyle_gn_iter_2000.caffemodel). The problem I have is when I attempt to use the Classifier example (https://github.com/BVLC/caffe/blob/master/examples/cpp_classification/classification.cpp) provided with both the vanilla caffe and his fork, I can't seem to get it to work. What happens is I built it and run it :

ImageClassifier.exe googlenet_test.prototxt kyle_gn_iter_2000.caffemodel mean.binaryproto labels.txt test_image_01.jpg

In the constructor of that class it does the following :
Classifier::Classifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const string& label_file) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif

/* Load the network. */
net_.reset(new Net<float>(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);

CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";

I get the error and it stops when it determines the net_->num_inputs is zero and net_->num_outputs is two. It is loading my kyle_gn_iter_2000.caffemodel and figuring out the inputs/outputs from there. However, I don't quite follow where it gets those from. I provide test images and training images with jpgs and labels when training. In the end, I do a softmax or accuracy or whatever. I'm not sure how to make it so that I have 1 input and 1 output. How are those determined?

name: "GoogleNet"
layers {
name: "GN"
type: IMAGE_DATA
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
mirror: false
crop_size: 224
mean_file: mean.binaryproto"
scale: 0.00446428571428571428571428571429
}
image_data_param {
source: "cars_training_256.txt"
batch_size: 10
new_height: 224
new_width: 224
}
}
...

...
layers {
bottom: "loss3_classifier_model"
top: "prob"
name: "prob"
type: SOFTMAX
}

Felix Abecassis

unread,
Jan 14, 2016, 7:20:00 PM1/14/16
to Caffe Users
I'm the author of cpp_classification, but I haven't tried on Windows.

You should look at the "deploy" network for googlenet:
https://github.com/BVLC/caffe/blob/master/models/bvlc_googlenet/deploy.prototxt

For instance, the following works for me:
$ ./build/examples/cpp_classification/classification.bin models/bvlc_googlenet/deploy.prototxt models/bvlc_googlenet/bvlc_googlenet.caffemodel data/ilsvrc12/imagenet_mean.binaryproto data/ilsvrc12/synset_words.txt   examples/images/cat.jpg
---------- Prediction for examples/images/cat.jpg ----------
0.5009 - "n02123159 tiger cat"                                                                                                                                                                                                                                                
0.2283 - "n02123045 tabby, tabby cat"                                                                                                                                                                                                                                         
0.1612 - "n02124075 Egyptian cat"                                                                                                                                                                                                                                             
0.0283 - "n02127052 lynx, catamount"                                                                                                                                                                                                                                          
0.0134 - "n02123394 Persian cat"

Kyle Glowacki

unread,
Jan 18, 2016, 8:28:48 AM1/18/16
to Caffe Users
That example worked for me too.
---------- Prediction for cat.jpg ----------
0.5009 - "tiger cat"
0.2283 - "tabby, tabby cat"
0.1612 - "Egyptian cat"
0.0283 - "lynx, catamount"
0.0134 - "Persian cat"

The problem seems to have been that I didn't understand that the deploy.prototxt format was different from the one you used for training. I had my image_data layer in the deploy file instead of a simple input declaration.

eg
input: "data"
input_shape {
dim: 10
dim: 3
dim: 224
dim: 224
}

Now I have input of 1 and just need to sort out why it thinks I have two outputs in my network. Thanks!

Felix Abecassis

unread,
Jan 18, 2016, 7:21:28 PM1/18/16
to Caffe Users
cpp_classification shows how to use the deploy file, but admittedly it's a low level example of using Caffe.
You should look at MemoryDataLayer too, that might be simpler to use if your C++ needs are simple.

anand...@gmail.com

unread,
Mar 14, 2016, 5:48:17 AM3/14/16
to Caffe Users
I have same problem. cpp_classification giving the error 'Check failed: net_->num_outputs() == 1 (3 vs. 1) Network should have exactly one output.'  I have trained my model using the 'DIGITS 3' and using the same deploy.prototxt created by DIGITS 3.

If I comment the line 'CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";' cpp_classification example runs correctly and prints the first output out of 3. I am getting 3 outputs in 'output_blob'.

Can you please tell me how to use these three outputs.

shervin.r...@gmail.com

unread,
Mar 6, 2017, 3:21:38 AM3/6/17
to Caffe Users
@Felix Abecassis  Hello, is there a similar C++ code for object detection rather than classification as the one in https://github.com/BVLC/caffe/tree/master/examples/cpp_classification?
Reply all
Reply to author
Forward
0 new messages