关于PyTorch含有的自适应池化Adaptive Pooling池化层
学习目标:自适应池化层
疑惑:在设计神经网络模型的时候,往往需要将特征图与分类对应上,即需要卷积层到全连接层的过渡。但在这个过渡期,不知道首个全连接层的初始化输入设置为多少?

- 学会使用pytorch的自适应池化层nn.AdaptiveMaxPool指定输出的维度,学会读神经网络模型层与层之间的变化。
学习内容:代码示例
提示:网络模型分为三部分,特征提取层,过渡层conv,全连接层
- 模型源码:
class KpClassify(nn.Module):
def __init__(self):
super().__init__()
self.feature = KeyPointsModel() # 特征提取网络
self.conv = nn.Sequential(
nn.Conv2d(in_channels=24, out_channels=48, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(num_features=48), # 输入图像的通道数量
nn.ReLU(inplace=True), # 此处inplace,选择是否覆盖。表示Relu得到的结果是否覆盖Relu之前的结果
# 使用inplace=True进行覆盖,可以节约内存,不需要单独创建变量保存数据
nn.Conv2d(in_channels=48, out_channels=48, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(num_features=48),
nn.ReLU(inplace=True),
# 如果想直接确定全连接层的维度,可以使用自适应池化,无论前面的卷积池化的维度变成什么,最后的输出维度都是batchsize*channels*n*n
# 将每个通道的输出特征固定为n*n.n=9
nn.AdaptiveMaxPool2d((9, 9))
)
self.fc = nn.Sequential(
nn.Linear(in_features=48 * 9 * 9, out_features=512), # 首个Linear的输入为:通道数*池化输出,即48*9*9
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(in_features=512, out_features=512),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(in_features=512, out_features=9)
)
def forward(self, x):
x = self.feature(x

最低0.47元/天 解锁文章
3294

被折叠的 条评论
为什么被折叠?



