凯哥英语视频

今天一个朋友用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' % dirfileList = 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").shapewith codecs.open("new_xml/" + json_file_ + ".xml", "w", "utf-8") as xml:xml.write('<annotation>\n')xml.write('\t<folder>' + 'WH_data' + '</folder>\n')xml.write('\t<filename>' + json_file_ + ".jpg" + '</filename>\n')xml.write('\t<source>\n')xml.write('\t\t<database>WH Data</database>\n')xml.write('\t\t<annotation>WH</annotation>\n')xml.write('\t\t<image>flickr</image>\n')xml.write('\t\t<flickrid>NULL</flickrid>\n')xml.write('\t</source>\n')xml.write('\t<owner>\n')xml.write('\t\t<flickrid>NULL</flickrid>\n')xml.write('\t\t<name>WH</name>\n')xml.write('\t</owner>\n')xml.write('\t<size>\n')xml.write('\t\t<width>' + str(width) + '</width>\n')xml.write('\t\t<height>' + str(height) + '</height>\n')xml.write('\t\t<depth>' + str(channels) + '</depth>\n')xml.write('\t</size>\n')xml.write('\t\t<segmented>0</segmented>\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:passelif ymax <= ymin:passelse:xml.write('\t<object>\n')xml.write('\t\t<name>' + labelName + '</name>\n')xml.write('\t\t<pose>Unspecified</pose>\n')xml.write('\t\t<truncated>1</truncated>\n')xml.write('\t\t<difficult>0</difficult>\n')xml.write('\t\t<bndbox>\n')xml.write('\t\t\t<xmin>' + str(int(xmin)) + '</xmin>\n')xml.write('\t\t\t<ymin>' + str(int(ymin)) + '</ymin>\n')xml.write('\t\t\t<xmax>' + str(int(xmax)) + '</xmax>\n')xml.write('\t\t\t<ymax>' + str(int(ymax)) + '</ymax>\n')xml.write('\t\t</bndbox>\n')xml.write('\t</object>\n')print(json_filename, xmin, ymin, xmax, ymax, label)xml.write('</annotation>')
4.然后整理整理,就可以train模型了

别的也没啥说的

ok,那就这样吧~

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

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

  1. size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([9

    1. 报错 RuntimeError: Error(s) in loading state_dict for FasterRCNN: size mismatch for roi_heads.box_p ...

  2. size mismatch for fc.weight: copying a param with shape torch.Size([1000, 2048]) from checkpoint, th

    问题描述 我想在我自己的项目更换其他的模型,下载的预训练模型出现了FC层不匹配的问题,找了好多人都写了这个点,今天总结一下: 首先我们遇到的问题如下: 他的意思是resnet50的fc层是1000分类 ...

  3. strict=False 但还是size mismatch for []: copying a param with shape [] from checkpoint,the shape in cur

    strict=False 但还是size mismatch for []: copying a param with shape [] from checkpoint,the shape in cur ...

  4. 【Python】解决CNN中训练权重参数不匹配size mismatch for fc.weight,size mismatch for fc.bias

    目录 1.问题描述 2.问题原因 3.问题解决 3.1思路1--忽视最后一层权重 额外说明:假如载入权重不写strict=False, 直接是model.load_state_dict(pre_wei ...

  5. 解决size mismatch for embedding.embed_dict.userid.weight

    文章目录 一.问题描述 二.解决方法 三.其他问题 Reference 一.问题描述 导入之前训练好的模型权重后使用模型预测时如题报错size mismatch for embedding.embed ...

  6. size mismatch for xx.weight错误的解决方法

    问题重现: RuntimeError: Error(s) in loading state_dict for xxxNet:size mismatch for bn1.weight: copying ...

  7. 2021-05-31 size mismatch for transformers copying a param

    size mismatch for transformers copying a param with shape torch.Size from checkpoint, the shape in c ...

  8. pytorch搭建cnn报错:RuntimeError: size mismatch, m1: [10 x 43264], m2: [10816 x 2] at C...

    具体报错信息: Traceback (most recent call last):File "E:/Program Files/PyCharm 2019.2/machinelearning ...

  9. 做项目遇到问题 2 AWS NLP 剽窃RuntimeError: size mismatch, m1: [10 x 3], m2: [2 x 10]检测部署报错

    报错 RuntimeError: size mismatch, m1: [10 x 3], m2: [2 x 10] 原因: train.csv 为100x4    4列  第一列  标签是否剽窃  ...

最新文章

  1. C++中的指针与饮用
  2. Python 图片亮度
  3. Linux下的命令总结笔记(一)
  4. 从零开始React项目架构(四)
  5. 字符串拼接,什么时候会走StringBuilder?
  6. Delphi WebBrowser控件的使用
  7. 20190909:(leetcode习题)第一个错误的版本
  8. [js]promise学习2
  9. day16 Python 类的继承关系
  10. linux232转usb接口驱动程序,USB转RS232串口驱动程序下载
  11. Python生成舒尔特的Excel表格
  12. QT D:\搜狗输入法\SogouInput\Components\ 13:53:42: 程序异常结束。 13:53:42: T
  13. android xml 注释快捷键,xml注释(xml注释掉一段代码)
  14. 华为路由器配置Telnet登录
  15. Rtklib-rinex文件的读取(rinex.c)-序言
  16. linux上复制文件命令是什么,Linux复制文件用什么命令怎么用
  17. 计算机分析桁架受力,桁架的结构设计和受力分析
  18. Ubuntu 查看IP、网关及DNS
  19. LaTeX:求和,积分,(上、下)极限,收敛符号,上下确界等
  20. [20190328]PPT中的png图片去底色(透明化)

热门文章

  1. 2023年中国博士后科学基金资助指南发布
  2. 征信“花”一点,才更容易借到钱!
  3. 关于解决chrome浏览器由贵单位管理的方法
  4. 【涵子来信python大全】——第二季——opencv第二篇
  5. 智慧大棚Web3D可视化系统 构建高效农业场景
  6. 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级) 题解
  7. JVM垃圾回收算法及垃圾回收器
  8. NND今天终于把KD树弄懂了,花了劳资两个小时的有效时间
  9. 人工智能期末模拟沙场秋点兵(部分题,有些太模糊就不做了)
  10. 如何联系Github作者