使用 PyTorch 实现 AlexNet

标签: 动手实现经典神经网络 发布于:2021-08-19 12:58:50 编辑于:2021-08-20 20:22:30 浏览量:1817

概述

ILSVR 2012 Winner

首个深度卷积神经网络,首次证明了学习到的特征可以超越手工设计的特征,从而一举打破计算机视觉研究的前状。

对应仓库地址:https://github.com/songquanpeng/pytorch-classifiers

网络架构

alexnet

PyTorch 实现

class AlexNet(nn.Module):
    def __init__(self, args):
        super().__init__()
        self.args = args
        assert args.img_size == 227
        layers = [
            nn.Conv2d(args.img_dim, 96, 11, 4),  # 96x55x55
            nn.ReLU(),
            nn.MaxPool2d(3, 2),  # 96x27x27
            nn.Conv2d(96, 256, 5, 1, 2),  # 256x27x27
            nn.ReLU(),
            nn.MaxPool2d(3, 2),  # 256x13x13
            nn.Conv2d(256, 384, 3, 1, 1),  # 384x13x13
            nn.ReLU(),
            nn.Conv2d(384, 384, 3, 1, 1),  # 384x13x13
            nn.ReLU(),
            nn.Conv2d(384, 256, 3, 1, 1),  # 256x13x13
            nn.ReLU(),
            nn.MaxPool2d(3, 2)  # 256x6x6
        ]
        self.conv = nn.Sequential(*layers)
        layers = [
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(),
            nn.Linear(4096, 4096),
            nn.ReLU(),
            nn.Linear(4096, args.num_classes)
        ]
        self.fc = nn.Sequential(*layers)

    def forward(self, x):
        h = self.conv(x)
        y = self.fc(h.view(x.shape[0], -1))
        return y

未经允许,禁止转载,本文源站链接:https://iamazing.cn/