参考:https://zh-v2.d2l.ai/chapter_convolutional-modern/vgg.html
VGG模型
VGG结构共有5个VGG块,主要模型为VGG-16和VGG-19,分别对应16层和19层结构(包括全连接层),所有卷积层均使用3x3的卷积核,每一个VGG块由若干相同的卷积层构成
VGG核AlexNet的比较,相当于是将AlexNet一部分抽象出来形成VGG块,然后堆叠VGG块以增加模型层数
复现(pytorch)
可以看到每一个VGG块中保持图像的长宽不变,由于kernel size = 3,根据公式计算得padding = 1
同时块内卷积层之间通道数也是相同的
实现代码如下
import torch
from torch import nn
from d2l import torch as d2l
"""
定义vgg块
num_convs: 块中卷积层数量
in_channels: 块输入通道数
out_channels: 块输出通道数
"""
def vgg_block(num_convs, in_channels, out_channels):
layers = []
for _ in range(num_convs):
layers.append(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1))
layers.append(nn.ReLU())
in_channels = out_channels
layers.append(nn.MaxPool2d(2))
return nn.Sequential(*layers)
# 定义vgg网络模型
def vgg(arch):
blks = []
in_channels = 1
for (num_convs, out_channels) in arch:
blks.append(vgg_block(num_convs, in_channels, out_channels))
in_channels = out_channels
return nn.Sequential(*blks, nn.Flatten(),
nn.Linear(out_channels * 7 * 7, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(0.5),
nn.Linear(4096, 10))
# 声明vgg的块(卷积层数,通道数)
conv_arch = ((2, 64), (2, 128), (4, 256), (4, 512), (4, 512))
# conv_arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))
net = vgg(conv_arch)
# X = torch.randn(size=(1, 1, 224, 224))
# for blk in net:
# X = blk(X)
# print(blk.__class__.__name__, 'output shape:\t', X.shape)
ratio = 4
small_conv_arch = [(pair[0], pair[1] // ratio) for pair in conv_arch]
net = vgg(small_conv_arch)
lr, num_epochs, batch_size = 0.05, 10, 128
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)
d2l.train_ch6(net, train_iter, test_iter, num_epochs, lr, d2l.try_gpu())
标签:Convolutional,Scale,nn,Very,VGG,vgg,channels,arch,out
From: https://www.cnblogs.com/dctwan/p/17100921.html