Error in torchvision.transforms.functional.Pad

887574002: <p>Hi, I have the following snippet in Unet structure:</p>
<pre><code class="lang-auto">class DoubleConv(nn.Module):
    def __init__(self,in_channels, out_channels, mid_channels=None):
        super(DoubleConv,self).__init__()
        if not mid_channels:
            mid_channels = out_channels
        self.d_conv = nn.Sequential(
        nn.Conv2d(in_channels,mid_channels,kernel_size=3, padding=1),
        nn.BatchNorm2d(mid_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(mid_channels,out_channels,kernel_size=3,padding=1),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True))
        
    def forward(self,x):  
        ## The spatial dim is not changed: (out_dim, h,w)
        return(self.d_conv(x))
   class Up(nn.Module):
    
    """Upscaling then double conv"""
    
    def __init__(self,in_channels, out_channels, bilinear=True):
        super(Up,self).__init__()
        # if bilinear, use the normal convelutions to reduce the number of channels if bilinear:
            
            self.up = nn.Upsample(scale_factor=2,mode='bilinear', align_corners=True)
            # here we devide the number of filters
            self.conv = DoubleConv(in_channels,out_channels,in_channels//2)
            
        else:
            
            self.up = nn.ConvTranspose2d(in_channels,in_channels//2,kernel_size=2,stride=2)
            self.conv = DoubleConv(in_channels,out_channels)
            
    def forward(self,x1,x2):
        
        x1 = self.up(x1)
        
        print(x1.size())
        
        # input is CHW
        diffy = x2.size()[2] - x1.size()[2]
        diffx = x2.size()[3]- x1.size()[3]
        
        x1 = to_pil_image(x1)
        
        x1 = F.pad(x1, [diffx // 2, diffx - diffx // 2,diffy // 2, diffy - diffy // 2])
        
        x1= to_tensor(x1)
        
        x = torch.cat([x2, x1], dim=1)
        return(self.conv(x)) 

</code></pre>
<p>But when I run it to two random tensors, I get two errors:<br>
First: img should be PIL Image. Got &lt;class ‘torch.Tensor’&gt;<br>
while in documentation of <a href="http://pytorch.org" rel="noopener nofollow ugc">pytorch.org</a> it was written that <code>torchvision.transforms.functional.pad</code> can be applied on both PIL and Tensor images.<br>
When I change the tensors in PIL then I get an error  in dimension. While in <a href="http://pytorch.org" rel="noopener nofollow ugc">pytorch.org</a> I read that it dose not matter what dimension you have , just it is important we have […, h,w]. In the following when I put tensors if dimension [batch_num, c,h,w] it gives me the error of size 4 that should be 2 or 3 and when I omit the batch_num from the tensors it returns an error of dimension again.</p>
<pre><code class="lang-auto">x2 = torch.randn(1,3,130,130)
x1 = torch.randn(1,3,126,126)
upp = Up(3,32,True)
result = upp(x1,x2)
</code></pre>
<p>Error:<br>
ValueError: pic should be 2/3 dimensional. Got 4 dimensions.</p>

Nikronic: <p>Hi,</p>
<p>About first error, make sure you have installed latest pytorch and torchvision packages as it has been added to latest build pytorch 1.7 and torchvision 0.8.</p>
<p>About second error, <code>pad</code> works on tensors too, and you don’t need to convert it to pil and convert back to tensor. The error is from to PIL where you should only pass 2d or 3d but you have also included batches.</p>
<aside class="quote no-group" data-username="887574002" data-post="1" data-topic="101925">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<pre><code class="lang-auto">        x1 = to_pil_image(x1)
        
        x1 = F.pad(x1, [diffx // 2, diffx - diffx // 2,diffy // 2, diffy - diffy // 2])
        
        x1= to_tensor(x1)
</code></pre>
</blockquote>
</aside>
<p>Bests</p>