Ask Your Question
0

How to train Decision Trees? How to fill TrainData with Images?

asked 2016-11-08 09:39:17 -0600

DerrickB gravatar image

Hello OpenCV Community,

after you helped me a lot with my Cascade Classifier problems, i have a new question about decision trees (C++).

I want to train Decision Trees to recognize Persons. I red the official Implementation and all forum themes i could find about this topic, but i still dont know how to train my decision tree.

The function cv::ml::train() wants cv::ml::TrainData() as argument. How do i become my positiv and negativ images in the array of cv::ml::TrainData()???

Does somebody has an example Code of the Training of his Decision/ Random or Boosting Tree? (Doesnt matter if its Face Recognition or something like that)

Thank you a lot, Kind regards, Derrick

edit retag flag offensive close merge delete

Comments

1
LBerger gravatar imageLBerger ( 2016-11-08 11:05:53 -0600 )edit

@LBerger thanks for your answer. I actually had red this Code, but i don't really get it completely. So i cant transfer it to my problem.

The Code transfers a vector<points> to a Mat and then into TrainData. Where should i put positive and negative Trainingsimages.

Sorry, i am pretty new...

DerrickB gravatar imageDerrickB ( 2016-11-08 11:49:40 -0600 )edit

Look for trainedPointsMarkers in onMouse function currentClass (0 or 1) is pushed in vector. Data are prepared using prepare_train_data() function. In this function you have trainedPoints and trainedPointsMarkers which are used to prepare Ptr<traindata>

LBerger gravatar imageLBerger ( 2016-11-08 12:23:18 -0600 )edit

@LBerger I don't get that either. I'm Sorry. 400 lines uncommented Code is like a foreign language for me.

i just wanna put positive Images (e.g. face) and negative Images (e.g. no faces) into the cv::ml::TrainData and train des Decision Tree. (like a CascadeClassifier) Please correct me if i am complete wrong and DTrees cant be trained like that.

DerrickB gravatar imageDerrickB ( 2016-11-08 12:56:56 -0600 )edit

Sorry example is not exactly for your problem. you should follow this tutorials.

LBerger gravatar imageLBerger ( 2016-11-08 13:35:11 -0600 )edit

@LBerger First Thank you for your patience with me!

This is the traincascade tutorial, i trained a CascadeClassifier with that. But i dont get how to use it for Decision Trees. Can I use it for Decision Trees or is there something comparable for Decision Trees?

DerrickB gravatar imageDerrickB ( 2016-11-08 17:10:58 -0600 )edit

@DerrickB, no, that's a completely seperate process.

also, do you need detection (person or not, 2 class), or recognition) (who is it, multi-class) ?

then note, that opencv's ml models can't do a full detection, they cannot give you the location of a found object, only say , if whole images are same or not.

berak gravatar imageberak ( 2016-11-09 01:30:25 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-11-09 01:57:49 -0600

berak gravatar image

updated 2016-11-09 02:42:25 -0600

to train opencv's ml classes(they're all very similar there), you need a single Mat with the train features (each on a row of its own), and a Mat with one class label for each feature, those 2 Mat's go into a cv::TrainData container later.

TrainData
      features             labels
         image1              1
         image2              0
         image3              2
...

again, all features need to have the same size (needs some cropping/resize / procrustes), for a 2-class problem you would use (-1,1) labels, for recognition e.g. person-id's

here's some (fairly pseudo) code:

Mat feats, labels; // start empty

for each image {
    Mat f = image.reshape(1,1); // flatten to a single row
    f.convertTo(f, CV_32F);     // ml needs float data
    feats.push_back(f);         // append at bottom

    labels.push_back(person_id); // an integer, this is, what you get back in the prediction
}

Ptr<ml::DTree> model = ml::DTree::create();
model->train(ml::TrainData::create(feats, ml::ROW_SAMPLE, labels));

for the prediction later, you'd treat your data in the same way:

Mat f = image.reshape(1,1); // flatten to a single row
f.convertTo(f, CV_32F);    // ml needs float data
prediction = model->predict(f);

sidenote: this is just to get you started with the ml setup. you'll soon find, that a dtree might not be the optimal approach (SVM, maybe ?), that using e.g. HOG or LBPH features instead of images should be considered, etc.

edit flag offensive delete link more

Comments

@berak Thank you very much. This helps a lot my understandings. I will try this an give later notice if made it.

I got a question about your last sentence ("that using e.g. HOG or LBPH features instead of images should be considered"):

Do you mean using SVM and HOG features? Or do you mean i should train my DTree with HOG Features?

I know that SVM and CascadeClassifier are the better options but i want to try + understand Dtrees.

DerrickB gravatar imageDerrickB ( 2016-11-09 07:58:14 -0600 )edit

images make very large feature vectors, e.g. 100x100 pixels are 10000 floats per row. (long training time, memory issues, also noisy)

whatever machine learning model you use, it's just a reminder, that you can try to optimize results, using different features there.

berak gravatar imageberak ( 2016-11-09 23:53:38 -0600 )edit

@berak I always get an Error when i want to train the Decision Tree (model->train(...))

it says:

... std::length_error at memory location ...

i cant find a solution in the Internet. Do you know where the Problem is. (Should i make a new topic for this question?)

DerrickB gravatar imageDerrickB ( 2016-11-14 08:18:47 -0600 )edit

i have some suspect there, but please make a new one !

berak gravatar imageberak ( 2016-11-14 08:30:30 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-11-08 09:39:17 -0600

Seen: 3,195 times

Last updated: Nov 09 '16