Python torchvision.models.ResNet() Examples

The following are 30 code examples of torchvision.models.ResNet(). 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 , or try the search function .
Example #1
Source File: resnet.py    From tracking_wo_bnw with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #2
Source File: resnet_encoder.py    From DF-VO with MIT License 6 votes vote down vote up
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 
Example #3
Source File: resnet_encoder.py    From SC-SfMLearner-Release with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #4
Source File: resnet_encoder.py    From packnet-sfm with MIT License 6 votes vote down vote up
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 
Example #5
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet34(num_classes=1000):
    """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 
Example #6
Source File: resnet_earlyexit.py    From dnn-quant-ocs with Apache License 2.0 5 votes vote down vote up
def resnet50_earlyexit(**kwargs):
    """Constructs a ResNet-50 model.
    """
    model = ResNetEarlyExit(Bottleneck, [3, 4, 6, 3], **kwargs)
    return model 
Example #7
Source File: resnet_earlyexit.py    From dnn-quant-ocs with Apache License 2.0 5 votes vote down vote up
def resnet101_earlyexit(**kwargs):
    """Constructs a ResNet-101 model.
    """
    model = ResNetEarlyExit(Bottleneck, [3, 4, 23, 3], **kwargs)
    return model 
Example #8
Source File: resnet_earlyexit.py    From dnn-quant-ocs with Apache License 2.0 5 votes vote down vote up
def resnet152_earlyexit(**kwargs):
    """Constructs a ResNet-152 model.
    """
    model = ResNetEarlyExit(Bottleneck, [3, 8, 36, 3], **kwargs)
    return model 
Example #9
Source File: resnet.py    From tracking_wo_bnw with GNU General Public License v3.0 5 votes vote down vote up
def resnet50(pretrained=False, **kwargs):
    """Constructs a ResNet-50 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained:
        model.load_pretrained_dict(model_zoo.load_url(model_urls['resnet50']))
    return model 
Example #10
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet18(num_classes=1000):
    """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 
Example #11
Source File: resnet_earlyexit.py    From dnn-quant-ocs with Apache License 2.0 5 votes vote down vote up
def resnet34_earlyexit(**kwargs):
    """Constructs a ResNet-34 model.
    """
    model = ResNetEarlyExit(BasicBlock, [3, 4, 6, 3], **kwargs)
    return model 
Example #12
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet50(num_classes=1000, pretrained=False):
    """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)
    if pretrained:
        model.load_state_dict(load_state_dict_from_url(
            "https://github.com/moskomule/senet.pytorch/releases/download/archive/seresnet50-60a8950a85b2b.pkl"))
    return model 
Example #13
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet101(num_classes=1000):
    """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 
Example #14
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet152(num_classes=1000):
    """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 
Example #15
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet32(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEResNet(CifarSEBasicBlock, 5, **kwargs)
    return model 
Example #16
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_resnet56(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEResNet(CifarSEBasicBlock, 9, **kwargs)
    return model 
Example #17
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_preactresnet20(**kwargs):
    """Constructs a ResNet-18 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 3, **kwargs)
    return model 
Example #18
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_preactresnet32(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 5, **kwargs)
    return model 
Example #19
Source File: se_resnet.py    From netharn with Apache License 2.0 5 votes vote down vote up
def se_preactresnet56(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 9, **kwargs)
    return model 
Example #20
Source File: resnet_earlyexit.py    From dnn-quant-ocs with Apache License 2.0 5 votes vote down vote up
def resnet18_earlyexit(**kwargs):
    """Constructs a ResNet-18 model.
    """
    model = ResNetEarlyExit(BasicBlock, [2, 2, 2, 2], **kwargs)
    return model 
Example #21
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
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 
Example #22
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
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 
Example #23
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
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 
Example #24
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
def se_resnet20(**kwargs):
    """Constructs a ResNet-18 model.

    """
    model = CifarSEResNet(CifarSEBasicBlock, 3, **kwargs)
    return model 
Example #25
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
def se_resnet56(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEResNet(CifarSEBasicBlock, 9, **kwargs)
    return model 
Example #26
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
def se_preactresnet20(**kwargs):
    """Constructs a ResNet-18 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 3, **kwargs)
    return model 
Example #27
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
def se_preactresnet32(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 5, **kwargs)
    return model 
Example #28
Source File: se_resnet.py    From senet.pytorch with MIT License 5 votes vote down vote up
def se_preactresnet56(**kwargs):
    """Constructs a ResNet-34 model.

    """
    model = CifarSEPreActResNet(CifarSEBasicBlock, 9, **kwargs)
    return model 
Example #29
Source File: se_nets.py    From DualResidualNetworks with MIT License 5 votes vote down vote up
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 
Example #30
Source File: senet.py    From pytorch2keras with MIT License 5 votes vote down vote up
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