数据集下载地址 https://www.robots.ox.ac.uk/%7Evgg/data/pets/
数据集下载后文件组织如下:
Oxford-IIIT Pet
--annotations
--trainval.txt
--test.txt
--images
Abyssinian_1.jpg
...
图片未根据类别分组,用代码处理
import os
import shutil
from pathlib import Path
file_type = ['trainval', 'test']
base_path = '../pets/' #标签train等文件的父目录
images = './images/' #总的图像文件路径
for tp in file_type:
annotation_path = base_path + 'annotations/' + tp+ '.txt'
print(annotation_path)
for line in open(annotation_path):
file = line.split()[0]
name = file.split("_")[0]
print(name)
photo = os.path.join(base_path + 'images', file)
photo = photo + '.jpg'
print(photo)
if os.path.exists(os.path.join(base_path + tp, name)):
shutil.move(photo, os.path.join(base_path + tp, name))
else:
os.mkdir(os.path.join(base_path + tp, name))
shutil.move(photo, os.path.join(base_path + tp, name))
完成如标签txt的分割方式分组
Oxford-IIIT Pet
--trainval
--Abyssinian
--american
...
--test
--Abyssinian
--american
...