Ask Your Question

ML svm, k-nn image recognition examples in c++

asked 2018-05-11 12:34:38 -0500

yoggiee gravatar image

I cannot find any c++ examples of ML alghoritms like svm, k-nn recognizing patterns, images. I want to train my classifier to recognize cat or dog and test it but I have no idea how to do this. I've read tutorials about svm, k-nn training but they are about digit or text recognition.

edit retag flag offensive close merge delete



we cannot mend your lack of research skill, but at least have a read here

berak gravatar imageberak ( 2018-05-11 15:24:54 -0500 )edit

3 answers

Sort by » oldest newest most voted

answered 2018-05-11 13:04:33 -0500

LBerger gravatar image
edit flag offensive delete link more

answered 2018-05-18 06:55:06 -0500

berak gravatar image

updated 2018-05-18 12:15:32 -0500

imho, dnn's rule for this kind of task, nowadays.

we could try to use transfer learning ,

that is: use an existing, pretrained model, and try to teach it some new tricks !

we can just "pipe" our images through the network, stop it at some layer (before it would do the final classification), grab the output neurons from there, and feed our own ml classifier with this data (instead of using the "raw" images) , like this:

(colour) image   --> DNN --> 1000 numbers  --> our own classifier (ANN_MLP for today)

since opencv's dnn module already supports various classification models, let's try with squeezenet (which is also small, and quite fast !)

it was trained on millions of images (imagenet), among them cats & dogs. so, it has "seen the world", already. ;)

there are 67 layers (!), here's how the last 10 look like: (i=input,o=output)

fire9/squeeze1x1                       Convolution   i[1, 512, 14, 14]  o[1, 64, 14, 14]  
fire9/relu_squeeze1x1                  ReLU          i[1, 64, 14, 14]  o[1, 64, 14, 14]  
fire9/expand1x1                        Convolution   i[1, 64, 14, 14]  o[1, 256, 14, 14]   
fire9/relu_expand1x1                   ReLU          i[1, 256, 14, 14]  o[1, 256, 14, 14]  
fire9/expand3x3                        Convolution   i[1, 64, 14, 14]  o[1, 256, 14, 14]   
fire9/relu_expand3x3                   ReLU          i[1, 256, 14, 14]  o[1, 256, 14, 14]  
fire9/concat                           Concat        i[1, 256, 14, 14]  i[1, 256, 14, 14]  o[1, 512, 14, 14]  
drop9                                  Dropout       i[1, 512, 14, 14]  o[1, 512, 14, 14]  
conv10                                 Convolution   i[1, 512, 14, 14]  o[1, 1000, 14, 14]   
relu_conv10                            ReLU          i[1, 1000, 14, 14]  o[1, 1000, 14, 14]  
pool10                                 Pooling       i[1, 1000, 14, 14]  o[1, 1000, 1, 1]  
prob                                   Softmax       i[1, 1000, 1, 1]  o[1, 1000, 1, 1]

so, pool10 looks like a good place to tap it !

(1000 features are a good number, if we have ~1000 images in our dataset)

you'll need to download the caffemodel and the prototxt , then we can start playing with your cats vs dogs dataset

#include "opencv2/opencv.hpp"
#include "opencv2/dnn.hpp"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
    vector<String> fn;
    glob("c:/data/cat-dog/*.jpg", fn, true);
    // glob() will conveniently sort names lexically, so the cats come first!
    // so we have 700 cats, 699 dogs, and split it into:
    // 100 test cats
    // 600 train cats
    // 100 test dogs
    // 599 train dogs

    std::string modelTxt = "c:/data/mdl/squeezenet/deploy.prototxt";
    std::string modelBin = "c:/data/mdl/squeezenet/squeezenet_v1.1.caffemodel";
    dnn::Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
    cv::Size inputImgSize = cv::Size(227, 227); // model was trained with this size

    Mat_<int> layers(4, 1);
    layers << 1000, 400, 100, 2; // the sqeezenet pool10 layer has 1000 neurons

    Ptr<ml::ANN_MLP> nn = ml::ANN_MLP::create();
    nn->setTrainMethod(ml::ANN_MLP::BACKPROP, 0.0001);
    nn->setTermCriteria(TermCriteria(TermCriteria::MAX_ITER+TermCriteria::EPS, 300, 0.0001));

    Mat train, test;
    Mat labels ...
edit flag offensive delete link more


I do not understand this post. What is the question? Who ask a question ? To be, or not to be, that....

LBerger gravatar imageLBerger ( 2018-05-18 07:25:59 -0500 )edit


I want to train my classifier to recognize cat or dog and test it but I have no idea how to do this.

(from the question above)

((it also turned out, that simply throwing images at knn or such, does not work here, and that it needs "a larger cannon"))

(((and maybe, we also need to mention more modern parts of machine-learning here)))

berak gravatar imageberak ( 2018-05-18 09:29:21 -0500 )edit

Yes you are right. "it also turned out, that simply throwing images at knn or such, does not work here, and that it needs "a larger cannon" : may be a test is necessary to check this if knn input is pool10 output

Finally answer to first question is You cannot train deep learning network using opencv

LBerger gravatar imageLBerger ( 2018-05-18 10:19:20 -0500 )edit

