本文整理汇总了Python中torchvision.models.resnet.ResNet方法的典型用法代码示例。如果您正苦于以下问题:Python resnet.ResNet方法的具体用法?Python resnet.ResNet怎么用?Python resnet.ResNet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models.resnet
的用法示例。
在下文中一共展示了resnet.ResNet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def __init__(self, raw_model_dir, use_flow, logger):
super(BackboneModel, self).__init__()
self.use_flow = use_flow
model = ResNet(Bottleneck, [3, 4, 6, 3])
model.load_state_dict(
model_zoo.load_url(model_urls['resnet50'], model_dir=raw_model_dir))
logger.info('Model restored from pretrained resnet50')
self.feature = nn.Sequential(*list(model.children())[:-2])
self.base = list(self.feature.parameters())
if self.use_flow:
self.flow_branch = self.get_flow_branch(model)
self.rgb_branch = nn.Sequential(model.conv1, model.bn1, model.relu, model.maxpool)
self.fuse_branch = nn.Sequential(*list(model.children())[4:-2])
self.fea_dim = model.fc.in_features
示例2: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def __init__(self, block, layers, dim=128, **kwargs):
"""Initializes original ResNet and overwrites fully connected layer."""
super(TriNet, self).__init__(block, layers, 1) # 0 classes thows an error
batch_norm = nn.BatchNorm1d(1024)
self.avgpool = nn.AvgPool2d((8,4))
self.fc = nn.Sequential(
nn.Linear(512 * block.expansion, 1024),
batch_norm,
nn.ReLU(),
nn.Linear(1024, dim)
)
batch_norm.weight.data.fill_(1)
batch_norm.bias.data.zero_()
self.dim = dim
self.dimensions = {'emb': (self.dim, )}
示例3: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def __init__(self, block, layers, num_classes, dim=128, **kwargs):
"""Initializes original ResNet and overwrites fully connected layer."""
super().__init__(block, layers, 1) # 0 classes thows an error
#overwrite self.inplanes which is set by make_layer
self.inplanes = 256 * block.expansion
self.layer4 = self._make_dilated_layer4(DilatedBottleneck, 512, layers[3])
self.avgpool = nn.AvgPool2d((16, 8))
self.fc1 = nn.Linear(512 * block.expansion, 1024)
self.batch_norm = nn.BatchNorm1d(1024)
self.relu = nn.ReLU()
self.fc_emb = nn.Linear(1024, dim)
self.fc_soft = nn.Linear(1024, num_classes)
self.batch_norm.weight.data.fill_(1)
self.batch_norm.bias.data.zero_()
self.dim = dim
示例4: resnet18
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet18(config_channels, anchors, num_cls, **kwargs):
model = ResNet(config_channels, anchors, num_cls, BasicBlock, [2, 2, 2, 2], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet18']
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: resnet34
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet34(config_channels, anchors, num_cls, **kwargs):
model = ResNet(config_channels, anchors, num_cls, BasicBlock, [3, 4, 6, 3], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet34']
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: resnet50
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet50(config_channels, anchors, num_cls, **kwargs):
model = ResNet(config_channels, anchors, num_cls, Bottleneck, [3, 4, 6, 3], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet50']
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: resnet101
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet101(config_channels, anchors, num_cls, **kwargs):
model = ResNet(config_channels, anchors, num_cls, Bottleneck, [3, 4, 23, 3], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet101']
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: resnet152
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet152(config_channels, anchors, num_cls, **kwargs):
model = ResNet(config_channels, anchors, num_cls, Bottleneck, [3, 8, 36, 3], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet152']
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: ResNet18C4
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def ResNet18C4():
return ResNet(layers=[2, 2, 2, 2], bottleneck=vrn.BasicBlock, outputs=[4], url=vrn.model_urls['resnet18'])
示例10: ResNet34C4
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def ResNet34C4():
return ResNet(layers=[3, 4, 6, 3], bottleneck=vrn.BasicBlock, outputs=[4], url=vrn.model_urls['resnet34'])
示例11: _resnext
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def _resnext(arch, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
model.load_state_dict(state_dict)
return model
示例12: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def __init__(self, block, layers, num_classes=1000, drop_prob=0., block_size=5):
super(ResNet, self).__init__()
self.inplanes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.dropblock = LinearScheduler(
DropBlock2D(drop_prob=drop_prob, block_size=block_size),
start_value=0.,
stop_value=drop_prob,
nr_steps=5e3
)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
示例13: __init__
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def __init__(self, layers=[3, 4, 6, 3]):
block = resnet.BasicBlock
num_classes = 7
self.model = resnet.ResNet(block, layers, num_classes)
if torch.cuda.is_available():
self.model.cuda()
self.bestaccur = 0.0
示例14: resnet18
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet18(config_channels, **kwargs):
model = ResNet(config_channels, BasicBlock, [2, 2, 2, 2], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet18']
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
示例15: resnet34
# 需要导入模块: from torchvision.models import resnet [as 别名]
# 或者: from torchvision.models.resnet import ResNet [as 别名]
def resnet34(config_channels, **kwargs):
model = ResNet(config_channels, BasicBlock, [3, 4, 6, 3], **kwargs)
if config_channels.config.getboolean('model', 'pretrained'):
url = _model.model_urls['resnet34']
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