Skip to content

Instantly share code, notes, and snippets.

@gau-nernst
Last active December 21, 2021 06:11
Show Gist options
  • Save gau-nernst/28ed1405acb426dd55566666ea03d1e9 to your computer and use it in GitHub Desktop.
Save gau-nernst/28ed1405acb426dd55566666ea03d1e9 to your computer and use it in GitHub Desktop.
Separable Convolution Block in PyTorch
from torch import nn
class SeparableConv2d(nn.Sequential):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, dilation=1, norm_layer=None, activation=None):
super().__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if activation is None:
activation = nn.ReLU6
self.dw = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size, stride=stride, padding=padding, dilation=dilation, groups=in_channels, bias=False),
norm_layer(in_channels),
activation(inplace=True)
)
self.pw = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, bias=False),
norm_layer(out_channels),
activation(inplace=True)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment