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

Created on 9 May 2020  路  4Comments  路  Source: eriklindernoren/PyTorch-YOLOv3

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)`
#

Most helpful comment

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

All 4 comments

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

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

no... I gave up...

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

鎴戜篃閬囧埌浜嗚繖涓棶棰橈紝鐢ㄧ殑鏄痮pencv3.4鐗堟湰鐨勶紝杈撳叆pip install --upgrade opencv-python锛屾洿鏂板埌opencv4.4锛岄噸鏂拌繍琛岀▼搴忓氨濂戒簡

Was this page helpful?
0 / 5 - 0 ratings