[c1b1c5]: / ViTPose / tests / test_backbones / test_hrformer.py

Download this file

188 lines (165 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright (c) OpenMMLab. All rights reserved.
import pytest
import torch
from mmpose.models.backbones.hrformer import (HRFomerModule, HRFormer,
HRFormerBlock)
def test_hrformer_module():
norm_cfg = dict(type='BN')
block = HRFormerBlock
# Test multiscale forward
num_channles = (32, 64)
num_inchannels = [c * block.expansion for c in num_channles]
hrmodule = HRFomerModule(
num_branches=2,
block=block,
num_blocks=(2, 2),
num_inchannels=num_inchannels,
num_channels=num_channles,
num_heads=(1, 2),
num_window_sizes=(7, 7),
num_mlp_ratios=(4, 4),
drop_paths=(0., 0.),
norm_cfg=norm_cfg)
feats = [
torch.randn(1, num_inchannels[0], 64, 64),
torch.randn(1, num_inchannels[1], 32, 32)
]
feats = hrmodule(feats)
assert len(str(hrmodule)) > 0
assert len(feats) == 2
assert feats[0].shape == torch.Size([1, num_inchannels[0], 64, 64])
assert feats[1].shape == torch.Size([1, num_inchannels[1], 32, 32])
# Test single scale forward
num_channles = (32, 64)
in_channels = [c * block.expansion for c in num_channles]
hrmodule = HRFomerModule(
num_branches=2,
block=block,
num_blocks=(2, 2),
num_inchannels=num_inchannels,
num_channels=num_channles,
num_heads=(1, 2),
num_window_sizes=(7, 7),
num_mlp_ratios=(4, 4),
drop_paths=(0., 0.),
norm_cfg=norm_cfg,
multiscale_output=False,
)
feats = [
torch.randn(1, in_channels[0], 64, 64),
torch.randn(1, in_channels[1], 32, 32)
]
feats = hrmodule(feats)
assert len(feats) == 1
assert feats[0].shape == torch.Size([1, in_channels[0], 64, 64])
# Test single branch HRFormer module
hrmodule = HRFomerModule(
num_branches=1,
block=block,
num_blocks=(1, ),
num_inchannels=[num_inchannels[0]],
num_channels=[num_channles[0]],
num_heads=(1, ),
num_window_sizes=(7, ),
num_mlp_ratios=(4, ),
drop_paths=(0.1, ),
norm_cfg=norm_cfg,
)
feats = [
torch.randn(1, in_channels[0], 64, 64),
]
feats = hrmodule(feats)
assert len(feats) == 1
assert feats[0].shape == torch.Size([1, in_channels[0], 64, 64])
# Value tests
kwargs = dict(
num_branches=2,
block=block,
num_blocks=(2, 2),
num_inchannels=num_inchannels,
num_channels=num_channles,
num_heads=(1, 2),
num_window_sizes=(7, 7),
num_mlp_ratios=(4, 4),
drop_paths=(0.1, 0.1),
norm_cfg=norm_cfg,
)
with pytest.raises(ValueError):
# len(num_blocks) should equal num_branches
kwargs['num_blocks'] = [2, 2, 2]
HRFomerModule(**kwargs)
kwargs['num_blocks'] = [2, 2]
with pytest.raises(ValueError):
# len(num_blocks) should equal num_branches
kwargs['num_channels'] = [2]
HRFomerModule(**kwargs)
kwargs['num_channels'] = [2, 2]
with pytest.raises(ValueError):
# len(num_blocks) should equal num_branches
kwargs['num_inchannels'] = [2]
HRFomerModule(**kwargs)
kwargs['num_inchannels'] = [2, 2]
def test_hrformer_backbone():
norm_cfg = dict(type='BN')
# only have 3 stages
extra = dict(
drop_path_rate=0.2,
stage1=dict(
num_modules=1,
num_branches=1,
block='BOTTLENECK',
num_blocks=(2, ),
num_channels=(64, )),
stage2=dict(
num_modules=1,
num_branches=2,
block='HRFORMERBLOCK',
window_sizes=(7, 7),
num_heads=(1, 2),
mlp_ratios=(4, 4),
num_blocks=(2, 2),
num_channels=(32, 64)),
stage3=dict(
num_modules=4,
num_branches=3,
block='HRFORMERBLOCK',
window_sizes=(7, 7, 7),
num_heads=(1, 2, 4),
mlp_ratios=(4, 4, 4),
num_blocks=(2, 2, 2),
num_channels=(32, 64, 128)),
stage4=dict(
num_modules=3,
num_branches=4,
block='HRFORMERBLOCK',
window_sizes=(7, 7, 7, 7),
num_heads=(1, 2, 4, 8),
mlp_ratios=(4, 4, 4, 4),
num_blocks=(2, 2, 2, 2),
num_channels=(32, 64, 128, 256),
multiscale_output=True))
with pytest.raises(ValueError):
# len(num_blocks) should equal num_branches
extra['stage4']['num_branches'] = 3
HRFormer(extra=extra)
extra['stage4']['num_branches'] = 4
# Test HRFormer-S
model = HRFormer(extra=extra, norm_cfg=norm_cfg)
model.init_weights()
model.train()
imgs = torch.randn(1, 3, 64, 64)
feats = model(imgs)
assert len(feats) == 4
assert feats[0].shape == torch.Size([1, 32, 16, 16])
assert feats[3].shape == torch.Size([1, 256, 2, 2])
# Test single scale output and model
# without relative position bias
extra['stage4']['multiscale_output'] = False
extra['with_rpe'] = False
model = HRFormer(extra=extra, norm_cfg=norm_cfg)
model.init_weights()
model.train()
imgs = torch.randn(1, 3, 64, 64)
feats = model(imgs)
assert len(feats) == 1
assert feats[0].shape == torch.Size([1, 32, 16, 16])