• paddlepaddle 复现论文LeNet-5网络结构(仅仅关注模型)


    paddlepaddle 复现论文LeNet-5网络结构(仅仅关注模型)

    选用LeNet-5网络结构,论文地址:Gradient-based learning applied to document recognition | IEEE Journals & Magazine | IEEE Xplore 

     每个阶段用到的Layer

     

     原论文模型复现(方法一)

    1. import paddle.nn as nn
    2. network = nn.Sequential(
    3. nn.Conv2D(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=0), # C1 卷积层
    4. nn.Tanh(),
    5. nn.AvgPool2D(kernel_size=2, stride=2), # S2 平局池化层
    6. nn.Sigmoid(), # Sigmoid激活函数
    7. nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0), # C3 卷积层
    8. nn.Tanh(),
    9. nn.AvgPool2D(kernel_size=2, stride=2), # S4 平均池化层
    10. nn.Sigmoid(), # Sigmoid激活函数
    11. nn.Conv2D(in_channels=16, out_channels=120, kernel_size=5, stride=1, padding=0), # C5 卷积层
    12. nn.Tanh(),
    13. nn.Flatten(),
    14. nn.Linear(in_features=120, out_features=84), # F6 全连接层
    15. nn.Tanh(),
    16. nn.Linear(in_features=84, out_features=10) # OUTPUT 全连接层
    17. )

     模型可视化

    paddle.summary(network, (1, 1, 32, 32))

     输出模型结果

    1. --------------------------------------------------------------------------
    2. Layer (type) Input Shape Output Shape Param #
    3. ===========================================================================
    4. Conv2D-1 [[1, 1, 32, 32]] [1, 6, 28, 28] 156
    5. Tanh-1 [[1, 6, 28, 28]] [1, 6, 28, 28] 0
    6. AvgPool2D-1 [[1, 6, 28, 28]] [1, 6, 14, 14] 0
    7. Sigmoid-1 [[1, 6, 14, 14]] [1, 6, 14, 14] 0
    8. Conv2D-2 [[1, 6, 14, 14]] [1, 16, 10, 10] 2,416
    9. Tanh-2 [[1, 16, 10, 10]] [1, 16, 10, 10] 0
    10. AvgPool2D-2 [[1, 16, 10, 10]] [1, 16, 5, 5] 0
    11. Sigmoid-2 [[1, 16, 5, 5]] [1, 16, 5, 5] 0
    12. Conv2D-3 [[1, 16, 5, 5]] [1, 120, 1, 1] 48,120
    13. Tanh-3 [[1, 120, 1, 1]] [1, 120, 1, 1] 0
    14. Flatten-1 [[1, 120, 1, 1]] [1, 120] 0
    15. Linear-1 [[1, 120]] [1, 84] 10,164
    16. Tanh-4 [[1, 84]] [1, 84] 0
    17. Linear-2 [[1, 84]] [1, 10] 850
    18. ===========================================================================
    19. Total params: 61,706
    20. Trainable params: 61,706
    21. Non-trainable params: 0
    22. ---------------------------------------------------------------------------
    23. Input size (MB): 0.00
    24. Forward/backward pass size (MB): 0.12
    25. Params size (MB): 0.24
    26. Estimated Total Size (MB): 0.36
    27. ---------------------------------------------------------------------------

    对模型进行简单修改(方法二

    改进模型

    Sequential写法

    1. import paddle.nn as nn
    2. network_2 = nn.Sequential(
    3. nn.Conv2D(in_channels=1, out_channels=6, kernel_size=3, stride=1, padding=1),
    4. nn.ReLU(),
    5. nn.MaxPool2D(kernel_size=2, stride=2),
    6. nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0),
    7. nn.ReLU(),
    8. nn.MaxPool2D(kernel_size=2, stride=2),
    9. nn.Flatten(),
    10. nn.Linear(in_features=400, out_features=120), # 400 = 5x5x16,输入形状为32x32, 输入形状为28x28时调整为256
    11. nn.Linear(in_features=120, out_features=84),
    12. nn.Linear(in_features=84, out_features=10)
    13. )

     模型可视化

    paddle.summary(network_2, (1, 1, 28, 28))

     输出结果

    1. ---------------------------------------------------------------------------
    2. Layer (type) Input Shape Output Shape Param #
    3. ===========================================================================
    4. Conv2D-4 [[1, 1, 28, 28]] [1, 6, 28, 28] 60
    5. ReLU-1 [[1, 6, 28, 28]] [1, 6, 28, 28] 0
    6. MaxPool2D-1 [[1, 6, 28, 28]] [1, 6, 14, 14] 0
    7. Conv2D-5 [[1, 6, 14, 14]] [1, 16, 10, 10] 2,416
    8. ReLU-2 [[1, 16, 10, 10]] [1, 16, 10, 10] 0
    9. MaxPool2D-2 [[1, 16, 10, 10]] [1, 16, 5, 5] 0
    10. Flatten-2 [[1, 16, 5, 5]] [1, 400] 0
    11. Linear-3 [[1, 400]] [1, 120] 48,120
    12. Linear-4 [[1, 120]] [1, 84] 10,164
    13. Linear-5 [[1, 84]] [1, 10] 850
    14. ===========================================================================
    15. Total params: 61,610
    16. Trainable params: 61,610
    17. Non-trainable params: 0
    18. ---------------------------------------------------------------------------
    19. Input size (MB): 0.00
    20. Forward/backward pass size (MB): 0.11
    21. Params size (MB): 0.24
    22. Estimated Total Size (MB): 0.35
    23. ---------------------------------------------------------------------------

    方法三

    Sub Class写法

    1. class LeNet(nn.Layer):
    2. """
    3. 继承paddle.nn.Layer定义网络结构
    4. """
    5. def __init__(self, num_classes=10):
    6. """
    7. 初始化函数
    8. """
    9. super(LeNet, self).__init__()
    10. self.features = nn.Sequential(
    11. nn.Conv2D(in_channels=1, out_channels=6, kernel_size=3, stride=1, padding=1), # 第一层卷积
    12. nn.ReLU(), # 激活函数
    13. nn.MaxPool2D(kernel_size=2, stride=2), # 最大池化,下采样
    14. nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1, padding=0), # 第二层卷积
    15. nn.ReLU(), # 激活函数
    16. nn.MaxPool2D(kernel_size=2, stride=2) # 最大池化,下采样
    17. )
    18. self.fc = nn.Sequential(
    19. nn.Linear(400, 120), # 全连接
    20. nn.Linear(120, 84), # 全连接
    21. nn.Linear(84, num_classes) # 输出层
    22. )
    23. def forward(self, inputs):
    24. """
    25. 前向计算
    26. """
    27. y = self.features(inputs)
    28. y = paddle.flatten(y, 1)
    29. out = self.fc(y)
    30. return out
    31. network_3 = LeNet()

    模型可视化

    paddle.summary(network_3, (1, 1, 28, 28))

    1. ---------------------------------------------------------------------------
    2. Layer (type) Input Shape Output Shape Param #
    3. ===========================================================================
    4. Conv2D-6 [[1, 1, 28, 28]] [1, 6, 28, 28] 60
    5. ReLU-3 [[1, 6, 28, 28]] [1, 6, 28, 28] 0
    6. MaxPool2D-3 [[1, 6, 28, 28]] [1, 6, 14, 14] 0
    7. Conv2D-7 [[1, 6, 14, 14]] [1, 16, 10, 10] 2,416
    8. ReLU-4 [[1, 16, 10, 10]] [1, 16, 10, 10] 0
    9. MaxPool2D-4 [[1, 16, 10, 10]] [1, 16, 5, 5] 0
    10. Linear-6 [[1, 400]] [1, 120] 48,120
    11. Linear-7 [[1, 120]] [1, 84] 10,164
    12. Linear-8 [[1, 84]] [1, 10] 850
    13. ===========================================================================
    14. Total params: 61,610
    15. Trainable params: 61,610
    16. Non-trainable params: 0
    17. ---------------------------------------------------------------------------
    18. Input size (MB): 0.00
    19. Forward/backward pass size (MB): 0.11
    20. Params size (MB): 0.24
    21. Estimated Total Size (MB): 0.35
    22. ---------------------------------------------------------------------------

    PaddlePaddle高层API写法(方法四

    高层API ,最简单,只需一行就可以。

    network_4 = paddle.vision.models.LeNet(num_classes=10)
    

     模型可视化

    paddle.summary(network_4, (1, 1, 28, 28))

     输出结果

    1. ---------------------------------------------------------------------------
    2. Layer (type) Input Shape Output Shape Param #
    3. ===========================================================================
    4. Conv2D-8 [[1, 1, 28, 28]] [1, 6, 28, 28] 60
    5. ReLU-5 [[1, 6, 28, 28]] [1, 6, 28, 28] 0
    6. MaxPool2D-5 [[1, 6, 28, 28]] [1, 6, 14, 14] 0
    7. Conv2D-9 [[1, 6, 14, 14]] [1, 16, 10, 10] 2,416
    8. ReLU-6 [[1, 16, 10, 10]] [1, 16, 10, 10] 0
    9. MaxPool2D-6 [[1, 16, 10, 10]] [1, 16, 5, 5] 0
    10. Linear-9 [[1, 400]] [1, 120] 48,120
    11. Linear-10 [[1, 120]] [1, 84] 10,164
    12. Linear-11 [[1, 84]] [1, 10] 850
    13. ===========================================================================
    14. Total params: 61,610
    15. Trainable params: 61,610
    16. Non-trainable params: 0
    17. ---------------------------------------------------------------------------
    18. Input size (MB): 0.00
    19. Forward/backward pass size (MB): 0.11
    20. Params size (MB): 0.24
    21. Estimated Total Size (MB): 0.35
    22. ---------------------------------------------------------------------------

    总结一下 方法二、方法三、方法四。 他们是同一种网络结构,不同的表现形式,网络的模型结构是一样的。

    最后是重要的 模型训练与调优

    选方法二的网络模型结构  进行训练与调优

    1. # 模型封装
    2. model = paddle.Model(network_2)
    3. # 模型配置
    4. model.prepare(paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters()), # 优化器
    5. paddle.nn.CrossEntropyLoss(), # 损失函数
    6. paddle.metric.Accuracy()) # 评估指标
    7. # 启动全流程训练
    8. model.fit(train_dataset, # 训练数据集
    9. eval_dataset, # 评估数据集
    10. epochs=5, # 训练轮次
    11. batch_size=64, # 单次计算数据样本量
    12. verbose=1) # 日志展示形式

    模型评估

    1. result = model.evaluate(eval_dataset, verbose=1)
    2. print(result)

    批量预测

    1. # 进行预测操作
    2. result = model.predict(eval_dataset)
    3. # 定义画图方法
    4. def show_img(img, predict):
    5. plt.figure()
    6. plt.title('predict: {}'.format(predict))
    7. plt.imshow(img.reshape([28, 28]), cmap=plt.cm.binary)
    8. plt.show()
    9. # 抽样展示
    10. indexs = [2, 15, 38, 211]
    11. for idx in indexs:
    12. show_img(eval_dataset[idx][0], np.argmax(result[0][idx]))

    参考链接:

    (79条消息) PaddlePaddle入门实践——初识卷积神经网络_面包猎人的博客-CSDN博客_paddle 卷积



    (117条消息) 深度学习之paddle之卷积神经网络定义,自定义数据集_小码农--向日葵的博客-CSDN博客_paddle 卷积
     

    (119条消息) 模型量化(2):Paddle 模型的静态量化和动态量化_AI Studio的博客-CSDN博客_paddle模型量化

    #Json 数据集加载和训练,适用于缺陷检测

    (130条消息) 飞桨深度学习零基础入门(二)——使用飞浆(Paddle)实现卷积神经网络的手写数字(mnist)识别_AiXing-w的博客-CSDN博客_mnist 飞桨 

  • 相关阅读:
    LeetCode二分查找系列(704,34,35,69,367)
    小程序的赛道越来越拥挤
    python机器学习库中Scikit-learn和TensorFlow如何选择?
    使用imx 8m 测试matter协议功能
    想用macbook录制视频?这几个技巧让你事半功倍!
    这就是艺术,优雅的二维码生成器「GitHub 热点速览」
    ffmpeg深度学习滤镜
    【Linux】:Kafka基础命令
    基于JSP技术的校园餐厅管理系统
    Vue源码总结
  • 原文地址:https://blog.csdn.net/Vertira/article/details/126661523