Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Rename init_weight to init_weights #971

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions mmcv/runner/base_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def __init__(self, init_cfg=None):
def is_init(self):
return self._is_init

def init_weight(self):
def init_weights(self):
"""Initialize the weights."""
from ..cnn import initialize

if not self._is_init:
if self.init_cfg:
initialize(self, self.init_cfg)
for m in self.children():
if hasattr(m, 'init_weight'):
m.init_weight()
if hasattr(m, 'init_weights'):
m.init_weights()
self._is_init = True
else:
warnings.warn(f'init_weight of {self.__class__.__name__} has '
warnings.warn(f'init_weights of {self.__class__.__name__} has '
f'been called more than once.')

def __repr__(self):
Expand Down
18 changes: 9 additions & 9 deletions tests/test_runner/test_basemodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_model_weight_init():
conv1d=dict(type='FooConv1d')))

model = build_from_cfg(model_cfg, FOOMODELS)
model.init_weight()
model.init_weights()

assert torch.equal(model.component1.conv1d.weight,
torch.full(model.component1.conv1d.weight.shape, 3.0))
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_nest_components_weight_init():
conv1d=dict(type='FooConv1d')))

model = build_from_cfg(model_cfg, FOOMODELS)
model.init_weight()
model.init_weights()

assert torch.equal(model.component1.conv1d.weight,
torch.full(model.component1.conv1d.weight.shape, 7.0))
Expand Down Expand Up @@ -243,7 +243,7 @@ def test_without_layer_weight_init():
component2=dict(type='FooConv2d'),
component3=dict(type='FooLinear'))
model = build_from_cfg(model_cfg, FOOMODELS)
model.init_weight()
model.init_weights()

assert torch.equal(model.component1.conv1d.weight,
torch.full(model.component1.conv1d.weight.shape, 3.0))
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_override_weight_init():
component1=dict(type='FooConv1d'),
component3=dict(type='FooLinear'))
model = build_from_cfg(model_cfg, FOOMODELS)
model.init_weight()
model.init_weights()
assert torch.equal(model.reg.weight,
torch.full(model.reg.weight.shape, 10.0))
assert torch.equal(model.reg.bias, torch.full(model.reg.bias.shape, 20.0))
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_override_weight_init():
component2=dict(type='FooConv2d'),
component3=dict(type='FooLinear'))
model = build_from_cfg(model_cfg, FOOMODELS)
model.init_weight()
model.init_weights()

assert torch.equal(model.reg.weight,
torch.full(model.reg.weight.shape, 30.0))
Expand All @@ -326,7 +326,7 @@ def test_sequential_model_weight_init():
]
layers = [build_from_cfg(cfg, COMPONENTS) for cfg in seq_model_cfg]
seq_model = Sequential(*layers)
seq_model.init_weight()
seq_model.init_weights()
assert torch.equal(seq_model[0].conv1d.weight,
torch.full(seq_model[0].conv1d.weight.shape, 0.))
assert torch.equal(seq_model[0].conv1d.bias,
Expand All @@ -341,7 +341,7 @@ def test_sequential_model_weight_init():
*layers,
init_cfg=dict(
type='Constant', layer=['Conv1d', 'Conv2d'], val=4., bias=5.))
seq_model.init_weight()
seq_model.init_weights()
assert torch.equal(seq_model[0].conv1d.weight,
torch.full(seq_model[0].conv1d.weight.shape, 0.))
assert torch.equal(seq_model[0].conv1d.bias,
Expand All @@ -363,7 +363,7 @@ def test_modulelist_weight_init():
]
layers = [build_from_cfg(cfg, COMPONENTS) for cfg in models_cfg]
modellist = ModuleList(layers)
modellist.init_weight()
modellist.init_weights()
assert torch.equal(modellist[0].conv1d.weight,
torch.full(modellist[0].conv1d.weight.shape, 0.))
assert torch.equal(modellist[0].conv1d.bias,
Expand All @@ -378,7 +378,7 @@ def test_modulelist_weight_init():
layers,
init_cfg=dict(
type='Constant', layer=['Conv1d', 'Conv2d'], val=4., bias=5.))
modellist.init_weight()
modellist.init_weights()
assert torch.equal(modellist[0].conv1d.weight,
torch.full(modellist[0].conv1d.weight.shape, 0.))
assert torch.equal(modellist[0].conv1d.bias,
Expand Down