Last active
January 31, 2025 09:38
-
-
Save bdsaglam/84b1e1ba848381848ac0a308bfe0d84c to your computer and use it in GitHub Desktop.
Separable 2D Convolution PyTorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SeparableConv2d(torch.nn.Module): | |
| def __init__(self, | |
| in_channels, | |
| out_channels, | |
| kernel_size=3, | |
| stride=1, | |
| padding=0, | |
| dilation=1, | |
| bias=True, | |
| padding_mode='zeros', | |
| depth_multiplier=1, | |
| ): | |
| super().__init__() | |
| intermediate_channels = in_channels * depth_multiplier | |
| self.spatialConv = torch.nn.Conv2d( | |
| in_channels=in_channels, | |
| out_channels=intermediate_channels, | |
| kernel_size=kernel_size, | |
| stride=stride, | |
| padding=padding, | |
| dilation=dilation, | |
| groups=in_channels, | |
| bias=bias, | |
| padding_mode=padding_mode | |
| ) | |
| self.pointConv = torch.nn.Conv2d( | |
| in_channels=intermediate_channels, | |
| out_channels=out_channels, | |
| kernel_size=1, | |
| stride=1, | |
| padding=0, | |
| dilation=1, | |
| bias=bias, | |
| padding_mode=padding_mode, | |
| ) | |
| def forward(self, x): | |
| return self.pointConv(self.spatialConv(x)) |
Hello @tejan-rgb, the groups = in_channels attribute is necessary to compute the depthwise convolution, you can see how it works on this website, https://animatedai.github.io/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, can you please explain why in
groups=in_channels?