基于Pytorch深度学习神经网络MNIST手写数字识别系统源码(带界面和手写画板)

 第一步:准备数据

mnist开源数据集

第二步:搭建模型

我们这里搭建了一个LeNet5网络

参考代码如下:

import torch
from torch import nnclass Reshape(nn.Module):def forward(self, x):return x.view(-1, 1, 28, 28)class LeNet5(nn.Module):def __init__(self):super(LeNet5, self).__init__()self.net = nn.Sequential(Reshape(),# CONV1, ReLU1, POOL1nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2),# nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5),nn.ReLU(),nn.MaxPool2d(kernel_size=2, stride=2),# CONV2, ReLU2, POOL2nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5),nn.ReLU(),nn.MaxPool2d(kernel_size=2, stride=2),nn.Flatten(),# FC1nn.Linear(in_features=16 * 5 * 5, out_features=120),nn.ReLU(),# FC2nn.Linear(in_features=120, out_features=84),nn.ReLU(),# FC3nn.Linear(in_features=84, out_features=10))# 添加softmax层self.softmax = nn.Softmax()def forward(self, x):logits = self.net(x)# 将logits转为概率prob = self.softmax(logits)return probif __name__ == '__main__':model = LeNet5()X = torch.rand(size=(256, 1, 28, 28), dtype=torch.float32)for layer in model.net:X = layer(X)print(layer.__class__.__name__, '\toutput shape: \t', X.shape)X = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32)print(model(X))

第三步:训练代码

import torch
from torch import nn
from torchvision import datasets
from torchvision.transforms import ToTensor
from torch.utils.data import DataLoaderfrom model import LeNet5# DATASET
train_data = datasets.MNIST(root='./data',train=False,download=True,transform=ToTensor()
)test_data = datasets.MNIST(root='./data',train=False,download=True,transform=ToTensor()
)# PREPROCESS
batch_size = 256
train_dataloader = DataLoader(dataset=train_data, batch_size=batch_size)
test_dataloader = DataLoader(dataset=test_data, batch_size=batch_size)
for X, y in train_dataloader:print(X.shape)		# torch.Size([256, 1, 28, 28])print(y.shape)		# torch.Size([256])break# MODEL
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = LeNet5().to(device)# TRAIN MODEL
loss_func = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(params=model.parameters())def train(dataloader, model, loss_func, optimizer, epoch):model.train()data_size = len(dataloader.dataset)for batch, (X, y) in enumerate(dataloader):X, y = X.to(device), y.to(device)y_hat = model(X)loss = loss_func(y_hat, y)optimizer.zero_grad()loss.backward()optimizer.step()loss, current = loss.item(), batch * len(X)print(f'EPOCH{epoch+1}\tloss: {loss:>7f}', end='\t')# Test model
def test(dataloader, model, loss_fn):size = len(dataloader.dataset)num_batches = len(dataloader)model.eval()test_loss, correct = 0, 0with torch.no_grad():for X, y in dataloader:X, y = X.to(device), y.to(device)pred = model(X)test_loss += loss_fn(pred, y).item()correct += (pred.argmax(1) == y).type(torch.float).sum().item()test_loss /= num_batchescorrect /= sizeprint(f'Test Error: Accuracy: {(100 * correct):>0.1f}%, Average loss: {test_loss:>8f}\n')if __name__ == '__main__':epoches = 80for epoch in range(epoches):train(train_dataloader, model, loss_func, optimizer, epoch)test(test_dataloader, model, loss_func)# Save modelstorch.save(model.state_dict(), 'model.pth')print('Saved PyTorch LeNet5 State to model.pth')

第四步:统计训练过程

EPOCH1	loss: 1.908403	Test Error: Accuracy: 58.3%, Average loss: 1.943602EPOCH2	loss: 1.776060	Test Error: Accuracy: 72.2%, Average loss: 1.750917EPOCH3	loss: 1.717706	Test Error: Accuracy: 73.6%, Average loss: 1.730332EPOCH4	loss: 1.719344	Test Error: Accuracy: 76.0%, Average loss: 1.703456EPOCH5	loss: 1.659312	Test Error: Accuracy: 76.6%, Average loss: 1.694500EPOCH6	loss: 1.647946	Test Error: Accuracy: 76.9%, Average loss: 1.691286EPOCH7	loss: 1.653712	Test Error: Accuracy: 77.0%, Average loss: 1.690819EPOCH8	loss: 1.653270	Test Error: Accuracy: 76.8%, Average loss: 1.692459EPOCH9	loss: 1.649021	Test Error: Accuracy: 77.5%, Average loss: 1.686158EPOCH10	loss: 1.648204	Test Error: Accuracy: 78.3%, Average loss: 1.678802EPOCH11	loss: 1.647159	Test Error: Accuracy: 78.4%, Average loss: 1.676133EPOCH12	loss: 1.647390	Test Error: Accuracy: 78.6%, Average loss: 1.674455EPOCH13	loss: 1.646807	Test Error: Accuracy: 78.4%, Average loss: 1.675752EPOCH14	loss: 1.630824	Test Error: Accuracy: 79.1%, Average loss: 1.668470EPOCH15	loss: 1.524222	Test Error: Accuracy: 86.3%, Average loss: 1.599240EPOCH16	loss: 1.524022	Test Error: Accuracy: 86.7%, Average loss: 1.594947EPOCH17	loss: 1.524296	Test Error: Accuracy: 87.1%, Average loss: 1.588946EPOCH18	loss: 1.523599	Test Error: Accuracy: 87.3%, Average loss: 1.588275EPOCH19	loss: 1.523655	Test Error: Accuracy: 87.5%, Average loss: 1.586576EPOCH20	loss: 1.523659	Test Error: Accuracy: 88.2%, Average loss: 1.579286EPOCH21	loss: 1.523733	Test Error: Accuracy: 87.9%, Average loss: 1.582472EPOCH22	loss: 1.523748	Test Error: Accuracy: 88.2%, Average loss: 1.578699EPOCH23	loss: 1.523788	Test Error: Accuracy: 88.0%, Average loss: 1.579700EPOCH24	loss: 1.523708	Test Error: Accuracy: 88.1%, Average loss: 1.579758EPOCH25	loss: 1.523683	Test Error: Accuracy: 88.4%, Average loss: 1.575913EPOCH26	loss: 1.523646	Test Error: Accuracy: 88.7%, Average loss: 1.572831EPOCH27	loss: 1.523654	Test Error: Accuracy: 88.9%, Average loss: 1.570528EPOCH28	loss: 1.523642	Test Error: Accuracy: 89.0%, Average loss: 1.570223EPOCH29	loss: 1.523663	Test Error: Accuracy: 89.0%, Average loss: 1.570385EPOCH30	loss: 1.523658	Test Error: Accuracy: 88.9%, Average loss: 1.571195EPOCH31	loss: 1.523653	Test Error: Accuracy: 88.4%, Average loss: 1.575981EPOCH32	loss: 1.523653	Test Error: Accuracy: 89.0%, Average loss: 1.570087EPOCH33	loss: 1.523642	Test Error: Accuracy: 88.9%, Average loss: 1.571018EPOCH34	loss: 1.523649	Test Error: Accuracy: 89.0%, Average loss: 1.570439EPOCH35	loss: 1.523629	Test Error: Accuracy: 90.4%, Average loss: 1.555473EPOCH36	loss: 1.461187	Test Error: Accuracy: 97.1%, Average loss: 1.491042EPOCH37	loss: 1.461230	Test Error: Accuracy: 97.7%, Average loss: 1.485049EPOCH38	loss: 1.461184	Test Error: Accuracy: 97.7%, Average loss: 1.485653EPOCH39	loss: 1.461156	Test Error: Accuracy: 98.2%, Average loss: 1.479966EPOCH40	loss: 1.461335	Test Error: Accuracy: 98.2%, Average loss: 1.479197EPOCH41	loss: 1.461152	Test Error: Accuracy: 98.7%, Average loss: 1.475477EPOCH42	loss: 1.461153	Test Error: Accuracy: 98.7%, Average loss: 1.475124EPOCH43	loss: 1.461153	Test Error: Accuracy: 98.9%, Average loss: 1.472885EPOCH44	loss: 1.461151	Test Error: Accuracy: 99.1%, Average loss: 1.470957EPOCH45	loss: 1.461156	Test Error: Accuracy: 99.1%, Average loss: 1.471141EPOCH46	loss: 1.461152	Test Error: Accuracy: 99.1%, Average loss: 1.470793EPOCH47	loss: 1.461151	Test Error: Accuracy: 98.8%, Average loss: 1.474548EPOCH48	loss: 1.461151	Test Error: Accuracy: 99.1%, Average loss: 1.470666EPOCH49	loss: 1.461151	Test Error: Accuracy: 99.1%, Average loss: 1.471546EPOCH50	loss: 1.461151	Test Error: Accuracy: 99.0%, Average loss: 1.471407EPOCH51	loss: 1.461151	Test Error: Accuracy: 98.8%, Average loss: 1.473795EPOCH52	loss: 1.461164	Test Error: Accuracy: 98.2%, Average loss: 1.480009EPOCH53	loss: 1.461151	Test Error: Accuracy: 99.2%, Average loss: 1.469931EPOCH54	loss: 1.461152	Test Error: Accuracy: 99.2%, Average loss: 1.469916EPOCH55	loss: 1.461151	Test Error: Accuracy: 98.9%, Average loss: 1.472574EPOCH56	loss: 1.461151	Test Error: Accuracy: 98.6%, Average loss: 1.476035EPOCH57	loss: 1.461151	Test Error: Accuracy: 98.2%, Average loss: 1.478933EPOCH58	loss: 1.461150	Test Error: Accuracy: 99.4%, Average loss: 1.468186EPOCH59	loss: 1.461151	Test Error: Accuracy: 99.4%, Average loss: 1.467602EPOCH60	loss: 1.461151	Test Error: Accuracy: 99.1%, Average loss: 1.471206EPOCH61	loss: 1.461151	Test Error: Accuracy: 98.8%, Average loss: 1.473356EPOCH62	loss: 1.461151	Test Error: Accuracy: 99.2%, Average loss: 1.470242EPOCH63	loss: 1.461150	Test Error: Accuracy: 99.1%, Average loss: 1.470826EPOCH64	loss: 1.461151	Test Error: Accuracy: 98.7%, Average loss: 1.474476EPOCH65	loss: 1.461150	Test Error: Accuracy: 99.3%, Average loss: 1.469116EPOCH66	loss: 1.461150	Test Error: Accuracy: 99.4%, Average loss: 1.467823EPOCH67	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466486EPOCH68	loss: 1.461152	Test Error: Accuracy: 99.3%, Average loss: 1.468688EPOCH69	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466256EPOCH70	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466588EPOCH71	loss: 1.461150	Test Error: Accuracy: 99.6%, Average loss: 1.465280EPOCH72	loss: 1.461150	Test Error: Accuracy: 99.4%, Average loss: 1.467110EPOCH73	loss: 1.461151	Test Error: Accuracy: 99.6%, Average loss: 1.465245EPOCH74	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466551EPOCH75	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466001EPOCH76	loss: 1.461150	Test Error: Accuracy: 99.3%, Average loss: 1.468074EPOCH77	loss: 1.461151	Test Error: Accuracy: 99.6%, Average loss: 1.465709EPOCH78	loss: 1.461150	Test Error: Accuracy: 99.5%, Average loss: 1.466567EPOCH79	loss: 1.461150	Test Error: Accuracy: 99.6%, Average loss: 1.464922EPOCH80	loss: 1.461150	Test Error: Accuracy: 99.6%, Average loss: 1.465109

第五步:搭建GUI界面

第六步:整个工程的内容

有训练代码和训练好的模型以及训练过程,提供数据,提供GUI界面代码,主要使用方法可以参考里面的“文档说明_必看.docx”

 代码的下载路径(新窗口打开链接)基于Pytorch深度学习神经网络MNIST手写数字识别系统源码(带界面和手写画板)

有问题可以私信或者留言,有问必答

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/328782.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

二.使用PgAdmin连接Postgresql

二.使用PgAdmin连接Postgresql PostgreSQL是一种开源的对象关系型数据库管理系统(ORDBMS),它支持大部分SQL标准并提供了许多高级功能,例如事务、外键、视图、触发器等。PostgreSQL由PostgreSQL全球开发组维护和开发,它是一种高度可扩展的数据库系统,可以在各种操作系统…

finallyshell激活-支持所有版本(老版 + 最新版) + 所有平台(mac + windows)

一:打开finally shell的激活页面 二:点击离线激活 三:复制机器码,然后执行一下代码 原文:大哥原文,但是这个大佬是用java实现的,执行因为依赖的问题一直报错 基于以上问题,所以使…

linux Docker在线/离线服务安装并支持centos7和centos8系统

注:以下内容都是经过测试;能在生产环境使用. 一、centos7版本的docker在线安装 1:运行以下命令,下载docker-ce的yum源。 sudo wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo…

Electron学习笔记(五)

文章目录 相关笔记笔记说明 七、系统1、系统对话框2、自定义窗口菜单3、系统右键菜单4、快捷键(1)、监听网页按键事件 (窗口需处于激活状态)(2)、监听全局按键事件 (窗口无需处于激活状态)(3)、补充:自定义窗口菜单快捷…

ModuleNotFoundError: No module named ‘sklearn‘

ModuleNotFoundError: No module named sklearn 解决办法: pip install scikit-learn

Qt---信号和槽

一、信号和槽机制 所谓信号槽,实际就是观察者模式。当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal)。这种发出是没有目的的,类似广播。如果有对象对这个信号…

