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.
37 lines
1.6 KiB
37 lines
1.6 KiB
5 years ago
|
""" Create Conv2d Factory Method
|
||
5 years ago
|
|
||
4 years ago
|
Hacked together by / Copyright 2020 Ross Wightman
|
||
5 years ago
|
"""
|
||
|
|
||
|
from .mixed_conv2d import MixedConv2d
|
||
|
from .cond_conv2d import CondConv2d
|
||
|
from .conv2d_same import create_conv2d_pad
|
||
|
|
||
|
|
||
5 years ago
|
def create_conv2d(in_channels, out_channels, kernel_size, **kwargs):
|
||
5 years ago
|
""" Select a 2d convolution implementation based on arguments
|
||
|
Creates and returns one of torch.nn.Conv2d, Conv2dSame, MixedConv2d, or CondConv2d.
|
||
|
|
||
|
Used extensively by EfficientNet, MobileNetv3 and related networks.
|
||
|
"""
|
||
|
if isinstance(kernel_size, list):
|
||
|
assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently
|
||
3 years ago
|
if 'groups' in kwargs:
|
||
|
groups = kwargs.pop('groups')
|
||
|
if groups == in_channels:
|
||
|
kwargs['depthwise'] = True
|
||
|
else:
|
||
|
assert groups == 1
|
||
5 years ago
|
# We're going to use only lists for defining the MixedConv2d kernel groups,
|
||
|
# ints, tuples, other iterables will continue to pass to normal conv and specify h, w.
|
||
5 years ago
|
m = MixedConv2d(in_channels, out_channels, kernel_size, **kwargs)
|
||
5 years ago
|
else:
|
||
|
depthwise = kwargs.pop('depthwise', False)
|
||
4 years ago
|
# for DW out_channels must be multiple of in_channels as must have out_channels % groups == 0
|
||
|
groups = in_channels if depthwise else kwargs.pop('groups', 1)
|
||
5 years ago
|
if 'num_experts' in kwargs and kwargs['num_experts'] > 0:
|
||
5 years ago
|
m = CondConv2d(in_channels, out_channels, kernel_size, groups=groups, **kwargs)
|
||
5 years ago
|
else:
|
||
5 years ago
|
m = create_conv2d_pad(in_channels, out_channels, kernel_size, groups=groups, **kwargs)
|
||
5 years ago
|
return m
|