darknet icon indicating copy to clipboard operation
darknet copied to clipboard

Getting 'Nan' errors for net.forward(ln)

Open reemastha706 opened this issue 2 years ago • 2 comments

I am trying to work on object detection in yolo. I trained my data using darknet using custom dataset, and it worked(at least i feel so). however, when I print the net.forward(ln) code, it gives me Nan output.

from google.colab.patches import cv2_imshow
import cv2
import numpy as np
import time
from matplotlib import pyplot as plt

path = '/content/test.jpeg'
img = cv2.imread(path)
plt.imshow(img)


# Give the configuration and weight files for the model and load the network.
net = cv2.dnn.readNetFromDarknet('/content/drive/MyDrive/yolo/darknet/cfg/yolov3.cfg', '/content/drive/MyDrive/yolo/custom_data/custom_weight/darknet53.conv.74')
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
# # net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

ln = net.getLayerNames()
# # print(len(ln), ln)

ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
print(ln)
# construct a blob from the image
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
r = blob[0, 0, :, :]

# cv2_imshow('blob', r)
# text = f'Blob shape={blob.shape}'
# cv.displayOverlay('blob', text)
# cv.waitKey(1)

net.setInput(blob)
t0 = time.time()
outputs = net.forward(ln)
print(outputs)

Actually, it had worked in the beginning, and now I don't know what happened.
output:

['yolo_82', 'yolo_94', 'yolo_106']
[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), 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 tried re-training, still not working. Anyone who could suggest a possible reason/solution?

reemastha706 avatar Apr 09 '22 23:04 reemastha706

SAME QUESTION! Can anybody help?.

I use ./darknet detector test [label_file] yolov3.cfg yolov3.weights helmet.jpg

It didn't detect, and I checked the feature maps, finding that in the backbone stage, it produced -nan,

but, when I use the same format for other tasks like./darknet detector test [label_file] data/yolov3.cfg data/yolov3.weights helmet.jpg. It performs just fine.

Lee-ray-a avatar Apr 18 '22 12:04 Lee-ray-a

is there any fix?

MADDY312 avatar Apr 20 '23 15:04 MADDY312