Can someone help me to explain this model?

I’m still on my way to learn pytorch, I find a model used to achieve fcn(semantic segmentation), there’re some statement really confused me. Can someone help to explain?

class fcn(nn.Module):
    def __init__(self, num_classes):
        super(fcn, self).__init__()
        self.stage1 = nn.Sequential(*list(pretrained_net.children())[:-4])
        self.stage2 = list(pretrained_net.children())[-4]
        self.stage3 = list(pretrained_net.children())[-3]

        self.scores1 = nn.Conv2d(512, num_classes, 1)
        self.scores2 = nn.Conv2d(256, num_classes, 1)
        self.scores3 = nn.Conv2d(128, num_classes, 1)

        self.upsample_8x = nn.ConvTranspose2d(num_classes, num_classes, 16, 8, 4, bias=False)
        self.upsample_8x.weight.data = bilinear_kernel(num_classes, num_classes, 16)

        self.upsample_4x = nn.ConvTranspose2d(num_classes, num_classes, 4, 2, 1, bias=False)
        self.upsample_4x.weight.data = bilinear_kernel(num_classes, num_classes, 4)

        self.upsample_2x = nn.ConvTranspose2d(num_classes, num_classes, 4, 2, 1, bias=False)
        self.upsample_2x.weight.data = bilinear_kernel(num_classes, num_classes, 4)

    def forward(self, x):
        x = self.stage1(x)
        s1 = x  # 1/8

        x = self.stage2(x)
        s2 = x  # 1/16

        x = self.stage3(x)
        s3 = x  # 1/32

        s3 = self.scores1(s3)
        s3 = self.upsample_2x(s3)
        s2 = self.scores2(s2)
        s2 = s2 + s3

        s1 = self.scores3(s1)
        s2 = self.upsample_4x(s2)
        s = s1 + s2
        s = self.upsample_8x(s2)
        return s

Which lines didn’t you understand?

to define the stage, I know the stage1 is to remove the last 4 layers maybe? but what is the stage2 and stage3? seems the format is different.

stage1 = all but the last 4 layers of pretrained_net
stage2 = the fourth layer from the end of pretrained net
stage3 = the third layer from the end of pretrained net

if the slicing notation causes you difficulties, you can try simple experiments, e.g.

fourth_from_end = list(range(10))[-4]
print(fourth_from_end)

is there any difference between
nn.Sequential(*list(pretrained_net.children())[:4]) and
list(pretrained_net.children())[-4]?

I mean nn.Sequential(*list(pretrained_net.children())[-4])and
list(pretrained_net.children())[-4]

Yes. The first is a module that runs the listed submodules in order.
The second is just a plain python list of submodules.

If you do

stage1 = nn.Sequential(*list(pretrained_net.children())[:-4])

then you can do

output = stage1(input)

but if you didn’t use nn.Sequential, then you would have to use a for loop

temp = input
for module in stage1:
    temp = module(temp)
output = temp