如何使用 PyTorch 从本地目录导入 MNIST 数据集 [英] How To Import The MNIST Dataset From Local Directory Using PyTorch

查看:45
本文介绍了如何使用 PyTorch 从本地目录导入 MNIST 数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 PyTorch 中编写一个众所周知的问题 MNIST 手写数字数据库的代码.我下载了训练和测试数据集(从主网站),包括标记数据集.数据集格式为t10k-images-idx3-ubyte.gz,提取后t10k-images-idx3-ubyte.我的数据集文件夹看起来像

I am writing a code of a well-known problem MNIST database of handwritten digits in PyTorch. I downloaded the train and testing dataset (from the main website) including the labeled dataset. The dataset format is t10k-images-idx3-ubyte.gz and after extract t10k-images-idx3-ubyte. My dataset folder looks like

MINST
 Data
  train-images-idx3-ubyte.gz
  train-labels-idx1-ubyte.gz
  t10k-images-idx3-ubyte.gz
  t10k-labels-idx1-ubyte.gz

现在,我写了一个代码来加载数据,如下所示

Now, I wrote a code to load data like bellow

def load_dataset():
    data_path = "/home/MNIST/Data/"
    xy_trainPT = torchvision.datasets.ImageFolder(
        root=data_path, transform=torchvision.transforms.ToTensor()
    )
    train_loader = torch.utils.data.DataLoader(
        xy_trainPT, batch_size=64, num_workers=0, shuffle=True
    )
    return train_loader

我的代码显示 支持的扩展名是:.jpg、.jpeg、.png、.ppm、.bmp、.pgm、.tif、.tiff、.webp

我该如何解决这个问题,我还想检查我的图像是否已从数据集中加载(只有一个图形包含前 5 个图像)?

How can I solve this problem and I also want to check that my images are loaded (just a figure contains the first 5 images) from the dataset?

推荐答案

阅读此内容 通过 Python 从 .idx3-ubyte 文件或 GZIP 中提取图像

更新

您可以使用这种格式导入数据

You can import data using this format

xy_trainPT = torchvision.datasets.MNIST(
    root="~/Handwritten_Deep_L/",
    train=True,
    download=True,
    transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor()]),
)

现在,download=True 发生了什么,首先您的代码将在根目录(您给定的路径)中检查是否包含任何数据集.

Now, what is happening at download=True first your code will check at the root directory (your given path) contains any datasets or not.

如果 no 那么数据集将从网络下载.

If no then datasets will be downloaded from the web.

如果 yes 此路径已包含数据集,则您的代码将使用现有数据集运行,而不会从 Internet 下载.

If yes this path already contains a dataset then your code will work using the existing dataset and will not download from the internet.

你可以查一下,先给一个路径没有数据集(数据会从网上下载),然后再给另一个路径已经包含数据集数据不会下载.

You can check, first give a path without any dataset (data will be downloaded from the internet), and then give another path which already contains dataset data will not be downloaded.

这篇关于如何使用 PyTorch 从本地目录导入 MNIST 数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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