wondrive / errors

solutions for errors i've met
0 stars 0 forks source link

RuntimeError: Given input size: (512x1x1). Calculated output size: (512x0x0). Output size is too small 에러 해결 #8

Open wondrive opened 1 year ago

wondrive commented 1 year ago

Error message

RuntimeError: Given input size: (512x1x1). Calculated output size: (512x0x0). Output size is too small


Cause of Error

This error occurs when the input size is smaller than the kernel size. In my case, I tried to run VGG16 on MNIST dataset, there were 5 pooling layers with 2x2 kernel.
The original image size of MNIST is 28x28, and it becomes 0x0 after 5th pooling layer.
So the 0x0 shaped feature map couldn't be put on remain layers.


Solution

You just need to make the image size bigger than the kernel size of pooling layer where error occurs.

I choosed Choice 1.


For option 1, I added transforms.Resize() to increase the input_size of a image. (Pytorch)

# Data load
transform = transforms.Compose([transforms.Resize(32), transforms.ToTensor(), transforms.Normalize(mean=(0.5,), std=(0.5,))])
train_dataset = datasets.MNIST(download=True, root='./', train=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=batch_size,shuffle=True, num_workers=0)

test_dataset = datasets.MNIST(download=True, root='./', train=False, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=batch_size,shuffle=False, num_workers=0)


For option 2 & 3, you can consider the fomula that calulates the size(H, W) of a output feature.