前段时间是将使用mmdetection中的Faster RCNN config 进行训练,将里边的L1 loss 换成了CIOU loss,训练的时候一显示loss值为nan,查看了情况,发现是因为xml文件中的含有候选框高宽为0的情况,在网上查没有找到相应去除高宽为零的候选框的代码,为此自己写了一个。
"""
read xml file and delete the object whose height or width is zero
"""
import os
import xml.etree.ElementTree as ET
from xml.dom.minidom import parse
from xml.etree.ElementTree import ElementTree,Element
import numpy as np
# file road
xml_ano = r'J:\ZNZ\数据预处理\5.统计标记框长宽比\anchor_ratio\Annotations2'
xml_ano1 = r'J:\ZNZ\数据预处理\5.统计标记框长宽比\anchor_ratio\Annotations2'
xml_list = os.listdir(xml_ano)
for xml_pa in xml_list:
xml_path = xml_ano + '/' + xml_pa#xml文件的绝对路径
tree = ET.parse(xml_path)
root = tree.getroot()
# print(root)
nodes = root.findall('object')
# print(nodes)
for element1 in root.findall('object'):
# print(element1)
for element2 in element1.findall('bndbox'):
xmin = element2.find('xmin').text
ymin= element2.find('ymin').text
xmax = element2.find('xmax').text
ymax = element2.find('ymax').text
mult = (int(xmax) - int(xmin)) * (int(ymax) - int(ymin))
if mult == 0:
# print(xml_path)
root.remove(element1)
print(xmin, ymin, xmax, ymax)
tree.write(xml_ano1+'/' + xml_pa, encoding="utf-8",xml_declaration=True)