Python torchvision.models 模块,resnet152() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用torchvision.models.resnet152()

项目:PyTorch-Encoding    作者:zhanghang1989    | 项目源码 | 文件源码
def __init__(self, args):
        nclass=args.nclass
        super(Net, self).__init__()
        self.backbone = args.backbone
        # copying modules from pretrained models
        if self.backbone == 'resnet50':
            self.pretrained = resnet.resnet50(pretrained=True)
        elif self.backbone == 'resnet101':
            self.pretrained = resnet.resnet101(pretrained=True)
        elif self.backbone == 'resnet152':
            self.pretrained = resnet.resnet152(pretrained=True)
        else:
            raise RuntimeError('unknown backbone: {}'.format(self.backbone))
        n_codes = 32
        self.head = nn.Sequential(
            nn.Conv2d(2048, 128, 1),
            nn.BatchNorm2d(128),
            nn.ReLU(inplace=True),
            encoding.nn.Encoding(D=128,K=n_codes),
            encoding.nn.View(-1, 128*n_codes),
            encoding.nn.Normalize(),
            nn.Linear(128*n_codes, nclass),
        )
项目:voc_classification    作者:HyeonwooNoh    | 项目源码 | 文件源码
def GetPretrainedModel(params, num_classes):
    if params['model'] == 'resnet18':
        model = models.resnet18(pretrained=True)
    elif params['model'] == 'resnet34':
        model = models.resnet34(pretrained=True)
    elif params['model'] == 'resnet50':
        model = models.resnet50(pretrained=True)
    elif params['model'] == 'resnet101':
        model = models.resnet101(pretrained=True)
    elif params['model'] == 'resnet152':
        model = models.resnet152(pretrained=True)
    else:
        raise ValueError('Unknown model type')
    num_features = model.fc.in_features
    model.fc = SigmoidLinear(num_features, num_classes)
    return model
项目:verb-attributes    作者:uwnlp    | 项目源码 | 文件源码
def get_pretrained_resnet(new_fc_dim=None):
    """
    Fetches a pretrained resnet model (downloading if necessary) and chops off the top linear
    layer. If new_fc_dim isn't None, then a new linear layer is added.
    :param new_fc_dim: 
    :return: 
    """
    resnet152 = models.resnet152(pretrained=True)
    del resnet152.fc

    if new_fc_dim is not None:
        resnet152.fc = nn.Linear(ENCODING_SIZE, new_fc_dim)

        _init_fc(resnet152.fc)
    else:
        resnet152.fc = lambda x: x

    return resnet152
项目:kaggle-planet    作者:ZijunDeng    | 项目源码 | 文件源码
def get_res152(num_classes, pretrained):
    net = models.resnet152()
    if pretrained:
        net.load_state_dict(torch.load(pretrained_res152_path))
    net.fc = nn.Linear(net.fc.in_features, num_classes)
    return net
项目:pytorch-tutorial    作者:yunjey    | 项目源码 | 文件源码
def __init__(self, embed_size):
        """Load the pretrained ResNet-152 and replace top fc layer."""
        super(EncoderCNN, self).__init__()
        resnet = models.resnet152(pretrained=True)
        modules = list(resnet.children())[:-1]      # delete the last fc layer.
        self.resnet = nn.Sequential(*modules)
        self.linear = nn.Linear(resnet.fc.in_features, embed_size)
        self.bn = nn.BatchNorm1d(embed_size, momentum=0.01)
        self.init_weights()
项目:pytorch-semantic-segmentation    作者:ZijunDeng    | 项目源码 | 文件源码
def __init__(self, num_classes, pretrained=True):
        super(ResNetDUC, self).__init__()
        resnet = models.resnet152()
        if pretrained:
            resnet.load_state_dict(torch.load(res152_path))
        self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool)
        self.layer1 = resnet.layer1
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4

        for n, m in self.layer3.named_modules():
            if 'conv2' in n:
                m.dilation = (2, 2)
                m.padding = (2, 2)
                m.stride = (1, 1)
            elif 'downsample.0' in n:
                m.stride = (1, 1)
        for n, m in self.layer4.named_modules():
            if 'conv2' in n:
                m.dilation = (4, 4)
                m.padding = (4, 4)
                m.stride = (1, 1)
            elif 'downsample.0' in n:
                m.stride = (1, 1)

        self.duc = _DenseUpsamplingConvModule(8, 2048, num_classes)
项目:pytorch-semantic-segmentation    作者:ZijunDeng    | 项目源码 | 文件源码
def __init__(self, num_classes, pretrained=True):
        super(ResNetDUCHDC, self).__init__()
        resnet = models.resnet152()
        if pretrained:
            resnet.load_state_dict(torch.load(res152_path))
        self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool)
        self.layer1 = resnet.layer1
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4

        for n, m in self.layer3.named_modules():
            if 'conv2' in n or 'downsample.0' in n:
                m.stride = (1, 1)
        for n, m in self.layer4.named_modules():
            if 'conv2' in n or 'downsample.0' in n:
                m.stride = (1, 1)
        layer3_group_config = [1, 2, 5, 9]
        for idx in range(len(self.layer3)):
            self.layer3[idx].conv2.dilation = (layer3_group_config[idx % 4], layer3_group_config[idx % 4])
            self.layer3[idx].conv2.padding = (layer3_group_config[idx % 4], layer3_group_config[idx % 4])
        layer4_group_config = [5, 9, 17]
        for idx in range(len(self.layer4)):
            self.layer4[idx].conv2.dilation = (layer4_group_config[idx], layer4_group_config[idx])
            self.layer4[idx].conv2.padding = (layer4_group_config[idx], layer4_group_config[idx])

        self.duc = _DenseUpsamplingConvModule(8, 2048, num_classes)
