本文整理汇总了Python中torchvision.models.ResNet方法的典型用法代码示例。如果您正苦于以下问题:Python models.ResNet方法的具体用法?Python models.ResNet怎么用?Python models.ResNet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torchvision.models
的用法示例。
在下文中一共展示了models.ResNet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resnet_multiimage_input
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def resnet_multiimage_input(num_layers, pretrained=False, num_input_images=1):
"""Constructs a ResNet model.
Args:
num_layers (int): Number of resnet layers. Must be 18 or 50
pretrained (bool): If True, returns a model pre-trained on ImageNet
num_input_images (int): Number of frames stacked as input
"""
assert num_layers in [18, 50], "Can only run with 18 or 50 layer resnet"
blocks = {18: [2, 2, 2, 2], 50: [3, 4, 6, 3]}[num_layers]
block_type = {18: models.resnet.BasicBlock, 50: models.resnet.Bottleneck}[num_layers]
model = ResNetMultiImageInput(block_type, blocks, num_input_images=num_input_images)
if pretrained:
loaded = model_zoo.load_url(models.resnet.model_urls['resnet{}'.format(num_layers)])
loaded['conv1.weight'] = torch.cat(
[loaded['conv1.weight']] * num_input_images, 1) / num_input_images
model.load_state_dict(loaded)
return model
示例2: __init__
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def __init__(self, block, layers, output_dim):
super(ResNet, self).__init__(block, layers)
self.name = "ResNet"
self.avgpool = nn.AvgPool2d((8,4), stride=1)
self.fc = nn.Linear(512 * block.expansion, 1024)
self.bn_fc = nn.BatchNorm1d(1024)
self.relu_fc = nn.ReLU(inplace=True)
self.fc_out = nn.Linear(1024, output_dim)
for m in self.modules():
if isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
self.fc_compare = nn.Linear(output_dim, 1)
示例3: se_resnet18
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet18(num_classes=1_000):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBasicBlock, [2, 2, 2, 2], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例4: se_resnet34
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet34(num_classes=1_000):
"""Constructs a ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例5: se_resnet101
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet101(num_classes=1_000):
"""Constructs a ResNet-101 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBottleneck, [3, 4, 23, 3], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例6: se_resnet152
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet152(num_classes=1_000):
"""Constructs a ResNet-152 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBottleneck, [3, 8, 36, 3], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例7: se_resnet20
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet20(**kwargs):
"""Constructs a ResNet-18 model.
"""
model = CifarSEResNet(CifarSEBasicBlock, 3, **kwargs)
return model
示例8: se_resnet56
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet56(**kwargs):
"""Constructs a ResNet-34 model.
"""
model = CifarSEResNet(CifarSEBasicBlock, 9, **kwargs)
return model
示例9: se_preactresnet20
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_preactresnet20(**kwargs):
"""Constructs a ResNet-18 model.
"""
model = CifarSEPreActResNet(CifarSEBasicBlock, 3, **kwargs)
return model
示例10: se_preactresnet32
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_preactresnet32(**kwargs):
"""Constructs a ResNet-34 model.
"""
model = CifarSEPreActResNet(CifarSEBasicBlock, 5, **kwargs)
return model
示例11: se_preactresnet56
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_preactresnet56(**kwargs):
"""Constructs a ResNet-34 model.
"""
model = CifarSEPreActResNet(CifarSEBasicBlock, 9, **kwargs)
return model
示例12: conv3x3
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=False)
# SE-ResNet Module
示例13: se_resnet18
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet18(num_classes):
"""Constructs a ResNet-18 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBasicBlock, [2, 2, 2, 2], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例14: se_resnet34
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet34(num_classes):
"""Constructs a ResNet-34 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBasicBlock, [3, 4, 6, 3], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model
示例15: se_resnet50
# 需要导入模块: from torchvision import models [as 别名]
# 或者: from torchvision.models import ResNet [as 别名]
def se_resnet50(num_classes):
"""Constructs a ResNet-50 model.
Args:
pretrained (bool): If True, returns a model pre-trained on ImageNet
"""
model = ResNet(SEBottleneck, [3, 4, 6, 3], num_classes=num_classes)
model.avgpool = nn.AdaptiveAvgPool2d(1)
return model