From 1e8e67541806225a9f5cfebbc29586b5c42b6383 Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Tue, 3 Dec 2019 00:27:30 -0300 Subject: [PATCH 1/6] Includes of dilation and groups params for nn.Conv2d --- octconv/octconv.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index f9d1288..d2750e4 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -11,6 +11,8 @@ def __init__(self, stride=1, padding=0, alpha=0.5, + dilation=1, + groups=1, bias=False): super(OctConv2d, self).__init__() @@ -46,6 +48,8 @@ def __init__(self, self.kernel_size = kernel_size self.stride = stride self.padding = padding + self.dilation = dilation + self.groups = groups self.bias = bias self.pool = nn.AvgPool2d(kernel_size=(2, 2), stride=2) @@ -54,6 +58,8 @@ def __init__(self, out_channels=self.out_channels['high'], kernel_size=kernel_size, padding=padding, + dilation=dilation, + groups=groups, bias=bias) \ if not (self.alpha_in == 1 or self.alpha_out == 1) else None @@ -61,6 +67,8 @@ def __init__(self, out_channels=self.out_channels['low'], kernel_size=kernel_size, padding=padding, + dilation=dilation, + groups=groups, bias=bias) \ if not (self.alpha_in == 1 or self.alpha_out == 0) else None @@ -68,6 +76,8 @@ def __init__(self, out_channels=self.out_channels['high'], kernel_size=kernel_size, padding=padding, + dilation=dilation, + groups=groups, bias=bias) \ if not (self.alpha_in == 0 or self.alpha_out == 1) else None @@ -75,6 +85,8 @@ def __init__(self, out_channels=self.out_channels['low'], kernel_size=kernel_size, padding=padding, + dilation=dilation, + groups=groups, bias=bias) \ if not (self.alpha_in == 0 or self.alpha_out == 0) else None @@ -127,10 +139,12 @@ def _check_inputs(self, x_h, x_l): def __repr__(self): s = """{}(in_channels=(low: {}, high: {}), out_channels=(low: {}, high: {}), kernel_size=({kernel}, {kernel}), stride=({stride}, {stride}), - padding={}, alphas=({}, {}), bias={})""".format( + padding={}, alphas=({}, {}), dilation={dilation}, groups={groups}, + bias={})""".format( self._get_name(), self.in_channels['low'], self.in_channels['high'], self.out_channels['low'], self.out_channels['high'], self.padding, self.alpha_in, self.alpha_out, self.bias, - kernel=self.kernel_size, stride=self.stride) + kernel=self.kernel_size, stride=self.stride, dilation=self.dilation, + groups=self.groups) return s From b248d012da393a1e8a15e78251692c70f39747d2 Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Tue, 24 Dec 2019 04:19:46 -0300 Subject: [PATCH 2/6] groups option fix --- octconv/octconv.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index d2750e4..a3a419a 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -12,7 +12,7 @@ def __init__(self, padding=0, alpha=0.5, dilation=1, - groups=1, + groups=False, bias=False): super(OctConv2d, self).__init__() @@ -52,6 +52,18 @@ def __init__(self, self.groups = groups self.bias = bias + # groups + if groups: + self.groups = { + 'high': in_ch_hf, + 'low': in_channels - in_ch_hf + } + else: + self.groups = { + 'high': 1, + 'low': 1 + } + self.pool = nn.AvgPool2d(kernel_size=(2, 2), stride=2) self.conv_h2h = nn.Conv2d(in_channels=self.in_channels['high'], @@ -59,7 +71,7 @@ def __init__(self, kernel_size=kernel_size, padding=padding, dilation=dilation, - groups=groups, + groups=self.groups['high'], bias=bias) \ if not (self.alpha_in == 1 or self.alpha_out == 1) else None @@ -68,7 +80,7 @@ def __init__(self, kernel_size=kernel_size, padding=padding, dilation=dilation, - groups=groups, + groups=self.groups['high'], bias=bias) \ if not (self.alpha_in == 1 or self.alpha_out == 0) else None @@ -77,7 +89,7 @@ def __init__(self, kernel_size=kernel_size, padding=padding, dilation=dilation, - groups=groups, + groups=self.groups['low'], bias=bias) \ if not (self.alpha_in == 0 or self.alpha_out == 1) else None @@ -86,7 +98,7 @@ def __init__(self, kernel_size=kernel_size, padding=padding, dilation=dilation, - groups=groups, + groups=self.groups['low'], bias=bias) \ if not (self.alpha_in == 0 or self.alpha_out == 0) else None From 5294af8fbcd0252ccab3ef500e623de19b7b63c6 Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Wed, 25 Dec 2019 04:31:31 -0300 Subject: [PATCH 3/6] groups param fix --- octconv/octconv.py | 48 ++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index a3a419a..0d240b9 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -1,7 +1,6 @@ import torch.nn as nn import torch.nn.functional as F - class OctConv2d(nn.Module): def __init__(self, @@ -15,6 +14,22 @@ def __init__(self, groups=False, bias=False): + """ + Octave convolution from the 2019 article + Drop an Octave: Reducing Spatial Redundancy in Convolutional Neural Networks with Octave Convolution + Extend the 2D convolution with the octave reduction. + Args: + in_channels (int): Number of channels in the input image + out_channels (int): Number of channels produced by the convolution + kernel_size (int or tuple): Size of the convolving kernel + stride (int or tuple, optional): Stride of the convolution. Default: 1 + padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0 + dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 + alpha (float or tuple, optional): Reduction for the (input, output) octave part of the convolution. Default: 0.5 + groups (bool, optional): Decides if the convolution must be group-wise, with groups=in_channels. Default: False + bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True`` + """ + super(OctConv2d, self).__init__() assert isinstance(in_channels, int) and in_channels > 0 @@ -45,25 +60,26 @@ def __init__(self, 'low': out_channels - out_ch_hf } + # groups + self.groups = { + 'high': 1, + 'low': 1 + } + + if groups: + if self.alpha_out > 0 and self.in_channels['high'] <= self.out_channels['high']: + self.groups['high'] = in_ch_hf + + if self.alpha_in > 0 and self.in_channels['low'] <= self.out_channels['low']: + self.groups['low'] = in_channels - in_ch_hf + + self.kernel_size = kernel_size self.stride = stride self.padding = padding self.dilation = dilation - self.groups = groups self.bias = bias - # groups - if groups: - self.groups = { - 'high': in_ch_hf, - 'low': in_channels - in_ch_hf - } - else: - self.groups = { - 'high': 1, - 'low': 1 - } - self.pool = nn.AvgPool2d(kernel_size=(2, 2), stride=2) self.conv_h2h = nn.Conv2d(in_channels=self.in_channels['high'], @@ -151,12 +167,12 @@ def _check_inputs(self, x_h, x_l): def __repr__(self): s = """{}(in_channels=(low: {}, high: {}), out_channels=(low: {}, high: {}), kernel_size=({kernel}, {kernel}), stride=({stride}, {stride}), - padding={}, alphas=({}, {}), dilation={dilation}, groups={groups}, + padding={}, alphas=({}, {}), dilation={dilation}, groups=(low: {groupsl}, high: {groupsh}), bias={})""".format( self._get_name(), self.in_channels['low'], self.in_channels['high'], self.out_channels['low'], self.out_channels['high'], self.padding, self.alpha_in, self.alpha_out, self.bias, kernel=self.kernel_size, stride=self.stride, dilation=self.dilation, - groups=self.groups) + groupsl=self.groups['low'], groupsh=self.groups['high']) return s From 88d6b4cb7a954affe8e80999b843e334875bc335 Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Thu, 26 Dec 2019 03:25:00 -0300 Subject: [PATCH 4/6] group param fix --- octconv/octconv.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index 0d240b9..2b89529 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -27,7 +27,7 @@ def __init__(self, dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 alpha (float or tuple, optional): Reduction for the (input, output) octave part of the convolution. Default: 0.5 groups (bool, optional): Decides if the convolution must be group-wise, with groups=in_channels. Default: False - bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``True`` + bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``False`` """ super(OctConv2d, self).__init__() @@ -66,7 +66,7 @@ def __init__(self, 'low': 1 } - if groups: + if type(groups) == bool and groups: if self.alpha_out > 0 and self.in_channels['high'] <= self.out_channels['high']: self.groups['high'] = in_ch_hf From 38d07dc14a93de3a65f18167078e71999cdd600e Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Thu, 2 Jan 2020 13:38:05 -0300 Subject: [PATCH 5/6] fix pep8 --- octconv/octconv.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index 2b89529..6bafdda 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -1,6 +1,7 @@ import torch.nn as nn import torch.nn.functional as F + class OctConv2d(nn.Module): def __init__(self, @@ -25,8 +26,10 @@ def __init__(self, stride (int or tuple, optional): Stride of the convolution. Default: 1 padding (int or tuple, optional): Zero-padding added to both sides of the input. Default: 0 dilation (int or tuple, optional): Spacing between kernel elements. Default: 1 - alpha (float or tuple, optional): Reduction for the (input, output) octave part of the convolution. Default: 0.5 - groups (bool, optional): Decides if the convolution must be group-wise, with groups=in_channels. Default: False + alpha (float or tuple, optional): Reduction for the (input, output) octave part of the convolution. + Default: 0.5 + groups (bool, optional): Decides if the convolution must be group-wise, with groups=in_channels. + Default: False bias (bool, optional): If ``True``, adds a learnable bias to the output. Default: ``False`` """ @@ -73,7 +76,6 @@ def __init__(self, if self.alpha_in > 0 and self.in_channels['low'] <= self.out_channels['low']: self.groups['low'] = in_channels - in_ch_hf - self.kernel_size = kernel_size self.stride = stride self.padding = padding From 3b80091fd29b5a6bd0c956d566663f9cc21a89d1 Mon Sep 17 00:00:00 2001 From: Angel Ayala Date: Thu, 2 Jan 2020 13:43:06 -0300 Subject: [PATCH 6/6] fix pep8 --- octconv/octconv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octconv/octconv.py b/octconv/octconv.py index 6bafdda..3e8f5f3 100644 --- a/octconv/octconv.py +++ b/octconv/octconv.py @@ -69,7 +69,7 @@ def __init__(self, 'low': 1 } - if type(groups) == bool and groups: + if type(groups) == bool and groups: if self.alpha_out > 0 and self.in_channels['high'] <= self.out_channels['high']: self.groups['high'] = in_ch_hf