尊龙凯时网址

[pytorch框架]3.2 mnist数据集手写数字识别 -尊龙凯时网址

2023-10-20

文章目录

3.2 mnist数据集手写数字识别
3.2.1 数据集介绍
3.2.2 手写数字识别

import torch
import torch.nn as nn
import torch.nn.functional as f
import torch.optim as optim
from torchvision import datasets, transforms
torch.__version__
'1.0.0'

3.2.1 数据集介绍

mnist 包括6万张28x28的训练样本,1万张测试样本,很多教程都会对它”下手”几乎成为一个 “典范”,可以说它就是计算机视觉里面的hello world。所以我们这里也会使用mnist来进行实战。

前面在介绍卷积神经网络的时候说到过lenet-5,lenet-5之所以强大就是因为在当时的环境下将mnist数据的识别率提高到了99%,这里我们也自己从头搭建一个卷积神经网络,也达到99%的准确率

3.2.2 手写数字识别

首先,我们定义一些超参数

batch_size=512 #大概需要2g的显存
epochs=20 # 总共训练批次
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 让torch判断是否使用gpu,建议使用gpu环境,因为会快很多

因为pytorch里面包含了mnist的数据集,所以我们这里直接使用即可。
如果第一次执行会生成data文件夹,并且需要一些时间下载,如果以前下载过就不会再次下载了

由于官方已经实现了dataset,所以这里可以直接使用dataloader来对数据进行读取