oh, i'm sure, knn or svm will work nicely too, it just needed that extra dnn preprocessing (instead of raw images)

right, you can't retrain the convolutional layers from opencv, but you can make good use of them with transfer learning like above.

berak gravatar imageberak ( 2018-05-18 10:21:36 -0500 )edit

Awesome! Thank you so much @berak

You’re the man!

sjhalayka gravatar imagesjhalayka ( 2018-05-18 12:05:45 -0500 )edit

@berak and @LBerger — Have you heard about TrackML from Kaggle? You have to create output from training input, using some kind of machine learning. There’s thousands in prize money.

It’s all about particle collisions and cascades in 3D space.

sjhalayka gravatar imagesjhalayka ( 2018-05-18 12:20:18 -0500 )edit

i know. but they have more, and even harder data ;)

but maybe you can get into the top 100, this way ;9

(for their cats vs dogs competition, which again, is pretty much: entry level :)

berak gravatar imageberak ( 2018-05-18 12:21:44 -0500 )edit

@sjhalayka , as long as you're trying with python, i'd recommend using google's colab utility

(you can burn their gpu's all day long, also, downloading data is for free ! also, cv2 preinstalled !)

berak gravatar imageberak ( 2018-05-18 13:10:46 -0500 )edit

@berak -- I'm getting linker errors related to cv::dnn::experimental_dnn_v3::Net::setInput and such.

Any idea on how to solve it?

sjhalayka gravatar imagesjhalayka ( 2018-05-18 13:17:58 -0500 )edit

@sjhalayka, ask a new question, and provide details, please. (it's getting somewhat crowded here..)

berak gravatar imageberak ( 2018-05-18 13:27:25 -0500 )edit

answered 2018-05-11 14:46:41 -0500

sjhalayka gravatar image

updated 2018-05-18 13:05:24 -0500

There is the code for the OpenCV Multi Layer Perceptron Artificial Neural Network, to do image classification:

The code uses your cat and dog image database. The code is written in Python and C++. The problem -- and it affects both the Python and C++ versions -- is that the predictor acts like a pseudorandom coin toss. Try getting mugshots of cats and dogs instead.

If you learn SVM and k-NN, you are encouraged to please post your code as an answer to your own question. Thank you.

I found this re: SVM

edit flag offensive delete link more


I've done .csv file containing cat, dog images with labels cat=0, dog=1 what to do next??

yoggiee gravatar imageyoggiee ( 2018-05-13 18:33:56 -0500 )edit

Right, you're using C++.

There's lots to do. Are you interested in the OpenCV MLP ANN ('the neural network')?

I noticed that you have quite a lot of dog and cat pictures. Very nice to have when it comes to training and testing the network.

Do you know the difference between binary encoding and one-hot encoding? Have you read any books or tutorials on neural networks?

Did you look at my neural network code that does image classification?

sjhalayka gravatar imagesjhalayka ( 2018-05-13 20:55:12 -0500 )edit

I can try mlp ann, but I'm looking for alghorithm that is the simpliest and does not take too much memory cause I want to launch my recognizing software on raspberry pi3. I did read some tutorials and your code but I don't know how to put my training data as csv file and train it.

yoggiee gravatar imageyoggiee ( 2018-05-14 09:43:01 -0500 )edit

That's OK, we will read in the images one at a time, which uses the least amount of memory. I don't think you'll need the CSV file at all, since I have code to enumerate a directory for all of the images. I will work on the base code, before I add in the neural network code. I will post it when it's ready.

sjhalayka gravatar imagesjhalayka ( 2018-05-14 09:48:54 -0500 )edit

I put the Python code up at

The steps you need to follow are in the repository readme:

Step 1) Copy your cat and dog images into the Images directory, then run

Step 2) Run to train and test the network

Now to do the C++ version. I'll do that after I train the network 10000 times. If the network doesn't do much better than a random coin toss at 10000 iterations, it's not worth porting the code to C++.

sjhalayka gravatar imagesjhalayka ( 2018-05-14 10:19:13 -0500 )edit

@berak -- I'm wondering: Should the neural network do much better than predicting just a little above 50% correct? That's what I'm seeing with this network -- it classifies correctly only a little more than half the time. Do you have any feedback to make the network operate better?

sjhalayka gravatar imagesjhalayka ( 2018-05-14 13:36:46 -0500 )edit

do you remember, there was an "AI winter" ?

cnn's solved it. try with the pretrained googlenet in the dnn module, and see, how that fares on cats & dogs

(but no, you should be able to get even opencv's ANN_MLP to 80 or 90% with cats and dogs)

berak gravatar imageberak ( 2018-05-14 13:44:14 -0500 )edit

I will check it out.

sjhalayka gravatar imagesjhalayka ( 2018-05-14 13:46:08 -0500 )edit

My bad, so I wonder what's wrong that I can't get 80-90% with cats and dogs!

sjhalayka gravatar imagesjhalayka ( 2018-05-14 13:52:31 -0500 )edit

sorry did not follow this thread entirely.

berak gravatar imageberak ( 2018-05-14 13:57:57 -0500 )edit

Question Tools

1 follower


Asked: 2018-05-11 12:34:38 -0500

Seen: 1,933 times

Last updated: May 18 '18