Search code examples
opencvcomputer-visionconv-neural-networkyolotransfer-learning

Tiny yolo v4 usage with opencv gives no detection


I have trained yolo-tiny-v4 on custom dataset on google colab and the detection works well . Then I've tried to load the yolo-tiny-v4 in other colab project with help of opencv's dnn module, no error appears, but the detection fails (no object detected, the output of the detection is a vector of Nan).

[array([[nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    ...,
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.]], dtype=float32),
array([[nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    ...,
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.],
    [nan, nan, nan, nan, nan,  0.]], dtype=float32)]

I'm using OpenCV Version 4.5.1 and Python 3.7 on colab. Any idea ?

This is the code

#Load YOLO
net = cv2.dnn.readNetFromDarknet("/content/custom-yolov4-tiny-detector.cfg","/content/custom-yolov4- 
tiny-detector_last.weights")
classes = []  
with open("obj.names","r") as f:
classes = [line.strip() for line in f.readlines()]
net.getLayerNames()
layer_names = net.getLayerNames()
outputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
#loading image
img = cv2.imread("/content/1.png")
#img = cv2.resize(img,None,fx=0.4,fy=0.3)
height,width,channels = img.shape
cv2_imshow(img)
blob = cv2.dnn.blobFromImage(img,0.00392,(416,416),(0,0,0),True,crop=False)
net.setInput(blob)
outs = net.forward(outputlayers)
outs

Solution

  • As a beginner i forgot to install darknet in colab