关于学习yolov3建立自己数据集所遇问题AttributeError: 'NoneType' object has no attribute 'xxxx' 的解决

关于学习yolov3建立自己数据集所遇到的问题AttributeError: ‘NoneType’ object has no attribute ‘xxxx’ 的解决

笔者自己主要是因为遇到了
‘NoneType’ object has no attribute ‘find’
‘NoneType’ object has no attribute ‘text’
这两个错误,搜查了解后解决
这个问题最主要是看自己的xml文件中有没有对应属性

在convert_annotation函数的定义中有很多属性,需要我们根据自己的数据集标注后的结果来确定怎么修改。

  • 原来的convert_annotation函数如下:
def convert_annotation(image_id):
    in_file = open('data/Annotations/%s.xml' % (image_id))
    out_file = open('data/labels/%s.txt' % (image_id), 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)
 
    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes or int(difficult) == 1:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

举例子:
笔者遇到’NoneType’ object has no attribute 'find’是因为xml中没有size的weight和height,最后导致

w = int(size.find(‘width’).text)
h = int(size.find(‘height’).text)
二者为空,解决办法是给所有的xml中添加属性weight和height,或者直接把对应数据集图片大小,设置给w,h,如:w=1000,h=600.
又或者参考这个超链接中博主的修改方式修改xml文件:
YOLOv2训练自己的数据集(VOC格式)

笔者遇到’NoneType’ object has no attribute 'txet’是因为xml中没有difficult这个属性内容,最后导致

difficult = obj.find(‘difficult’).text没起作用
因此解决办法就是看自己的xml文件中是否存在这一个属性,没有的话直接把含有difficult的语句注释掉即可,当然也有的是xml中含有Difficult,其中D是大写,只要把程序中含有difficult的句子中的difficult变为Difficult即可。

最后总结:笔主还是知识面太窄,了解太片面了
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值