size mismatch for yolo_head2.1.bias: copying a param with shape torch.Size(【75】) from checkpoint...

凯哥英语视频

今天一个朋友用YOLO4预测图片报错:size mismatch for yolo_head2.1.bias: copying a param with shape torch.Size(【75】) from checkpoint, the shape in current model is torch.Size(【18】).

网上有说,是在dataloader环节出现了问题。在数据读取时一个判断写的越界了,如下所示

def make_dataset(dir, opt):
    images = []
    assert os.path.isdir(dir), '%s is not a valid directory' % dir
    fileList = sorted(os.walk(dir))    
    for root, _, fnames in fileList:
        for fname in fnames:
            if is_image_file(fname):
                path = os.path.join(root, fname)
                if ((opt.phase=='test') or (opt.phase=='train') and min(Image.open(path).size) >= 512):
                    images.append(path)        
    return images

当加载的图片小于512就会报错,把判断改小就解决了bug。
所以出现这个报错,就说明是训练数据没有加载成功,检查数据加载的相关程序。

然而以上并不能解决报错

然后我结合了VOC2007的数据集,发现应该是朋友训练图片尺寸问题(VOC2007基本都是500乘以(350-450)的长宽)。

1.首先对图片进行尺寸转换
import PIL.Image as Image
infile = 'images/train/202108315.jpg'
outfile = 'images/train/1202108315.jpg'
im = Image.open(infile)
(x, y) = im.size  
x_s = 500  
y_s = int(y * x_s / x)  
out = im.resize((x_s, y_s), Image.ANTIALIAS)  
out.save(outfile)

print('infile size: ', x, y)
print('outfile size: ', x_s, y_s)
2.使用labelme进行标注,生成json文件(自行脑补)
3.将json文件转化为xml文件,然后进行训练
# -*- coding: utf-8 -*-
import numpy as np
import codecs
import json
from glob import glob
import cv2
# 1.标签路径
labelme_path = "labelme_json/"
# 保存路径
isUseTest = True  # 是否创建test集
# # 2.创建要求文件夹
# if not os.path.exists("Annotations"):
#     os.makedirs("Annotations")
# if not os.path.exists("JPEGImages/"):
#     os.makedirs("JPEGImages/")
# if not os.path.exists("ImageSets/Main/"):
#     os.makedirs("ImageSets/Main/")
# 3.获取待处理文件
files = glob(labelme_path + "*.json")
files = [i.replace("\\", "/").split("/")[-1].split(".json")[0] for i in files]
print(files)
# 4.读取标注信息并写入 xml
for json_file_ in files:
    json_filename = labelme_path + json_file_ + ".json"
    json_file = json.load(open(json_filename, "r", encoding="utf-8"))
    height, width, channels = cv2.imread('labelme_json/' + json_file_ + ".jpg").shape
    with codecs.open("new_xml/" + json_file_ + ".xml", "w", "utf-8") as xml:

        xml.write('\n')
        xml.write('\t' + 'WH_data' + '\n')
        xml.write('\t' + json_file_ + ".jpg" + '\n')
        xml.write('\t\n')
        xml.write('\t\tWH Data\n')
        xml.write('\t\tWH\n')
        xml.write('\t\tflickr\n')
        xml.write('\t\tNULL\n')
        xml.write('\t\n')
        xml.write('\t\n')
        xml.write('\t\tNULL\n')
        xml.write('\t\tWH\n')
        xml.write('\t\n')
        xml.write('\t\n')
        xml.write('\t\t' + str(width) + '\n')
        xml.write('\t\t' + str(height) + '\n')
        xml.write('\t\t' + str(channels) + '\n')
        xml.write('\t\n')
        xml.write('\t\t0\n')
        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            labelName = multi["label"]
            xmin = min(points[:, 0])
            xmax = max(points[:, 0])
            ymin = min(points[:, 1])
            ymax = max(points[:, 1])
            label = multi["label"]
            if xmax <= xmin:
                pass
            elif ymax <= ymin:
                pass
            else:
                xml.write('\t\n')
                xml.write('\t\t' + labelName + '\n')
                xml.write('\t\tUnspecified\n')
                xml.write('\t\t1\n')
                xml.write('\t\t0\n')
                xml.write('\t\t\n')
                xml.write('\t\t\t' + str(int(xmin)) + '\n')
                xml.write('\t\t\t' + str(int(ymin)) + '\n')
                xml.write('\t\t\t' + str(int(xmax)) + '\n')
                xml.write('\t\t\t' + str(int(ymax)) + '\n')
                xml.write('\t\t\n')
                xml.write('\t\n')
                print(json_filename, xmin, ymin, xmax, ymax, label)
        xml.write('')

4.然后整理整理,就可以train模型了

别的也没啥说的

ok,那就这样吧~

欢迎各位大佬留言吐槽,也可以深入交流~

你可能感兴趣的:(工作小记,pytorch,python)