pytorch 使用AlexNet实现Mnist手写数字识别
程序员文章站
2022-03-16 19:27:15
...
AlexNet网络框架如下:AlexNet的原始输入图片大小为224*224,Mnist数据集中图片大小为28*28,所以需要对网络参数进行修改。
先掉用train函数进行训练,训练好的参数会保存在params.pth文件中,训练好使用本地图片(画图软件生成)进行测试。
完整程序如下:
import torch
import torchvision as tv
import torchvision.transforms as transforms
import torch.nn as nn
import torch.optim as optim
import cv2
# 定义是否使用GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# 定义AlexNet网络结构
class AlexNet(nn.Module):
def __init__(self, width_mult=1):
super(AlexNet, self).__init__()
self.layer1 = nn.Sequential( # 输入1*28*28
nn.Conv2d(1, 32, kernel_size=3, padding=1), # 32*28*28
nn.MaxPool2d(kernel_size=2, stride=2), # 32*14*14
nn.ReLU(inplace=True),
)
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1), # 64*14*14
nn.MaxPool2d(kernel_size=2, stride=2), # 64*7*7
nn.ReLU(inplace=True),
)
self.layer3 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1), # 128*7*7
)
self.layer4 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, padding=1), # 256*7*7
)
self.layer5 = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, padding=1), # 256*7*7
nn.MaxPool2d(kernel_size=3, stride=2), # 256*3*3
nn.ReLU(inplace=True),
)
self.fc1 = nn.Linear(256*3*3, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 10)
def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = x.view(-1, 256*3*3)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return x
# 超参数设置
EPOCH = 10 # 遍历数据集次数
BATCH_SIZE = 64 # 批处理尺寸(batch_size)
LR = 0.01 # 学习率
# 定义数据预处理方式
transform = transforms.ToTensor()
# 定义训练数据集
trainset = tv.datasets.MNIST(
root='./data/',
train=True,
download=False,
transform=transform)
# 定义训练批处理数据
trainloader = torch.utils.data.DataLoader(
trainset,
batch_size=BATCH_SIZE,
shuffle=True,
)
# 定义测试数据集
testset = tv.datasets.MNIST(
root='./data/',
train=False,
download=False,
transform=transform)
# 定义测试批处理数据
testloader = torch.utils.data.DataLoader(
testset,
batch_size=BATCH_SIZE,
shuffle=False,
)
# 定义损失函数loss function 和优化方式(采用SGD)
net = AlexNet().to(device)
a = torch.load('./params.pth')
net.load_state_dict(torch.load('./params.pth'))
criterion = nn.CrossEntropyLoss() # 交叉熵损失函数,通常用于多分类问题上
optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9)
# 训练并保存模型参数
def train():
for epoch in range(EPOCH):
sum_loss = 0.0
# 数据读取
for i, data in enumerate(trainloader):
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
# 梯度清零
optimizer.zero_grad()
# forward + backward
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
# 每训练100个batch打印一次平均loss
sum_loss += loss.item()
if i % 100 == 99:
print('[%d, %d] loss: %.03f'
% (epoch + 1, i + 1, sum_loss / 100))
sum_loss = 0.0
# 每跑完一次epoch测试一下准确率
with torch.no_grad():
correct = 0
total = 0
for data in testloader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = net(images)
# 取得分最高的那个类
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('第%d个epoch的识别准确率为:%d%%' % (epoch + 1, (100 * correct / total)))
# 保存模型参数
torch.save(net.state_dict(), './params.pth')
if __name__ == "__main__":
# train()
img = cv2.imread('./2.png', cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(28, 28))
img = torch.from_numpy(img).float()
img = img.view(1, 1, 28, 28)
img = img.to(device)
outputs = net(img)
_, predicted = torch.max(outputs.data, 1)
print(predicted.to('cpu').numpy().squeeze())
# cv2.imshow('', img)
# cv2.waitKey(0)
上一篇: 关于JTAG引脚设置为普通IO口
下一篇: Git工作流