在本目录下创建文件夹myData/mnist
import os
os.makedirs("./myData/mnist",exist_ok=True)
使用Pytorch下的Datasets包自动下载MNIST数据集到本地
import torch
from torchvision import datasets
import torchvision.transforms as transforms
img_shape = (1,28,28)
composedTransform = transforms.Compose([transforms.Resize(28),transforms.ToTensor(),transforms.Normalize([0.5],[0.5])])
mnist = datasets.MNIST(
"./myData/mnist",
train = True,
download = True,
transform = composedTransform
)
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz to ./myData/mnist\MNIST\raw\train-images-idx3-ubyte.gz
100.0%
Extracting ./myData/mnist\MNIST\raw\train-images-idx3-ubyte.gz to ./myData/mnist\MNIST\raw
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz to ./myData/mnist\MNIST\raw\train-labels-idx1-ubyte.gz
100.0%
Extracting ./myData/mnist\MNIST\raw\train-labels-idx1-ubyte.gz to ./myData/mnist\MNIST\raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz to ./myData/mnist\MNIST\raw\t10k-images-idx3-ubyte.gz
100.0%
Extracting ./myData/mnist\MNIST\raw\t10k-images-idx3-ubyte.gz to ./myData/mnist\MNIST\raw
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz to ./myData/mnist\MNIST\raw\t10k-labels-idx1-ubyte.gz
100.0%
Extracting ./myData/mnist\MNIST\raw\t10k-labels-idx1-ubyte.gz to ./myData/mnist\MNIST\raw
使用PyTorch的DataLoader类实现数据读取
batch_size = 128
dataloader = torch.utils.data.DataLoader(
mnist,
batch_size = batch_size,
shuffle = True
)
数据读取成功
for i in dataloader:
print(i)
break
[tensor([[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]],
[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]],
[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]],
...,
[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]],
[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]],
[[[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
...,
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.],
[-1., -1., -1., ..., -1., -1., -1.]]]]), tensor([3, 5, 9, 8, 5, 9, 3, 8, 2, 7, 9, 9, 6, 1, 9, 3, 8, 1, 4, 1, 4, 3, 8, 0,
2, 3, 0, 0, 0, 3, 9, 5, 8, 8, 8, 5, 0, 5, 6, 7, 6, 4, 9, 9, 2, 1, 4, 2,
7, 3, 6, 9, 1, 6, 7, 9, 1, 3, 7, 2, 4, 7, 4, 1, 4, 1, 0, 2, 5, 5, 9, 6,
0, 8, 5, 2, 0, 8, 5, 1, 1, 9, 5, 0, 0, 1, 8, 7, 2, 1, 8, 6, 7, 2, 1, 7,
5, 9, 2, 8, 6, 1, 7, 7, 4, 9, 5, 0, 1, 2, 1, 9, 1, 0, 4, 6, 6, 7, 7, 8,
7, 8, 1, 3, 3, 7, 0, 1])]