train_loader = torch.utils.data.dataloader(
datasets.mnist('data', train=true, download=true,
transform=transforms.compose([
transforms.totensor(),
transforms.normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=true)
downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
processing...
done!

测试集

test_loader = torch.utils.data.dataloader(
datasets.mnist('data', train=false, transform=transforms.compose([
transforms.totensor(),
transforms.normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=true)

下面我们定义一个网络,网络包含两个卷积层,conv1和conv2,然后紧接着两个线性层作为输出,最后输出10个维度,这10个维度我们作为0-9的标识来确定识别出的是那个数字

在这里建议大家将每一层的输入和输出维度都作为注释标注出来,这样后面阅读代码的会方便很多

class convnet(nn.module):
def __init__(self):
super().__init__()
# batch*1*28*28(每次会送入batch个样本,输入通道数1(黑白图像),图像分辨率是28x28)
# 下面的卷积层conv2d的第一个参数指输入通道数,第二个参数指输出通道数,第三个参数指卷积核的大小
self.conv1 = nn.conv2d(1, 10, 5) # 输入通道数1,输出通道数10,核的大小5
self.conv2 = nn.conv2d(10, 20, 3) # 输入通道数10,输出通道数20,核的大小3
# 下面的全连接层linear的第一个参数指输入通道数,第二个参数指输出通道数
self.fc1 = nn.linear(20*10*10, 500) # 输入通道数是2000,输出通道数是500
self.fc2 = nn.linear(500, 10) # 输入通道数是500,输出通道数是10,即10分类
def forward(self,x):
in_size = x.size(0) # 在本例中in_size=512,也就是batch_size的值。输入的x可以看成是512*1*28*28的张量。
out = self.conv1(x) # batch*1*28*28 -> batch*10*24*24(28x28的图像经过一次核为5x5的卷积,输出变为24x24)
out = f.relu(out) # batch*10*24*24(激活函数relu不改变形状))
out = f.max_pool2d(out, 2, 2) # batch*10*24*24 -> batch*10*12*12(2*2的池化层会减半)
out = self.conv2(out) # batch*10*12*12 -> batch*20*10*10(再卷积一次,核的大小是3)
out = f.relu(out) # batch*20*10*10
out = out.view(in_size, -1) # batch*20*10*10 -> batch*2000(out的第二维是-1,说明是自动推算,本例中第二维是20*10*10)
out = self.fc1(out) # batch*2000 -> batch*500
out = f.relu(out) # batch*500
out = self.fc2(out) # batch*500 -> batch*10
out = f.log_softmax(out, dim=1) # 计算log(softmax(x))
return out

我们实例化一个网络,实例化后使用.to方法将网络移动到gpu

优化器我们也直接选择简单暴力的adam

model = convnet().to(device)
optimizer = optim.adam(model.parameters())

下面定义一下训练的函数,我们将训练的所有操作都封装到这个函数中

def train(model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = f.nll_loss(output, target)
loss.backward()
optimizer.step()
if(batch_idx 1)0 == 0:
print('train epoch: {} [{}/{} ({:.0f}%)]\tloss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))

测试的操作也一样封装成一个函数

def test(model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss = f.nll_loss(output, target, reduction='sum').item() # 将一批的损失相加
pred = output.max(1, keepdim=true)[1] # 找到概率最大的下标
correct = pred.eq(target.view_as(pred)).sum().item() test_loss /= len(test_loader.dataset)
print('\ntest set: average loss: {:.4f}, accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))

下面开始训练,这里就体现出封装起来的好处了,只要写两行就可以了

for epoch in range(1, epochs   1):
train(model, device, train_loader, optimizer, epoch)
test(model, device, test_loader)
train epoch: 1 [14848/60000 (25%)]	loss: 0.272529
train epoch: 1 [30208/60000 (50%)] loss: 0.235455
train epoch: 1 [45568/60000 (75%)] loss: 0.101858 test set: average loss: 0.1018, accuracy: 9695/10000 (97%) train epoch: 2 [14848/60000 (25%)] loss: 0.057989
train epoch: 2 [30208/60000 (50%)] loss: 0.083935
train epoch: 2 [45568/60000 (75%)] loss: 0.051921 test set: average loss: 0.0523, accuracy: 9825/10000 (98%) train epoch: 3 [14848/60000 (25%)] loss: 0.045383
train epoch: 3 [30208/60000 (50%)] loss: 0.049402
train epoch: 3 [45568/60000 (75%)] loss: 0.061366 test set: average loss: 0.0408, accuracy: 9866/10000 (99%) train epoch: 4 [14848/60000 (25%)] loss: 0.035253
train epoch: 4 [30208/60000 (50%)] loss: 0.038444
train epoch: 4 [45568/60000 (75%)] loss: 0.036877 test set: average loss: 0.0433, accuracy: 9859/10000 (99%) train epoch: 5 [14848/60000 (25%)] loss: 0.038996
train epoch: 5 [30208/60000 (50%)] loss: 0.020670
train epoch: 5 [45568/60000 (75%)] loss: 0.034658 test set: average loss: 0.0339, accuracy: 9885/10000 (99%) train epoch: 6 [14848/60000 (25%)] loss: 0.067320
train epoch: 6 [30208/60000 (50%)] loss: 0.016328
train epoch: 6 [45568/60000 (75%)] loss: 0.017037 test set: average loss: 0.0348, accuracy: 9881/10000 (99%) train epoch: 7 [14848/60000 (25%)] loss: 0.022150
train epoch: 7 [30208/60000 (50%)] loss: 0.009608
train epoch: 7 [45568/60000 (75%)] loss: 0.012742 test set: average loss: 0.0346, accuracy: 9895/10000 (99%) train epoch: 8 [14848/60000 (25%)] loss: 0.010173
train epoch: 8 [30208/60000 (50%)] loss: 0.019482
train epoch: 8 [45568/60000 (75%)] loss: 0.012159 test set: average loss: 0.0323, accuracy: 9886/10000 (99%) train epoch: 9 [14848/60000 (25%)] loss: 0.007792
train epoch: 9 [30208/60000 (50%)] loss: 0.006970
train epoch: 9 [45568/60000 (75%)] loss: 0.004989 test set: average loss: 0.0294, accuracy: 9909/10000 (99%) train epoch: 10 [14848/60000 (25%)] loss: 0.003764
train epoch: 10 [30208/60000 (50%)] loss: 0.005944
train epoch: 10 [45568/60000 (75%)] loss: 0.001866 test set: average loss: 0.0361, accuracy: 9902/10000 (99%) train epoch: 11 [14848/60000 (25%)] loss: 0.002737
train epoch: 11 [30208/60000 (50%)] loss: 0.014134
train epoch: 11 [45568/60000 (75%)] loss: 0.001365 test set: average loss: 0.0309, accuracy: 9905/10000 (99%) train epoch: 12 [14848/60000 (25%)] loss: 0.003344
train epoch: 12 [30208/60000 (50%)] loss: 0.003090
train epoch: 12 [45568/60000 (75%)] loss: 0.004847 test set: average loss: 0.0318, accuracy: 9902/10000 (99%) train epoch: 13 [14848/60000 (25%)] loss: 0.001278
train epoch: 13 [30208/60000 (50%)] loss: 0.003016
train epoch: 13 [45568/60000 (75%)] loss: 0.001328 test set: average loss: 0.0358, accuracy: 9906/10000 (99%) train epoch: 14 [14848/60000 (25%)] loss: 0.002219
train epoch: 14 [30208/60000 (50%)] loss: 0.003487
train epoch: 14 [45568/60000 (75%)] loss: 0.014429 test set: average loss: 0.0376, accuracy: 9896/10000 (99%) train epoch: 15 [14848/60000 (25%)] loss: 0.003042
train epoch: 15 [30208/60000 (50%)] loss: 0.002974
train epoch: 15 [45568/60000 (75%)] loss: 0.000871 test set: average loss: 0.0346, accuracy: 9909/10000 (99%) train epoch: 16 [14848/60000 (25%)] loss: 0.000618
train epoch: 16 [30208/60000 (50%)] loss: 0.003164
train epoch: 16 [45568/60000 (75%)] loss: 0.007245 test set: average loss: 0.0357, accuracy: 9905/10000 (99%) train epoch: 17 [14848/60000 (25%)] loss: 0.001874
train epoch: 17 [30208/60000 (50%)] loss: 0.013951
train epoch: 17 [45568/60000 (75%)] loss: 0.000729 test set: average loss: 0.0322, accuracy: 9922/10000 (99%) train epoch: 18 [14848/60000 (25%)] loss: 0.002581
train epoch: 18 [30208/60000 (50%)] loss: 0.001396
train epoch: 18 [45568/60000 (75%)] loss: 0.015521 test set: average loss: 0.0389, accuracy: 9914/10000 (99%) train epoch: 19 [14848/60000 (25%)] loss: 0.000283
train epoch: 19 [30208/60000 (50%)] loss: 0.001385
train epoch: 19 [45568/60000 (75%)] loss: 0.011184 test set: average loss: 0.0383, accuracy: 9901/10000 (99%) train epoch: 20 [14848/60000 (25%)] loss: 0.000472
train epoch: 20 [30208/60000 (50%)] loss: 0.003306
train epoch: 20 [45568/60000 (75%)] loss: 0.018017 test set: average loss: 0.0393, accuracy: 9899/10000 (99%)

我们看一下结果,准确率99%,没问题

如果你的模型连mnist都搞不定,那么你的模型没有任何的价值

即使你的模型搞定了mnist,你的模型也可能没有任何的价值

mnist是一个很简单的数据集,由于它的局限性只能作为研究用途,对实际应用带来的价值非常有限。但是通过这个例子,我们可以完全了解一个实际项目的工作流程

我们找到数据集,对数据做预处理,定义我们的模型,调整超参数,测试训练,再通过训练结果对超参数进行调整或者对模型进行调整。

并且通过这个实战我们已经有了一个很好的模板,以后的项目都可以以这个模板为样例

[pytorch框架]3.2 mnist数据集手写数字识别的相关教程结束。

网站地图