Bilinear upsampling vs Bilinear interpolation

username: sigma_x
cooked: <p>The input is</p>
<blockquote>
<p>a=torch.Tensor([[1,3],[2,4]])<br>
a.unsqueeze_(0).unsqueeze_(0)</p>
</blockquote>
<p>I try two methods:</p>
<blockquote>
<p>F.upsample_bilinear(a, scale_factor=2)</p>
</blockquote>
<p>to get</p>
<blockquote>
<p>tensor([[[[1.0000, 1.6667, 2.3333, 3.0000],<br>
[1.3333, 2.0000, 2.6667, 3.3333],<br>
[1.6667, 2.3333, 3.0000, 3.6667],<br>
[2.0000, 2.6667, 3.3333, 4.0000]]]])</p>
</blockquote>
<p>The second one</p>
<blockquote>
<p>F.interpolate(a, scale_factor=2, mode=‘bilinear’)</p>
</blockquote>
<p>returns</p>
<blockquote>
<p>tensor([[[[1.0000, 1.5000, 2.5000, 3.0000],<br>
[1.2500, 1.7500, 2.7500, 3.2500],<br>
[1.7500, 2.2500, 3.2500, 3.7500],<br>
[2.0000, 2.5000, 3.5000, 4.0000]]]])</p>
</blockquote>
<p>Why do they return two different outputs? Both do bilinear interpolation.</p>

username: Nikronic
cooked: <p>Hi,</p>
<p>First of all, <code>upsample_*</code> methods are deprecated in favor of <code>interpolate</code>.</p>
<p>But the difference is because of the fact that <code>upsample_*</code> uses <code>interpolate</code> function with arg <code>align_corners=True</code> while default value for <code>interpolate</code> method is <code>align_corners=False</code>. Actually, if you use <code>upsample_*</code> method, it gives you a deprecation warning that states the mentioned behavior.</p>
<p>Following code gives you the same output as old (deprecated) code:</p>
<pre><code class="lang-auto">nn.functional.interpolate(a, scale_factor=2, mode='bilinear', align_corners=True)
</code></pre>
<p>Official documentations also explain this behavior clearly:<br>
<a href="https://pytorch.org/docs/stable/generated/torch.nn.UpsamplingBilinear2d.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.nn.UpsamplingBilinear2d.html</a><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/d/1de2be0b16933d4c5e600dd940be866f6f53b69a.png" data-download-href="https://discuss.pytorch.org/uploads/default/1de2be0b16933d4c5e600dd940be866f6f53b69a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/d/1de2be0b16933d4c5e600dd940be866f6f53b69a.png" alt="image" data-base62-sha1="4gnDFMvJ3cAcUenpXaQQgWDJAKu" width="690" height="113" data-dominant-color="3B2623"><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">image</span><span class="informations">698×115 5.36 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><br>
<a href="https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.interpolate" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/nn.functional.html#torch.nn.functional.interpolate</a><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd706542c616e9b2d164d9069bc6f552f454ceff.png" data-download-href="https://discuss.pytorch.org/uploads/default/cd706542c616e9b2d164d9069bc6f552f454ceff" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd706542c616e9b2d164d9069bc6f552f454ceff.png" alt="image" data-base62-sha1="tjoGDTqZCC3vV1BD6JVZRtJbvgb" width="690" height="159" data-dominant-color="3A2C2B"><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">image</span><span class="informations">701×162 15.2 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>