AlexeyAB / darknet

YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )
http://pjreddie.com/darknet/
Other
21.79k stars 7.97k forks source link

Yolov4-tiny - support on opencv dnn #6131

Open brianng0305 opened 4 years ago

brianng0305 commented 4 years ago

I used yolov4-tiny to train my dataset for 10 hours with 0.7 average loss. I am very satisfied with the darknet test very accurate but using opencv yolo dnn to detect show nothing.

My project need yolov4-tiny running with opencv. Please help.

issued to opencv: https://github.com/opencv/opencv/issues/17749

AlexeyAB commented 4 years ago

Wait a week until they add YOLOv4-tiny to the OpenCV master branch and release OpenCV. Currently YOlOlv4-tiny is added only to 3.4 branch https://github.com/opencv/opencv/pull/17733

brianng0305 commented 4 years ago

Thank you AlexeyAB.

brianng0305 commented 4 years ago

opencv github updated yolov4-tiny dnn https://github.com/opencv/opencv_extra/pull/770/commits/ba751a8775cdfa1ed54de2232bb8ed239a3ceece

ark- commented 4 years ago

I believe it's been merged into master now as per https://github.com/opencv/opencv/commit/524a2fffe96195b906a95b548b0a185d3251eb7e

The file modules/dnn/perf/perf_net.cpp

Should we still parse the blobs the same way as Yolov3? As described here: https://github.com/opencv/opencv/blob/8c25a8eb7b10fb50cda323ee6bec68aa1a9ce43c/samples/dnn/object_detection.py#L129-L150

I'm currently having issues trying to parse the output from net.forward

AlexeyAB commented 4 years ago

Yes, all should be the same.

As I understand this test is passed: https://github.com/opencv/opencv/blob/master/modules/dnn/test/test_darknet_importer.cpp#L167-L267

I'm currently having issues trying to parse the output from net.forward

What issue do you get?

ark- commented 4 years ago

What issue do you get?

I was getting 0 confidence scores using the same code I had previously. Digging into the detection data I note the scores are in a different location. To get it working I had to change

scores = detection[5:]

to

scores = detection[4:]

This seems to give me some predictions which make sense but I haven't tested properly. Unfortunately I can't dig any deeper until tomorrow. Happy to provide more info then.

ark- commented 4 years ago

After reviewing today it must have been something else which fixed my issue.

As per the comment on this line here, If the output of net.forward() a Region layer output could be interpreted as:

[center_x, center_y, width, height, scores1, scores2, ..., scoresN]

However looking at the code I can only see elements 0-3 and then 5 onwards being written to https://github.com/opencv/opencv/blob/master/modules/dnn/src/layers/region_layer.cpp#L308-L313. So format must be:

[center_x, center_y, width, height, ???, scores1, scores2, ..., scoresN]

No idea what element 4 is...

AlexeyAB commented 4 years ago

0-3: x,y,w,h https://github.com/opencv/opencv/blob/524a2fffe96195b906a95b548b0a185d3251eb7e/modules/dnn/src/layers/region_layer.cpp#L308-L311

4: T0_objectness https://github.com/opencv/opencv/blob/524a2fffe96195b906a95b548b0a185d3251eb7e/modules/dnn/src/layers/region_layer.cpp#L301-L302

5 - 84: probability for each class https://github.com/opencv/opencv/blob/524a2fffe96195b906a95b548b0a185d3251eb7e/modules/dnn/src/layers/region_layer.cpp#L313-L317

You should use [5 - 84] for class probabilities

ark- commented 4 years ago

Oh no, [4] was JUST above where I was looking. So close. This was my first time looking at OpenCV code under the hood so took some navigating. Thanks very much for clarifying @AlexeyAB.

Now I need to look up what objectness is...

brianng0305 commented 4 years ago

I installed opencv 4.4 but I cannot use yolov4-tiny via python and opencv. Is there any difference to previous version?

May share the code?

not-matthias commented 4 years ago

I installed opencv 4.4 but I cannot use yolov4-tiny via python and opencv. Is there any difference to previous version?

May share the code?

I'm not sure if there's a difference but I wanted to have CUDA support so I had to build it from source anyway. I simply cloned the https://github.com/opencv/opencv.git which is 4.4.-dev.

The v4.4 release also supports yolov4-tiny since it has originally been added to the 3.4 branch (https://github.com/opencv/opencv/pull/17733) which has been merged into the master branch (https://github.com/opencv/opencv/pull/17769).

brianng0305 commented 4 years ago

I installed opencv 4.4 but I cannot use yolov4-tiny via python and opencv. Is there any difference to previous version? May share the code?

I'm not sure if there's a difference but I wanted to have CUDA support so I had to build it from source anyway. I simply cloned the https://github.com/opencv/opencv.git which is 4.4.-dev.

The v4.4 release also supports yolov4-tiny since it has originally been added to the 3.4 branch (opencv/opencv#17733) which has been merged into the master branch (opencv/opencv#17769).

I would like to report opencv can run yolo-tiny4. I cannot run the code since I used the old computer to run the code(the config of my old computer is bad due to the hardware is a little bit old; another computer is using for training data).

platform: ubuntu 18.04 and anaconda