AlexNet Pytorch实现【初学者】

1. 内容来源

24 深度卷积神经网络 AlexNet【动手学深度学习v2】_哔哩哔哩_bilibili24 深度卷积神经网络 AlexNet【动手学深度学习v2】共计3条视频,包括:AlexNet、代码、QA等,UP主更多精彩视频,请关注UP账号。https://www.bilibili.com/video/BV1h54y1L7oe/?spm_id_from=333.999.0.0&vd_source=f94822d3eca79b8e245bb58bbced6b77

2. Pytorch实现

2.1 模型构建

AlexNet网络结构图(本文输入224*224,图中大小均需减一)如下:

(图源:深度学习卷积神经网络-AlexNet - 知乎

总结网络结构如下:

  1. 卷积层1(输入1通道,输出96通道,11*11卷积核,4步长,1填充)-- 激活层1(ReLU)--最大池化层1(3*3核,2步长)
  2. 卷积层2(输入96通道,输出256通道,5*5卷积核,1步长,2填充)-- 激活层2(ReLU)--最大池化层2(3*3核,2步长)
  3. 卷积层3(输入256通道,输出384通道,3*3卷积核,1步长,1填充)-- 激活层3(ReLU)
  4. 卷积层4(输入384通道,输出384通道,3*3卷积核,1步长,1填充)-- 激活层4(ReLU)
  5. 卷积层5(输入384通道,输出256通道,3*3卷积核,1步长,1填充)-- 激活层5(ReLU)--最大池化层3(3*3核,2步长)
  6. 展开成单维向量
  7. 线性层1(输入6400维,输出4096维)-- 激活层6(ReLU)--Dropout层1(50%丢弃率)
  8. 线性层2(输入5096维,输出4096维)-- 激活层7(ReLU)--Dropout层2(50%丢弃率)
  9. 输出层(输入4096维,输出10维)

代码实现:

import torch
from torch import nn
from d2l import torch as d2l

net = nn.Sequential(
    nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(), 
    nn.MaxPool2d(kernel_size=3, stride=2), 
    nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(), 
    nn.MaxPool2d(kernel_size=3, stride=2), 
    nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(), 
    nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(), 
    nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(), 
    nn.MaxPool2d(kernel_size=3, stride=2), 
    nn.Flatten(), 
    nn.Linear(6400, 4096), nn.ReLU(), nn.Dropout(p=0.5), 
    nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(p=0.5), 
    nn.Linear(4096, 10)
)

维度打印:

Conv2d output shape :     	 torch.Size([1, 96, 54, 54])
ReLU output shape :     	 torch.Size([1, 96, 54, 54])
MaxPool2d output shape :     torch.Size([1, 96, 26, 26])
Conv2d output shape :     	 torch.Size([1, 256, 26, 26])
ReLU output shape :     	 torch.Size([1, 256, 26, 26])
MaxPool2d output shape :     torch.Size([1, 256, 12, 12])
Conv2d output shape :     	 torch.Size([1, 384, 12, 12])
ReLU output shape :     	 torch.Size([1, 384, 12, 12])
Conv2d output shape :     	 torch.Size([1, 384, 12, 12])
ReLU output shape :     	 torch.Size([1, 384, 12, 12])
Conv2d output shape :     	 torch.Size([1, 256, 12, 12])
ReLU output shape :     	 torch.Size([1, 256, 12, 12])
MaxPool2d output shape :     torch.Size([1, 256, 5, 5])
Flatten output shape :     	 torch.Size([1, 6400])
Linear output shape :     	 torch.Size([1, 4096])
ReLU output shape :     	 torch.Size([1, 4096])
Dropout output shape :     	 torch.Size([1, 4096])
Linear output shape :     	 torch.Size([1, 4096])
ReLU output shape :     	 torch.Size([1, 4096])
Dropout output shape :     	 torch.Size([1, 4096])
Linear output shape :     	 torch.Size([1, 10])

2.2 模型训练

#Data
batch_size = 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)

#Train
lr, num_epochs = 0.01, 10
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())

其中,为了与本系列中LeNet等模型进行性能对比,采用的Fashion Mnist数据集样本大小为28*28,本模型输入为224*224,需要拉伸(仅用于测试该网络,特征信息不会增加)

结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云龙弓手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值