-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] Support hrnet frozen stage (#743)
* support hrnet frozen stage * support hrnet frozen stage
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from mmcv.utils.parrots_wrapper import _BatchNorm | ||
|
||
from mmseg.models.backbones import HRNet | ||
|
||
|
||
def test_hrnet_backbone(): | ||
# Test HRNET with two stage frozen | ||
|
||
extra = dict( | ||
stage1=dict( | ||
num_modules=1, | ||
num_branches=1, | ||
block='BOTTLENECK', | ||
num_blocks=(4, ), | ||
num_channels=(64, )), | ||
stage2=dict( | ||
num_modules=1, | ||
num_branches=2, | ||
block='BASIC', | ||
num_blocks=(4, 4), | ||
num_channels=(32, 64)), | ||
stage3=dict( | ||
num_modules=4, | ||
num_branches=3, | ||
block='BASIC', | ||
num_blocks=(4, 4, 4), | ||
num_channels=(32, 64, 128)), | ||
stage4=dict( | ||
num_modules=3, | ||
num_branches=4, | ||
block='BASIC', | ||
num_blocks=(4, 4, 4, 4), | ||
num_channels=(32, 64, 128, 256))) | ||
frozen_stages = 2 | ||
model = HRNet(extra, frozen_stages=frozen_stages) | ||
model.init_weights() | ||
model.train() | ||
assert model.norm1.training is False | ||
|
||
for layer in [model.conv1, model.norm1]: | ||
for param in layer.parameters(): | ||
assert param.requires_grad is False | ||
for i in range(1, frozen_stages + 1): | ||
if i == 1: | ||
layer = getattr(model, f'layer{i}') | ||
transition = getattr(model, f'transition{i}') | ||
elif i == 4: | ||
layer = getattr(model, f'stage{i}') | ||
else: | ||
layer = getattr(model, f'stage{i}') | ||
transition = getattr(model, f'transition{i}') | ||
|
||
for mod in layer.modules(): | ||
if isinstance(mod, _BatchNorm): | ||
assert mod.training is False | ||
for param in layer.parameters(): | ||
assert param.requires_grad is False | ||
|
||
for mod in transition.modules(): | ||
if isinstance(mod, _BatchNorm): | ||
assert mod.training is False | ||
for param in transition.parameters(): | ||
assert param.requires_grad is False |