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

[Feature] Support FastFCN #885

Merged
merged 25 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
51 changes: 51 additions & 0 deletions configs/_base_/models/fastfcn_r50-d32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# model settings
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
type='EncoderDecoder',
pretrained='open-mmlab://resnet50_v1c',
backbone=dict(
type='ResNetV1c',
depth=50,
num_stages=4,
dilations=(1, 1, 1, 1),
strides=(1, 2, 2, 2),
norm_cfg=norm_cfg,
norm_eval=False,
style='pytorch',
contract_dilation=True),
neck=dict(
type='JPU',
in_channels=(256, 512, 1024, 2048),
start_level=1,
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
end_level=-1,
dilations=(1, 2, 4, 8),
align_corners=False,
out_indices=(0, 1, 2, 3),
norm_cfg=norm_cfg),
decode_head=dict(
type='PSPHead',
in_channels=2048,
in_index=3,
channels=512,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
auxiliary_head=dict(
type='FCNHead',
in_channels=1024,
in_index=2,
channels=256,
num_convs=1,
concat_input=False,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=0.4)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
23 changes: 23 additions & 0 deletions configs/fastfcn/fastfcn_jpu_deeplabv3_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# model settings
_base_ = './fastfcn_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
decode_head=dict(
type='ASPPHead',
in_channels=2048,
in_index=3,
channels=512,
dilations=(1, 12, 24, 36),
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
27 changes: 27 additions & 0 deletions configs/fastfcn/fastfcn_jpu_enc_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# model settings
_base_ = './fastfcn_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
decode_head=dict(
type='EncHead',
in_channels=[512, 1024, 2048],
in_index=(1, 2, 3),
channels=512,
num_codes=32,
use_se_loss=True,
add_lateral=False,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
loss_se_decode=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=0.2)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
8 changes: 8 additions & 0 deletions configs/fastfcn/fastfcn_jpu_psp_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
_base_ = [
'../_base_/models/fastfcn_r50-d32.py', '../_base_/datasets/cityscapes.py',
'../_base_/default_runtime.py', '../_base_/schedules/schedule_80k.py'
]
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# model settings
_base_ = './fastfcn_r50-d8_d1124_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 2, 2),
),
decode_head=dict(
type='ASPPHead',
in_channels=2048,
in_index=3,
channels=512,
dilations=(1, 12, 24, 36),
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
31 changes: 31 additions & 0 deletions configs/fastfcn/fastfcn_r50-d32_d1124_jpu_enc_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# model settings
_base_ = './fastfcn_r50-d8_d1124_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 2, 2),
),
decode_head=dict(
type='EncHead',
in_channels=[512, 1024, 2048],
in_index=(1, 2, 3),
channels=512,
num_codes=32,
use_se_loss=True,
add_lateral=False,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
loss_se_decode=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=0.2)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
12 changes: 12 additions & 0 deletions configs/fastfcn/fastfcn_r50-d32_d1124_jpu_psp_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_base_ = [
'../_base_/models/fastfcn_r50-d32.py', '../_base_/datasets/cityscapes.py',
'../_base_/default_runtime.py', '../_base_/schedules/schedule_80k.py'
]
model = dict(backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 2, 2),
))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# model settings
_base_ = './fastfcn_r50-d8_d1124_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 1, 1),
),
decode_head=dict(
type='ASPPHead',
in_channels=2048,
in_index=3,
channels=512,
dilations=(1, 12, 24, 36),
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
31 changes: 31 additions & 0 deletions configs/fastfcn/fastfcn_r50-d8_d1124_jpu_enc_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# model settings
_base_ = './fastfcn_r50-d8_d1124_jpu_psp_4x4_cityscapes.py'
norm_cfg = dict(type='SyncBN', requires_grad=True)
model = dict(
backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 1, 1),
),
decode_head=dict(
type='EncHead',
in_channels=[512, 1024, 2048],
in_index=(1, 2, 3),
channels=512,
num_codes=32,
use_se_loss=True,
add_lateral=False,
dropout_ratio=0.1,
num_classes=19,
norm_cfg=norm_cfg,
align_corners=False,
loss_decode=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
loss_se_decode=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=0.2)),
# model training and testing settings
train_cfg=dict(),
test_cfg=dict(mode='whole'))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
12 changes: 12 additions & 0 deletions configs/fastfcn/fastfcn_r50-d8_d1124_jpu_psp_4x4_cityscapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_base_ = [
'../_base_/models/fastfcn_r50-d32.py', '../_base_/datasets/cityscapes.py',
'../_base_/default_runtime.py', '../_base_/schedules/schedule_80k.py'
]
model = dict(backbone=dict(
dilations=(1, 1, 2, 4),
strides=(1, 2, 1, 1),
))
data = dict(
samples_per_gpu=4,
workers_per_gpu=4,
)
3 changes: 2 additions & 1 deletion mmseg/models/necks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .fpn import FPN
from .jpu import JPU
from .mla_neck import MLANeck
from .multilevel_neck import MultiLevelNeck

