eriklindernoren / PyTorch-YOLOv3

Minimal PyTorch implementation of YOLOv3
GNU General Public License v3.0
7.32k stars 2.63k forks source link

Got lots of nan when I'm trying to load weights file converted from pth in opencv-DNN... #497

Closed sean-wade closed 3 years ago

sean-wade commented 4 years ago

This is the opencv result:

image

But when I'm using the "weights_init_normal" weights, the result is not nan......

Like this:

image

This is my transfer code:

######################################################################### ` device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Initiate model
model = Darknet(opt.m).to(device)

#if I'm using this one , result is well...
#model.apply(weights_init_normal)

if opt.w.endswith(".weights"):
    # Load darknet weights
    model.load_darknet_weights(opt.w)
else:
    # Load checkpoint weights
    #model.load_state_dict(torch.load(opt.w))
    model.load_state_dict(torch.load(opt.w, map_location=torch.device('cpu')))

#model.save_darknet_weights("test.weights")
Darknet.save_darknet_weights(model, 'newYolov3.weights', cutoff=-1)`

#########################################################################

#########################################################################

This is the opencv code:

` import os import cv2 as cv

def getOutputsNames(net):
    layersNames = net.getLayerNames()
    return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]

#Initialize the parameters
confThreshold = 0.01  # Confidence threshold
nmsThreshold = 0.3  # Non-maximum suppression threshold
inpWidth = 416       # Width of network's input image
inpHeight = 416      # Height of network's input image

modelConfiguration = "config/yolov3-mytiny.cfg" 
modelWeights = "./newYolov3.weights"

net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) 

frame = cv.imread("1.jpg")
blob = cv.dnn.blobFromImage(frame, 1./255, (inpWidth, inpHeight), [0,0,0], 1, crop=False)
print(blob)
net.setInput(blob)
outs = net.forward(getOutputsNames(net))

print(outs)`

#########################################################################

Adrian-Fleck commented 4 years ago

hi I have the same problem, did you manage to solve it ?

sean-wade commented 4 years ago

hi I have the same problem, did you manage to solve it ?

no... I gave up...

zerocool95 commented 4 years ago

I got this error on an older version of opencv. Upgraded the version to the latest version opencv(4.4) and it worked like a charm !

pip install --upgrade opencv-python

hpc203 commented 4 years ago

我也遇到了这个问题,用的是opencv3.4版本的,输入pip install --upgrade opencv-python,更新到opencv4.4,重新运行程序就好了