项目:pytorch-semantic-segmentation    作者:ZijunDeng    | 项目源码 | 文件源码
def __init__(self, num_classes, input_size, pretrained=True):
        super(GCN, self).__init__()
        self.input_size = input_size
        resnet = models.resnet152()
        if pretrained:
            resnet.load_state_dict(torch.load(res152_path))
        self.layer0 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu)
        self.layer1 = nn.Sequential(resnet.maxpool, resnet.layer1)
        self.layer2 = resnet.layer2
        self.layer3 = resnet.layer3
        self.layer4 = resnet.layer4

        self.gcm1 = _GlobalConvModule(2048, num_classes, (7, 7))
        self.gcm2 = _GlobalConvModule(1024, num_classes, (7, 7))
        self.gcm3 = _GlobalConvModule(512, num_classes, (7, 7))
        self.gcm4 = _GlobalConvModule(256, num_classes, (7, 7))

        self.brm1 = _BoundaryRefineModule(num_classes)
        self.brm2 = _BoundaryRefineModule(num_classes)
        self.brm3 = _BoundaryRefineModule(num_classes)
        self.brm4 = _BoundaryRefineModule(num_classes)
        self.brm5 = _BoundaryRefineModule(num_classes)
        self.brm6 = _BoundaryRefineModule(num_classes)
        self.brm7 = _BoundaryRefineModule(num_classes)
        self.brm8 = _BoundaryRefineModule(num_classes)
        self.brm9 = _BoundaryRefineModule(num_classes)

        initialize_weights(self.gcm1, self.gcm2, self.gcm3, self.gcm4, self.brm1, self.brm2, self.brm3,
                           self.brm4, self.brm5, self.brm6, self.brm7, self.brm8, self.brm9)
项目:weldon.resnet.pytorch    作者:durandtibo    | 项目源码 | 文件源码
def resnet152_weldon(num_classes, pretrained=True, kmax=1, kmin=None):
    model = models.resnet152(pretrained)
    pooling = WeldonPool2d(kmax, kmin)
    return ResNetWSL(model, num_classes, pooling=pooling)
项目:pretrained-models.pytorch    作者:Cadene    | 项目源码 | 文件源码
def resnet152(num_classes=1000, pretrained='imagenet'):
    """Constructs a ResNet-152 model.
    """
    model = models.resnet152(pretrained=False)
    if pretrained is not None:
        settings = pretrained_settings['resnet152'][pretrained]
        model = load_pretrained(model, num_classes, settings)
    model = modify_resnets(model)
    return model

###############################################################
# SqueezeNets
项目:verb-attributes    作者:uwnlp    | 项目源码 | 文件源码
def __init__(self, zeroshot, embed_dim=None, att_domains=None, num_train_classes=None, l2_weight=None):
        """
        :param zeroshot: Whether we're running in zeroshot mode (
            can be true or False).
        :param embed_dim: Dimension of embeddings (probably 300)
        :param att_dims: List of domain sizes per attribute.
        :param num_train_classes: If we're doing pretraining, number of classes to use
        """
        super(ImsituModel, self).__init__()

        self.l2_weight = l2_weight
        if zeroshot:
            if (embed_dim is not None) and (att_domains is not None):
                print("Using embeddings and attributes for zeroshot")
            elif embed_dim is not None:
                print("Using embeddings for zeroshot")
            elif att_domains is not None:
                print("using attributes for zeroshot")
            else:
                raise ValueError("Must supply embeddings or attributes for zeroshot")
            self.fc_dim = None
            self.att_domains = att_domains if att_domains is not None else []
            self.embed_dim = embed_dim
        else:
            if num_train_classes is None:
                raise ValueError("Must supply a # of training classes")
            self.fc_dim = num_train_classes
            self.att_domains = []
            self.embed_dim = None

        self.resnet152 = get_pretrained_resnet(self.fc_dim)

        if self.embed_dim is not None:
            self.embed_linear = nn.Linear(ENCODING_SIZE, self.embed_dim)
            _init_fc(self.embed_linear)

        if self.att_dim is not None:
            self.att_linear = nn.Linear(ENCODING_SIZE, self.att_dim)
            _init_fc(self.att_linear)
项目:verb-attributes    作者:uwnlp    | 项目源码 | 文件源码
def __call__(self, imgs):
        img_feats = self.resnet152(imgs)

        if not self.is_zeroshot:
            return img_feats

        att_res = self.att_linear(img_feats) if self.att_dim is not None else None
        embed_res = self.embed_linear(img_feats) if self.embed_dim is not None else None
        return ZSResult(att_res, embed_res)