如何将 MNIST 图像加载到 Pytorch DataLoader 中? [英] How do you load MNIST images into Pytorch DataLoader?

查看:38
本文介绍了如何将 MNIST 图像加载到 Pytorch DataLoader 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于数据加载和处理的 pytorch 教程非常具体到一个示例,有人可以帮助我了解更通用的简单图像加载的函数应该是什么样的吗?

The pytorch tutorial for data loading and processing is quite specific to one example, could someone help me with what the function should look like for a more generic simple loading of images?

教程:http://pytorch.org/tutorials/beginner/data_loading_tutorial.html

我的数据:

我在以下文件夹结构中有 MINST 数据集作为 jpg.(我知道我可以只使用数据集类,但这纯粹是为了看看如何将简单的图像加载到 pytorch 中,而没有 csv 或复杂的特征).

I have the MINST dataset as jpg's in the following folder structure. (I know I can just use the dataset class, but this is purely to see how to load simple images into pytorch without csv's or complex features).

文件夹名称是标签,图像是 28x28 的灰度 png,无需转换.

The folder name is the label and the images are 28x28 png's in greyscale, no transformations required.

data
    train
        0
            3.png
            5.png
            13.png
            23.png
            ...
        1
            3.png
            10.png
            11.png
            ...
        2
            4.png
            13.png
            ...
        3
            8.png
            ...
        4
            ...
        5
            ...
        6
            ...
        7
            ...
        8
            ...
        9
            ...

推荐答案

这是我为 pytorch 0.4.1 所做的(应该仍然适用于 1.3)

Here's what I did for pytorch 0.4.1 (should still work in 1.3)

def load_dataset():
    data_path = 'data/train/'
    train_dataset = torchvision.datasets.ImageFolder(
        root=data_path,
        transform=torchvision.transforms.ToTensor()
    )
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=64,
        num_workers=0,
        shuffle=True
    )
    return train_loader

for batch_idx, (data, target) in enumerate(load_dataset()):
    #train network

这篇关于如何将 MNIST 图像加载到 Pytorch DataLoader 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