关于学习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即可。