__all__ = ['FPN', 'MultiLevelNeck', 'MLANeck']
__all__ = ['FPN', 'MultiLevelNeck', 'MLANeck', 'JPU']
129 changes: 129 additions & 0 deletions mmseg/models/necks/jpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Copyright (c) OpenMMLab. All rights reserved.
import torch
import torch.nn as nn
from mmcv.cnn import ConvModule, DepthwiseSeparableConvModule
from mmcv.runner import BaseModule

from mmseg.ops import resize
from ..builder import NECKS


@NECKS.register_module()
class JPU(BaseModule):
"""FastFCN: Rethinking Dilated Convolution in the Backbone
for Semantic Segmentation.

This Joint Pyramid Upsampling (JPU) neck is the implementation of
`FastFCN <https://arxiv.org/abs/1903.11816>`_.

Args:
in_channels (Tuple[int], optional): The number of input channels
for each convolution operations before upsampling.
Default: (256, 512, 1024, 2048).
start_level (int): Index of the start input backbone level used to
build the feature pyramid. Default: 1.
end_level (int): Index of the end input backbone level (exclusive) to
build the feature pyramid. Default: -1, which means the last level.
dilations (tuple[int]): Dilation rate of each Depthwise
Separable ConvModule.
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
out_indices (Tuple[int] | int, optional): Output from which stages.
Default: (0, 1, 2, 3).
align_corners (bool, optional): The align_corners argument of
resize operation. Default: False.
conv_cfg (dict | None): Config of conv layers.
Default: None.
norm_cfg (dict | None): Config of norm layers.
Default: dict(type='BN').
act_cfg (dict): Config of activation layers.
Default: dict(type='ReLU').
init_cfg (dict or list[dict], optional): Initialization config dict.
Default: None.
"""

def __init__(self,
in_channels=(256, 512, 1024, 2048),
start_level=1,
end_level=-1,
dilations=(1, 2, 4, 8),
out_indices=(0, 1, 2, 3),
align_corners=False,
conv_cfg=None,
norm_cfg=dict(type='BN'),
act_cfg=dict(type='ReLU'),
init_cfg=None):
super(JPU, self).__init__(init_cfg=init_cfg)
assert isinstance(in_channels, tuple)
assert isinstance(dilations, tuple)
self.in_channels = in_channels
self.out_channels = in_channels[start_level]
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
self.start_level = start_level
self.num_ins = len(in_channels)
if end_level == -1:
self.backbone_end_level = self.num_ins
else:
self.backbone_end_level = end_level
assert end_level <= len(in_channels)

self.dilations = dilations
self.align_corners = align_corners
self.out_indices = out_indices

MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
self.conv_layers = nn.ModuleList()
self.dilation_layers = nn.ModuleList()
for i in range(self.start_level, self.backbone_end_level):
conv_layer = nn.Sequential(
ConvModule(
self.in_channels[i],
self.out_channels,
kernel_size=3,
padding=1,
bias=False,
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
conv_cfg=conv_cfg,
norm_cfg=norm_cfg,
act_cfg=act_cfg))
self.conv_layers.append(conv_layer)
for i in range(len(dilations)):
dilation_layer = nn.Sequential(
DepthwiseSeparableConvModule(
in_channels=(len(in_channels) - 1) * self.out_channels,
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
out_channels=self.out_channels,
kernel_size=3,
stride=1,
padding=dilations[i],
dilation=dilations[i],
dw_norm_cfg=norm_cfg,
dw_act_cfg=None,
pw_norm_cfg=norm_cfg,
pw_act_cfg=act_cfg))
self.dilation_layers.append(dilation_layer)

def forward(self, inputs):
"""Forward function."""
assert len(inputs) == len(self.in_channels), 'Length of inputs must \
be the same with self.in_channels!'

feats = [
self.conv_layers[i](inputs[i + 1])
for i in range(len(self.in_channels) - 1)
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
]

_, _, h, w = feats[0].size()
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(feats) - 1):
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
feats[i + 1] = resize(
feats[i + 1],
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
size=(h, w),
mode='bilinear',
align_corners=self.align_corners)

feat = torch.cat(feats[::-1], dim=1)
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
feat = torch.cat([
self.dilation_layers[i](feat) for i in range(len(self.dilations))
],
dim=1)

outs = []
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(inputs) - 1):
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
outs.append(inputs[i])
outs.append(feat)
outs = [outs[i] for i in self.out_indices]
MengzhangLI marked this conversation as resolved.
Show resolved Hide resolved
return tuple(outs)
Loading