I am referring to this implementation here:
https://github.com/hszhao/semseg/blob/master/model/pspnet.py
In line 49-58, the author writes:
for n, m in self.layer3.named_modules():
if 'conv2' in n:
m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
elif 'downsample.0' in n:
m.stride = (1, 1)
for n, m in self.layer4.named_modules():
if 'conv2' in n:
m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1)
elif 'downsample.0' in n:
m.stride = (1, 1)
What exactly is happening in these loops?
My understanding is, that the author is creatig a resnet model (his resnet.py here https://github.com/hszhao/semseg/blob/master/model/resnet.py ) and then is calling the different layers, which he implemented in his resnet class to forward them below.
layer3 and layer4 in resnet.py are made by calling the function def _make_layer(self, block, planes, blocks, stride=1):
, so I assume that when .named_modules()
is used in the loops, it is looping through the modules in this def _make_layer
function, is it? If so, what happens in the elif part? There is no module, that is called downsample.0
? (The only modules are nn.Conv2d and nn.BatchNorm2d )
Below is an example of resnet that used there.
model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
In Resnet class, it calls super because of this, it has self.downsample If it's not none:
if self.downsample is not None:
residual = self.downsample(x)
it could have Sequential or another layer.
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.