在查看pytorch官方文档的时候,在这里链接中https://pytorch.org/tutorials/beginner/basics/data_tutorial.html的Creating a Custom Dataset for your files章节,有提到要自定义数据集,需要用到实际的图片和标签。
在网上找了半天没找到,写了一个脚本将图片和标签文本下载到本地。
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor# Download training data from open datasets.
training_data = datasets.FashionMNIST(root="data",train=True,download=True,transform=ToTensor(),
)# Download test data from open datasets.
test_data = datasets.FashionMNIST(root="data",train=False,download=True,transform=ToTensor(),
)# 写入到本地
count=0
for index,x in test_data:print(index.size(),x)count=count+1classes = ["T-shirttop","Trouser","Pullover","Dress","Coat","Sandal","Shirt","Sneaker","Bag","Ankleboot",]import torchfrom torchvision.utils import save_imagefolder_path = './data/imageandlableTest' # 替换为你的文件夹路径filename = '{}{}.jpg'.format(classes[x],count) # 图片文件名# 确保文件夹存在import osif not os.path.exists(folder_path):os.makedirs(folder_path)# 保存图片save_path = os.path.join(folder_path, filename)save_image(index, save_path)with open('./data/imageandlableTest/output.txt', 'a') as f: f.write("{},{}\n".format(filename,x))print(count)