作者:紫岚蓝魂_578 | 来源:互联网 | 2023-08-28 13:50
这篇文章主要研究CNN模型的深度对大规模图像识别任务准确率的影响。主要贡献是在使用非常小的卷积核(3×3)结构下,对神经网络加深层数做了一个深入的评估,研究结果表明通过把层数增加到
这篇文章主要研究CNN模型的深度对大规模图像识别任务准确率的影响。
主要贡献是在使用非常小的卷积核(3×3)结构下,对神经网络加深层数做了一个深入的评估,研究结果表明通过把层数增加到16-19层,模型的效果得到了非常大的提升。
模型结构
通用的结构如下:
- 使用非常小的卷积核:3×3(这是能够捕捉上下左右中间各个位置的最小尺寸),在有些设置下还使用了1×1卷积核
- stride固定是1
- 使用same padding,即卷积后保持feature map的尺度不变。例如对于3×3卷积核,padding=1
- 使用5个Max-pooling层,不是所有卷积层后面都跟池化层。window size = 2,stride = 2。这里和AlexNet不同,AlexNet使用重叠的池化,Vgg使用不重叠的池化。
- 卷积层后是3个全连接层,前两个是4096维的,第三个是1000维。
- 所有隐藏层都使用ReLU。
- LRN并不能提升性能,只会增加内从消耗和计算时间。
作者设置的几个网络的结构如下图所示:
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/d34245582687a4e6.webp)
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/d34245582687a4e6.webp)
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/d67981797265d9c7.webp)
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/d67981797265d9c7.webp)
VGG的CNN设置与ILSVRC-2012(Krizhevsky et al.)和ILSVRC-2013(Zeiler&Fergus,2013,Sermanet etal.,2014)获胜者的设置非常不同。之前的模型在第一个卷积层都使用较大的感受野,如11×11,stride=4(Krizhevsky et al.),7×7,stirde=2(Zeiler& Fergus, 2013; Sermanet et al., 2014)。VGG在整个网络中使用3×3的感受野,stride设置为1。
3×3卷积的作用
连续使用两个3×3的卷积核的感受野相当于5×5,连续使用三个3×3的卷积核的感受野相当于7×7。 那么举例来说,使用3个3×3的卷积核而不是一个7×7的卷积核的好处是什么?有两点原因:
- 我们使用了3次非线性函数而不是1次,这样增加了函数的判别能力。
- 这样减少了参数的数量:假设三个堆叠的3×3卷积层的输入和输出的通道数都是C,参数数目为。对于一个7×7的卷积核,参数数目为。这相当于对7×7的卷积核施加了正则化。
1×1卷积的作用
增加决策函数(decision function)的非线性,因为引入了非线性激活函数。
模型的具体实现
下面给出VGG的Pytorch实现:
class VGG(nn.Module):
def __init__(self, features, num_classes=1000):
super(VGG, self).__init__()
self.features = features
self.classifier = nn.Sequential(
nn.Linear(512 * 7 * 7, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(True),
nn.Dropout(),
nn.Linear(4096, num_classes),
)
self._initialize_weights()
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def make_layers(cfg, batch_norm=False):
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
cfg = {
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'B': [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
'D': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'],
'E': [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'],
}
def vgg11(**kwargs):
""" VGG 11-layer model (configuration "A") """
model = VGG(make_layers(cfg['A']), **kwargs)
return model
def vgg13(**kwargs):
""" VGG 13-layer model (configuration "B") """
model = VGG(make_layers(cfg['B']), **kwargs)
return model
def vgg16(**kwargs):
""" VGG 16-layer model (configuration "D") """
model = VGG(make_layers(cfg['D']), **kwargs)
return model
def vgg19(**kwargs):
""" VGG 19-layer model (configuration "E") """
model = VGG(make_layers(cfg['E']), **kwargs)
return model
实验结果
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
![《CNN模型之VGG》](https://img.php1.cn/3cd4a/1eebe/cd5/99b88427bc9ce0dc.webp)
参考
原文:Very Deep Convolutional Networks for Large-scale Image Recongnition