2021-09-26 18:52:16 +08:00
|
|
|
# Copyright (c) OpenMMLab. All rights reserved.
|
|
|
|
import torch
|
|
|
|
from mmcv.cnn import ConvModule
|
|
|
|
|
|
|
|
from mmseg.models.backbones import BiSeNetV2
|
|
|
|
from mmseg.models.backbones.bisenetv2 import (BGALayer, DetailBranch,
|
|
|
|
SemanticBranch)
|
|
|
|
|
|
|
|
|
|
|
|
def test_bisenetv2_backbone():
|
|
|
|
# Test BiSeNetV2 Standard Forward
|
|
|
|
model = BiSeNetV2()
|
|
|
|
model.init_weights()
|
|
|
|
model.train()
|
|
|
|
batch_size = 2
|
2021-11-01 22:47:43 +08:00
|
|
|
imgs = torch.randn(batch_size, 3, 128, 256)
|
2021-09-26 18:52:16 +08:00
|
|
|
feat = model(imgs)
|
|
|
|
|
|
|
|
assert len(feat) == 5
|
|
|
|
# output for segment Head
|
2021-11-01 22:47:43 +08:00
|
|
|
assert feat[0].shape == torch.Size([batch_size, 128, 16, 32])
|
2021-09-26 18:52:16 +08:00
|
|
|
# for auxiliary head 1
|
2021-11-01 22:47:43 +08:00
|
|
|
assert feat[1].shape == torch.Size([batch_size, 16, 32, 64])
|
2021-09-26 18:52:16 +08:00
|
|
|
# for auxiliary head 2
|
2021-11-01 22:47:43 +08:00
|
|
|
assert feat[2].shape == torch.Size([batch_size, 32, 16, 32])
|
2021-09-26 18:52:16 +08:00
|
|
|
# for auxiliary head 3
|
2021-11-01 22:47:43 +08:00
|
|
|
assert feat[3].shape == torch.Size([batch_size, 64, 8, 16])
|
2021-09-26 18:52:16 +08:00
|
|
|
# for auxiliary head 4
|
2021-11-01 22:47:43 +08:00
|
|
|
assert feat[4].shape == torch.Size([batch_size, 128, 4, 8])
|
2021-09-26 18:52:16 +08:00
|
|
|
|
|
|
|
# Test input with rare shape
|
|
|
|
batch_size = 2
|
2021-11-01 22:47:43 +08:00
|
|
|
imgs = torch.randn(batch_size, 3, 95, 27)
|
2021-09-26 18:52:16 +08:00
|
|
|
feat = model(imgs)
|
|
|
|
assert len(feat) == 5
|
|
|
|
|
|
|
|
|
|
|
|
def test_bisenetv2_DetailBranch():
|
2021-11-01 22:47:43 +08:00
|
|
|
x = torch.randn(1, 3, 32, 64)
|
|
|
|
detail_branch = DetailBranch(detail_channels=(64, 16, 32))
|
2021-09-26 18:52:16 +08:00
|
|
|
assert isinstance(detail_branch.detail_branch[0][0], ConvModule)
|
|
|
|
x_out = detail_branch(x)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert x_out.shape == torch.Size([1, 32, 4, 8])
|
2021-09-26 18:52:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_bisenetv2_SemanticBranch():
|
|
|
|
semantic_branch = SemanticBranch(semantic_channels=(16, 32, 64, 128))
|
|
|
|
assert semantic_branch.stage1.pool.stride == 2
|
|
|
|
|
|
|
|
|
|
|
|
def test_bisenetv2_BGALayer():
|
2021-11-01 22:47:43 +08:00
|
|
|
x_a = torch.randn(1, 8, 8, 16)
|
|
|
|
x_b = torch.randn(1, 8, 2, 4)
|
|
|
|
bga = BGALayer(out_channels=8)
|
2021-09-26 18:52:16 +08:00
|
|
|
assert isinstance(bga.conv, ConvModule)
|
|
|
|
x_out = bga(x_a, x_b)
|
2021-11-01 22:47:43 +08:00
|
|
|
assert x_out.shape == torch.Size([1, 8, 8, 16])
|