当前位置:   article > 正文

菜菜学paddle第六篇:利用LeNet卷积神经网络识别手写数字_lenet数字识别的神经网络模型文件

lenet数字识别的神经网络模型文件

前言

LeNet是最早的卷积神经网络之一。1998年,Yann LeCun第一次将LeNet卷积神经网络应用到图像分类上,它通过连续使用卷积和池化层的组合提取图像特征,在手写数字识别任务中取得了巨大成功。

LeNet模型网络结构示意图

 

模型设计

1、新建文件leNet.py,根据leNet的结构示意图写出对应的代码:

  1. import paddle
  2. from paddle.nn import Conv2D,MaxPool2D,Linear
  3. import paddle.nn.functional as F
  4. class LeNet(paddle.nn.Layer):
  5. def __init__(self, num_classes = 1):
  6. super(LeNet, self).__init__()
  7. self.conv1 = Conv2D(in_channels=1,out_channels=6,kernel_size=5)
  8. self.max_pool1 = MaxPool2D(kernel_size=2,stride=2)
  9. self.conv2 = Conv2D(in_channels=6,out_channels=16,kernel_size=5)
  10. self.max_pool2 = MaxPool2D(kernel_size=2,stride=2)
  11. self.conv3 = Conv2D(in_channels=16,out_channels=120,kernel_size=4)
  12. self.fc1 = Linear(in_features=120, out_features=64)
  13. self.fc2 = Linear(in_features=64,out_features=num_classes)
  14. def forward(self, x):
  15. x = self.conv1(x)
  16. x = F.sigmoid(x)
  17. x = self.max_pool1(x)
  18. x = F.sigmoid(x)
  19. x = self.conv2(x)
  20. x = self.max_pool2(x)
  21. x = self.conv3(x)
  22. x = paddle.reshape(x , [x.shape[0], -1])
  23. x = self.fc1(x)
  24. x = F.sigmoid(x)
  25. x = self.fc2(x)
  26. return x

模型训练

1、新建文件:LeNetTrain.py

  1. import numpy as np
  2. import paddle
  3. import paddle.nn.functional as F
  4. from paddle.vision.transforms import ToTensor
  5. from paddle.vision.datasets import MNIST
  6. from leNet import LeNet
  7. def train(model, opt, train_loader, valid_loader):
  8. model.train()
  9. for epoch in range(EPOCH_NUM):
  10. for batch_id,data in enumerate(train_loader()):
  11. img = data[0]
  12. label = data[1]
  13. logits = model(img)
  14. loss_func = paddle.nn.CrossEntropyLoss(reduction='none')
  15. loss = loss_func(logits,label)
  16. avg_loss = paddle.mean(loss)
  17. if batch_id % 2000 == 0:
  18. print("epoch:{},batch_id:{},loss is:{:.4f}".format(epoch,batch_id,float(avg_loss.numpy())))
  19. avg_loss.backward()
  20. opt.step()
  21. opt.clear_grad()
  22. model.eval()
  23. accuracies = []
  24. losses = []
  25. for batch_id, data in enumerate(valid_loader()):
  26. img = data[0]
  27. label = data[1]
  28. # 计算模型输出
  29. logits = model(img)
  30. pred = F.softmax(logits)
  31. # 计算损失函数
  32. loss_func = paddle.nn.CrossEntropyLoss(reduction='none')
  33. loss = loss_func(logits, label)
  34. acc = paddle.metric.accuracy(pred, label)
  35. accuracies.append(acc.numpy())
  36. losses.append(loss.numpy())
  37. print("[validation] accuracy/loss: {:.4f}/{:.4f}".format(np.mean(accuracies), np.mean(losses)))
  38. model.train()
  39. # 保存模型参数
  40. paddle.save(model.state_dict(), 'leNet.mnist.pdparams')
  41. # 创建模型
  42. model = LeNet(num_classes=10)
  43. # 设置迭代轮数
  44. EPOCH_NUM = 5
  45. # 设置优化器为Momentum,学习率为0.001
  46. opt = paddle.optimizer.Momentum(learning_rate=0.001, momentum=0.9, parameters=model.parameters())
  47. # 定义数据读取器
  48. train_loader = paddle.io.DataLoader(MNIST(mode='train', transform=ToTensor()), batch_size=10, shuffle=True)
  49. valid_loader = paddle.io.DataLoader(MNIST(mode='test', transform=ToTensor()), batch_size=10)
  50. # 启动训练过程
  51. train(model, opt, train_loader, valid_loader)

模型验证

1、新建文件LeNetEval.py:

  1. from leNet import LeNet
  2. import numpy as np
  3. import paddle
  4. from paddle.vision.transforms import ToTensor
  5. from paddle.vision.datasets import MNIST
  6. # 创建模型
  7. model = LeNet(num_classes=10)
  8. params_file_path = 'leNet.mnist.pdparams'
  9. param_dict = paddle.load(params_file_path)
  10. # 加载模型参数
  11. model.load_dict(param_dict)
  12. # 定义预测过程
  13. model.eval()
  14. # 加载测试集 batch_size 设为 1
  15. valid_loader = paddle.io.DataLoader(MNIST(mode='test', transform=ToTensor()), batch_size=1)
  16. success = 0
  17. error = 0
  18. for batch_id, data in enumerate(valid_loader()):
  19. img = data[0]
  20. label = data[1]
  21. #预测
  22. logits = model(img)
  23. #转换为整数
  24. label = label.numpy().astype('int32')[0]
  25. #取最大的那个预测概率的下标
  26. result = np.argsort(logits[0])[-1]
  27. if (label == result) :
  28. success = success + 1
  29. else:
  30. error = error + 1
  31. #打印结果
  32. print("本次预测的正确的数量是{}, 错误的数量是{}".format(success, error))

2、结果输出:

 本次预测的正确的数量是9553, 错误的数量是447

总结:

1、本次进行了5次迭代,预测的准确率就非常高了,超过了95%

2、先画出模型图,然后再编写对应的代码,非常的高效

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/92672
推荐阅读
  

闽ICP备14008679号