pytorch-image-models
36 строк · 1.6 Кб
1""" Create Conv2d Factory Method
2
3Hacked together by / Copyright 2020 Ross Wightman
4"""
5
6from .mixed_conv2d import MixedConv2d7from .cond_conv2d import CondConv2d8from .conv2d_same import create_conv2d_pad9
10
11def create_conv2d(in_channels, out_channels, kernel_size, **kwargs):12""" Select a 2d convolution implementation based on arguments13Creates and returns one of torch.nn.Conv2d, Conv2dSame, MixedConv2d, or CondConv2d.
14
15Used extensively by EfficientNet, MobileNetv3 and related networks.
16"""
17if isinstance(kernel_size, list):18assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently19if 'groups' in kwargs:20groups = kwargs.pop('groups')21if groups == in_channels:22kwargs['depthwise'] = True23else:24assert groups == 125# We're going to use only lists for defining the MixedConv2d kernel groups,26# ints, tuples, other iterables will continue to pass to normal conv and specify h, w.27m = MixedConv2d(in_channels, out_channels, kernel_size, **kwargs)28else:29depthwise = kwargs.pop('depthwise', False)30# for DW out_channels must be multiple of in_channels as must have out_channels % groups == 031groups = in_channels if depthwise else kwargs.pop('groups', 1)32if 'num_experts' in kwargs and kwargs['num_experts'] > 0:33m = CondConv2d(in_channels, out_channels, kernel_size, groups=groups, **kwargs)34else:35m = create_conv2d_pad(in_channels, out_channels, kernel_size, groups=groups, **kwargs)36return m37