最近两天在做图像多标签分类,首先选用的数据集为pascal voc2012, 网上关于这个数据集的介绍有很多,此处不做过多介绍。pascal voc2012的标注格式是xml,对于图像多标签分类任务,首先需将xml文件中name标签提取出来并整理成txt格式。
07-08的数据集作为测试集,09-12的数据集作为训练集,文件夹格式为:
dataset
train
JPEGImages
annotations.txt
test
JPEGImages
annotations.txt
其中JPEGImages里装的是图片数据,annotations.txt的格式为:图片名(去除后缀) 类型名
代码如下:做了很多注释处理,很简单。
#将xml中的类别属性写在txt文件中
#09-12做训练 07-08做测试用
# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
import os
ann_filePath='D:/multi_label_classification/VOCdevkit/VOC2012/Annotations/'
train_filePath='D:/multi_label_classification/dataset/train/'
test_filePath='D:/multi_label_classification/dataset/test/'
xml_list=os.listdir(ann_filePath)
xml_name_list=[]
cls_list=[]
cls_name=""
# print(xml_list)
for xml_name in xml_list:
if xml_name[0:4]=="2007" or xml_name[0:4]=="2008":
filename=os.path.join(ann_filePath,xml_name)
xml_name=os.path.splitext(xml_name)[0] #去除后缀
xml_name_list.append(xml_name) #图片名列表
tree=ET.parse(filename) #ElementTree对象
objs=tree.findall('object')
for obj in objs:
cls=obj.find('name').text
cls_name+=" "
cls_name+=cls
# cls_list.append(cls)
# 去除重复名字
[cls_list.append(x) for x in cls_name.split() if x not in cls_list]
cls_name=' '.join(cls_list)
with open("test.txt","a") as f:
f.write(xml_name+' '+cls_name+'\n')
cls_list=[]
cls_name=""
else:
filename=os.path.join(ann_filePath,xml_name)
xml_name=os.path.splitext(xml_name)[0]
xml_name_list.append(xml_name) #图片名列表
tree=ET.parse(filename) #ElementTree对象
objs=tree.findall('object')
for obj in objs:
cls=obj.find('name').text
cls_name+=" "
cls_name+=cls
# cls_list.append(cls)
#去除重复名字
[cls_list.append(x) for x in cls_name.split() if x not in cls_list]
cls_name=' '.join(cls_list)
with open("train.txt","a") as f:
f.write(xml_name+' '+cls_name+'\n')
cls_list=[]
cls_name=""
有什么不懂得可以评论区提问,看到必回。