RuntimeError: Calculated padded input size per channel: (1 x 1). Kernel size: (4 x 4). Kernel size can't be greater than actual input size

Question:

I am trying to train GAN to transfer style. I am getting error when passing images through discriminator

for epoch in range(epochs):
        #code for stats
        for real_images in tqdm(t_dl):

            optimizer["discriminator"].zero_grad()

            real_preds = model["discriminator"](real_images)#-----------------------error here
#code

And here is model

model = {
    "discriminator": discriminator.to(device),
    "generator": generator.to(device)
}

And code for discriminator

discriminator = nn.Sequential(
    # in: 3 x 256 x 256
    PrintLayer(),
    nn.Conv2d(3, 64, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(64),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 64 x 128 x 128
    PrintLayer(),
    nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(128),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 128 x 64 x 64
    PrintLayer(),
    nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(256),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 256 x 32 x 32
    PrintLayer(),
    nn.Conv2d(256, 512, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(512),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 512 x 16 x 16
    PrintLayer(),
    nn.Conv2d(512, 1024, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(1024),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 512 x 8 x 8
    PrintLayer(),
    nn.Conv2d(1024, 1024, kernel_size=4, stride=2, padding=1, bias=False),
    nn.BatchNorm2d(1024),
    nn.LeakyReLU(0.2, inplace=True),
    # out: 1024 x 4 x 4
    PrintLayer(),
    nn.Conv2d(1024, 1, kernel_size=4, stride=1, padding=0, bias=False),
    # out: 1 x 1 x 1
    PrintLayer(),
    nn.Flatten(),
    nn.Sigmoid())

So I added PrintLayer() to check dimensions after convolutions

class PrintLayer(nn.Module):
    def __init__(self):
        super(PrintLayer, self).__init__()
    
    def forward(self, x):
        print(x.shape)
        return x

All images in batch are 256*256, I printed images sizes right before passing them to discriminator

0 torch.Size([3, 256, 256])
1 torch.Size([3, 256, 256])
2 torch.Size([3, 256, 256])
3 torch.Size([3, 256, 256])
4 torch.Size([3, 256, 256])
5 torch.Size([3, 256, 256])
6 torch.Size([3, 256, 256])
7 torch.Size([3, 256, 256])
8 torch.Size([3, 256, 256])
9 torch.Size([3, 256, 256])

It works with first image but somehow second image is 112*112

torch.Size([10, 3, 256, 256])
torch.Size([10, 64, 128, 128])
torch.Size([10, 128, 64, 64])
torch.Size([10, 256, 32, 32])
torch.Size([10, 512, 16, 16])
torch.Size([10, 1024, 8, 8])
torch.Size([10, 1024, 4, 4])
torch.Size([10, 1, 1, 1])
torch.Size([10, 3, 112, 112])
torch.Size([10, 64, 56, 56])
torch.Size([10, 128, 28, 28])
torch.Size([10, 256, 14, 14])
torch.Size([10, 512, 7, 7])
torch.Size([10, 1024, 3, 3])
torch.Size([10, 1024, 1, 1])
Asked By: PA Nik

||

Answers:

My problem was solved here https://discuss.pytorch.org/t/runtimeerror-calculated-padded-input-size-per-channel-1-x-1-kernel-size-4-x-4-kernel-size-cant-be-greater-than-actual-input-size/160184/11

Briefly:Generator generated images of wrong shape,so discriminator was getting 112*112.

Answered By: PA Nik