Skip to content

Commit ddce375

Browse files
authored
smaller input & channels of unittest (#1004)
1 parent 349fc2d commit ddce375

35 files changed

+425
-409
lines changed

tests/test_models/test_backbones/test_bisenetv1.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ def test_bisenetv1_backbone():
2525
model.init_weights()
2626
model.train()
2727
batch_size = 2
28-
imgs = torch.randn(batch_size, 3, 256, 512)
28+
imgs = torch.randn(batch_size, 3, 64, 128)
2929
feat = model(imgs)
3030

3131
assert len(feat) == 3
3232
# output for segment Head
33-
assert feat[0].shape == torch.Size([batch_size, 256, 32, 64])
33+
assert feat[0].shape == torch.Size([batch_size, 256, 8, 16])
3434
# for auxiliary head 1
35-
assert feat[1].shape == torch.Size([batch_size, 128, 32, 64])
35+
assert feat[1].shape == torch.Size([batch_size, 128, 8, 16])
3636
# for auxiliary head 2
37-
assert feat[2].shape == torch.Size([batch_size, 128, 16, 32])
37+
assert feat[2].shape == torch.Size([batch_size, 128, 4, 8])
3838

3939
# Test input with rare shape
4040
batch_size = 2
41-
imgs = torch.randn(batch_size, 3, 527, 279)
41+
imgs = torch.randn(batch_size, 3, 95, 27)
4242
feat = model(imgs)
4343
assert len(feat) == 3
4444

@@ -47,20 +47,20 @@ def test_bisenetv1_backbone():
4747
BiSeNetV1(
4848
backbone_cfg=backbone_cfg,
4949
in_channels=3,
50-
spatial_channels=(64, 64, 64))
50+
spatial_channels=(16, 16, 16))
5151

5252
with pytest.raises(AssertionError):
5353
# BiSeNetV1 context path constraints.
5454
BiSeNetV1(
5555
backbone_cfg=backbone_cfg,
5656
in_channels=3,
57-
context_channels=(128, 256, 512, 1024))
57+
context_channels=(16, 32, 64, 128))
5858

5959

6060
def test_bisenetv1_spatial_path():
6161
with pytest.raises(AssertionError):
6262
# BiSeNetV1 spatial path channel constraints.
63-
SpatialPath(num_channels=(64, 64, 64), in_channels=3)
63+
SpatialPath(num_channels=(16, 16, 16), in_channels=3)
6464

6565

6666
def test_bisenetv1_context_path():
@@ -79,31 +79,31 @@ def test_bisenetv1_context_path():
7979
with pytest.raises(AssertionError):
8080
# BiSeNetV1 context path constraints.
8181
ContextPath(
82-
backbone_cfg=backbone_cfg, context_channels=(128, 256, 512, 1024))
82+
backbone_cfg=backbone_cfg, context_channels=(16, 32, 64, 128))
8383

8484

8585
def test_bisenetv1_attention_refinement_module():
86-
x_arm = AttentionRefinementModule(256, 64)
87-
assert x_arm.conv_layer.in_channels == 256
88-
assert x_arm.conv_layer.out_channels == 64
86+
x_arm = AttentionRefinementModule(32, 8)
87+
assert x_arm.conv_layer.in_channels == 32
88+
assert x_arm.conv_layer.out_channels == 8
8989
assert x_arm.conv_layer.kernel_size == (3, 3)
90-
x = torch.randn(2, 256, 32, 64)
90+
x = torch.randn(2, 32, 8, 16)
9191
x_out = x_arm(x)
92-
assert x_out.shape == torch.Size([2, 64, 32, 64])
92+
assert x_out.shape == torch.Size([2, 8, 8, 16])
9393

9494

9595
def test_bisenetv1_feature_fusion_module():
96-
ffm = FeatureFusionModule(128, 256)
97-
assert ffm.conv1.in_channels == 128
98-
assert ffm.conv1.out_channels == 256
96+
ffm = FeatureFusionModule(16, 32)
97+
assert ffm.conv1.in_channels == 16
98+
assert ffm.conv1.out_channels == 32
9999
assert ffm.conv1.kernel_size == (1, 1)
100100
assert ffm.gap.output_size == (1, 1)
101-
assert ffm.conv_atten[0].in_channels == 256
102-
assert ffm.conv_atten[0].out_channels == 256
101+
assert ffm.conv_atten[0].in_channels == 32
102+
assert ffm.conv_atten[0].out_channels == 32
103103
assert ffm.conv_atten[0].kernel_size == (1, 1)
104104

