python-pytorch获取FashionMNIST实际图片标签数据集

在查看pytorch官方文档的时候,在这里链接中https://pytorch.org/tutorials/beginner/basics/data_tutorial.html的Creating a Custom Dataset for your files章节,有提到要自定义数据集,需要用到实际的图片和标签。

在网上找了半天没找到,写了一个脚本将图片和标签文本下载到本地。

python 复制代码
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+1
    

    classes = [
        "T-shirttop",
        "Trouser",
        "Pullover",
        "Dress",
        "Coat",
        "Sandal",
        "Shirt",
        "Sneaker",
        "Bag",
        "Ankleboot",
    ]



    import torch
    from torchvision.utils import save_image
    folder_path = './data/imageandlableTest'  # 替换为你的文件夹路径
    filename = '{}{}.jpg'.format(classes[x],count)  # 图片文件名

    # 确保文件夹存在
    import os
    if 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)
相关推荐
曲幽8 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805113 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon14 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly14 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程14 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly16 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
数据智能老司机21 小时前
PyTorch 深度学习——使用神经网络来拟合数据
pytorch·深度学习
数据智能老司机21 小时前
PyTorch 深度学习——用于图像的扩散模型
pytorch·深度学习
数据智能老司机21 小时前
PyTorch 深度学习——Transformer 是如何工作的
pytorch·深度学习
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python