本文主要是介绍2020.3.31 miniImagenet数据集处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
miniImagenet数据集的结构为:所有的照片放在一个文件夹中,另外有三个csv文件分别是:train.csv,val.csv,test.csv,三个csv文件都有两列,第一列是文件名,第二列是标签。我在使用数据集时,把train数据,test数据,val数据分别都保存到对应的标签下。
处理后的目录结构如下:
具体处理代码:
import csv
import os
from PIL import Image
train_csv_path="C:/Users/MMatx/Desktop/研究生/mini-imagenet/mini-imagenet/train.csv"
val_csv_path="C:/Users/MMatx/Desktop/研究生/mini-imagenet/mini-imagenet/val.csv"
test_csv_path="C:/Users/MMatx/Desktop/研究生/mini-imagenet/mini-imagenet/test.csv"train_label={}
val_label={}
test_label={}
with open(train_csv_path) as csvfile:csv_reader=csv.reader(csvfile)birth_header=next(csv_reader)for row in csv_reader:train_label[row[0]]=row[1]with open(val_csv_path) as csvfile:csv_reader=csv.reader(csvfile)birth_header=next(csv_reader)for row in csv_reader:val_label[row[0]]=row[1]with open(test_csv_path) as csvfile:csv_reader=csv.reader(csvfile)birth_header=next(csv_reader)for row in csv_reader:test_label[row[0]]=row[1]img_path="C:/Users/MMatx/Desktop/研究生/mini-imagenet/mini-imagenet/images"
new_img_path="C:/Users/MMatx/Desktop/研究生/mini-imagenet/mini-imagenet/ok"
for png in os.listdir(img_path):path = img_path+ '/' + pngim=Image.open(path)if(png in train_label.keys()):tmp=train_label[png]temp_path=new_img_path+'/train'+'/'+tmpif(os.path.exists(temp_path)==False):os.makedirs(temp_path)t=temp_path+'/'+pngim.save(t)# with open(temp_path, 'wb') as f:# f.write(path)elif(png in val_label.keys()):tmp = val_label[png]temp_path = new_img_path + '/val' + '/' + tmpif (os.path.exists(temp_path) == False):os.makedirs(temp_path)t = temp_path + '/' + pngim.save(t)elif(png in test_label.keys()):tmp = test_label[png]temp_path = new_img_path + '/test' + '/' + tmpif (os.path.exists(temp_path) == False):os.makedirs(temp_path)t = temp_path + '/' + pngim.save(t)
涉及到的python知识:
1、python独写csv文件
使用pythonI/O读取csv文件是按照行读取,每一行都是一个List列表,可以通过使用List列表带获取每一行每一列的元素
2、python判断文件/目录是否存在
(1)判断文件是否存在:os.path.exists(path)
(2)新建一个目录:os.makedirs(path)
3、将图片保存在新的文件夹
使用 fromPIL import Image
img=Image.open(path)
img.save(new_path)
4、python中自带的glob支持文件的通配检索
关于模型的训练,数据集的加载,resnet模型的使用,
可以看另一篇博客:https://blog.csdn.net/Smiler_/article/details/117472023
这篇关于2020.3.31 miniImagenet数据集处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!