|
|
|
""" Conv2d w/ Same Padding
|
|
|
|
|
|
|
|
Hacked together by Ross Wightman
|
|
|
|
"""
|
|
|
|
import torch
|
|
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.functional as F
|
|
|
|
from typing import Tuple, Optional
|
|
|
|
|
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 .padding import pad_same, get_padding_value
|
|
|
|
|
|
|
|
|
|
|
|
def conv2d_same(
|
|
|
|
x, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, stride: Tuple[int, int] = (1, 1),
|
|
|
|
padding: Tuple[int, int] = (0, 0), dilation: Tuple[int, int] = (1, 1), groups: int = 1):
|
|
|
|
x = pad_same(x, weight.shape[-2:], stride, dilation)
|
|
|
|
return F.conv2d(x, weight, bias, stride, (0, 0), dilation, groups)
|
|
|
|
|
|
|
|
|
|
|
|
class Conv2dSame(nn.Conv2d):
|
|
|
|
""" Tensorflow like 'SAME' convolution wrapper for 2D convolutions
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
|
|
|
|
padding=0, dilation=1, groups=1, bias=True):
|
|
|
|
super(Conv2dSame, self).__init__(
|
|
|
|
in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias)
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
return conv2d_same(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
|
|
|
|
|
|
|
|
|
|
|
|
def create_conv2d_pad(in_chs, out_chs, kernel_size, **kwargs):
|
|
|
|
padding = kwargs.pop('padding', '')
|
|
|
|
kwargs.setdefault('bias', False)
|
|
|
|
padding, is_dynamic = get_padding_value(padding, kernel_size, **kwargs)
|
|
|
|
if is_dynamic:
|
|
|
|
return Conv2dSame(in_chs, out_chs, kernel_size, **kwargs)
|
|
|
|
else:
|
|
|
|
return nn.Conv2d(in_chs, out_chs, kernel_size, padding=padding, **kwargs)
|
|
|
|
|
|
|
|
|