现代卷积网络实战系列4:PyTorch从零构建VGGNet训练MNIST数据集

🌈🌈🌈现代卷积网络实战系列 总目录

本篇文章的代码运行界面均在Pycharm中进行
本篇文章配套的代码资源已经上传

1、MNIST数据集处理、加载、网络初始化、测试函数

2、训练函数、PyTorch构建LeNet网络

3、PyTorch从零构建AlexNet训练MNIST数据集

4、PyTorch从零构建VGGNet训练MNIST数据集

5、PyTorch从零构建GoogLeNet训练MNIST数据集

6、PyTorch从零构建ResNet训练MNIST数据集

8、VGGNet

2014年,牛津大学计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研发出了新的深度卷积神经网络:VGGNet,并取得了ILSVRC2014比赛分类项目的第二名(第一名是GoogLeNet,也是同年提出的).论文下载 Very Deep Convolutional Networks for Large-Scale Image Recognition。论文主要针对卷积神经网络的深度对大规模图像集识别精度的影响,主要贡献是使用很小的卷积核(3×3)构建各种深度的卷积神经网络结构,并对这些网络结构进行了评估,最终证明16-19层的网络深度,能够取得较好的识别精度。 这也就是常用来提取图像特征的VGG-16和VGG-19。

VGG可以看成是加深版的AlexNet,整个网络由卷积层和全连接层叠加而成,和AlexNet不同的是,VGG中使用的都是小尺寸的卷积核(3×3)。

我这里使用的是VGG-16,但是又因为这个系列全部是处理MNIST数据集的,所以我这里的VGG网络只用了3个VGG块,FC也减少了很多参数。

9、VGGNet网络架构

VGGNet(

(vgg1): VGGBlock(

(conv1): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu1): ReLU(inplace=True)

(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu2): ReLU(inplace=True)

(conv3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3): ReLU(inplace=True)

(maxpool1): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)

)

(vgg2): VGGBlock(

(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu1): ReLU(inplace=True)

(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu2): ReLU(inplace=True)

(conv3): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3): ReLU(inplace=True)

(maxpool1): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)

)

(vgg3): VGGBlock(

(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu1): ReLU(inplace=True)

(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu2): ReLU(inplace=True)

(conv3): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

(relu3): ReLU(inplace=True)

(maxpool1): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)

)

(classifier): Sequential(

(0): Linear(in_features=12544, out_features=1024, bias=True)

(1): ReLU(inplace=True)

(2): Linear(in_features=1024, out_features=512, bias=True)

(3): ReLU(inplace=True)

(4): Linear(in_features=512, out_features=10, bias=True)

)

)

VGG实际上就是很简单,主要是由VGG块组成:

前两组卷积形式一样,每组都是:conv-relu-conv-relu-pool

中间三组卷积形式一样,每组都是:conv-relu-conv-relu-conv-relu-pool

最后分类的三个全连接层:fc-relu-dropout-fc-relu-dropout-fc-softmax

10、PyTorch构建VGGBlock

python 复制代码
class VGGBlock(nn.Module):
    def __init__(self, in_channel, out_channel, num_conv):
        super(VGGBlock, self).__init__()
        self.num_conv = num_conv
        self.conv1 = nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=1, padding=1)
        self.relu1 = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1)
        self.relu2 = nn.ReLU(inplace=True)
        self.conv3 = nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1)
        self.relu3 = nn.ReLU(inplace=True)
        self.maxpool1 = nn.MaxPool2d(3, stride=2, padding=1)

    def forward(self, x):
        x = self.relu1(self.conv1(x))
        x = self.relu2(self.conv2(x))
        if self.num_conv==3:
            x = self.relu3(self.conv3(x))
        else:
            x = self.maxpool1(x)
        return x

11、PyTorch构建VGGNet

python 复制代码
class VGGNet(nn.Module):
    def __init__(self, num_classes):
        super(VGGNet, self).__init__()
        self.vgg1 = VGGBlock(1,64,2)
        self.vgg2 = VGGBlock(64,128,2)
        self.vgg3 = VGGBlock(128,256,3)
        self.classifier = nn.Sequential(
            nn.Linear(256 * 7 * 7, 1024),
            nn.ReLU(inplace=True),
            nn.Linear(1024, 512),
            nn.ReLU(inplace=True),
            nn.Linear(512, num_classes)
        )

    def forward(self, x):
        x = self.vgg1(x)
        x = self.vgg2(x)
        x = self.vgg3(x)
        x = x.reshape(x.shape[0], -1)
        x = self.classifier(x)
        return x

D:\conda\envs\pytorch\python.exe A:\0_MNIST\train.py

Reading data...

train_data: (60000, 28, 28) train_label (60000,)

test_data: (10000, 28, 28) test_label (10000,)

Initialize neural network

test loss 2303.1

test accuracy 10.1%

epoch step 1

training time 8.9s

training loss 204.3

test loss 39.6

test accuracy 98.8%

epoch step 2

training time 8.3s

training loss 48.8

test loss 39.7 test

accuracy 98.8%

epoch step 3

training time 8.1s

training loss 35.9

test loss 26.4

test accuracy 99.1%

Training finished

3 epoch training time 25.4s

One epoch average training time 8.5s

进程已结束,退出代码为 0

相关推荐
羊小猪~~6 分钟前
深度学习基础--CNN经典网络之InceptionV1研究与复现(pytorch)
网络·人工智能·pytorch·深度学习·神经网络·机器学习·cnn
硅谷秋水27 分钟前
AutoEval:现实世界中通才机器人操作策略的自主评估
人工智能·机器学习·计算机视觉·机器人
weixin_4578858231 分钟前
DeepSeek的神经元革命:穿透搜索引擎算法的下一代内容基建
人工智能·算法·搜索引擎·deepseek·虎跃办公
大模型真好玩34 分钟前
不写一行代码! VsCode+Cline+高德地图MCP Server 帮你搞定和女友的出行规划(附原理解析)
人工智能·python·mcp
GetcharZp43 分钟前
SAM2全面解析:Meta新一代“分割一切”大模型,图像视频一键精准分割!
计算机视觉·llm
zskj_zhyl43 分钟前
数据驱动的温暖守护:智慧康养平台如何实现 “千人千面” 的精准照护?
人工智能·科技·生活
视觉语言导航1 小时前
ICRA-2025 | 视觉预测助力机器人自主导航!NavigateDiff:视觉引导的零样本导航助理
人工智能·机器人·具身智能
Seon塞翁1 小时前
2025年Q1 AI 学习应用总结
人工智能·学习
jndingxin1 小时前
OpenCV 图形API(30)图像滤波-----腐蚀操作函数erode()
人工智能·opencv
AIGC-Lison1 小时前
Stable Diffusion ComfyUI 基础教程(一) ComfyUI安装与常用插件
人工智能·stable diffusion·教程·ai绘画·sd·sd教程