Pytorch:修改 VGG16 架构
Pytorch: Modifying VGG16 Architecture
我目前正在尝试修改 VGG16 网络架构,以便它能够接受 400x400 像素的图像。
根据我读过的文献,这样做的方法是将完全连接 (FC) 层转换为卷积 (CONV) 层。这实质上将“允许网络有效地‘滑过’更大的输入图像,并对图像的不同部分进行多次评估,并结合所有可用的上下文信息。”之后,使用 Average Pooling 层 "average the multiple feature vectors into a single feature vector that summarizes the input image"。
我已经做到了 ,并提出了以下网络架构:
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 400, 400] 1,792
ReLU-2 [-1, 64, 400, 400] 0
Conv2d-3 [-1, 64, 400, 400] 36,928
ReLU-4 [-1, 64, 400, 400] 0
MaxPool2d-5 [-1, 64, 200, 200] 0
Conv2d-6 [-1, 128, 200, 200] 73,856
ReLU-7 [-1, 128, 200, 200] 0
Conv2d-8 [-1, 128, 200, 200] 147,584
ReLU-9 [-1, 128, 200, 200] 0
MaxPool2d-10 [-1, 128, 100, 100] 0
Conv2d-11 [-1, 256, 100, 100] 295,168
ReLU-12 [-1, 256, 100, 100] 0
Conv2d-13 [-1, 256, 100, 100] 590,080
ReLU-14 [-1, 256, 100, 100] 0
Conv2d-15 [-1, 256, 100, 100] 590,080
ReLU-16 [-1, 256, 100, 100] 0
MaxPool2d-17 [-1, 256, 50, 50] 0
Conv2d-18 [-1, 512, 50, 50] 1,180,160
ReLU-19 [-1, 512, 50, 50] 0
Conv2d-20 [-1, 512, 50, 50] 2,359,808
ReLU-21 [-1, 512, 50, 50] 0
Conv2d-22 [-1, 512, 50, 50] 2,359,808
ReLU-23 [-1, 512, 50, 50] 0
MaxPool2d-24 [-1, 512, 25, 25] 0
Conv2d-25 [-1, 512, 25, 25] 2,359,808
ReLU-26 [-1, 512, 25, 25] 0
Conv2d-27 [-1, 512, 25, 25] 2,359,808
ReLU-28 [-1, 512, 25, 25] 0
Conv2d-29 [-1, 512, 25, 25] 2,359,808
ReLU-30 [-1, 512, 25, 25] 0
MaxPool2d-31 [-1, 512, 12, 12] 0
Conv2d-32 [-1, 4096, 1, 1] 301,993,984
ReLU-33 [-1, 4096, 1, 1] 0
Dropout-34 [-1, 4096, 1, 1] 0
Conv2d-35 [-1, 4096, 1, 1] 16,781,312
ReLU-36 [-1, 4096, 1, 1] 0
Dropout-37 [-1, 4096, 1, 1] 0
Conv2d-38 [-1, 3, 1, 1] 12,291
AdaptiveAvgPool2d-39 [-1, 3, 1, 1] 0
Softmax-40 [-1, 3, 1, 1] 0
================================================================
Total params: 333,502,275
Trainable params: 318,787,587
Non-trainable params: 14,714,688
----------------------------------------------------------------
Input size (MB): 1.83
Forward/backward pass size (MB): 696.55
Params size (MB): 1272.21
Estimated Total Size (MB): 1970.59
----------------------------------------------------------------
我的问题很简单:最后是否需要使用平均池化层?似乎在最后一个卷积层,我们得到了一个 1x1 的图像,有 3 个通道。对此进行平均汇集似乎没有任何效果。
如果我的逻辑/架构有任何不妥之处,请随时指出。
谢谢!
AdaptiveAvgPool2d
的目的是让 convnet 处理任意大小的输入(并产生固定大小的输出)。在您的情况下,由于输入大小固定为 400x400,您可能不需要它。
我认为本文可能会让您更好地了解这种方法 - https://arxiv.org/pdf/1406.4729v3.pdf
How to convert VGG to except input size of 400 x 400 ?
第一种方法
VGG
风格架构的问题是我们在线性层中对输入和输出特征的数量进行硬编码。
即
vgg.classifier[0]: Linear(in_features=25088, out_features=4096, bias=True)
需要 25,088 个输入特征。
如果我们传递尺寸为 (3, 224, 224)
的图像
通过 vgg.features
输出特征图的维度为:
(512, 7, 7) => 512 * 7 * 7 => 25,088
如果我们将输入图像大小更改为(3, 400, 400)
并通过
通过 vgg.features
输出特征图的维度为:
(512, 12, 12) => 512 * 12 * 12 => 73,728
throws `sizemismatch` error.
解决此问题的一种方法是使用 nn.AdaptiveAvgPool
代替 nn.AvgPool
。 AdaptiveAvgPool 有助于定义层的输出大小,无论通过 vgg.features
层的输入大小如何,该层都保持不变。
例如:
vgg.features[30] = nn.AdaptiveAvgPool(output_size=(7,7))
will make sure the final feature maps have a dimension of `(512, 7, 7)`
irrespective of the input size.
您可以在 here 中阅读有关自适应池化的更多信息。
第二种方法
如果你使用技术 将你的线性层转换为卷积层,你不必担心输入维度,但是你必须改变权重初始化技术,因为参数数量。
Is the use of the average pooling layer at the end necessary?
不,在这种情况下。它不会改变输入特征图的大小,因此它不会对一组节点进行平均。
我目前正在尝试修改 VGG16 网络架构,以便它能够接受 400x400 像素的图像。
根据我读过的文献,这样做的方法是将完全连接 (FC) 层转换为卷积 (CONV) 层。这实质上将“允许网络有效地‘滑过’更大的输入图像,并对图像的不同部分进行多次评估,并结合所有可用的上下文信息。”之后,使用 Average Pooling 层 "average the multiple feature vectors into a single feature vector that summarizes the input image"。
我已经做到了
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [-1, 64, 400, 400] 1,792
ReLU-2 [-1, 64, 400, 400] 0
Conv2d-3 [-1, 64, 400, 400] 36,928
ReLU-4 [-1, 64, 400, 400] 0
MaxPool2d-5 [-1, 64, 200, 200] 0
Conv2d-6 [-1, 128, 200, 200] 73,856
ReLU-7 [-1, 128, 200, 200] 0
Conv2d-8 [-1, 128, 200, 200] 147,584
ReLU-9 [-1, 128, 200, 200] 0
MaxPool2d-10 [-1, 128, 100, 100] 0
Conv2d-11 [-1, 256, 100, 100] 295,168
ReLU-12 [-1, 256, 100, 100] 0
Conv2d-13 [-1, 256, 100, 100] 590,080
ReLU-14 [-1, 256, 100, 100] 0
Conv2d-15 [-1, 256, 100, 100] 590,080
ReLU-16 [-1, 256, 100, 100] 0
MaxPool2d-17 [-1, 256, 50, 50] 0
Conv2d-18 [-1, 512, 50, 50] 1,180,160
ReLU-19 [-1, 512, 50, 50] 0
Conv2d-20 [-1, 512, 50, 50] 2,359,808
ReLU-21 [-1, 512, 50, 50] 0
Conv2d-22 [-1, 512, 50, 50] 2,359,808
ReLU-23 [-1, 512, 50, 50] 0
MaxPool2d-24 [-1, 512, 25, 25] 0
Conv2d-25 [-1, 512, 25, 25] 2,359,808
ReLU-26 [-1, 512, 25, 25] 0
Conv2d-27 [-1, 512, 25, 25] 2,359,808
ReLU-28 [-1, 512, 25, 25] 0
Conv2d-29 [-1, 512, 25, 25] 2,359,808
ReLU-30 [-1, 512, 25, 25] 0
MaxPool2d-31 [-1, 512, 12, 12] 0
Conv2d-32 [-1, 4096, 1, 1] 301,993,984
ReLU-33 [-1, 4096, 1, 1] 0
Dropout-34 [-1, 4096, 1, 1] 0
Conv2d-35 [-1, 4096, 1, 1] 16,781,312
ReLU-36 [-1, 4096, 1, 1] 0
Dropout-37 [-1, 4096, 1, 1] 0
Conv2d-38 [-1, 3, 1, 1] 12,291
AdaptiveAvgPool2d-39 [-1, 3, 1, 1] 0
Softmax-40 [-1, 3, 1, 1] 0
================================================================
Total params: 333,502,275
Trainable params: 318,787,587
Non-trainable params: 14,714,688
----------------------------------------------------------------
Input size (MB): 1.83
Forward/backward pass size (MB): 696.55
Params size (MB): 1272.21
Estimated Total Size (MB): 1970.59
----------------------------------------------------------------
我的问题很简单:最后是否需要使用平均池化层?似乎在最后一个卷积层,我们得到了一个 1x1 的图像,有 3 个通道。对此进行平均汇集似乎没有任何效果。
如果我的逻辑/架构有任何不妥之处,请随时指出。 谢谢!
AdaptiveAvgPool2d
的目的是让 convnet 处理任意大小的输入(并产生固定大小的输出)。在您的情况下,由于输入大小固定为 400x400,您可能不需要它。
我认为本文可能会让您更好地了解这种方法 - https://arxiv.org/pdf/1406.4729v3.pdf
How to convert VGG to except input size of 400 x 400 ?
第一种方法
VGG
风格架构的问题是我们在线性层中对输入和输出特征的数量进行硬编码。
即
vgg.classifier[0]: Linear(in_features=25088, out_features=4096, bias=True)
需要 25,088 个输入特征。
如果我们传递尺寸为 (3, 224, 224)
的图像
通过 vgg.features
输出特征图的维度为:
(512, 7, 7) => 512 * 7 * 7 => 25,088
如果我们将输入图像大小更改为(3, 400, 400)
并通过
通过 vgg.features
输出特征图的维度为:
(512, 12, 12) => 512 * 12 * 12 => 73,728
throws `sizemismatch` error.
解决此问题的一种方法是使用 nn.AdaptiveAvgPool
代替 nn.AvgPool
。 AdaptiveAvgPool 有助于定义层的输出大小,无论通过 vgg.features
层的输入大小如何,该层都保持不变。
例如:
vgg.features[30] = nn.AdaptiveAvgPool(output_size=(7,7))
will make sure the final feature maps have a dimension of `(512, 7, 7)`
irrespective of the input size.
您可以在 here 中阅读有关自适应池化的更多信息。
第二种方法
如果你使用技术
Is the use of the average pooling layer at the end necessary?
不,在这种情况下。它不会改变输入特征图的大小,因此它不会对一组节点进行平均。