create_conv2d.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """ Create Conv2d Factory Method
  2. Hacked together by / Copyright 2020 Ross Wightman
  3. """
  4. from .mixed_conv2d import MixedConv2d
  5. from .cond_conv2d import CondConv2d
  6. from .conv2d_same import create_conv2d_pad
  7. def create_conv2d(in_channels, out_channels, kernel_size, **kwargs):
  8. """ Select a 2d convolution implementation based on arguments
  9. Creates and returns one of torch.nn.Conv2d, Conv2dSame, MixedConv2d, or CondConv2d.
  10. Used extensively by EfficientNet, MobileNetv3 and related networks.
  11. """
  12. if isinstance(kernel_size, list):
  13. assert 'num_experts' not in kwargs # MixNet + CondConv combo not supported currently
  14. if 'groups' in kwargs:
  15. groups = kwargs.pop('groups')
  16. if groups == in_channels:
  17. kwargs['depthwise'] = True
  18. else:
  19. assert groups == 1
  20. # We're going to use only lists for defining the MixedConv2d kernel groups,
  21. # ints, tuples, other iterables will continue to pass to normal conv and specify h, w.
  22. m = MixedConv2d(in_channels, out_channels, kernel_size, **kwargs)
  23. else:
  24. depthwise = kwargs.pop('depthwise', False)
  25. # for DW out_channels must be multiple of in_channels as must have out_channels % groups == 0
  26. groups = in_channels if depthwise else kwargs.pop('groups', 1)
  27. if 'num_experts' in kwargs and kwargs['num_experts'] > 0:
  28. m = CondConv2d(in_channels, out_channels, kernel_size, groups=groups, **kwargs)
  29. else:
  30. m = create_conv2d_pad(in_channels, out_channels, kernel_size, groups=groups, **kwargs)
  31. return m