说明:训练的过程,参考了DeepLung的训练形式,进行交叉训练。但不太明白,交叉训练有什么意义和优势?欢迎大家告知。
1、将注释文件(.xml)转化为YOLO格式:xml2txt.py
classes = ["nodule"]
# 定义一个函数,将坐标信息转换为YOLO格式
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return x, y, w, h
# 定义一个函数,接受 XML 文件和输出的文本文件作为参数
def convert_annotation(xml_file, txt_file):
in_file = open(xml_file, 'r') # 打开 XML 文件,'r' 表示读取模式
out_file = open(txt_file, 'w') # 打开输出的文本文件,'w' 表示写入模式
tree = ET.parse(in_file) # 使用 ElementTree 解析 XML 文件
root = tree.getroot() # 获取 XML 树的根节点
size = root.find('size') # 在根节点中找到 'size' 元素
w = int(size.find('width').text) # 获取图像宽度
h = int(size.find('height').text) # 获取图像高度
# 遍历 XML 文件中的每个 'object' 元素
for obj in root.iter('object'):
difficult = 0 # difficult默认为0
cls = obj.find('name').text # 获取 'name' 元素的文本内容,即物体类别
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls) # 获取类别在类别列表中的索引
xmlbox = obj.find('bndbox') # 获取 'bndbox' 元素
b = (
float(xmlbox.find('xmin