当前位置: 首页>>代码示例>>Python>>正文


Python model_zoo.load_url函数代码示例

本文整理汇总了Python中torch.utils.model_zoo.load_url函数的典型用法代码示例。如果您正苦于以下问题:Python load_url函数的具体用法?Python load_url怎么用?Python load_url使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了load_url函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _load_xception_pretrained

    def _load_xception_pretrained(self):
        pretrain_dict = model_zoo.load_url('http://data.lip6.fr/cadene/pretrainedmodels/xception-b5690688.pth')
        model_dict = {}
        state_dict = self.state_dict()

        for k, v in pretrain_dict.items():
            if k in state_dict:
                if 'pointwise' in k:
                    v = v.unsqueeze(-1).unsqueeze(-1)
                if k.startswith('block12'):
                    model_dict[k.replace('block12', 'block20')] = v
                elif k.startswith('block11'):
                    model_dict[k.replace('block11', 'block12')] = v
                    model_dict[k.replace('block11', 'block13')] = v
                    model_dict[k.replace('block11', 'block14')] = v
                    model_dict[k.replace('block11', 'block15')] = v
                    model_dict[k.replace('block11', 'block16')] = v
                    model_dict[k.replace('block11', 'block17')] = v
                    model_dict[k.replace('block11', 'block18')] = v
                    model_dict[k.replace('block11', 'block19')] = v
                elif k.startswith('conv3'):
                    model_dict[k] = v
                elif k.startswith('bn3'):
                    model_dict[k] = v
                    model_dict[k.replace('bn3', 'bn4')] = v
                elif k.startswith('conv4'):
                    model_dict[k.replace('conv4', 'conv5')] = v
                elif k.startswith('bn4'):
                    model_dict[k.replace('bn4', 'bn5')] = v
                else:
                    model_dict[k] = v
        state_dict.update(model_dict)
        self.load_state_dict(state_dict)
开发者ID:codes-kzhan,项目名称:pytorch-deeplab-xception,代码行数:33,代码来源:deeplab_xception.py

示例2: inceptionresnetv2

