VGG16白板复现

一、网络结构
VGG16网络结构1
VGG16网络结构2
模型实现结果为:
模型实现结果

二、遇到的问题及感想

  1. 在模型网络和全连接层之间,需要将张量进行展平。具体为,在nn.Sequential()之中加上nn.Flatten()
  2. 之前在白板复现AlexNet的时候,直接把网络结构描述出来了,代码如下:
self.conv=nn.Sequential(
            nn.Conv2d(1, 96, 11, 4),
            nn.ReLU(),
            nn.MaxPool2d(3, 2),
            nn.Conv2d(96, 256, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(3,2),
            nn.Conv2d(256, 384, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2d(384, 384, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2d(384,256, 3, 1, 1),
            nn.ReLU(),
            nn.MaxPool2d(3, 2),
        )

然而,如果把VGG16也这样平铺直叙的描述,就过于繁琐了,因此可以用for循环进行描述,比较简洁:

conv_params = ((2, 1, 64), (2, 64, 128), (3, 128, 256), (3, 256,512), (3, 512, 512))

# VGG子模块
def vgg_block(num_convs, in_channels, out_channels):
    block = []
    for i in range(num_convs):
        if i == 0:
            block.append(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1))
            block.append(nn.ReLU())
        elif i == num_convs-1:
            block.append(nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1))
            block.append(nn.ReLU())
            block.append(nn.MaxPool2d(kernel_size=2, stride=2, padding=0))
        else:
            block.append(nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, padding=1))
            block.append(nn.ReLU())
    return nn.Sequential(*block)

# VGG16整体网络
def VGG16(conv_params):
    net = nn.Sequential()
    # 模型网络
    for i, (num_convs, in_channels, out_channels) in enumerate(conv_params):
        net.add_module("vgg_block_" + str(i+1), vgg_block(num_convs, in_channels, out_channels))

    # 全连接层
    net.add_module("fully_connected", nn.Sequential(
        nn.Flatten(),
        nn.Linear(512 * 7 * 7, 4096),
        nn.ReLU(),
        nn.Dropout(0.5),
        nn.Linear(4096, 4096),
        nn.ReLU(),
        nn.Dropout(0.5),
        nn.Linear(4096, 10),
    ))
    return net
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值