本文整理匯總了Python中torchvision.models.vgg.cfg方法的典型用法代碼示例。如果您正苦於以下問題:Python vgg.cfg方法的具體用法?Python vgg.cfg怎麽用?Python vgg.cfg使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torchvision.models.vgg
的用法示例。
在下文中一共展示了vgg.cfg方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: make_layers
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def make_layers(cfg, batch_norm=False):
"""This is almost verbatim from torchvision.models.vgg, except that the
MaxPool2d modules are configured with ceil_mode=True.
"""
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers.append(nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
modules = [conv2d, nn.ReLU(inplace=True)]
if batch_norm:
modules.insert(1, nn.BatchNorm2d(v))
layers.extend(modules)
in_channels = v
return nn.Sequential(*layers)
示例2: make_layers
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def make_layers(cfg, batch_norm=False):
"""This is almost verbatim from torchvision.models.vgg, except that the
MaxPool2d modules are configured with ceil_mode=True.
"""
layers = []
in_channels = 3
for v in cfg:
if v == 'M':
layers.append(nn.MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
modules = [conv2d, nn.ReLU(inplace=True)]
if batch_norm:
modules.insert(1, nn.BatchNorm2d(v))
layers.extend(modules)
in_channels = v
return nn.Sequential(*layers)
示例3: make_layers
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def make_layers(config_channels, cfg, batch_norm=False):
features = []
for v in cfg:
if v == 'M':
features += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(config_channels.channels, config_channels(v, 'features.%d.weight' % len(features)), kernel_size=3, padding=1)
if batch_norm:
features += [conv2d, nn.BatchNorm2d(config_channels.channels), nn.ReLU(inplace=True)]
else:
features += [conv2d, nn.ReLU(inplace=True)]
return nn.Sequential(*features)
示例4: vgg11
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg11(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['A']))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg11']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例5: vgg11_bn
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg11_bn(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['A'], batch_norm=True))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg11_bn']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例6: vgg13
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg13(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['B']))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg13']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例7: vgg13_bn
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg13_bn(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['B'], batch_norm=True))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg13_bn']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例8: vgg16_bn
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg16_bn(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['D'], batch_norm=True))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg16_bn']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例9: vgg19
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg19(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['E']))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg19']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例10: vgg19_bn
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg19_bn(config_channels, anchors, num_cls):
model = VGG(config_channels, anchors, num_cls, make_layers(config_channels, cfg['E'], batch_norm=True))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg19_bn']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model
示例11: __init__
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def __init__(self, num_cls=19, pretrained=True, weights_init=None,
output_last_ft=False):
super().__init__()
self.output_last_ft = output_last_ft
self.vgg = make_layers(vgg.cfg['D'])
self.vgg_head = nn.Sequential(
nn.Conv2d(512, 4096, 7),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5),
nn.Conv2d(4096, 4096, 1),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5),
nn.Conv2d(4096, num_cls, 1)
)
self.upscore2 = self.upscore_pool4 = Bilinear(2, num_cls)
self.upscore8 = Bilinear(8, num_cls)
self.score_pool4 = nn.Conv2d(512, num_cls, 1)
for param in self.score_pool4.parameters():
init.constant(param, 0)
self.score_pool3 = nn.Conv2d(256, num_cls, 1)
for param in self.score_pool3.parameters():
init.constant(param, 0)
if pretrained:
if weights_init is not None:
self.load_weights(torch.load(weights_init))
else:
self.load_base_weights()
示例12: make_layers
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def make_layers(cfg, in_channels=3, batch_norm=False):
layers = []
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=1)
if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
return nn.Sequential(*layers)
示例13: get_vgg
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def get_vgg(in_channels=3, **kwargs):
model = VGG(make_layers(cfg['D'], in_channels), **kwargs)
return model
示例14: __init__
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def __init__(self, num_cls=19, pretrained=True, weights_init=None,
output_last_ft=False):
super().__init__()
self.output_last_ft = output_last_ft
if weights_init:
batch_norm = False
else:
batch_norm = True
self.vgg = make_layers(vgg.cfg['D'], batch_norm=False)
self.vgg_head = nn.Sequential(
nn.Conv2d(512, 4096, 7),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5),
nn.Conv2d(4096, 4096, 1),
nn.ReLU(inplace=True),
nn.Dropout2d(p=0.5),
nn.Conv2d(4096, num_cls, 1)
)
self.upscore2 = self.upscore_pool4 = Bilinear(2, num_cls)
self.upscore8 = Bilinear(8, num_cls)
self.score_pool4 = nn.Conv2d(512, num_cls, 1)
for param in self.score_pool4.parameters():
# init.constant(param, 0)
init.constant_(param, 0)
self.score_pool3 = nn.Conv2d(256, num_cls, 1)
for param in self.score_pool3.parameters():
# init.constant(param, 0)
init.constant_(param, 0)
if pretrained:
if weights_init is not None:
self.load_weights(torch.load(weights_init))
else:
self.load_base_weights()
示例15: vgg11
# 需要導入模塊: from torchvision.models import vgg [as 別名]
# 或者: from torchvision.models.vgg import cfg [as 別名]
def vgg11(config_channels):
model = VGG(config_channels, make_layers(config_channels, cfg['A']))
if config_channels.config.getboolean('model', 'pretrained'):
url = model_urls['vgg11']
logging.info('use pretrained model: ' + url)
state_dict = model.state_dict()
for key, value in model_zoo.load_url(url).items():
if key in state_dict:
state_dict[key] = value
model.load_state_dict(state_dict)
return model