Python爬虫从入门到精通:一篇涵盖所有细节的高质量教程

目录 第一部分:Python爬虫基础 1.1 爬虫原理 1.2 Python爬虫常用库 1.3 爬虫实战案例 1.4 注意事项 第二部分:爬虫进阶技巧 2.1 处理动态加载的内容 2.2 登录认证 2.3 分布式爬取 2.4 反爬虫策略 第三部分:爬虫实战项目 3.1 豆瓣…

党务政务服务热线|基于SSM的党务政务服务热线平台(源码+数据库+文档)

目录 基于SprinBootvue的党务政务服务热线平台 一、前言 二、系统设计 三、系统功能设计 1系统功能模块 2后台功能模块 5.2.1管理员功能模块 5.2.2部门功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取: …

开源的图形化Windows软件安装升级方案:WingetUI

WingetUI:简化数字生活,WingetUI让软件管理轻松便捷- 精选真开源,释放新价值。 概览 WingetUI是在GitHub上开发的一个实用工具,专为Windows用户设计,旨在为常见的命令行包管理工具(如Winget、Scoop、Pip、…

爬虫入门经典(七) | 采集淘宝电场相关信息

大家好,我是不温卜火,昵称来源于成语—不温不火,本意是希望自己性情温和。 PS:由于现在越来越多的人未经本人同意直接爬取博主本人文章,博主在此特别声明:未经本人允许,禁止转载!&a…

【Leetcode每日一题】 动态规划 - 简单多状态 dp 问题 - 删除并获得点数(难度⭐⭐)(76)

1. 题目解析 题目链接:LCR 091. 粉刷房子 这个问题的理解其实相当简单,只需看一下示例,基本就能明白其含义了。 2.算法原理 1. 状态定义 在解决这类问题时,我们首先需要根据题目的具体要求来定义状态。针对房屋粉刷问题&#…

C语言 | Leetcode C语言题解之第85题最大矩形

题目&#xff1a; 题解&#xff1a; int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize) {int m matrixSize;if (m 0) {return 0;}int n matrixColSize[0];int left[m][n];memset(left, 0, sizeof(left));for (int i 0; i < m; i) {for (int j …

AI图书推荐:ChatGPT 和Power BI驱动未来金融投资变革

《ChatGPT 和Power BI驱动未来金融变革》&#xff08;The Future of Finance with ChatGPT and Power BI&#xff09;由James Bryant和Aloke Mukherjee撰写&#xff0c;探讨了ChatGPT和Power BI在金融领域的应用。 主要特点&#xff1a; - 使用ChatGPT自动化Power BI&#xff…

01 | 为什么需要消息队列?

哪些问题适合使用消息队列来解决&#xff1f; 1. 异步处理 2. 流量控制 使用消息队列隔离网关和后端服务&#xff0c;以达到流量控制和保护后端服务的目的。 3. 服务解耦 无论增加、减少下游系统或是下游系统需求如何变化&#xff0c;订单服务都无需做任何更改&#xff0c…

Linux上编译安装和卸载软件

在maven官网下载maven时候&#xff0c;看到maven-3.9.5这个版本有2份安装包&#xff0c;一个是binaries&#xff0c;一个是source binaries是已编译好的文件&#xff0c;可以直接使用的版本&#xff1b;source是源代码版本&#xff0c;需要自己编译 源码的安装一般由这三个步…

Python函数之旅专栏(导航)

Python内置函数(参考版本:3.11.8)AELRabs( )enumerate( )len( )range( )aiter( )eval( )list( )repr( )all( )exec( )locals( )reversed( )anext( )round( )any( ) ascii( )FM  filter( )map( )S float( )max( )set( )Bformat( )memoryview( )setattr( )bin( )frozenset( )…

Foxmail使用经验总结

目录 1.概述 2.版本历史 3.使用方法 3.1.安装和设置账户 3.2.收取和阅读邮件 ​​​​​​​3.3.发送邮件 ​​​​​​​3.4.管理联系人 ​​​​​​​3.5.日程安排和任务管理 ​​​​​​​3.6.定制设置和插件 ​​​​​​​3.7.跨平台同步 4.小结 1.概述 Fox…

QT:QML制作线形图

目录 一.介绍 二.引入库 三.自定义属性 四.悬停处理函数 五.设置X轴 六.设置Y轴 七.画线 八.测试点坐标 九.设置值 十.效果演示 十一.代码演示 1.LineGraph.qml 2.main.qml 一.介绍 线形图&#xff08;也称为折线图&#xff09;是一种常用的数据可视化工具&#…

Springboot开发 -- Postman 调试 session 验证 接口

当我们在开发Spring Boot应用时&#xff0c;经常会遇到带有Session验证的接口&#xff0c;这些接口需要用户先登录并获取到Session ID&#xff08;或称为cookie中的JSESSIONID&#xff09;&#xff0c;然后在后续的请求中携带这个Session ID来保持会话状态。下面我将以一个实际…

Java 插入数据到Elasticsearch中进行各种类型文档的内容检索

源码下载&#xff1a;链接&#xff1a;https://pan.baidu.com/s/1D3yszkTzjwQz0vFRozQl2g?pwdz6kb 提取码&#xff1a;z6kb 实现思路 1.搭建一个新的springboot项目&#xff0c;不会的请看我这篇博客&#xff1a;springboot项目搭建 2.添加maven依赖 <dependency><…