🤵 Author :Horizon Max
✨ 编程技巧篇:各种操作小结
🎇 机器视觉篇:会变魔术 OpenCV
💥 深度学习篇:简单入门 PyTorch
🏆 神经网络篇:经典网络模型
💻 算法篇:再忙也别忘了 LeetCode
文章目录
MINIST
Size:
28×28 灰度手写数字图像
Num:
训练集 60000 和 测试集 10000,一共70000张图片
Classes:
0,1,2,3,4,5,6,7,8,9
官方下载链接:MINIST
数据集读取
1)MNIST数据集文件夹
一共包含四个文件夹:
train-images-idx3-ubyte.gz
:训练集图像(9912422 字节)55000张训练集 + 5000张验证集;
train-labels-idx1-ubyte.gz
:训练集标签(28881 字节)训练集对应的标签;
t10k-images-idx3-ubyte.gz
:测试集图像(1648877 字节)10000张测试集;
t10k-labels-idx1-ubyte.gz
:测试集标签(4542 字节)测试集对应的标签;
2)读取MNIST数据集
如果数据集没有下载,修改参数:
download=True
from torchvision import datasets, transforms
train_data = datasets.MNIST(root="./MNIST",
train=True,
transform=transforms.ToTensor(),
download=False)
test_data = datasets.MNIST(root="./MNIST",
train=False,
transform=transforms.ToTensor(),
download=False)print(train_data)print(test_data)
输出结果:
Dataset MNIST
Number of datapoints:60000
Root location:./MNIST
Split: Train
StandardTransform
Transform: ToTensor()
Dataset MNIST
Number of datapoints:10000
Root location:./MNIST
Split: Test
StandardTransform
Transform: ToTensor()
完整的数据集读取代码:
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
train_data = datasets.MNIST(root="./MNIST",
train=True,
transform=transforms.ToTensor(),
download=False)
test_data = datasets.MNIST(root="./MNIST",
train=False,
transform=transforms.ToTensor(),
download=False)
train_loader = DataLoader(dataset=train_data,
batch_size=64,
shuffle=True)
test_loader = DataLoader(dataset=test_data,
batch_size=64,
shuffle=True)
数据可视化
以训练集为例:
import torchvision
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import numpy as np
import matplotlib.pyplot as plt
train_data = datasets.MNIST(root="./MNIST",
train=True,
transform=transforms.ToTensor(),
download=False)
train_loader = DataLoader(dataset=train_data,
batch_size=64,
shuffle=True)for num,(image, label)inenumerate(train_loader):
image_batch = torchvision.utils.make_grid(image, padding=2)
plt.imshow(np.transpose(image_batch.numpy(),(1,2,0)), vmin=0, vmax=255)
plt.show()print(label)
1)
image
:
2)
label
:
tensor([1,8,9,6,8,9,9,9,4,0,4,9,0,1,6,5,2,6,1,6,4,2,8,5,1,7,7,8,9,3,5,0,8,9,3,6,5,4,0,2,4,2,4,5,8,7,1,5,9,8,6,8,6,8,3,8,7,7,3,0,8,6,2,0])
版权归原作者 Horizon Max 所有, 如有侵权,请联系我们删除。