How can I duplicate the Resnet50 to five branches?

Below you see the network structure of the ResNet50. What I want to do is duplicate the last convolution layers to five branches for some spesific task, where each branch will consist of two FC layers. How can I do that in the Pytorch, where Resnet50 is already loaded as

ResNet50 = torchvision.models.resnet50(pretrained=True)

enter image description here

1 answer

  • answered 2021-07-27 18:06 SarthakJain

    One way to accomplish this is to index the children of the resnet model and then attatch a sequential after that pair of conv blocks. One great implementation can be found here:

    You can use this same principal to replace the vgg with your resnet.Play close attention to how they slice the model and then add a linear sequential.

    class BCNN(nn.Module):
        def __init__(self):
            super(BCNN,self).__init__()
            # Load pretrained model
            vgg_model = models.vgg16_bn(pretrained=True)
            self.Conv1 = nn.Sequential(*list(vgg_model.features.children())[0:7])
            self.Conv2 = nn.Sequential(*list(vgg_model.features.children())[7:14])
            # Level-1 classifier after second conv block
            self.level_one_clf = nn.Sequential(nn.Linear(128*56*56, 256), 
                                               nn.ReLU(), 
                                               nn.BatchNorm1d(256), 
                                               nn.Dropout(0.5), 
                                               nn.Linear(256, 256), 
                                               nn.BatchNorm1d(256), 
                                               nn.Dropout(0.5), 
                                               nn.Linear(256, 2))
            self.Conv3 = nn.Sequential(*list(vgg_model.features.children())[14:24])
            # Level-2 classifier after third conv block
            self.level_two_clf = nn.Sequential(nn.Linear(256*28*28, 1024), 
                                               nn.ReLU(), 
                                               nn.BatchNorm1d(1024), 
                                               nn.Dropout(0.5), 
                                               nn.Linear(1024, 1024), 
                                               nn.BatchNorm1d(1024), 
                                               nn.Dropout(0.5), 
                                               nn.Linear(1024, 7))
            self.Conv4 = nn.Sequential(*list(vgg_model.features.children())[24:34])
            self.Conv5 = nn.Sequential(*list(vgg_model.features.children())[34:44])
            # Level-3 classifier after fifth conv block
            self.level_three_clf = nn.Sequential(nn.Linear(512*7*7, 4096), 
                                                 nn.ReLU(), 
                                                 nn.BatchNorm1d(4096), 
                                                 nn.Dropout(0.5), 
                                                 nn.Linear(4096, 4096), 
                                                 nn.BatchNorm1d(4096), 
                                                 nn.Dropout(0.5), 
                                                 nn.Linear(4096, 25))     
        def forward(self,x):
            x = self.Conv1(x)
            x = self.Conv2(x)
            lvl_one = x.view(x.size(0), -1)
            lvl_one = self.level_one_clf(lvl_one)
            x = self.Conv3(x)
            lvl_two = x.view(x.size(0), -1)
            lvl_two = self.level_two_clf(lvl_two)
            x = self.Conv4(x)
            x = self.Conv5(x)
            lvl_three = x.view(x.size(0), -1)
            lvl_three = self.level_three_clf(lvl_three)
            return lvl_one, lvl_two, lvl_three
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum