|
import pytest |
|
import torch |
|
|
|
from mmdet.models.backbones import Res2Net |
|
from mmdet.models.backbones.res2net import Bottle2neck |
|
from .utils import is_block |
|
|
|
|
|
def test_res2net_bottle2neck(): |
|
with pytest.raises(AssertionError): |
|
|
|
Bottle2neck(64, 64, base_width=26, scales=4, style='tensorflow') |
|
|
|
with pytest.raises(AssertionError): |
|
|
|
Bottle2neck(64, 64, base_width=26, scales=1, style='pytorch') |
|
|
|
|
|
block = Bottle2neck( |
|
64, 64, base_width=26, stride=2, scales=4, style='pytorch') |
|
assert block.scales == 4 |
|
|
|
|
|
dcn = dict(type='DCN', deform_groups=1, fallback_on_stride=False) |
|
with pytest.raises(AssertionError): |
|
|
|
Bottle2neck( |
|
64, |
|
64, |
|
base_width=26, |
|
scales=4, |
|
dcn=dcn, |
|
conv_cfg=dict(type='Conv')) |
|
Bottle2neck(64, 64, dcn=dcn) |
|
|
|
|
|
block = Bottle2neck(64, 16, base_width=26, scales=4) |
|
x = torch.randn(1, 64, 56, 56) |
|
x_out = block(x) |
|
assert x_out.shape == torch.Size([1, 64, 56, 56]) |
|
|
|
|
|
def test_res2net_backbone(): |
|
with pytest.raises(KeyError): |
|
|
|
Res2Net(depth=18) |
|
|
|
|
|
model = Res2Net(depth=50, scales=4, base_width=26) |
|
for m in model.modules(): |
|
if is_block(m): |
|
assert m.scales == 4 |
|
model.init_weights() |
|
model.train() |
|
|
|
imgs = torch.randn(1, 3, 224, 224) |
|
feat = model(imgs) |
|
assert len(feat) == 4 |
|
assert feat[0].shape == torch.Size([1, 256, 56, 56]) |
|
assert feat[1].shape == torch.Size([1, 512, 28, 28]) |
|
assert feat[2].shape == torch.Size([1, 1024, 14, 14]) |
|
assert feat[3].shape == torch.Size([1, 2048, 7, 7]) |
|
|