|
|
|
""" Normalization + Activation Layers
|
|
|
|
"""
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
import torch
|
|
|
|
from torch import nn as nn
|
|
|
|
from torch.nn import functional as F
|
|
|
|
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
from .create_act import get_act_layer
|
|
|
|
|
|
|
|
|
|
|
|
class BatchNormAct2d(nn.BatchNorm2d):
|
|
|
|
"""BatchNorm + Activation
|
|
|
|
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
This module performs BatchNorm + Activation in a manner that will remain backwards
|
|
|
|
compatible with weights trained with separate bn, act. This is why we inherit from BN
|
|
|
|
instead of composing it as a .bn member.
|
|
|
|
"""
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True,
|
|
|
|
apply_act=True, act_layer=nn.ReLU, inplace=True, drop_block=None):
|
|
|
|
super(BatchNormAct2d, self).__init__(
|
|
|
|
num_features, eps=eps, momentum=momentum, affine=affine, track_running_stats=track_running_stats)
|
|
|
|
if isinstance(act_layer, str):
|
|
|
|
act_layer = get_act_layer(act_layer)
|
|
|
|
if act_layer is not None and apply_act:
|
|
|
|
act_args = dict(inplace=True) if inplace else {}
|
|
|
|
self.act = act_layer(**act_args)
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
else:
|
|
|
|
self.act = None
|
|
|
|
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
def _forward_jit(self, x):
|
|
|
|
""" A cut & paste of the contents of the PyTorch BatchNorm2d forward function
|
|
|
|
"""
|
|
|
|
# exponential_average_factor is self.momentum set to
|
|
|
|
# (when it is available) only so that if gets updated
|
|
|
|
# in ONNX graph when this node is exported to ONNX.
|
|
|
|
if self.momentum is None:
|
|
|
|
exponential_average_factor = 0.0
|
|
|
|
else:
|
|
|
|
exponential_average_factor = self.momentum
|
|
|
|
|
|
|
|
if self.training and self.track_running_stats:
|
|
|
|
# TODO: if statement only here to tell the jit to skip emitting this when it is None
|
|
|
|
if self.num_batches_tracked is not None:
|
|
|
|
self.num_batches_tracked += 1
|
|
|
|
if self.momentum is None: # use cumulative moving average
|
|
|
|
exponential_average_factor = 1.0 / float(self.num_batches_tracked)
|
|
|
|
else: # use exponential moving average
|
|
|
|
exponential_average_factor = self.momentum
|
|
|
|
|
|
|
|
x = F.batch_norm(
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
x, self.running_mean, self.running_var, self.weight, self.bias,
|
|
|
|
self.training or not self.track_running_stats,
|
|
|
|
exponential_average_factor, self.eps)
|
|
|
|
return x
|
|
|
|
|
|
|
|
@torch.jit.ignore
|
|
|
|
def _forward_python(self, x):
|
|
|
|
return super(BatchNormAct2d, self).forward(x)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
# FIXME cannot call parent forward() and maintain jit.script compatibility?
|
|
|
|
if torch.jit.is_scripting():
|
|
|
|
x = self._forward_jit(x)
|
|
|
|
else:
|
|
|
|
x = self._forward_python(x)
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
if self.act is not None:
|
|
|
|
x = self.act(x)
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
Monster commit, activation refactor, VoVNet, norm_act improvements, more
* refactor activations into basic PyTorch, jit scripted, and memory efficient custom auto
* implement hard-mish, better grad for hard-swish
* add initial VovNet V1/V2 impl, fix #151
* VovNet and DenseNet first models to use NormAct layers (support BatchNormAct2d, EvoNorm, InplaceIABN)
* Wrap IABN for any models that use it
* make more models torchscript compatible (DPN, PNasNet, Res2Net, SelecSLS) and add tests
5 years ago
|
|
|
class GroupNormAct(nn.GroupNorm):
|
|
|
|
|
|
|
|
def __init__(self, num_groups, num_channels, eps=1e-5, affine=True,
|
|
|
|
apply_act=True, act_layer=nn.ReLU, inplace=True, drop_block=None):
|
|
|
|
super(GroupNormAct, self).__init__(num_groups, num_channels, eps=eps, affine=affine)
|
|
|
|
if isinstance(act_layer, str):
|
|
|
|
act_layer = get_act_layer(act_layer)
|
|
|
|
if act_layer is not None and apply_act:
|
|
|
|
self.act = act_layer(inplace=inplace)
|
|
|
|
else:
|
|
|
|
self.act = None
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
x = F.group_norm(x, self.num_groups, self.weight, self.bias, self.eps)
|
|
|
|
if self.act is not None:
|
|
|
|
x = self.act(x)
|
|
|
|
return x
|