def inceptionresnetv2(num_classes=1000, pretrained='imagenet'):
    r"""InceptionResNetV2 model architecture from the
    `"InceptionV4, Inception-ResNet..." <https://arxiv.org/abs/1602.07261>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['inceptionresnetv2'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = InceptionResNetV2(num_classes=1001)
        model.load_state_dict(model_zoo.load_url(settings['url']))
        
        if pretrained == 'imagenet':
            new_last_linear = nn.Linear(1536, 1000)
            new_last_linear.weight.data = model.last_linear.weight.data[1:]
            new_last_linear.bias.data = model.last_linear.bias.data[1:]
            model.last_linear = new_last_linear
        
        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']
        
        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = InceptionResNetV2(num_classes=num_classes)
    return model
开发者ID:SiddharthTiwari,项目名称:fastai,代码行数:28,代码来源:inceptionresnetv2.py

示例3: load_state_dict

def load_state_dict(model, model_urls, model_root):
    from torch.utils import model_zoo
    from torch import nn
    import re
    from collections import OrderedDict
    own_state_old = model.state_dict()
    own_state = OrderedDict() # remove all 'group' string
    for k, v in own_state_old.items():
        k = re.sub('group\d+\.', '', k)
        own_state[k] = v

    state_dict = model_zoo.load_url(model_urls, model_root)

    for name, param in state_dict.items():
        if name not in own_state:
            print(own_state.keys())
            raise KeyError('unexpected key "{}" in state_dict'
                           .format(name))
        if isinstance(param, nn.Parameter):
            # backwards compatibility for serialized parameters
            param = param.data
        own_state[name].copy_(param)

    missing = set(own_state.keys()) - set(state_dict.keys())
    if len(missing) > 0:
        raise KeyError('missing keys in state_dict: "{}"'.format(missing))
开发者ID:ZJU-PLP,项目名称:pytorch-playground,代码行数:26,代码来源:misc.py

示例4: resnet

def resnet(name, **kwargs):
    pretrained_urls = {
        'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
        'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
        'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
        'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
        'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth'
    }

    blocks = {
        'resnet18': BasicBlock,
        'resnet34': BasicBlock,
        'resnet50': Bottleneck,
        'resnet101': Bottleneck,
        'resnet152': Bottleneck
    }

    layers = {
        'resnet18': [2, 2, 2, 2],
        'resnet34': [3, 4, 6, 3],
        'resnet50': [3, 4, 6, 3],
        'resnet101': [3, 4, 23, 3],
        'resnet152': [3, 8, 36, 3]
    }

    model = ResNet(blocks[name], layers[name], **kwargs).cuda()
    sc.convert(model, model_zoo.load_url(pretrained_urls[name]))
    return model
开发者ID:ptillet,项目名称:isaac,代码行数:28,代码来源:resnet.py

示例5: test_super_resolution

 def test_super_resolution(self):
     super_resolution_net = SuperResolutionNet(upscale_factor=3)
     state_dict = model_zoo.load_url(model_urls['super_resolution'], progress=False)
     x = Variable(torch.randn(1, 1, 224, 224), requires_grad=True)
     self.run_model_test(super_resolution_net, train=False,
                         batch_size=BATCH_SIZE, state_dict=state_dict,
                         input=x, use_gpu=False, atol=1e-6)
开发者ID:gtgalone,项目名称:pytorch,代码行数:7,代码来源:test_caffe2.py

示例6: densenet161

def densenet161(pretrained=False, **kwargs):
    r"""Densenet-161 model from
    `"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = DenseNet(num_init_features=96, growth_rate=48, block_config=(6, 12, 36, 24),
                     **kwargs)
    if pretrained:
        # '.'s are no longer allowed in module names, but pervious _DenseLayer
        # has keys 'norm.1', 'relu.1', 'conv.1', 'norm.2', 'relu.2', 'conv.2'.
        # They are also in the checkpoints in model_urls. This pattern is used
        # to find such keys.
        pattern = re.compile(
            r'^(.*denselayer\d+\.(?:norm|relu|conv))\.((?:[12])\.(?:weight|bias|running_mean|running_var))$')
        state_dict = model_zoo.load_url(model_urls['densenet161'])
        for key in list(state_dict.keys()):
            res = pattern.match(key)
            if res:
                new_key = res.group(1) + res.group(2)
                state_dict[new_key] = state_dict[key]
                del state_dict[key]
        model.load_state_dict(state_dict)
    return model
开发者ID:Lynkzhang,项目名称:vision,代码行数:25,代码来源:densenet.py

示例7: flow_resnet50_aux

def flow_resnet50_aux(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_state_dict(model_zoo.load_url(model_urls['resnet50']))
        pretrained_dict = model_zoo.load_url(model_urls['resnet50'])

        model_dict = model.state_dict()
        fc_origin_weight = pretrained_dict["fc.weight"].data.numpy()
        fc_origin_bias = pretrained_dict["fc.bias"].data.numpy()

        # 1. filter out unnecessary keys
        pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
        # 2. overwrite entries in the existing state dict
        model_dict.update(pretrained_dict) 
        # print(model_dict)
        fc_new_weight = model_dict["fc_aux.weight"].numpy() 
        fc_new_bias = model_dict["fc_aux.bias"].numpy() 

        fc_new_weight[:1000, :] = fc_origin_weight
        fc_new_bias[:1000] = fc_origin_bias

        model_dict["fc_aux.weight"] = torch.from_numpy(fc_new_weight)
        model_dict["fc_aux.bias"] = torch.from_numpy(fc_new_bias)

        # 3. load the new state dict
        model.load_state_dict(model_dict)

    return model
开发者ID:Alawaka,项目名称:two-stream-pytorch,代码行数:33,代码来源:flow_resnet.py

示例8: nasnetalarge

def nasnetalarge(num_classes=1000, pretrained='imagenet'):
    r"""NASNetALarge model architecture from the
    `"NASNet" <https://arxiv.org/abs/1707.07012>`_ paper.
    """
    if pretrained:
        settings = pretrained_settings['nasnetalarge'][pretrained]
        assert num_classes == settings['num_classes'], \
            "num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)

        # both 'imagenet'&'imagenet+background' are loaded from same parameters
        model = NASNetALarge(num_classes=1001)
        model.load_state_dict(model_zoo.load_url(settings['url']))

        if pretrained == 'imagenet':
            new_last_linear = nn.Linear(model.last_linear.in_features, 1000)
            new_last_linear.weight.data = model.last_linear.weight.data[1:]
            new_last_linear.bias.data = model.last_linear.bias.data[1:]
            model.last_linear = new_last_linear

        model.input_space = settings['input_space']
        model.input_size = settings['input_size']
        model.input_range = settings['input_range']

        model.mean = settings['mean']
        model.std = settings['std']
    else:
        model = NASNetALarge(num_classes=num_classes)
    return model
开发者ID:aaguirre-rdit,项目名称:fastai,代码行数:28,代码来源:nasnet.py

示例9: create_mtcnn_net

    def create_mtcnn_net(self, use_cuda=True):
        self.device = torch.device(
            "cuda" if use_cuda and torch.cuda.is_available() else "cpu")

        pnet = PNet()
        pnet.load_state_dict(model_zoo.load_url(model_urls['pnet']))
        pnet.to(self.device).eval()

        onet = ONet()
        onet.load_state_dict(model_zoo.load_url(model_urls['onet']))
        onet.to(self.device).eval()

        rnet = RNet()
        rnet.load_state_dict(model_zoo.load_url(model_urls['rnet']))
        rnet.to(self.device).eval()

        return pnet, rnet, onet
开发者ID:Fresh-Z,项目名称:mtcnn_pytorch,代码行数:17,代码来源:detect.py

示例10: test_srresnet

 def test_srresnet(self):
     super_resolution_net = SRResNet(
         rescale_factor=4, n_filters=64, n_blocks=8)
     state_dict = model_zoo.load_url(model_urls['srresNet'], progress=False)
     x = Variable(torch.randn(1, 3, 224, 224), requires_grad=True)
     self.run_model_test(super_resolution_net, train=False,
                         batch_size=1, state_dict=state_dict,
                         input=x, use_gpu=False)
开发者ID:gtgalone,项目名称:pytorch,代码行数:8,代码来源:test_caffe2.py

示例11: init_params

 def init_params(self):
     """Load ImageNet pretrained weights"""
     settings = pretrained_settings['nasnetamobile']['imagenet']
     pretrained_dict = model_zoo.load_url(settings['url'], map_location=None)
     model_dict = self.state_dict()
     pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
     model_dict.update(pretrained_dict)
     self.load_state_dict(model_dict)
开发者ID:zysolanine,项目名称:deep-person-reid,代码行数:8,代码来源:nasnet.py

示例12: resnet50

def resnet50(pretrained=False, channel= 20, **kwargs):

    model = ResNet(Bottleneck, [3, 4, 6, 3], nb_classes=101, channel=channel, **kwargs)
    if pretrained:
       pretrain_dict = model_zoo.load_url(model_urls['resnet50'])                  # modify pretrain code
       model_dict = model.state_dict()
       model_dict=weight_transform(model_dict, pretrain_dict, channel)
       model.load_state_dict(model_dict)
    return model
开发者ID:Alawaka,项目名称:two-stream-action-recognition,代码行数:9,代码来源:network.py

示例13: xresnet50_2

def xresnet50_2(pretrained=False, **kwargs):
    """Constructs a XResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = XResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
    if pretrained: model.load_state_dict(model_zoo.load_url(model_urls['xresnet50']))
    return model
开发者ID:SiddharthTiwari,项目名称:fastai,代码行数:9,代码来源:xresnet2.py

示例14: ResNet18_imagenet

def ResNet18_imagenet(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet_imagenet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet18']))
    return model
开发者ID:yingzhenyang,项目名称:deep-filter-panorama,代码行数:9,代码来源:resnet_imagenet.py

示例15: _load_pretrained_model

 def _load_pretrained_model(self):
     pretrain_dict = model_zoo.load_url('https://download.pytorch.org/models/resnet101-5d3b4d8f.pth')
     model_dict = {}
     state_dict = self.state_dict()
     for k, v in pretrain_dict.items():
         if k in state_dict:
             model_dict[k] = v
     state_dict.update(model_dict)
     self.load_state_dict(state_dict)
开发者ID:WenmuZhou,项目名称:pytorch-deeplab-xception,代码行数:9,代码来源:deeplab_resnet.py


注:本文中的torch.utils.model_zoo.load_url函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。