Cannot detect cars

asked 2019-09-14 01:21:24 -0500

crobits_team gravatar image

I am trying to create my own cascade to detect cars. I have 2000 negative images. By using following command i am creating samples:

opencv_createsamples -img positive_images/1_car.jpg -bg bg.txt -info info/info.lst -pngoutput info -maxxangle 0.5 -maxyangle 0.5 -maxzangle 0.5 -num 1000

Then vec file is created with following command:

opencv_createsamples -info info/info.lst -num 1000 -w 20 -h 20 -vec positives.vec

And then:

opencv_traincascade -data data -vec positives.vec -bg bg.txt -numPos 1000 -numNeg 2000 -numStages 10 -w 20 -h 20

After the cascade is created, the results are very bad. I mean there are only false positives. But when i detect the samples created above in info folder, it detects that object and very low number of false positives are detected.

I also tried using opencv_annotation tool. I downloaded around 8000 positive images and created 400 samples using this tool. But the results are same. I mean i don't know what to do now.

What am i doing wrong?

Is it the number of samples?

Is it width or height?

Please suggest a solution.

edit retag flag offensive close merge delete



you can find many ready and better solutions like

sturkmen gravatar imagesturkmen ( 2019-09-14 02:54:47 -0500 )edit

Yes pretrained models are available for this.

holger gravatar imageholger ( 2019-09-14 09:26:47 -0500 )edit

I know models are available for this but i need to create my own cascade and this the first part. Further on i have to detect license plates of cars as well. And i know for license plates, models are available too, but only for some countries. I need to be able to train my model for anything that i want to detect.

crobits_team gravatar imagecrobits_team ( 2019-09-16 00:11:00 -0500 )edit

So your main concern is that you want to able to train on custom data? Well you can retrain these model from scratch on your custom data or fine tune theme(transfer learning).

But then you have another technology stack. For me this was the way to go. I use opencv dnn module sometimes in the case i have no gpu support available, otherwise i use tensorflow/yolo

holger gravatar imageholger ( 2019-09-17 03:13:25 -0500 )edit

@holger thanks for your comment. I am also working on tensorflow but i want to be able to train on custom data in opencv. I have listed all the steps in my question, can you please point where am i missing something or doing something wrong?

crobits_team gravatar imagecrobits_team ( 2019-09-18 00:23:10 -0500 )edit

Sigh - for gods sake if you reall insist on training on opencv (for what reason ever): Lets take a look what your wrote:

After the cascade is created, the results are very bad. I mean there are only false positives. But when i detect the samples created above in info folder, it detects that object and very low number of false positives are detected.

So you are saying the accuracy is bad on new samples(it has never seen before) but good on training samples(it has seen before and was trained on). In machine learning this phenomenon is called "overfitting". What it means is that your model fails to generalize but is just "remembering" the training samples. So supply a bigger variety of lumination, rotation, sizes, backgrounds for your objects to fight this. Or train shorter.

holger gravatar imageholger ( 2019-09-18 07:41:35 -0500 )edit

using opencv_annotation tool i created 400 samples of cars and provided 1000 background images of roads and 10 stages to train but still i got the same results. Background images are 100x100 and positive images are 50x50. And the reason i want to train on opencv is because i have been assigned this task. And can you please a link to tutorial or something for retraining model in opencv?

crobits_team gravatar imagecrobits_team ( 2019-09-19 00:13:29 -0500 )edit

Well 400 samples is not a huge amount. Easy to overfit. Regarding your request - Please forget about training a neuronal network in opencv, its just not possible.

Opencv can do inference (predictions) but not training. You would have to do this in the framework of your choice (tensorflow, pytorch, yolo - just to name some)

And if someone asks me to do a thing which makes no sense i am saying clearly it make no sense(and also why - in this case low accuracy / high errors).

So i recommend you to get familar in the area of object detection (maybe do a online course on coursera) and then build a decent model which does the job. For me it took 8 month to get familar, but i am slow learner and not the reference :-)

holger gravatar imageholger ( 2019-09-19 14:44:27 -0500 )edit

Thank you for clearing everything. Currently i am working on PyTorch and trying to train a model which performs the task with "good" accuracy.

crobits_team gravatar imagecrobits_team ( 2019-09-19 23:46:22 -0500 )edit

+1 for Pytorch This is a good choise for learning too as its pretty "imperative" - theres not so much magic hidden.

Good Luck with your task - dont be disappointed if you dont get the results you expect on your first try. This whole machine learning has a steep learning curve - give it some time.

holger gravatar imageholger ( 2019-09-20 02:46:47 -0500 )edit