关于 AttributeError: ‘NoneType‘ object has no attribute ‘text‘ 的三种解决方法

今天训练模型时,报错:AttributeError: 'NoneType' object has no attribute 'text'

经查阅,错因在于 .xml 文件,并总结了三种方法

一、原因

原因是标注文件 .xml的<object>没有<difficult>的标签。

difficlut 表明这个待检测目标很难识别,有可能是虽然视觉上很清楚,但是没有上下文的话还是很难确认它属于哪个分类;标为difficult的目标在测试成绩的评估中一般会被忽略

二、解决方法(方法二最高效)

方法一、在.xml标注文件中把没有difficlut标签的补上。

== > 一般数据集不在少数,一个一个加太耗时了

方法二、忽略这个标签,在代码里把difficlut置为0

(1)找到报错的地方

 

 (2)发现 difficult 的值通过 object.find 找到,那么我们就添加一句判断,如果找不到(即处理 .xml文件 无 difficult标签 的情况),那就默认为0

if object.find('difficult'):
    difficult = float(object.find('difficult').text)
else:
    difficult = 0

修改后:

可以正常训练了

 

方法三、批量对数据集中xml文件做处理

import xml.etree.ElementTree as ET
from os import getcwd

sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')]

classes = ["hat","person"]


def convert_annotation(year, image_id, list_file):
    in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id),encoding='utf-8')
    tree=ET.parse(in_file)
    root = tree.getroot()
    a=1
    i=a
    

    for obj in root.iter('item'):
        #difficult = obj.find('').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 = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
        #print(image_id)
        list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
    
    for obj in root.iter('object'):#找xml文档里的object 
        try:
            difficult = obj.find('difficult').text#找object对里的difficult对  在不同格式里可能找不到
            cls = obj.find('name').text
            if cls not in classes or int(difficult)==1:
                continue
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            #print(image_id)
            b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text))
            list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id))
            
        except Exception:
            print(i,end="")

wd = getcwd()

for year, image_set in sets:
    image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set),encoding='utf-8').read().strip().split()
    list_file = open('%s_%s.txt'%(year, image_set), 'w',encoding='utf-8')
    for image_id in image_ids:
        list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg'%(wd, year, image_id))
        convert_annotation(year, image_id, list_file)
        list_file.write('\n')
    list_file.close()

  • 27
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DLNovice

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值