composer.algorithms.blurpool.blurpool#

composer.algorithms.blurpool.blurpool

Functions

apply_blurpool

Add anti-aliasing filters to strided torch.nn.Conv2d and/or torch.nn.MaxPool2d modules.

Classes

Algorithm

Base class for algorithms.

BlurConv2d

This module is a drop-in replacement for torch.nn.Conv2d, but with an anti-aliasing filter.

BlurMaxPool2d

This module is a (nearly) drop-in replacement for torch.nn.MaxPool2d, but with an anti-aliasing filter.

BlurPool

BlurPool adds anti-aliasing filters to convolutional layers.

Event

Enum to represent training loop events.

Logger

An interface to record training data.

Optimizer

Base class for all optimizers.

State

The state of the trainer.

Exceptions

NoEffectWarning

Warns when an algorithm did not have an effect.

Attributes

  • Optional

  • Sequence

  • Union

  • annotations

  • log

class composer.algorithms.blurpool.blurpool.BlurPool(replace_convs=True, replace_maxpools=True, blur_first=True, min_channels=16)[source]#

Bases: composer.core.algorithm.Algorithm

BlurPool adds anti-aliasing filters to convolutional layers.

This algorithm increases accuracy and invariance to small shifts in the input. It runs on INIT.

Parameters
  • replace_convs (bool) โ€“ replace strided torch.nn.Conv2d modules with BlurConv2d modules. Default: True.

  • replace_maxpools (bool) โ€“ replace eligible torch.nn.MaxPool2d modules with BlurMaxPool2d modules. Default: True.

  • blur_first (bool) โ€“ when replace_convs is True, blur input before the associated convolution. When set to False, the convolution is applied with a stride of 1 before the blurring, resulting in significant overhead (though more closely matching the paper). See BlurConv2d for further discussion. Default: True.

  • min_channels (int, optional) โ€“ Skip replacing layers with in_channels < min_channels. Commonly used to prevent the blurring of the first layer. Default: 16.

composer.algorithms.blurpool.blurpool.apply_blurpool(model, replace_convs=True, replace_maxpools=True, blur_first=True, min_channels=16, optimizers=None)[source]#

Add anti-aliasing filters to strided torch.nn.Conv2d and/or torch.nn.MaxPool2d modules.

These filters increase invariance to small spatial shifts in the input (Zhang 2019).

Parameters
  • model (torch.nn.Module) โ€“ the model to modify in-place

  • replace_convs (bool, optional) โ€“ replace strided torch.nn.Conv2d modules with BlurConv2d modules. Default: True.

  • replace_maxpools (bool, optional) โ€“ replace eligible torch.nn.MaxPool2d modules with BlurMaxPool2d modules. Default: True.

  • blur_first (bool, optional) โ€“ for replace_convs, blur input before the associated convolution. When set to False, the convolution is applied with a stride of 1 before the blurring, resulting in significant overhead (though more closely matching the paper). See BlurConv2d for further discussion. Default: True.

  • min_channels (int, optional) โ€“ Skip replacing layers with in_channels < min_channels. Commonly used to prevent the blurring of the first layer. Default: 16.

  • optimizers (Optimizer | Sequence[Optimizer], optional) โ€“

    Existing optimizers bound to model.parameters(). All optimizers that have already been constructed with model.parameters() must be specified here so they will optimize the correct parameters.

    If the optimizer(s) are constructed after calling this function, then it is safe to omit this parameter. These optimizers will see the correct model parameters.

Returns

The modified model

Example

import composer.functional as cf
from torchvision import models
model = models.resnet50()
cf.apply_blurpool(model)