用torchvision的例子进行分类预测试验,所用数据集为flowers102,所用模型为Resnet152
1。下载地址:
https://www.robots.ox.ac.uk/~vgg/data/flowers/102/
2。训练测试集分类代码处理:
2.1 下载数据如红框所示,解压后的数据在jpg文件夹中,未对训练,验证集,测试集进行划分;
2.2 用如下代码进行train, valid, test集划分:
import scipy.io
import numpy as np
import os
from PIL import Image
import shutil
labels = scipy.io.loadmat('./flower_data/flowers-102/imagelabels.mat')#该地址为imagelabels.mat的绝对地址
labels = np.array(labels['labels'][0]) - 1
print("labels:", labels)
setid = scipy.io.loadmat('./flower_data/flowers-102/setid.mat')#该地址为setid.mat的绝对地址
validation = np.array(setid['valid'][0]) - 1
np.random.shuffle(validation)
train = np.array(setid['trnid'][0]) - 1
np.random.shuffle(train)
test = np.array(setid['tstid'][0]) - 1
np.random.shuffle(test)
flower_dir = list()
for img in os.listdir("./flower_data/flowers-102/jpg"):#该地址为源数据图片的绝对地址
flower_dir.append(os.path.join("./flower_data/flowers-102/jpg", img))
flower_dir.sort()
print(flower_dir)
des_folder_train = "./flower_data/train"#该地址为新建的训练数据集文件夹的绝对地址
for tid in train:#打开图片并获取标签
img = Image.open(flower_dir[tid])
# print(img)
# print(flower_dir[tid])
img = img.resize((256, 256), Image.ANTIALIAS)
lable = labels[tid]
# print(lable)
path = flower_dir[tid]
# print("path:", path)
base_path = os.path.basename(path)
# print("base_path:", base_path)
classes = "c" + str(lable)
class_path = os.path.join(des_folder_train, classes)
# 判断结果
if not os.path.exists(class_path):
os.makedirs(class_path)
# print("class_path:", class_path)
despath = os.path.join(class_path, base_path)
# print("despath:", despath)
img.save(despath)
des_folder_validation = "./flower_data/valid"#该地址为新建的验证数据集文件夹的绝对地址
for tid in validation:
img = Image.open(flower_dir[tid])
# print(flower_dir[tid])
img = img.resize((256, 256), Image.ANTIALIAS)
lable = labels[tid]
# print(lable)
path = flower_dir[tid]
# print("path:", path)
base_path = os.path.basename(path)
# print("base_path:", base_path)
classes = "c" + str(lable)
class_path = os.path.join(des_folder_validation, classes)
# 判断结果
if not os.path.exists(class_path):
os.makedirs(class_path)
# print("class_path:", class_path)
despath = os.path.join(class_path, base_path)
# print("despath:", despath)
img.save(despath)
des_folder_test = "./flower_data/test"#该地址为新建的测试数据集文件夹的绝对地址
for tid in test:
img = Image.open(flower_dir[tid])
# print(flower_dir[tid])
img = img.resize((256, 256), Image.ANTIALIAS)
lable = labels[tid]
# print(lable)
path = flower_dir[tid]
# print("path:", path)
base_path = os.path.basename(path)
# print("base_path:", base_path)
classes = "c" + str(lable)
class_path = os.path.join(des_folder_test, classes)
# 判断结果
if not os.path.exists(class_path):
os.makedirs(class_path)
# print("class_path:", class_path)
despath = os.path.join(class_path, base_path)
# print("despath:", despath)
img.save(despath)