Skip to content

Commit

Permalink
make weight initialization optional to speed vgg-construction (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyuntc authored and alykhantejani committed Jan 2, 2018
1 parent 2b2aa9c commit 005bc47
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions torchvision/models/vgg.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class VGG(nn.Module):

def __init__(self, features, num_classes=1000):
def __init__(self, features, num_classes=1000, init_weights=True):
super(VGG, self).__init__()
self.features = features
self.classifier = nn.Sequential(
Expand All @@ -35,7 +35,8 @@ def __init__(self, features, num_classes=1000):
nn.Dropout(),
nn.Linear(4096, num_classes),
)
self._initialize_weights()
if init_weights:
self._initialize_weights()

def forward(self, x):
x = self.features(x)
Expand Down Expand Up @@ -88,6 +89,8 @@ def vgg11(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['A']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg11']))
Expand All @@ -100,6 +103,8 @@ def vgg11_bn(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['A'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg11_bn']))
Expand All @@ -112,6 +117,8 @@ def vgg13(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['B']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg13']))
Expand All @@ -124,6 +131,8 @@ def vgg13_bn(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['B'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg13_bn']))
Expand All @@ -136,6 +145,8 @@ def vgg16(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['D']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg16']))
Expand All @@ -148,6 +159,8 @@ def vgg16_bn(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['D'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg16_bn']))
Expand All @@ -160,6 +173,8 @@ def vgg19(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['E']), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg19']))
Expand All @@ -172,6 +187,8 @@ def vgg19_bn(pretrained=False, **kwargs):
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
if pretrained:
kwargs['init_weights'] = False
model = VGG(make_layers(cfg['E'], batch_norm=True), **kwargs)
if pretrained:
model.load_state_dict(model_zoo.load_url(model_urls['vgg19_bn']))
Expand Down

0 comments on commit 005bc47

Please sign in to comment.