简体   繁体   中英

How to train or merge multiple .svm and detect multiple classes using dlib

I would like to do a very simple example: train using dlib to detect "cat" and "dog" (two classes) and provide box coordinate.

So far the example I found is to only train with one class and produce one .svm file: http://dlib.net/train_object_detector.cpp.html

I am not good at C++ (but I can learn) and I prefer to do things in Python. After several days of research (I'm new in Deep Learning as well), I figured I have to change these lines:

object_detector<image_scanner_type> detector = trainer.train(images, object_locations, ignore);
serialize("object_detector.svm") << detector;

So that I should use below in http://dlib.net/dlib/image_processing/object_detector_abstract.h.html :

explicit object_detector (
  const std::vector<object_detector>& detectors
);

Questions:

  1. I need to produce a .dat file like the Face landmarks detection here http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 . So how do I train and serialize at once or combine .svm files afteward?

  2. Then I need to run a detection to detect all .svm inside the .dat file. Can I get an example how to do this with C++ or Python?

Thanks.

I can tell you how to do so it in C++. Than I hope you should be able to figure out how to you can do it in Python.

You don't need to have single .dat file to do so. You can train detectors separately and save to separate files. This allows you add new detectors, replace existing etc. without retraining. After you need to create vector of detectors. ie:

std::vector<object_detector> detectors(3);
dlib::deserialize(detectors[0], "object_detector.svm");
dlib::deserialize(detectors[1], "object_detector_2.svm");
dlib::deserialize(detectors[2], "object_detector_3.svm");

And then you run detection like this:

std::vector<dlib::rect_detection> detections;
dlib::evaluate_detectors(detectors, image, detections);

Then you can access detected objects this:

for (auto& det : detections) {
    det.rect;          // found rectangle
    det.weight_index;  // detector index in vector (to indentify object class)
}

In my case running all detectors (5) at once executes about 2.5-3 times faster than running them sequentially. But it will depend on how similar detection windows are for each of detector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM