-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change resnet AvgPool2d to Adaptive_AvgPool_2d(global avg pooling) to make the input size changeable #155
Conversation
I'm unsure about merging this in. resnet = torchvision.models.resnet18()
resnet.avgpool = nn.AdaptiveAvgPool2d(1) would be enough, and explicit to the user. On the other hand, it hides a potential problem, which is that the pre-trained models might not work well if too big images are fed to the network, as they were trained to identify patterns at a specific resolution. |
I'm for using adaptive pooling in all models. If you are willing to accept this change, I will create a PR for all models. |
Well, I suppose most common use-cases are just feeding images for classification without any changes in the architecture. Other scenarios (like segmentation) will anyway require some modification on the structure of the network. |
there are two common use cases:
fully dense is the case where we give bigger inputs and we get bigger outputs than 1000x1x1 adaptive is when we give bigger inputs and always get out 1000x4096 to be fed into fc layers. they are both common and in conflicting paths. we should plan for these both via constructor arguments |
As there have been a few issues as PRs around this topic (#166, #184, #155, #190), we should probably make a decision on what to do my 2¢ on these two cases: Adaptive Pooling I think the model surgery is quite straight forward, and can be well documented in a tutorial/example. IMO the cost of cluttering the constructor + the docs to support this case is not worth it as it reads almost/if not more clearly by the user being explicit by doing: resnet = torchvision.models.resnet18()
resnet.avgpool = nn.AdaptiveAvgPool2d(1) The one problem with this is that in some models the final pooling is done using the functional interface (e.g. densenet), but this can be easily remedied by creating members on the module for the feature pooling (and is backwards compatible) Fully Convolutional Networks What are your thoughts @fmassa @soumith @colesbury? |
For DenseNet model, it would be good at least to write explicitly that input image size is 224x224, otherwise |
I have the similar problem but the model I use is a custom RecurrentAttention one and it has no attribute "AdaptivePooling" --> https://github.com/kevinzakka/recurrent-visual-attention/blob/master/model.py I did modify the nn parts that are imported and modified from torch models (in modules.py and model.py) but it didn't work. |
I have added the adaptive avg pooling but error still remain the same. Please help? Detailed Error Exception NameError: "global name 'FileNotFoundError' is not defined" in <bound method _DataLoaderIter.del of <torch.utils.data.dataloader._DataLoaderIter object at 0x7fcf3cf65990>> ignored RuntimeError Traceback (most recent call last) in train_model(model, criterion, optimizer, scheduler, num_epochs) /usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs) in forward(self, x) /usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs) /usr/local/lib/python2.7/dist-packages/torch/nn/modules/container.pyc in forward(self, input) /usr/local/lib/python2.7/dist-packages/torch/nn/modules/module.pyc in call(self, *input, **kwargs) /usr/local/lib/python2.7/dist-packages/torch/nn/modules/linear.pyc in forward(self, input) /usr/local/lib/python2.7/dist-packages/torch/nn/functional.pyc in linear(input, weight, bias) RuntimeError: size mismatch, m1: [512 x 1], m2: [512 x 2] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:249 |
@mamunir the error that you are facing happens because you added the wrong output size for the |
@fmassa thanks man for reply. I did input call wrong as not properly flow from the network. now fine :) |
This has been implemented in #643 Thanks for the PR! |
This PR change
resnet
final pooling layer before classifier from fixedAvgPool2d(7)
to adaptiveAdaptive_AvgPool_2d(1)
When we use
AvgPool2d(7)
, the input size is forced to be 224*224. Otherwise it will cause the wrong incomplete output, even raise the errorRuntimeError: size mismatch, m1: [1 x 2048], m2: [512 x 1000] at /b/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:1229