How can I replace the forward method of a predefined torchvision model with my customized forward function?

username: Philipp_Friebertshau
cooked: <p>How can I replace the forward method of a predefined torchvision model with my customized forward function?</p>
<p>I tried the following:<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6.png" data-download-href="https://discuss.pytorch.org/uploads/default/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6" title="method_replace"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6_2_639x500.png" alt="method_replace" data-base62-sha1="hNTBqSH97WhHCdXNDaZKeFW0omi" width="639" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6_2_639x500.png, https://discuss.pytorch.org/uploads/default/optimized/2X/7/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6_2_958x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/7/7cc8e2f46ecec9bed9a374b2db84c6452c5809c6_2_1278x1000.png 2x" data-dominant-color="D7CECE"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">method_replace</span><span class="informations">1382×1080 103 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ajhanwar
cooked: <p>Each layer within the resnet model has its own forward function, hence you would need to apply a change to the forward method explicitly to each layer</p>

username: Philipp_Friebertshau
cooked: <p>What kind of change do I have to make to the sublayer forwardfunctions if they are not to be changed at all? Are the forward functions of the sublayers overwritten by this action? what would the solution look like in the code?</p>

username: Shisho_Sama
cooked: <p>What do you want to do? do you want to add some new layers to a pretrained model(resnet34) and then in your forward() do as you wish?<br>
it would help much more if you can say what exactly you are after</p>

username: Philipp_Friebertshau
cooked: <p>In this case I just want to delete the self.avgpool, the flatten operation and the fc layer, so that the output of the model is just the [bs x 512 x 7 x 7] Featuremaps (when 3 <em>224</em> 224 image tensors are feeded), as you can see in the image in the appendix. But beyond this case i am interested in general how i can modify predefined torchvision models.</p>

username: ptrblck
cooked: <p>You could derive a custom class using the resnet class as its parent:</p>
<pre><code class="lang-python">import torchvision.models as models
from torchvision.models.resnet import ResNet, BasicBlock

class MyResNet18(ResNet):
    def __init__(self):
        super(MyResNet18, self).__init__(BasicBlock, [2, 2, 2, 2])
        
    def forward(self, x):
        # change forward here
        x = self.conv1(x)
        return x


model = MyResNet18()
# if you need pretrained weights
model.load_state_dict(models.resnet18(pretrained=True).state_dict())
</code></pre>

username: Shisho_Sama
cooked: <p>aside from the solution kindly provided by <a class="mention" href="/u/ptrblck">@ptrblck</a> , you can also do sth like this :</p>
<pre><code class="lang-auto">class MyCustomResnet18(nn.Module):
    def __init__(self, pretrained=True):
        super().__init__()
        
        resnet18 = models.resnet18(pretrained=pretrained)
        # here we get all the modules(layers) before the fc layer at the end
        # note that currently at pytorch 1.0 the named_children() is not supported
        # and using that instead of children() will fail with an error
        self.features = nn.ModuleList(resnet18.children())[:-1]
        # Now we have our layers up to the fc layer, but we are not finished yet 
        # we need to feed these to nn.Sequential() as well, this is needed because,
        # nn.ModuleList doesnt implement forward() 
        # so you cant do sth like self.features(images). Therefore we use 
        # nn.Sequential and since sequential doesnt accept lists, we 
        # unpack all the items and send them like this
        self.features = nn.Sequential(*self.features)
        # now lets add our new layers 
        in_features = resnet18.fc.in_features
        # from now, you can add any kind of layers in any quantity!  
        # Here I'm creating two new layers 
        self.fc0 = nn.Linear(in_features, 256)
        self.fc0_bn = nn.BatchNorm1d(256, eps = 1e-2)
        self.fc1 = nn.Linear(256, 256)
        self.fc1_bn = nn.BatchNorm1d(256, eps = 1e-2)
        
        # initialize all fc layers to xavier
        for m in self.modules():
            if isinstance(m, nn.Linear):
                torch.nn.init.xavier_normal_(m.weight, gain = 1)

    def forward(self, input_imgs):
       # now in forward pass, you have the full control, 
       # we can use the feature part from our pretrained model  like this
        output = self.features(input_imgs)
        # since we are using fc layers from now on, we need to flatten the output.
        # we used the avgpooling but we still need to flatten from the shape (batch, 1,1, features)
        # to (batch, features) so we reshape like this. input_imgs.size(0) gives the batchsize, and 
        # we use -1 for inferring the rest
        output = output.view(input_imgs.size(0), -1)
       # and also our new layers. 
        output = self.fc0_bn(F.relu(self.fc0(output)))
        output = self.fc1_bn(F.relu(self.fc1(output)))
                
        return output
</code></pre>
<p>You can get fancy and add new methods for your network (e.g. for freezing, unfreezing different parts of your network, that can come handy in finetuning)</p>

username: Sanju
cooked: <p>So is it impossible to view what the actual forward function description of a pretrained torch vision model… I facing a problem with that …<br>
In a pretrained model, when I view the description, I will be able to see all the data members of the model defined i.e individual layers and its parameters here. But for a model, all residual connections are and its operation are defined in the forward function which the pretrained model will not show. Is there any other way by which I an get the information about where the residual connections are present in the pre-trained model ??</p>

username: ptrblck
cooked: <p>You would have to check the source code, e.g. <a href="https://github.com/pytorch/vision/tree/master/torchvision/models">here</a> for <code>torchvision.models</code>.</p>

username: ccalafiore
cooked: <p>An alternative to <a class="mention" href="/u/ptrblck">@ptrblck</a> 's solution is this:</p>
<pre><code class="lang-auto">import torchvision.models as models
import torch


def new_forward(self, x):
    x = self.conv1(x)
    x = self.bn1(x)
    x = self.relu(x)
    x = self.maxpool(x)

    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    return x


# define a resnet instance
resent = models.resnet18()

# add new_forward function to the resnet instance as a class method
bound_method = new_forward.__get__(resent, resent.__class__)
setattr(resent, 'forward', bound_method)

# you can also remove the 2 layers resent.avgpool and resent.fc because you are not using them in the new forward method
delattr(resent, 'avgpool')
delattr(resent, 'fc')

# call the new forward method
inputs = torch.rand(1, 3, 224, 224)
outputs = resent(inputs)

print('type(resent) = ', type(resent))
print('type(resent.forward) = ', type(resent.forward))
print('outputs.shape = ', outputs.shape)
</code></pre>

username: GHALI_Ahmed
cooked: <p>you make a very good job, you just save me !! <img src="https://discuss.pytorch.org/images/emoji/apple/innocent.png?v=12" title=":innocent:" class="emoji" alt=":innocent:" loading="lazy" width="20" height="20"></p>

username: lalalabox
cooked: <p>amazing, it works perfect! Thank you! <img src="https://discuss.pytorch.org/images/emoji/apple/kissing_heart.png?v=12" title=":kissing_heart:" class="emoji" alt=":kissing_heart:" loading="lazy" width="20" height="20"></p>