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

大城市里の小女人 提交于 2019-12-24 06:07:40

问题


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.


回答1:


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. i.e.:

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.



来源:https://stackoverflow.com/questions/44465354/how-to-train-or-merge-multiple-svm-and-detect-multiple-classes-using-dlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!