deep-learning - 在 pytorch 代码中如何在 ResNet 中进行下采样?

标签 deep-learning conv-neural-network pytorch torchvision

在这个 pytorch ResNet 代码示例中,他们在第 44 行将下采样定义为变量。第 58 行将其用作函数。作为 CNN 的观点和 Python 代码的观点,这个下采样在这里是如何工作的。

代码示例:pytorch ResNet

我搜索了下采样是否是任何 pytorch 内置函数。但事实并非如此。

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, norm_layer=None):
        super(BasicBlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        if groups != 1:
            raise ValueError('BasicBlock only supports groups=1')
        # Both self.conv1 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = norm_layer(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = norm_layer(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

return out

最佳答案

如果您查看原始 ResNet 论文 (http://openaccess.thecvf.com/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf),他们使用跨步卷积对图像进行下采样。使用这些跨步卷积自动对主路径进行下采样,就像在您的代码中所做的那样。残差路径使用 (a) 添加零条目的身份映射以不添加额外参数,或 (b) 具有相同步幅参数的 1x1 卷积。

第二个选项可能如下所示:

if downsample:
    self.downsample = conv1x1(inplanes, planes, strides)

关于deep-learning - 在 pytorch 代码中如何在 ResNet 中进行下采样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688645/

相关文章:

conv-neural-network - CNN 目标检测和带有水印图像的训练,它会起作用吗?

machine-learning - Pytorch 中的自定义 LSTM 模型显示输入大小不匹配

numpy - 将矩阵的每一行与另一个矩阵的每一行相乘

machine-learning - 如何使用加载的 LSTM 注意力模型对输入进行预测?

machine-learning - 深度学习是否不擅长在训练范围之外拟合简单的非线性函数(外推)?

tensorflow - 我应该对 3D 灰度图像使用 2D 还是 3D 卷积?

python - 如何根据索引向量求和

python - 当您的模型不能过度拟合一小部分数据时,这意味着什么?

python - 如何将Python数据生成器转换为Tensorflow张量?

image-processing - 我有一个包含图像描述的图像数据和元数据,我如何使用图像图像和元数据来训练图像