Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dilated RedNet support #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion det/mmdet/models/backbones/rednet.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self,
stride=self.conv1_stride,
bias=False)
self.add_module(self.norm1_name, norm1)
self.conv2 = involution(self.mid_channels, 7, self.conv2_stride)
self.conv2 = involution(self.mid_channels, 7, self.conv2_stride, dilation=dilation)

self.add_module(self.norm2_name, norm2)
self.conv3 = build_conv_layer(
Expand Down Expand Up @@ -201,6 +201,7 @@ def __init__(self,
out_channels,
expansion=None,
stride=1,
dilation=1,
avg_down=False,
conv_cfg=None,
norm_cfg=dict(type='BN'),
Expand Down Expand Up @@ -239,6 +240,7 @@ def __init__(self,
out_channels=out_channels,
expansion=self.expansion,
stride=stride,
dilation=dilation,
downsample=downsample,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
Expand All @@ -251,6 +253,7 @@ def __init__(self,
out_channels=out_channels,
expansion=self.expansion,
stride=1,
dilation=dilation,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
**kwargs))
Expand Down
6 changes: 4 additions & 2 deletions det/mmdet/models/utils/involution_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride):
stride,
dilation=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.dilation = dilation
self.channels = channels
reduction_ratio = 4
self.group_channels = 16
Expand All @@ -278,5 +280,5 @@ def forward(self, x):
weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
b, c, h, w = weight.shape
weight = weight.view(b, self.groups, self.kernel_size, self.kernel_size, h, w)
out = _involution_cuda(x, weight, stride=self.stride, padding=(self.kernel_size-1)//2)
out = _involution_cuda(x, weight, stride=self.stride, padding=self.dilation * (self.kernel_size-1) // 2, dilation=self.dilation)
return out
6 changes: 4 additions & 2 deletions det/mmdet/models/utils/involution_naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride):
stride,
dilation=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.dilation=dilation
self.channels = channels
reduction_ratio = 4
self.group_channels = 16
Expand All @@ -32,7 +34,7 @@ def __init__(self,
act_cfg=None)
if stride > 1:
self.avgpool = nn.AvgPool2d(stride, stride)
self.unfold = nn.Unfold(kernel_size, 1, (kernel_size-1)//2, stride)
self.unfold = nn.Unfold(kernel_size, dilation, dilation * (kernel_size-1) // 2, stride)

def forward(self, x):
weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
Expand Down
5 changes: 4 additions & 1 deletion seg/mmseg/models/backbones/rednet.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __init__(self,
stride=self.conv1_stride,
bias=False)
self.add_module(self.norm1_name, norm1)
self.conv2 = involution(self.mid_channels, 7, self.conv2_stride)
self.conv2 = involution(self.mid_channels, 7, self.conv2_stride, dilation=dilation)

self.add_module(self.norm2_name, norm2)
self.conv3 = build_conv_layer(
Expand Down Expand Up @@ -201,6 +201,7 @@ def __init__(self,
out_channels,
expansion=None,
stride=1,
dilation=1,
avg_down=False,
conv_cfg=None,
norm_cfg=dict(type='BN'),
Expand Down Expand Up @@ -239,6 +240,7 @@ def __init__(self,
out_channels=out_channels,
expansion=self.expansion,
stride=stride,
dilation=dilation,
downsample=downsample,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
Expand All @@ -251,6 +253,7 @@ def __init__(self,
out_channels=out_channels,
expansion=self.expansion,
stride=1,
dilation=dilation,
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
**kwargs))
Expand Down
6 changes: 4 additions & 2 deletions seg/mmseg/models/utils/involution_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride):
stride,
dilation=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.dilation = dilation
self.channels = channels
reduction_ratio = 4
self.group_channels = 16
Expand All @@ -278,5 +280,5 @@ def forward(self, x):
weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
b, c, h, w = weight.shape
weight = weight.view(b, self.groups, self.kernel_size, self.kernel_size, h, w)
out = _involution_cuda(x, weight, stride=self.stride, padding=(self.kernel_size-1)//2)
out = _involution_cuda(x, weight, stride=self.stride, padding=self.dilation * (self.kernel_size-1) // 2, dilation=self.dilation)
return out
6 changes: 4 additions & 2 deletions seg/mmseg/models/utils/involution_naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride):
stride,
dilation=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.dilation=dilation
self.channels = channels
reduction_ratio = 4
self.group_channels = 16
Expand All @@ -32,7 +34,7 @@ def __init__(self,
act_cfg=None)
if stride > 1:
self.avgpool = nn.AvgPool2d(stride, stride)
self.unfold = nn.Unfold(kernel_size, 1, (kernel_size-1)//2, stride)
self.unfold = nn.Unfold(kernel_size, dilation, dilation * (kernel_size-1) // 2, stride)

def forward(self, x):
weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
Expand Down