You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.6 KiB
77 lines
2.6 KiB
4 years ago
|
""" Depthwise Separable Conv Modules
|
||
|
|
||
|
Basic DWS convs. Other variations of DWS exist with batch norm or activations between the
|
||
|
DW and PW convs such as the Depthwise modules in MobileNetV2 / EfficientNet and Xception.
|
||
|
|
||
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
|
"""
|
||
5 years ago
|
from torch import nn as nn
|
||
|
|
||
|
from .create_conv2d import create_conv2d
|
||
3 years ago
|
from .create_norm_act import get_norm_act_layer
|
||
5 years ago
|
|
||
|
|
||
3 years ago
|
class SeparableConvNormAct(nn.Module):
|
||
5 years ago
|
""" Separable Conv w/ trailing Norm and Activation
|
||
|
"""
|
||
|
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, dilation=1, padding='', bias=False,
|
||
4 years ago
|
channel_multiplier=1.0, pw_kernel_size=1, norm_layer=nn.BatchNorm2d, act_layer=nn.ReLU,
|
||
3 years ago
|
apply_act=True, drop_layer=None):
|
||
|
super(SeparableConvNormAct, self).__init__()
|
||
5 years ago
|
|
||
|
self.conv_dw = create_conv2d(
|
||
|
in_channels, int(in_channels * channel_multiplier), kernel_size,
|
||
|
stride=stride, dilation=dilation, padding=padding, depthwise=True)
|
||
|
|
||
|
self.conv_pw = create_conv2d(
|
||
|
int(in_channels * channel_multiplier), out_channels, pw_kernel_size, padding=padding, bias=bias)
|
||
|
|
||
3 years ago
|
norm_act_layer = get_norm_act_layer(norm_layer, act_layer)
|
||
|
norm_kwargs = dict(drop_layer=drop_layer) if drop_layer is not None else {}
|
||
|
self.bn = norm_act_layer(out_channels, apply_act=apply_act, **norm_kwargs)
|
||
5 years ago
|
|
||
4 years ago
|
@property
|
||
|
def in_channels(self):
|
||
|
return self.conv_dw.in_channels
|
||
|
|
||
|
@property
|
||
|
def out_channels(self):
|
||
|
return self.conv_pw.out_channels
|
||
|
|
||
5 years ago
|
def forward(self, x):
|
||
|
x = self.conv_dw(x)
|
||
|
x = self.conv_pw(x)
|
||
3 years ago
|
x = self.bn(x)
|
||
5 years ago
|
return x
|
||
|
|
||
|
|
||
3 years ago
|
SeparableConvBnAct = SeparableConvNormAct
|
||
|
|
||
|
|
||
5 years ago
|
class SeparableConv2d(nn.Module):
|
||
|
""" Separable Conv
|
||
|
"""
|
||
|
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, dilation=1, padding='', bias=False,
|
||
|
channel_multiplier=1.0, pw_kernel_size=1):
|
||
|
super(SeparableConv2d, self).__init__()
|
||
|
|
||
|
self.conv_dw = create_conv2d(
|
||
|
in_channels, int(in_channels * channel_multiplier), kernel_size,
|
||
|
stride=stride, dilation=dilation, padding=padding, depthwise=True)
|
||
|
|
||
|
self.conv_pw = create_conv2d(
|
||
|
int(in_channels * channel_multiplier), out_channels, pw_kernel_size, padding=padding, bias=bias)
|
||
|
|
||
4 years ago
|
@property
|
||
|
def in_channels(self):
|
||
|
return self.conv_dw.in_channels
|
||
|
|
||
|
@property
|
||
|
def out_channels(self):
|
||
|
return self.conv_pw.out_channels
|
||
|
|
||
5 years ago
|
def forward(self, x):
|
||
|
x = self.conv_dw(x)
|
||
|
x = self.conv_pw(x)
|
||
|
return x
|