赞
踩
网上的大多数例子都是基于Mnist数据集进行测试的,今天实现一个自己手写数字的识别。
首先训练模型,使用Mnist数据集,网络的backbone采用LeNet。
1. 导入需要的模块并添加GPU设备
- 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")
2. 定义网络结构
- class LeNet(nn.Module):
- def __init__(self):
- super(LeNet, self).__init__()
- self.conv1 = nn.Sequential( # input_size=(1*28*28)
- nn.Conv2d(1, 6, 5, 1, 2), # padding=2保证输入输出尺寸相同
- nn.ReLU(), # input_size=(6*28*28)
- nn.MaxPool2d(kernel_size=2, stride=2), # output_size=(6*14*14)
- )
- self.conv2 = nn.Sequential(
- nn.Conv2d(6, 16, 5),
- nn.ReLU(), # input_size=(16*10*10)
- nn.MaxPool2d(2, 2) # output_size=(16*5*5)
- )
- self.fc1 = nn.Sequential(
- nn.Linear(16 * 5 * 5, 120),
- nn.ReLU()
- )
- self.fc2 = nn.Sequential(
- nn.Linear(120, 84),
- nn.ReLU()
- )
- self.fc3 = nn.Linear(84, 10)
-
- # 定义前向传播过程,输入为x
- def forward(self, x):
- x = self.conv1(x)
- x = self.conv2(x)
- # nn.Linear()的输入输出都是维度为一的值,所以要把多维度的tensor展平成一维(一行)
- x = x.view(x.size()[0], -1)
- x = self.fc1(x)
- x = self.fc2(x)
- x = self.fc3(x)
- return x
3. 设置超参数和定义训练和测试数据提取器
- # 超参数设置
- EPOCH = 10 # 遍历数据集次数
- BATCH_SIZE = 256 # 批处理尺寸(batch_size)
- LR = 0.001 # 学习率
-
- # 定义数据预处理方式
- 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)
4. 定义训练函数
- def train():
- # 定义损失函数loss function 和优化方式(采用SGD)
- net = LeNet().to(device)
- criterion = nn.CrossEntropyLoss() # 交叉熵损失函数,通常用于多分类问题上
- optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9)
- 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')
5. 先进行训练,训练结果会保存在params.pth中。
- if __name__ == "__main__":
- train()
6. 训练完成后注释掉训练函数,读取训练好的模型参数并进行测试。
- # 读取训练好的网络参数
- net = LeNet().to(device)
- a = torch.load('./params.pth')
- net.load_state_dict(torch.load('./params.pth'))
-
-
- if __name__ == "__main__":
- # train()
- img = cv2.imread('./2.png', cv2.IMREAD_GRAYSCALE) #读取图片
- img = cv2.resize(img,(28, 28)) # 调整图片为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())
测试图片使用windows软件画图绘制,如下:
输出结果如下:
完整代码如下:
- 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")
-
-
- # 定义网络结构
- class LeNet(nn.Module):
- def __init__(self):
- super(LeNet, self).__init__()
- self.conv1 = nn.Sequential( # input_size=(1*28*28)
- nn.Conv2d(1, 6, 5, 1, 2), # padding=2保证输入输出尺寸相同
- nn.ReLU(), # input_size=(6*28*28)
- nn.MaxPool2d(kernel_size=2, stride=2), # output_size=(6*14*14)
- )
- self.conv2 = nn.Sequential(
- nn.Conv2d(6, 16, 5),
- nn.ReLU(), # input_size=(16*10*10)
- nn.MaxPool2d(2, 2) # output_size=(16*5*5)
- )
- self.fc1 = nn.Sequential(
- nn.Linear(16 * 5 * 5, 120),
- nn.ReLU()
- )
- self.fc2 = nn.Sequential(
- nn.Linear(120, 84),
- nn.ReLU()
- )
- self.fc3 = nn.Linear(84, 10)
-
- # 定义前向传播过程,输入为x
- def forward(self, x):
- x = self.conv1(x)
- x = self.conv2(x)
- # nn.Linear()的输入输出都是维度为一的值,所以要把多维度的tensor展平成一维(一行)
- x = x.view(x.size()[0], -1)
- x = self.fc1(x)
- x = self.fc2(x)
- x = self.fc3(x)
- return x
-
-
-
-
- # 使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多
- # parser = argparse.ArgumentParser()
- # parser.add_argument('--outf', default='./model/', help='folder to output images and model checkpoints') # 模型保存路径
- # parser.add_argument('--net', default='./model/net.pth', help="path to netG (to continue training)") # 模型加载路径
- # opt = parser.parse_args()
-
- # 超参数设置
- EPOCH = 10 # 遍历数据集次数
- BATCH_SIZE = 256 # 批处理尺寸(batch_size)
- LR = 0.001 # 学习率
-
- # 定义数据预处理方式
- 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 = LeNet().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())
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。