deep-learning - 如何在 pytorch 代码中的 ResNet 中进行下采样?
问题描述
在这个 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)
推荐阅读
- mysql - mysql中date_trunc的替代品是什么?
- database - TDengine taosdump 工具正确使用指定参数
- vba - 使用 Word VBA 隐藏或取消隐藏当前列表段落后,如何调整下一个单词列表段落的编号?
- python - 如何在 firebase 托管上托管我的 python 功能烧瓶应用程序?
- asp.net - 我想在 ASP.NET Core 5.0 MVC 中通过 jQuery 在 foreach 循环中获取第一次迭代的值
- c# - 在 C# 中抽象出条件收益
- c++ - 推荐的 C++ 包含实践
- c++ - 我不明白如何计算以下 c++ 代码的时间复杂度?
- c - 如何在 VLA 中输入 n 个元素并打印它们?
- python - 使用另一个列表中的索引查找列表中值的位置