When I try to forward a tensor of size Bx3x64x64 through resnet.ResNet(...),
at x = self.avgpool(x)
it complains
RuntimeError: Given input size: (512x2x2). Calculated output size: (512x-4x-4). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
which does not make sense.
I found that the errors were gone if the AdaptivePool2d is intialized as
self.avgpool = nn.AdaptiveAvgPool2d(1) instead of self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
It might be a bug in nn.AdaptiveAvgPool2d
This is very weird, because the error message that you have is from AvgPool2d. Maybe you have an older version of torchvision that doesn't have the nn.AdaptiveAvgPool2d improvement?
I think you should update the torchvision version and see if the issue is solved.
Thnaks. Will try on a clean conda environment shortly and confirm it when
I'm back from a short trip.
On Sun., 23 Dec. 2018, 12:19 am Dhirendra Kumar <[email protected]
wrote:
I think you should update the torchvision version and see if the issue is
solved.—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/pytorch/vision/issues/696#issuecomment-449615225, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ABfshdfg04ZSaKjflUwsVrMRM8Bniy7zks5u7xJRgaJpZM4ZbLpz
.
Just confirmed the bug on a clean conda environment with Python 3.6.8, Ubuntu18.04, cuda9.1
outputs of pip freeze | grep torch
torch==1.0.0
torchvision==0.2.1
To reproduce,
from torchvision.models.resnet import resnet18, resnet50, resnet101
import torch
for sz in (64, 96, 128, 256):
for func in (resnet18, resnet50, resnet101):
try:
print('== Testing 3x{}x{} input for {}'.format(sz, sz, func))
model = func()
model.cuda()
x = torch.FloatTensor(1, 3, sz, sz).cuda()
model(x)
print('!! Passed !!')
except RuntimeError as e:
print(e)
Output log on my side:
== Testing 3x64x64 input for <function resnet18 at 0x7fcac6297730>
Given input size: (512x2x2). Calculated output size: (512x-4x-4). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x64x64 input for <function resnet50 at 0x7fcac6297bf8>
Given input size: (2048x2x2). Calculated output size: (2048x-4x-4). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x64x64 input for <function resnet101 at 0x7fcac6297c80>
Given input size: (2048x2x2). Calculated output size: (2048x-4x-4). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x96x96 input for <function resnet18 at 0x7fcac6297730>
Given input size: (512x3x3). Calculated output size: (512x-3x-3). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x96x96 input for <function resnet50 at 0x7fcac6297bf8>
Given input size: (2048x3x3). Calculated output size: (2048x-3x-3). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x96x96 input for <function resnet101 at 0x7fcac6297c80>
Given input size: (2048x3x3). Calculated output size: (2048x-3x-3). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x128x128 input for <function resnet18 at 0x7fcac6297730>
Given input size: (512x4x4). Calculated output size: (512x-2x-2). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x128x128 input for <function resnet50 at 0x7fcac6297bf8>
Given input size: (2048x4x4). Calculated output size: (2048x-2x-2). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x128x128 input for <function resnet101 at 0x7fcac6297c80>
Given input size: (2048x4x4). Calculated output size: (2048x-2x-2). Output size is too small at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THCUNN/generic/SpatialAveragePooling.cu:47
== Testing 3x256x256 input for <function resnet18 at 0x7fcac6297730>
size mismatch, m1: [1 x 2048], m2: [512 x 1000] at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THC/generic/THCTensorMathBlas.cu:266
== Testing 3x256x256 input for <function resnet50 at 0x7fcac6297bf8>
size mismatch, m1: [1 x 8192], m2: [2048 x 1000] at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THC/generic/THCTensorMathBlas.cu:266
== Testing 3x256x256 input for <function resnet101 at 0x7fcac6297c80>
size mismatch, m1: [1 x 8192], m2: [2048 x 1000] at /opt/conda/conda-bld/pytorch_1544174967633/work/aten/src/THC/generic/THCTensorMathBlas.cu:266
It is should be caused by a bug in SpatialAveragePooling.cu. But the simple fix in https://github.com/pytorch/vision/pull/697 should work it around for torchvision.
i think torchvision==0.2.1 does not have the AdaptiveAvgPool2d layer yet...
@timonbimon is right, the latest torchvision version available doesn't have AdaptiveAvgPool2d.
We will be cutting a new release soon, which will have this fix.
Still cannot solve this problem in the latest or 0.2.1 version torchvision.
@wangjinjie722 if you want to use a very small image size, you need to modify the network architecture, as we don't provide models for cifar-sized images, only for imagenet-sized
This is annoying and I experience the same problem when using 64x64 image size. Replacing with AdaptiveAvgPool2d fixed it.
@Quetzalcohuatl we already use AdaptiveAvgPool2d in resnet, so this shouldn't be a problem with torchvision 0.4 or 0.5
@fmassa I encountered this problem when dealing with se-resnext in the package pretrainedmodels. So I made the post on the wrong Python package. My bad, I was not distinguishing between pretrainedmodels and torchvision
Just reporting, 64x64 images do not work for AvgPool2d as well.
self.Avergepool = nn.Sequential(
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
nn.AvgPool2d(kernel_size=6, stride=1)
)
```x = self.Avergepool(x)
File "/people/kimd999/bin/Miniconda3-latest-Linux-x86_64/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(input, *kwargs)
File "/people/kimd999/bin/Miniconda3-latest-Linux-x86_64/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/container.py", line 100, in forward
input = module(input)
File "/people/kimd999/bin/Miniconda3-latest-Linux-x86_64/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(input, *kwargs)
File "/people/kimd999/bin/Miniconda3-latest-Linux-x86_64/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/pooling.py", line 553, in forward
return F.avg_pool2d(input, self.kernel_size, self.stride,
RuntimeError: Given input size: (512x4x4). Calculated output size: (512x-1x-1). Output size is too small```
Most helpful comment
Just confirmed the bug on a clean conda environment with Python 3.6.8, Ubuntu18.04, cuda9.1
outputs of
pip freeze | grep torchTo reproduce,
Output log on my side:
It is should be caused by a bug in
SpatialAveragePooling.cu. But the simple fix in https://github.com/pytorch/vision/pull/697 should work it around for torchvision.