mini-ImageNet
首先配置好数据集,images,train.csv,test.csv,val.csv,目录如下
miniimagenet/ ├── images ├── n0210891500001298.jpg ├── n0287152500001298.jpg ... ├── test.csv ├── val.csv └── train.csv └── proc_images.py
images文件夹下是60000张图片,先对其归一化成84*84大小;csv文件中是图片文件名和其对应的标签
按照csv对应的标签分成训练集、测试集、验证集。训练集中每个文件夹都代表一类,其文件夹名称就是标签
例如:'images/n0153282900000005.jpg' -> 'train/n01532829/n0153282900000005.jpg'
proc_images.py
首先给定images,train.csv,test.csv,val.csv文件,将images中的图片按照csv文件对应的分成训练集、测试集、验证集,自动生成三个文件夹(train、test、val)
'''
windows版本
'''
from __future__ import print_function
import csv
import glob
import os
from PIL import Image
path_to_images = 'imagess/'
all_images = glob.glob(path_to_images + '*')#调用glob函数读取文件中图片
# 将图片归一化为84*84大小
for i, image_file in enumerate(all_images):
im = Image.open(image_file)
im = im.resize((84, 84), resample=Image.LANCZOS)
im.save(image_file)
if i % 500 == 0:
print(i)
# 根据csv文件从images中读取数据并分成三类,创建相应的目录文件夹(train、val、test)
for datatype in ['train', 'val', 'test']:
os.mkdir(datatype)
with open(datatype + '.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
last_label = ''
for i, row in enumerate(reader):
if i == 0: # skip the headers
continue
label = row[1]
image_name = row[0]
if label != last_label:
cur_dir = datatype + '/' + label + '/'
os.mkdir(cur_dir)
last_label = label
os.rename('imagess/' + image_name,cur_dir + image_name)
'''
首先配置好数据集,images,train.csv,test.csv,val.csv。
images文件夹下是60000张图片,先对其归一化成84*84大小;csv文件中是图片文件名和其对应的标签
按照csv对应的标签分成训练集、测试集、验证集。训练集中每个文件夹都代表一类,其文件夹名称就是标签
例如:'images/n0153282900000005.jpg' -> 'train/n01532829/n0153282900000005.jpg'
'''
'''
linux版本
'''
from __future__ import print_function
import csv
import glob
import os
from PIL import Image
path_to_images = 'images/'
all_images = glob.glob(path_to_images + '*')
# Resize images
for i, image_file in enumerate(all_images):
im = Image.open(image_file)
im = im.resize((84, 84), resample=Image.LANCZOS)
im.save(image_file)
if i % 500 == 0:
print(i)
# Put in correct directory
for datatype in ['train', 'val', 'test']:
os.system('mkdir ' + datatype)
with open(datatype + '.csv', 'r') as f:
reader = csv.reader(f, delimiter=',')
last_label = ''
for i, row in enumerate(reader):
if i == 0: # skip the headers
continue
label = row[1]
image_name = row[0]
if labe