函数不懂的参考:imagenet数据格式以及python文件处理的一些函数
import glob
import os
from shutil import move
from os import rmdir
target_folder = './imagenet/tiny-imagenet-200/val/'
val_dict = {}
with open('./imagenet/tiny-imagenet-200/val/val_annotations.txt', 'r') as f:
for line in f.readlines():
split_line = line.split('\t')
val_dict[split_line[0]] = split_line[1]
print(val_dict)
paths = glob.glob(r'D:/jiaoyidi/swin_transformer/imagenet/tiny-imagenet-200/val/images/*')
for path in paths:
file = path.split('\\')[-1]
print(file)
folder = val_dict[file]
print(folder)
if not os.path.exists(target_folder + str(folder)):
os.mkdir(target_folder + str(folder))
os.mkdir(target_folder + str(folder) + '/images')
for path in paths:
file = path.split('\\')[-1]
folder = val_dict[file]
dest = target_folder + str(folder) + '/images/' + str(file)
move(path, dest)
rmdir('./imagenet/tiny-imagenet-200/val/images')
|