105-
ffm = FeatureFusionModule(128, 128)
106-
x1 = torch.randn(2, 64, 64, 128)
107-
x2 = torch.randn(2, 64, 64, 128)
105+
ffm = FeatureFusionModule(16, 16)
106+
x1 = torch.randn(2, 8, 8, 16)
107+
x2 = torch.randn(2, 8, 8, 16)
108108
x_out = ffm(x1, x2)
109-
assert x_out.shape == torch.Size([2, 128, 64, 128])
109+
assert x_out.shape == torch.Size([2, 16, 8, 16])

tests/test_models/test_backbones/test_bisenetv2.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@ def test_bisenetv2_backbone():
1313
model.init_weights()
1414
model.train()
1515
batch_size = 2
16-
imgs = torch.randn(batch_size, 3, 512, 1024)
16+
imgs = torch.randn(batch_size, 3, 128, 256)
1717
feat = model(imgs)
1818

1919
assert len(feat) == 5
2020
# output for segment Head
21-
assert feat[0].shape == torch.Size([batch_size, 128, 64, 128])
21+
assert feat[0].shape == torch.Size([batch_size, 128, 16, 32])
2222
# for auxiliary head 1
23-
assert feat[1].shape == torch.Size([batch_size, 16, 128, 256])
23+
assert feat[1].shape == torch.Size([batch_size, 16, 32, 64])
2424
# for auxiliary head 2
25-
assert feat[2].shape == torch.Size([batch_size, 32, 64, 128])
25+
assert feat[2].shape == torch.Size([batch_size, 32, 16, 32])
2626
# for auxiliary head 3
27-
assert feat[3].shape == torch.Size([batch_size, 64, 32, 64])
27+
assert feat[3].shape == torch.Size([batch_size, 64, 8, 16])
2828
# for auxiliary head 4
29-
assert feat[4].shape == torch.Size([batch_size, 128, 16, 32])
29+
assert feat[4].shape == torch.Size([batch_size, 128, 4, 8])
3030

3131
# Test input with rare shape
3232
batch_size = 2
33-
imgs = torch.randn(batch_size, 3, 527, 952)
33+
imgs = torch.randn(batch_size, 3, 95, 27)
3434
feat = model(imgs)
3535
assert len(feat) == 5
3636

3737

3838
def test_bisenetv2_DetailBranch():
39-
x = torch.randn(1, 3, 512, 1024)
40-
detail_branch = DetailBranch(detail_channels=(64, 64, 128))
39+
x = torch.randn(1, 3, 32, 64)
40+
detail_branch = DetailBranch(detail_channels=(64, 16, 32))
4141
assert isinstance(detail_branch.detail_branch[0][0], ConvModule)
4242
x_out = detail_branch(x)
43-
assert x_out.shape == torch.Size([1, 128, 64, 128])
43+
assert x_out.shape == torch.Size([1, 32, 4, 8])
4444

4545

4646
def test_bisenetv2_SemanticBranch():
@@ -49,9 +49,9 @@ def test_bisenetv2_SemanticBranch():
4949

5050

5151
def test_bisenetv2_BGALayer():
52-
x_a = torch.randn(1, 128, 64, 128)
53-
x_b = torch.randn(1, 128, 16, 32)
54-
bga = BGALayer()
52+
x_a = torch.randn(1, 8, 8, 16)
53+
x_b = torch.randn(1, 8, 2, 4)
54+
bga = BGALayer(out_channels=8)
5555
assert isinstance(bga.conv, ConvModule)
5656
x_out = bga(x_a, x_b)
57-
assert x_out.shape == torch.Size([1, 128, 64, 128])
57+
assert x_out.shape == torch.Size([1, 8, 8, 16])

tests/test_models/test_backbones/test_fast_scnn.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ def test_fastscnn_backbone():
1616
lower_in_channels=128)
1717

