Making Convolutional Networks Shift-Invariant Again
Richard Zhang. In ICML, 2019.
Quick & easy start
pip install antialiased-cnns
import antialiased_cnns model = antialiased_cnns.resnet50(pretrained=True)
If you have a model already and want to antialias and continue training, copy your old weights over:
import torchvision.models as models old_model = models.resnet50(pretrained=True) # old (aliased) model antialiased_cnns.copy_params_buffers(old_model, model) # copy the weights over
If you want to modify your own model, use the BlurPool layer. More information about our provided models and how to use BlurPool is below.
C = 10 # example feature channel size blurpool = antialiased_cnns.BlurPool(C, stride=2) # BlurPool layer; use to downsample a feature map ex_tens = torch.Tensor(1,C,128,128) print(blurpool(ex_tens).shape) # 1xCx64x64 tensor
- (Oct 2020) Finetune I initialize the antialiased model with weights from baseline model, and finetune. Before, I was training from scratch. The results are better.
- (Oct 2020) Additional models We now have 23 total model variants. I added variants of vgg, densenet, resnext, wide resnet varieties! The same conclusions hold.
- (Sept 2020) Pip install You can also now
pip install antialiased-cnnsand load models with the
- (Sept 2020) Kernel 4 I have added kernel size 4 experiments. When downsampling an even sized feature map (e.g., a 128x128-->64x64), this is actually the correct size to use to keep the indices from drifting.
Table of contents
- More information about antialiased models
- Instructions for antialiasing your own model, using the
- ImageNet training and evaluation code. Achieving better consistency, while maintaining or improving accuracy, is an open problem. Help improve the results!
Pip install this package
pip install antialiased-cnns
Or clone this repository and install requirements (notably, PyTorch)
https://github.com/adobe/antialiased-cnns.git cd antialiased-cnns pip install -r requirements.txt
(1) Loading an antialiased model
The following loads a pretrained antialiased model, perhaps as a backbone for your application.
import antialiased_cnns model = antialiased_cnns.resnet50(pretrained=True, filter_size=4)
We also provide weights for antialiased
MobileNetv2 (see example_usage.py).
(2) How to antialias your own architecture
antialiased_cnns module contains the
BlurPool class, which does blur+subsampling. Run
pip install antialiased-cnns or copy the
Methodology The methodology is simple -- first evaluate with stride 1, and then use our
BlurPool layer to do antialiased downsampling. Make the following architectural changes.
import antialiased_cnns # MaxPool --> MaxBlurPool baseline = nn.MaxPool2d(kernel_size=2, stride=2) antialiased = [nn.MaxPool2d(kernel_size=2, stride=1), antialiased_cnns.BlurPool(C, stride=2)] # Conv --> ConvBlurPool baseline = [nn.Conv2d(Cin, C, kernel_size=3, stride=2, padding=1), nn.ReLU(inplace=True)] antialiased = [nn.Conv2d(Cin, C, kernel_size=3, stride=1, padding=1), nn.ReLU(inplace=True), antialiased_cnns.BlurPool(C, stride=2)] # AvgPool --> BlurPool baseline = nn.AvgPool2d(kernel_size=2, stride=2) antialiased = antialiased_cnns.BlurPool(C, stride=2)
We assume incoming tensor has
C channels. Computing a layer at stride 1 instead of stride 2 adds memory and run-time. As such, we typically skip antialiasing at the highest-resolution (early in the network), to prevent large increases.
Add antialiasing and then continue training If you already trained a model, and then add antialiasing, you can fine-tune from that old model:
If this doesn't work, you can just copy the parameters (and not buffers). Adding antialiasing doesn't add any parameters, so the parameter lists are identical. (It does add buffers, so some heuristic is used to match the buffers, which may throw an error.)
(3) ImageNet Evaluation, Results, and Training code
We observe improvements in both accuracy (how often the image is classified correctly) and consistency (how often two shifts of the same image are classified the same).