How to use readNet (or readFromDarknet) instead of readNetFromCaffe?
I did an object detection using opencv by loading pre-trained MobileNet SSD model. from this post.
It reads a video and detects objects without any problem. But I would like to use readNet
(or readFromDarknet
) instead of readNetFromCaffe
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
because I have pre-trained weights and cfg file of my own objects only in Darknet framework. Therefore I simply changed readNetFromCaffe
into readNet
in above post and got an error:
Traceback (most recent call last):
File "people_counter.py", line 124, in <module>
for i in np.arange(0, detections.shape[2]):
IndexError: tuple index out of range
Here detections
is an output from
blob = cv2.dnn.blobFromImage(frame, 1.0/255.0, (416, 416), True, crop=False)
net.setInput(blob)
detections = net.forward()
Its shape is (1, 1, 100, 7) tuple (when using readNetFromCaffe
).
I was kinda expecting it wouldn't work just by changing the model. Then I decided to look for an object detector code where readNet
was used and I found it here. I read through the code and found the same lines as follows:
blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(get_output_layers(net))
Here, the shape of outs
is (1, 845, 6) list. But in order for me to be able to use it right away (here), outs
should be of the same size with detections
. I've come up to this part and have no clue about how I should proceed.
If something isn't clear, I just need help to use readNet
(or readFromDarknet
) instead of readNetFromCaffe
in this post
darknet can train several different architectures (and e.g. the output shapes from yolo2 and yolo3 will differ), so please be more specific about your model.
and maybe you should use code from here not things you find in blogposts on the net.
@berak, my cfg file is very similar to this and weights file is a pre-trained one of yolo-voc.weights. The reason I am using that is it gives everything I need but it uses caffe, I'd like to change it Darknet's yolo for above mentioned reasons. Could you help to get solve this issue?
again, different architectures require different handling. have a look e.g. here
I had a look, thank you for that reference, I do understand that. Since Darknet produces different output than Caffe, do you mean I can't use Darknet's output inside this post? I am asking about some code modifications (that I have to do ) inside the post so that I can handle with Darknet's blob output. Sorry if I couldn't deliver what I mean.
i won't read any blogposts, to clear up your confusion (no time for that)
but mobilenet and yolo v2 / v3 have each a different output structure, and you need to adapt your code respectively. (even yolo v2 / v3 differ, and you're unclear about, what you're using, exactly !)
@berak thank you for your time, let me work on that for some time and if I success I'll come and leave an answer.
@voo_doo , -- that would be perfect ! ;)
@berak, I could solve the issue by looking at the different architectures as you referred to. Please visit this flow if you are interested. Thanks
@voo_doo , if you could do a short writeup here, that would be really useful for this site. a link to SO - not so much.