1818
# Test FastSCNN Standard Forward
19-
model = FastSCNN()
19+
model = FastSCNN(
20+
in_channels=3,
21+
downsample_dw_channels=(4, 6),
22+
global_in_channels=8,
23+
global_block_channels=(8, 12, 16),
24+
global_block_strides=(2, 2, 1),
25+
global_out_channels=16,
26+
higher_in_channels=8,
27+
lower_in_channels=16,
28+
fusion_out_channels=16,
29+
)
2030
model.init_weights()
2131
model.train()
2232
batch_size = 4
23-
imgs = torch.randn(batch_size, 3, 512, 1024)
33+
imgs = torch.randn(batch_size, 3, 64, 128)
2434
feat = model(imgs)
2535

2636
assert len(feat) == 3
2737
# higher-res
28-
assert feat[0].shape == torch.Size([batch_size, 64, 64, 128])
38+
assert feat[0].shape == torch.Size([batch_size, 8, 8, 16])
2939
# lower-res
30-
assert feat[1].shape == torch.Size([batch_size, 128, 16, 32])
40+
assert feat[1].shape == torch.Size([batch_size, 16, 2, 4])
3141
# FFM output
32-
assert feat[2].shape == torch.Size([batch_size, 128, 64, 128])
42+
assert feat[2].shape == torch.Size([batch_size, 16, 8, 16])

tests/test_models/test_backbones/test_hrnet.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,21 @@ def test_hrnet_backbone():
9595
model.init_weights()
9696
model.train()
9797

98-
imgs = torch.randn(1, 3, 256, 256)
98+
imgs = torch.randn(1, 3, 64, 64)
9999
feats = model(imgs)
100100
assert len(feats) == 4
101-
assert feats[0].shape == torch.Size([1, 32, 64, 64])
102-
assert feats[3].shape == torch.Size([1, 256, 8, 8])
101+
assert feats[0].shape == torch.Size([1, 32, 16, 16])
102+
assert feats[3].shape == torch.Size([1, 256, 2, 2])
103103

104104
# Test single scale output
105105
model = HRNet(extra=extra, multiscale_output=False)
106106
model.init_weights()
107107
model.train()
108108

109-
imgs = torch.randn(1, 3, 256, 256)
109+
imgs = torch.randn(1, 3, 64, 64)
110110
feats = model(imgs)
111111
assert len(feats) == 1
112-
assert feats[0].shape == torch.Size([1, 32, 64, 64])
112+
assert feats[0].shape == torch.Size([1, 32, 16, 16])
113113

114114
# Test HRNET with two stage frozen
115115
frozen_stages = 2

tests/test_models/test_backbones/test_icnet.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,34 @@ def test_icnet_backbone():
1010
# Must give backbone dict in config file.
1111
ICNet(
1212
in_channels=3,
13-
layer_channels=(512, 2048),
14-
light_branch_middle_channels=32,
15-
psp_out_channels=512,
16-
out_channels=(64, 256, 256),
13+
layer_channels=(128, 512),
14+
light_branch_middle_channels=8,
15+
psp_out_channels=128,
16+
out_channels=(16, 128, 128),
1717
backbone_cfg=None)
1818

1919
# Test ICNet Standard Forward
2020
model = ICNet(
21+
layer_channels=(128, 512),
2122
backbone_cfg=dict(
2223
type='ResNetV1c',
2324
in_channels=3,
24-
depth=50,
25+
depth=18,
2526
num_stages=4,
2627
out_indices=(0, 1, 2, 3),
2728
dilations=(1, 1, 2, 4),
2829
strides=(1, 2, 1, 1),
2930
norm_cfg=dict(type='BN', requires_grad=True),
3031
norm_eval=False,
3132
style='pytorch',
32-
contract_dilation=True), )
33+
contract_dilation=True),
34+
)
3335
assert hasattr(model.backbone,
3436
'maxpool') and model.backbone.maxpool.ceil_mode is True
3537
model.init_weights()
3638
model.train()
3739
batch_size = 2
38-
imgs = torch.randn(batch_size, 3, 512, 1024)
40+
imgs = torch.randn(batch_size, 3, 32, 64)
3941
feat = model(imgs)
4042

4143
assert model.psp_modules[0][0].output_size == 1
@@ -45,4 +47,4 @@ def test_icnet_backbone():
4547
assert model.conv_sub1[0].padding == 1
4648

4749
assert len(feat) == 3
48-
assert feat[0].shape == torch.Size([batch_size, 64, 64, 128])
50+
assert feat[0].shape == torch.Size([batch_size, 64, 4, 8])

