Tianxiaomo / pytorch-YOLOv4

PyTorch ,ONNX and TensorRT implementation of YOLOv4
Apache License 2.0
4.46k stars 1.49k forks source link

error for loading state_dict for YOLOv4 #339

Open fx7056 opened 3 years ago

fx7056 commented 3 years ago

image 运行model.py文件时报错,是什么?是不是没有指定配置文件?

a28293971 commented 3 years ago

please, more information

liangsiping2019 commented 3 years ago

我这边也遇到了这个问题,测试了一下,发现: (1)多个GPU训练的模型,使用models.py去测试的时候,就会报这个错误,根据网上的说法,加了一个参数strict=False,最后识别没有框的输出。 (2)单个GPU训练的模型,就不会出现这个问题,但是训练的速度比较慢。

Liu-Yuanqiu commented 3 years ago

我也遇到了这个问题,多GPU训练时使用models.py会报错,加上参数不报错但是没有识别框的输出,请问有解决的办法了吗?

liangsiping2019 commented 3 years ago

我也遇到了这个问题,多GPU训练时使用models.py会报错,加上参数不报错但是没有识别框的输出,请问有解决的办法了吗? 暂时了解到问题在哪,但是还没有时间去试过,是pytorch的坑,多GPU训练的时候,会在head前加个moudle层,,网上也有人遇到过这个问题,你可以去看看怎么去掉这个多余的层。

j22054022 commented 1 year ago

I also encounter this error in google colab by executing, also I used same dependency version as requirements.txt, any help?

%cd /content

import torch  
import sys  
sys.path.append('/content/pytorch-YOLOv4')  
from models import Yolov4  

the_model = Yolov4()  
the_model.load_state_dict(torch.load("/content/yolov4.pth")) 

RuntimeError: Error(s) in loading state_dict for Yolov4: Missing key(s) in state_dict: "neck.conv1.conv.0.weight", "neck.conv1.conv.1.weight", "neck.conv1.conv.1.bias", "neck.conv1.conv.1.running_mean", "neck.conv1.conv.1.running_var", "neck.conv2.conv.0.weight", "neck.conv2.conv.1.weight", "neck.conv2.conv.1.bias", "neck.conv2.conv.1.running_mean", "neck.conv2.conv.1.running_var", "neck.conv3.conv.0.weight", "neck.conv3.conv.1.weight", "neck.conv3.conv.1.bias", "neck.conv3.conv.1.running_mean", "neck.conv3.conv.1.running_var", "neck.conv4.conv.0.weight", "neck.conv4.conv.1.weight", "neck.conv4.conv.1.bias", "neck.conv4.conv.1.running_mean", "neck.conv4.conv.1.running_var", "neck.conv5.conv.0.weight", "neck.conv5.conv.1.weight", "neck.conv5.conv.1.bias", "neck.conv5.conv.1.running_mean", "neck.conv5.conv.1.running_var", "neck.conv6.conv.0.weight", "neck.conv6.conv.1.weight", "neck.conv6.conv.1.bias", "neck.conv6.conv.1.running_mean", "neck.conv6.conv.1.running_var", "neck.conv7.conv.0.weight", "neck.conv7.conv.1.weight", "neck.conv7.conv.1.bias", "neck.conv7.conv.1.running_mean", "neck.conv7.conv.1.running_var", "neck.conv8.conv.0.weight", "neck.conv8.conv.1.weight", "neck.conv8.conv.1.bias", "neck.conv8.conv.1.running_mean", "neck.conv8.conv.1.running_var", "neck.conv9.conv.0.weight", "neck.conv9.conv.1.weight", "neck.conv9.conv.1.bias", "neck.conv9.conv.1.running_mean", "neck.conv9.conv.1.running_var", "neck.conv10.conv.0.weight", "neck.conv10.conv.1.weight", "neck.conv10.conv.1.bias", "neck.conv10.conv.1.running_mean", "neck... Unexpected key(s) in state_dict: "neek.conv1.conv.0.weight", "neek.conv1.conv.1.weight", "neek.conv1.conv.1.bias", "neek.conv1.conv.1.running_mean", "neek.conv1.conv.1.running_var", "neek.conv1.conv.1.num_batches_tracked", "neek.conv2.conv.0.weight", "neek.conv2.conv.1.weight", "neek.conv2.conv.1.bias", "neek.conv2.conv.1.running_mean", "neek.conv2.conv.1.running_var", "neek.conv2.conv.1.num_batches_tracked", "neek.conv3.conv.0.weight", "neek.conv3.conv.1.weight", "neek.conv3.conv.1.bias", "neek.conv3.conv.1.running_mean", "neek.conv3.conv.1.running_var", "neek.conv3.conv.1.num_batches_tracked", "neek.conv4.conv.0.weight", "neek.conv4.conv.1.weight", "neek.conv4.conv.1.bias", "neek.conv4.conv.1.running_mean", "neek.conv4.conv.1.running_var", "neek.conv4.conv.1.num_batches_tracked", "neek.conv5.conv.0.weight", "neek.conv5.conv.1.weight", "neek.conv5.conv.1.bias", "neek.conv5.conv.1.running_mean", "neek.conv5.conv.1.running_var", "neek.conv5.conv.1.num_batches_tracked", "neek.conv6.conv.0.weight", "neek.conv6.conv.1.weight", "neek.conv6.conv.1.bias", "neek.conv6.conv.1.running_mean", "neek.conv6.conv.1.running_var", "neek.conv6.conv.1.num_batches_tracked", "neek.conv7.conv.0.weight", "neek.conv7.conv.1.weight", "neek.conv7.conv.1.bias", "neek.conv7.conv.1.running_mean", "neek.conv7.conv.1.running_var", "neek.conv7.conv.1.num_batches_tracked", "neek.conv8.conv.0.weight", "neek.conv8.conv.1.weight", "neek.conv8.conv.1.bias", "neek.conv8.conv.1.running_mean", "neek.conv8.conv....

HODOOpyupya commented 1 month ago

I encountered this one too. In my case, I found torch pth file's keys what include "neek" not "neck". I assumed that's the problem. So, I loaded the pth file by torch load, created new OrderedDict(), and created New torch model. loaded model was yolov4.pth, downloaded from google drive link. then, It works. If you need, use code under here.

import torch
from collections import OrderedDict

from torch import nn

file_path = r"path/to/yolov4.pth"
save_path = r"path/to/save/yolov4_convert.pth"

device = "cuda" if torch.cuda.is_available() else "cpu"
model = torch.load(file_path, map_location=device)

new_dict = OrderedDict()

for i in model:
    if "neek" in i:
        new_i = i.replace("neek", "neck")
        new_dict[new_i] = model[i]
    else:
        new_dict[i] = model[i]

print(new_dict)
torch.save(new_dict, save_path)