# CNN卷积神经网络
# 实验内容和要求
- 编写程序,实现 LeNet-5 卷积神经网络,对 MNIST 手写数字数据库进行训练与识别,展示准确率等。
- 自己选择神经网络,对 CIFAR-10 数据库进行图像物体训练与识别。
# 实验器材
Python 3.7
开发平台:Windows10 Visual Studio Code
机器学习库:torch 1.6.0 torchvision 0.7.0
辅助:CUDA 10.2,用于进行 GPU 加速
# 具体实现
## 3.1 LeNet-5 实现
使用 torch 的 nn.Module 类的派生,可以编写 LeNet5 的结构如下:其中调用 nn.Conv2d()函数进行卷积层设置,用 nn.Linear()函数进行全连接操作。在正向传导的过程中,规定了两次池化,使用 F.max_pool2d 函数。每一经过一层,对结果调用 F.relu()函数进行激活,形成新的输出。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/41bdaf6475f7634baf3f0dfd54857bad.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/12/47f5e57cca1b9803c01131e2fe46886b.writebug)
在实现卷积神经网络的过程中,调用 pytorch 的数据加载模块的部分是遇到的一个难点。调用 torch.utils.data.DataLoader(),设定批的大小,是否随机重组,以及 num_workers(进程数),由于使用的是 Windows 所以对多线程支持的并不好。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/a9f8524c84685af9f3063aee429e565d.writebug)
训练过程:使用优化函数 optimizer(选用 Adam 算法)和损失函数(交叉熵函数 CrossEntropyLoss),对 loss 调用 backard()函数进行反向传播过程。注意在训练前对网络进行 train()设置,启用 batchnormalization 和 dropout,防止网络过拟合。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/ac03d9c4b58c882435ee9e7eb5499579.writebug)
测试过程:启用 eval()模式,对输入数据进入网络进行传播,对输出的 output 取极大值作为预测结果 pred。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/b5e2422c77cff768171f863d57493608.writebug)
## 3.2 AlexNet 实现
网络定义如下图:
![](https://www.writebug.com/myres/static/uploads/2022/6/12/083b15376a2abba5fd478002e2ad43c9.writebug)
![](https://www.writebug.com/myres/static/uploads/2022/6/12/e37b7312212bcb449c40958738ed5e47.writebug)
注意在训练前也要对数据先做预处理,利用 torchvision 的处理函数进行 resize 和转换成张量(tensor)的处理。另外调用 Normalize 函数,将原来的 tensor 从(0,1) 变换到(-1,1)区间。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/5646e00584c7d76ef96fe3fddf668485.writebug)
对 CIFAR-10 的训练和检测与 MNIST 的思想是类似的,不再赘述。
# 实验结果与分析
## 4.1 LeNet-5 对 MNIST 的训练与识别
设置 BATCH_SIZE 为 512,总共训练 10 个 epoch。每次一个 epoch 在过完一遍训练数据之后再过一遍测试数据,得到一次准确度和损失函数的值。训练和测试的输出结果保存在 LeNet.log 里,模型保存为 LeNet.pth。
对训练结果进行可视化处理如下:
![](https://www.writebug.com/myres/static/uploads/2022/6/12/2df4e5fc0773a5a9a0470f750fee4a9e.writebug)
## 4.2 AlexNet 对 CIFAR-10 的训练与识别
设置 BATCH_SIZE 为 32,总共训练 20 个 epoch。每次一个 epoch 在过完一遍训练数据之后再过一遍测试数据,得到一次准确度和损失函数的值。训练和测试的输出结果保存 AlexNet.log 里,模型保存为 AlexNet.pth。
因为 AlexNet 网络比较复杂,而且 CIFAR-10 数据量也较大,现将训练的网络结构打印如下验证是否正确:
![](https://www.writebug.com/myres/static/uploads/2022/6/12/0d36839bbc79130594a26ebf2a63201b.writebug)
我们对训练后的结果先随机选择一批数据进行测试:
![](https://www.writebug.com/myres/static/uploads/2022/6/12/ba0274d8d7dc8c4df94ffe3e463cf68b.writebug)
对比实际标签和预测标签:在 32 张图中正确判断了 27 张,正确率约为 84%。
```c++
GroundTruth: cat ship ship airplane frog frog automobile frog cat automobile airplane truck dog horse truck ship dog horse ship frog horse airplane deer truck
dog bird deer airplane truck frog frog dog
Predicted: cat ship ship airplane frog frog truck frog cat automobile airplane truck dog horse truck ship dog horse ship frog horse bird airplane truck deer frog deer airplane truck frog frog dog
```
另外,在五万张训练数据中测试的结果显示正确率为 92%,在一万张新的测试数据中的结果为 77%。在十个标签中,正确率最高的 ship 达到了 91%,最低的 cat 也有近六成的判断正确率。
![](https://www.writebug.com/myres/static/uploads/2022/6/12/1da41d7beed07fd024c5c689659beb92.writebug)
# 心得与体会
我之前曾经用过 tensorflow 做过 MNIST 的识别,所以这次使用了 torch 这一工具,拓展一下视野和能力。torch 在对于小型网络方面的编写和运算都要比 tensorflow 快,适合轻量开发。
在跑 AlexNet 的时候,因为数据量大,网络层数多,计算量很大,用 CPU 跑的话要炸掉。所以花了很长时间配置 CUDA 和 pytorch 的 gpu 加速版本适配。但是最后很遗憾的发现 Windows 对于多线程支持很差,所以虽然有所提速,但还是跑的很慢很慢。最终用了大概五六个小时才得到网络模型,而且模型文件竟然有 200 多 M(就不附在上交的压缩文件里了)。以后要加速还是要用 Linux 啊。
编写的过程是简单的,对照着参考论文和示意图一步一步设定网络的层的功能和参数即可,训练和识别的过程大同小异,需要根据不同的数据类别加以调整即可。
通过本次实验,再次体验了 CNN 网络的训练和测试流程与原理,进一步理解的 CNN 的 pipeline,对机器学习有了初步了解。
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
资源包含文件:设计报告word+代码 LeNet-5 对 MNIST 的训练与识别 AlexNet 对 CIFAR-10 的训练与识别 Python 3.7 开发平台:Windows10 Visual Studio Code 机器学习库:torch 1.6.0 torchvision 0.7.0 辅助:CUDA 10.2,用于进行 GPU 加速 详细介绍参考:https://biyezuopin.blog.csdn.net/article/details/125256884
资源推荐
资源详情
资源评论
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ipynb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/DOCX.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 7 条
- 1
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
shejizuopin
- 粉丝: 1w+
- 资源: 1294
![benefits](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-1.c8e153b4.png)
下载权益
![privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-2.ec46750a.png)
C知道特权
![article](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-3.fc5e5fb6.png)
VIP文章
![course-privilege](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-4.320a6894.png)
课程特权
![rights](https://csdnimg.cn/release/downloadcmsfe/public/img/vip-rights-icon.fe0226a8.png)
开通VIP
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
- 3
- 4
前往页