tests/test_models/test_backbones/test_mit.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_mit():
2424
assert outs[3].shape == (1, 256, H // 32, W // 32)
2525

2626
# Test non-squared input
27-
H, W = (224, 320)
27+
H, W = (224, 256)
2828
temp = torch.randn((1, 3, H, W))
2929
outs = model(temp)
3030
assert outs[0].shape == (1, 32, H // 4, W // 4)
@@ -33,25 +33,25 @@ def test_mit():
3333
assert outs[3].shape == (1, 256, H // 32, W // 32)
3434

3535
# Test MixFFN
36-
FFN = MixFFN(128, 512)
36+
FFN = MixFFN(64, 128)
3737
hw_shape = (32, 32)
3838
token_len = 32 * 32
39-
temp = torch.randn((1, token_len, 128))
39+
temp = torch.randn((1, token_len, 64))
4040
# Self identity
4141
out = FFN(temp, hw_shape)
42-
assert out.shape == (1, token_len, 128)
42+
assert out.shape == (1, token_len, 64)
4343
# Out identity
4444
outs = FFN(temp, hw_shape, temp)
45-
assert out.shape == (1, token_len, 128)
45+
assert out.shape == (1, token_len, 64)
4646

4747
# Test EfficientMHA
48-
MHA = EfficientMultiheadAttention(128, 2)
48+
MHA = EfficientMultiheadAttention(64, 2)
4949
hw_shape = (32, 32)
5050
token_len = 32 * 32
51-
temp = torch.randn((1, token_len, 128))
51+
temp = torch.randn((1, token_len, 64))
5252
# Self identity
5353
out = MHA(temp, hw_shape)
54-
assert out.shape == (1, token_len, 128)
54+
assert out.shape == (1, token_len, 64)
5555
# Out identity
5656
outs = MHA(temp, hw_shape, temp)
57-
assert out.shape == (1, token_len, 128)
57+
assert out.shape == (1, token_len, 64)

tests/test_models/test_backbones/test_mobilenet_v3.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ def test_mobilenet_v3():
3232
model.init_weights()
3333
model.train()
3434

35-
imgs = torch.randn(2, 3, 224, 224)
35+
imgs = torch.randn(2, 3, 56, 56)
3636
feat = model(imgs)
3737
assert len(feat) == 3
38-
assert feat[0].shape == (2, 16, 112, 112)
39-
assert feat[1].shape == (2, 16, 56, 56)
40-
assert feat[2].shape == (2, 576, 28, 28)
38+
assert feat[0].shape == (2, 16, 28, 28)
39+
assert feat[1].shape == (2, 16, 14, 14)
40+
assert feat[2].shape == (2, 576, 7, 7)
4141

4242
# Test MobileNetV3 with arch = 'large'
4343
model = MobileNetV3(arch='large', out_indices=(1, 3, 16))
4444
model.init_weights()
4545
model.train()
4646

47-
imgs = torch.randn(2, 3, 224, 224)
47+
imgs = torch.randn(2, 3, 56, 56)
4848
feat = model(imgs)
4949
assert len(feat) == 3
50-
assert feat[0].shape == (2, 16, 112, 112)
51-
assert feat[1].shape == (2, 24, 56, 56)
52-
assert feat[2].shape == (2, 960, 28, 28)
50+
assert feat[0].shape == (2, 16, 28, 28)
51+
assert feat[1].shape == (2, 24, 14, 14)
52+
assert feat[2].shape == (2, 960, 7, 7)
5353

5454
# Test MobileNetV3 with norm_eval True, with_cp True and frozen_stages=5
5555
model = MobileNetV3(norm_eval=True, with_cp=True, frozen_stages=5)
@@ -59,9 +59,9 @@ def test_mobilenet_v3():
5959
model.init_weights()
6060
model.train()
6161

62-
imgs = torch.randn(2, 3, 224, 224)
62+
imgs = torch.randn(2, 3, 56, 56)
6363
feat = model(imgs)
6464
assert len(feat) == 3
65-
assert feat[0].shape == (2, 16, 112, 112)
66-
assert feat[1].shape == (2, 16, 56, 56)
67-
assert feat[2].shape == (2, 576, 28, 28)
65+
assert feat[0].shape == (2, 16, 28, 28)
66+
assert feat[1].shape == (2, 16, 14, 14)
67+
assert feat[2].shape == (2, 576, 7, 7)

0 commit comments

Comments
 (0)