|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | +import math |
| 5 | + |
| 6 | + |
| 7 | +def _is_static_pad(kernel_size, stride=1, dilation=1, **_): |
| 8 | + return stride == 1 and (dilation * (kernel_size - 1)) % 2 == 0 |
| 9 | + |
| 10 | + |
| 11 | +def _get_padding(kernel_size, stride=1, dilation=1, **_): |
| 12 | + padding = ((stride - 1) + dilation * (kernel_size - 1)) // 2 |
| 13 | + return padding |
| 14 | + |
| 15 | + |
| 16 | +def _calc_same_pad(i, k, s, d): |
| 17 | + return max((math.ceil(i / s) - 1) * s + (k - 1) * d + 1 - i, 0) |
| 18 | + |
| 19 | + |
| 20 | +def _split_channels(num_chan, num_groups): |
| 21 | + split = [num_chan // num_groups for _ in range(num_groups)] |
| 22 | + split[0] += num_chan - sum(split) |
| 23 | + return split |
| 24 | + |
| 25 | + |
| 26 | +class Conv2dSame(nn.Conv2d): |
| 27 | + """ Tensorflow like 'SAME' convolution wrapper for 2D convolutions |
| 28 | + """ |
| 29 | + def __init__(self, in_channels, out_channels, kernel_size, stride=1, |
| 30 | + padding=0, dilation=1, groups=1, bias=True): |
| 31 | + super(Conv2dSame, self).__init__( |
| 32 | + in_channels, out_channels, kernel_size, stride, 0, dilation, |
| 33 | + groups, bias) |
| 34 | + |
| 35 | + def forward(self, x): |
| 36 | + ih, iw = x.size()[-2:] |
| 37 | + kh, kw = self.weight.size()[-2:] |
| 38 | + pad_h = _calc_same_pad(ih, kh, self.stride[0], self.dilation[0]) |
| 39 | + pad_w = _calc_same_pad(iw, kw, self.stride[1], self.dilation[1]) |
| 40 | + if pad_h > 0 or pad_w > 0: |
| 41 | + x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2]) |
| 42 | + return F.conv2d(x, self.weight, self.bias, self.stride, |
| 43 | + self.padding, self.dilation, self.groups) |
| 44 | + |
| 45 | + |
| 46 | +# def conv2d_pad(in_chs, out_chs, kernel_size, **kwargs): |
| 47 | +# padding = kwargs.pop('padding', '') |
| 48 | +# kwargs.setdefault('bias', False) |
| 49 | +# if isinstance(padding, str): |
| 50 | +# # for any string padding, the padding will be calculated for you, one of three ways |
| 51 | +# padding = padding.lower() |
| 52 | +# if padding == 'same': |
| 53 | +# # TF compatible 'SAME' padding, has a performance and GPU memory allocation impact |
| 54 | +# if _is_static_pad(kernel_size, **kwargs): |
| 55 | +# # static case, no extra overhead |
| 56 | +# padding = _get_padding(kernel_size, **kwargs) |
| 57 | +# return nn.Conv2d(in_chs, out_chs, kernel_size, padding=padding, **kwargs) |
| 58 | +# else: |
| 59 | +# # dynamic padding |
| 60 | +# return Conv2dSame(in_chs, out_chs, kernel_size, **kwargs) |
| 61 | +# elif padding == 'valid': |
| 62 | +# # 'VALID' padding, same as padding=0 |
| 63 | +# return nn.Conv2d(in_chs, out_chs, kernel_size, padding=0, **kwargs) |
| 64 | +# else: |
| 65 | +# # Default to PyTorch style 'same'-ish symmetric padding |
| 66 | +# padding = _get_padding(kernel_size, **kwargs) |
| 67 | +# return nn.Conv2d(in_chs, out_chs, kernel_size, padding=padding, **kwargs) |
| 68 | +# else: |
| 69 | +# # padding was specified as a number or pair |
| 70 | +# return nn.Conv2d(in_chs, out_chs, kernel_size, padding=padding, **kwargs) |
| 71 | + |
| 72 | + |
| 73 | +# class MixedConv2d(nn.Module): |
| 74 | +# """ Mixed Grouped Convolution |
| 75 | +# Based on MDConv and GroupedConv in MixNet impl: |
| 76 | +# https://github.com/tensorflow/tpu/blob/master/models/official/mnasnet/mixnet/custom_layers.py |
| 77 | +# """ |
| 78 | + |
| 79 | +# def __init__(self, in_channels, out_channels, kernel_size=3, |
| 80 | +# stride=1, padding='', dilated=False, depthwise=False, **kwargs): |
| 81 | +# super(MixedConv2d, self).__init__() |
| 82 | + |
| 83 | +# kernel_size = kernel_size if isinstance(kernel_size, list) else [kernel_size] |
| 84 | +# num_groups = len(kernel_size) |
| 85 | +# in_splits = _split_channels(in_channels, num_groups) |
| 86 | +# out_splits = _split_channels(out_channels, num_groups) |
| 87 | +# for idx, (k, in_ch, out_ch) in enumerate(zip(kernel_size, in_splits, out_splits)): |
| 88 | +# d = 1 |
| 89 | +# # FIXME make compat with non-square kernel/dilations/strides |
| 90 | +# if stride == 1 and dilated: |
| 91 | +# d, k = (k - 1) // 2, 3 |
| 92 | +# conv_groups = out_ch if depthwise else 1 |
| 93 | +# # use add_module to keep key space clean |
| 94 | +# self.add_module( |
| 95 | +# str(idx), |
| 96 | +# conv2d_pad( |
| 97 | +# in_ch, out_ch, k, stride=stride, |
| 98 | +# padding=padding, dilation=d, groups=conv_groups, **kwargs) |
| 99 | +# ) |
| 100 | +# self.splits = in_splits |
| 101 | + |
| 102 | +# def forward(self, x): |
| 103 | +# x_split = torch.split(x, self.splits, 1) |
| 104 | +# x_out = [c(x) for x, c in zip(x_split, self._modules.values())] |
| 105 | +# x = torch.cat(x_out, 1) |
| 106 | +# return x |
| 107 | + |
| 108 | + |
| 109 | +# # helper method |
| 110 | +# def select_conv2d(in_chs, out_chs, kernel_size, **kwargs): |
| 111 | +# assert 'groups' not in kwargs # only use 'depthwise' bool arg |
| 112 | +# if isinstance(kernel_size, list): |
| 113 | +# # We're going to use only lists for defining the MixedConv2d kernel groups, |
| 114 | +# # ints, tuples, other iterables will continue to pass to normal conv and specify h, w. |
| 115 | +# return MixedConv2d(in_chs, out_chs, kernel_size, **kwargs) |
| 116 | +# else: |
| 117 | +# depthwise = kwargs.pop('depthwise', False) |
| 118 | +# groups = out_chs if depthwise else 1 |
| 119 | +# return conv2d_pad(in_chs, out_chs, kernel_size, groups=groups, **kwargs) |
0 commit comments