Python torchvision.models.vgg.cfg() Examples

The following are 24 code examples of torchvision.models.vgg.cfg(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torchvision.models.vgg , or try the search function .
Example #1
Source File: fcn8s.py    From cycada_release with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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) 
Example #2
Source File: fcn8s.py    From MADAN with MIT License 6 votes vote down vote up
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) 
Example #3
Source File: fcn8s.py    From MADAN with MIT License 5 votes vote down vote up
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() 
Example #4
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def hand21(config_channels):
    cfg = [
        64, 64, 'M',
        128, 128, 'M',
        256, 256, 256, 256, 'M',
        512, 512, 512, 512,
        512, 512, 128,
    ]
    return VGG(config_channels, make_layers(config_channels, cfg)) 
Example #5
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def person18_19(config_channels):
    cfg = [
        64, 64, 'M',
        128, 128, 'M',
        256, 256, 256, 256, 'M',
        512, 512,
        256, 128,
    ]
    return VGG(config_channels, make_layers(config_channels, cfg)) 
Example #6
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg19_bn(config_channels):
    model = VGG(config_channels, 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 
Example #7
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg19(config_channels):
    model = VGG(config_channels, 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 
Example #8
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg16_bn(config_channels):
    model = VGG(config_channels, 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 
Example #9
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg13_bn(config_channels):
    model = VGG(config_channels, 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 
Example #10
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg13(config_channels):
    model = VGG(config_channels, 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 
Example #11
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
def vgg11_bn(config_channels):
    model = VGG(config_channels, 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 
Example #12
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #13
Source File: vgg.py    From openpose-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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) 
Example #14
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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) 
Example #15
Source File: get_cnn.py    From graph_distillation with Apache License 2.0 5 votes vote down vote up
def get_vgg(in_channels=3, **kwargs):
  model = VGG(make_layers(cfg['D'], in_channels), **kwargs)
  return model 
Example #16
Source File: get_cnn.py    From graph_distillation with Apache License 2.0 5 votes vote down vote up
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) 
Example #17
Source File: fcn8s.py    From cycada_release with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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() 
Example #18
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #19
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #20
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #21
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #22
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #23
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #24
Source File: vgg.py    From yolo2-pytorch with GNU Lesser General Public License v3.0 5 votes vote down vote up
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