在我们做YOLO类检测网络的自定义训练时,有时会将多个独立的数据集合并训练,但往往遇到一个问题,比如两个独立的数据集中有相同的一类,比如船。但是在一个数据集中船的标注文件(txt文件)的索引为0,在另一个数据集中的索引为1,这样就不能直接合并在一起。需要将某一个数据集中的标签做修改后再合并。
修改标签的小demo如下:
import os
import re
path = 'C:/Users/aa/Desktop/img/' // txt文件路径
files = []
for file in os.listdir(path):
if file.endswith(".txt"):
files.append(path+file)
for file in files:
with open(file, 'r') as f:
new_data = re.sub('^0', '1', f.read(), flags=re.MULTILINE) # 将列中的0替换为1
print("