Using nn.function.interpolate() inside nn.Sequential()

username: vinaykumar2491
cooked: <p><code>nn.Upsample()</code> is depecated in <code>pytorch version &gt; 0.4.0</code> in favor of <code>nn.functional.interpolate()</code><br>
I’m not able to use <code>interpolate()</code> inside <code>nn.Sequential()</code>:</p>
<p>Below is my network:</p>
<pre><code class="lang-auto">class MeeDecoder(torch.nn.Module):
    def __init__(self):
        super(MeeDecoder, self).__init__()

        self.de_layer1 = torch.nn.Conv2d(in_channels=40, out_channels=30, kernel_size=(1,1), stride=1, bias=True)
        self.de_layer2 = torch.nn.Conv2d(in_channels=30, out_channels=20, kernel_size=(1,1), stride=1, bias=True)
        self.de_layer3 = torch.nn.functional.interpolate(size=(205, 5), mode='bilinear')
        self.de_layer4 = torch.nn.Conv2d(in_channels=20, out_channels=12, kernel_size=(3,3), stride=1, bias=True)
        self.de_layer5 = torch.nn.Upsample(size=(1025, 15), mode='bilinear')
        self.de_layer6 = torch.nn.Conv2d(in_channels=12, out_channels=1, kernel_size=(1,1), stride=1, bias=True)
        self.de_layerReLU = torch.nn.ReLU()

        self.decoder = torch.nn.Sequential(
            self.de_layer1,
            self.de_layerReLU,
            self.de_layer2,
            self.de_layerReLU,
            self.de_layer3,
            self.de_layer4,
            self.de_layerReLU,
            self.de_layer5,
            self.de_layer6,
            self.de_layerReLU
        )
    
    def forward(self, x):
        return self.decoder(x)
</code></pre>
<p>Any help.<br>
Thanks</p>

username: ptrblck
cooked: <p>You could create a <code>nn.Module</code> with your <code>interpolate</code> function as a workaround:</p>
<pre><code class="lang-python">class Interpolate(nn.Module):
    def __init__(self, size, mode):
        super(Interpolate, self).__init__()
        self.interp = nn.functional.interpolate
        self.size = size
        self.mode = mode
        
    def forward(self, x):
        x = self.interp(x, size=self.size, mode=self.mode, align_corners=False)
        return x
    

class MeeDecoder(torch.nn.Module):
    def __init__(self):
        super(MeeDecoder, self).__init__()

        self.de_layer1 = torch.nn.Conv2d(in_channels=40, out_channels=30, kernel_size=(1,1), stride=1, bias=True)
        self.de_layer2 = torch.nn.Conv2d(in_channels=30, out_channels=20, kernel_size=(1,1), stride=1, bias=True)
        self.de_layer3 = Interpolate(size=(205, 5), mode='bilinear')
        self.de_layer4 = torch.nn.Conv2d(in_channels=20, out_channels=12, kernel_size=(3,3), stride=1, bias=True)
        self.de_layer5 = Interpolate(size=(1025, 15), mode='bilinear')
        self.de_layer6 = torch.nn.Conv2d(in_channels=12, out_channels=1, kernel_size=(1,1), stride=1, bias=True)
        self.de_layerReLU = torch.nn.ReLU()

        self.decoder = torch.nn.Sequential(
            self.de_layer1,
            self.de_layerReLU,
            self.de_layer2,
            self.de_layerReLU,
            self.de_layer3,
            self.de_layer4,
            self.de_layerReLU,
            self.de_layer5,
            self.de_layer6,
            self.de_layerReLU
        )
    
    def forward(self, x):
        return self.decoder(x)
</code></pre>

username: vinaykumar2491
cooked: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>. It works. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=6" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: rchavezj
cooked: <p>Why provide align_corners as an additional argument for Interpolate class may I ask? It wasn’t a passing argument for the deprecated upsampling.</p>

username: Vijay
cooked: <p>You doesn’t specify input</p>

username: degawong
cooked: <p>the two functions were designed for different cases.obviously, the interpolate class has more wide range of application scenarios.</p>

username: AM.MO
cooked: <p>could you pls tell me how can I calculate the size of the “interpolate”??</p>

username: ptrblck
cooked: <p>You are not calculating but defining the <code>size</code> argument for <code>interpolate</code> as it depends on your use case.<br>
E.g. you could use this operator to down/upscale a tensor using a defined shape.</p>