A question about UNet Crop and nn.functional.interpolate

I’m trying to code the UNet paper, however I’ve come across the issue of implementing the crop when trying to code the decoder.

One person did this:

class Decoder(nn.Module):
    def __init__(self, chs=(1024, 512, 256, 128, 64)):
        super().__init__()
        self.chs         = chs
        self.upconvs    = nn.ModuleList([nn.ConvTranspose2d(chs[i], chs[i+1], 2, 2) for i in range(len(chs)-1)])
        self.dec_blocks = nn.ModuleList([Block(chs[i], chs[i+1]) for i in range(len(chs)-1)]) 
        
    def forward(self, x, encoder_features):
        for i in range(len(self.chs)-1):
            x        = self.upconvs[i](x)
            enc_ftrs = self.crop(encoder_features[i], x)
            x        = torch.cat([x, enc_ftrs], dim=1)
            x        = self.dec_blocks[i](x)
        return x
    
    def crop(self, enc_ftrs, x):
        _, _, H, W = x.shape
        enc_ftrs   = torchvision.transforms.CenterCrop([H, W])(enc_ftrs)
        return enc_ftrs

If I used nn.functional.interpolate() instead, would I get the same effect as the crop function in the above code statement?

I don’t know what you mean by “effect”, but an interpolation is not producing the same output as a cropping. Both manipulate the spatial size and you can use both to create a smaller output but the values won’t necessarily be equal.

1 Like