Ejemplo n.º 1
0
def get_resnet(resnet):
    if resnet == 'resnet18':
        return ResNet(BasicBlock, [2, 2, 2, 2])
    elif resnet == 'resnet34':
        return ResNet(BasicBlock, [3, 4, 6, 3])
    elif resnet == 'resnet50':
        return ResNet(Bottleneck, [3, 4, 6, 3])
    elif resnet == 'resnet101':
        return ResNet(Bottleneck, [3, 4, 23, 3])
    elif resnet == 'resnet152':
        return ResNet(Bottleneck, [3, 8, 36, 3])
Ejemplo n.º 2
0
 def __init__(self, pretrained=False, **kwargs):
     super(MyNet, self).__init__()
     netname = args.arch.split('_')[0]
     if netname == 'resnet152':
         self.model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
     else:
         self.model = ResNet(Bottleneck, [3, 4, 6, 3], **kwargs)
     if pretrained:
         parameters = model_zoo.load_url(model_urls[netname])
         self.model.load_state_dict(parameters)
     self.model.avgpool = nn.AvgPool2d(8)
     self.model.fc = nn.Linear(1024, 1)
     self.model.sig = nn.Sigmoid()
Ejemplo n.º 3
0
    def __init__(self, feature_dim=128, group_norm=False):
        super(Model, self).__init__()

        self.f = []
        for name, module in ResNet(BasicBlock, [1, 1, 1, 1],
                                   num_classes=10).named_children():
            if name == 'conv1':
                module = nn.Conv2d(3,
                                   64,
                                   kernel_size=3,
                                   stride=1,
                                   padding=1,
                                   bias=False)
            if not isinstance(module, nn.Linear) and not isinstance(
                    module, nn.MaxPool2d):
                self.f.append(module)
        # encoder
        self.f = nn.Sequential(*self.f)
        # projection head
        self.g = nn.Sequential(nn.Linear(512, 512, bias=False),
                               nn.BatchNorm1d(512), nn.ReLU(inplace=True),
                               nn.Linear(512, feature_dim, bias=True))

        if group_norm:
            apply_gn(self)
    def __init__(self, arch, norm, pre=False):
        super().__init__()
        if arch == 'ResX50':
            m = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4)
            print('Loaded Model RexNextssl')

        elif arch == 'ResS50':
            m = resnest50(pretrained=pre)
            print('Loaded model ResNest')

        blocks = [*m.children()]
        enc = blocks[:-2]
        self.enc = nn.Sequential(*enc)
        C = blocks[-1].in_features
        head = [
            AdaptiveConcatPool2d(),
            Flatten(),  #bs x 2*C
            nn.Linear(2 * C, 512),
            Mish()
        ]
        if norm == 'GN':
            head.append(nn.GroupNorm(32, 512))
            print('Group Norm')
        elif norm == 'BN':
            head.append(nn.BatchNorm1d(512))
            print('Batch Norm')
        else:
            print('No Norm')

        head.append(nn.Dropout(0.5))
        head.append(nn.Linear(512, NUM_CLASSES - 1))
        self.head = nn.Sequential(*head)
def fresnet100_v3(**kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    keep_dict = {'180M': [58, 39, 45, 32, 45, 52, 45, 103, 116, 77, 116, 64, 116,
                          90, 116, 103, 116, 90, 116, 103, 116, 77, 116, 90, 116,
                          116, 116, 77, 116, 103, 116, 116, 116, 231, 231, 180,
                          231, 154, 231, 205, 231, 205, 231, 180, 231, 77, 231,
                          205, 231, 128, 231, 154, 231, 154, 231, 128, 231, 205,
                          231, 154, 231, 205, 231, 231, 231, 154, 231, 231, 231,
                          154, 231, 180, 231, 205, 231, 154, 231, 128, 231, 77,
                          231, 205, 231, 180, 231, 77, 231, 77, 231, 128, 231, 77,
                          231, 461, 461, 154, 461, 154, 461],
                 '140M': [58, 13, 26, 20, 26, 20, 26, 77, 116, 39, 116, 39, 116,
                          52, 116, 77, 116, 64, 116, 52, 116, 39, 116, 64, 116, 64,
                          116, 39, 116, 64, 116, 52, 116, 231, 231, 128, 231, 103,
                          231, 77, 231, 154, 231, 103, 231, 77, 231, 128, 231, 77,
                          231, 77, 231, 103, 231, 77, 231, 128, 231, 77, 231, 128,
                          231, 128, 231, 103, 231, 77, 231, 103, 231, 128, 231, 128,
                          231, 77, 231, 77, 231, 77, 231, 128, 231, 77, 231, 52, 231,
                          77, 231, 77, 231, 52, 231, 410, 461, 103, 461, 103, 461]}

    model = ResNet(BasicBlock_v3, [3, 13, 30, 3], keep_dict['140M'], **kwargs)

    return model
Ejemplo n.º 6
0
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model = load_partial_weights(model, state_dict, verbose=progress)
    return model
def fresnet50_v3(**kwargs):
    """Constructs a ResNet-50 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    keep_dict = {
        'big': [
            64, 20, 64, 26, 64, 32, 64, 116, 128, 52, 128, 39, 128, 52, 128,
            231, 256, 77, 256, 231, 256, 205, 256, 103, 256, 180, 256, 128,
            256, 205, 256, 205, 256, 103, 256, 103, 256, 154, 256, 77, 256,
            103, 256, 410, 512, 359, 512, 154, 512
        ],
        'medium': [
            64, 13, 64, 13, 64, 13, 64, 52, 128, 26, 128, 26, 128, 26, 128,
            103, 256, 52, 256, 77, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52,
            256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 52, 256, 154,
            512, 103, 512, 103, 512
        ],
        'small': [
            64, 7, 64, 7, 64, 7, 64, 26, 128, 13, 128, 13, 128, 13, 128, 52,
            256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256,
            26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 26, 256, 103, 512, 52,
            512, 52, 512
        ]
    }

    model = ResNet(BasicBlock_v3, [3, 4, 14, 3], keep_dict['small'], **kwargs)
    model.load_state_dict(
        torch.load(
            '/media/user1/Ubuntu 16.0/resnet50/model_resnet50_66.7M_0.1.pt'))

    return model
Ejemplo n.º 8
0
def _resnext(arch, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict)
    return model
Ejemplo n.º 9
0
def _resnet(arch, block, layers, pretrained=False, progress=True,
            imagenet_pretrained=False, num_classes=1, lin_features=512,
            dropout_prob=0.5, bn_final=False, concat_pool=True, **kwargs):

    # Model creation
    base_model = ResNet(block, layers, num_classes=num_classes, **kwargs)
    # Imagenet pretraining
    if imagenet_pretrained:
        if pretrained:
            raise ValueError('imagenet_pretrained cannot be set to True if pretrained=True')
        state_dict = load_state_dict_from_url(imagenet_urls[arch],
                                              progress=progress)
        # Remove FC params from dict
        for key in ('fc.weight', 'fc.bias'):
            state_dict.pop(key, None)
        missing, unexpected = base_model.load_state_dict(state_dict, strict=False)
        if any(unexpected) or any(not elt.startswith('fc.') for elt in missing):
            raise KeyError(f"Missing parameters: {missing}\nUnexpected parameters: {unexpected}")

    # Cut at last conv layers
    model = cnn_model(base_model, model_cut, base_model.fc.in_features, num_classes,
                      lin_features, dropout_prob, bn_final=bn_final, concat_pool=concat_pool)

    # Parameter loading
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict)

    return model
Ejemplo n.º 10
0
def resnext_wsl(arch, pretrained, progress=True, **kwargs):
    """
    models trained in weakly-supervised fashion on 940 million public images with 1.5K hashtags matching with 1000 ImageNet1K synsets, followed by fine-tuning on ImageNet1K dataset.
    https://github.com/facebookresearch/WSL-Images/
    """
    from torch.hub import load_state_dict_from_url
    from torchvision.models.resnet import ResNet, Bottleneck

    model_args = {'resnext101_32x8d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=8),
                  'resnext101_32x16d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=16),
                  'resnext101_32x32d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=32),
                  'resnext101_32x48d': dict(block=Bottleneck, layers=[3, 4, 23, 3], groups=32, width_per_group=48)}

    args = model_args[arch]
    args.update(kwargs)
    model = ResNet(**args)

    if pretrained:
        model_urls = {
            'resnext101_32x8d': 'https://download.pytorch.org/models/ig_resnext101_32x8-c38310e5.pth',
            'resnext101_32x16d': 'https://download.pytorch.org/models/ig_resnext101_32x16-c6f796b0.pth',
            'resnext101_32x32d': 'https://download.pytorch.org/models/ig_resnext101_32x32-e4b90b00.pth',
            'resnext101_32x48d': 'https://download.pytorch.org/models/ig_resnext101_32x48-3e41cc8a.pth',
        }
        state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)
        model.load_state_dict(state_dict)

    return model
Ejemplo n.º 11
0
 def __init__(self, stride=1, **kwargs):
     super().__init__()
     #encoder
     m = ResNet(Bottleneck, [3, 4, 23, 3], groups=32, width_per_group=4)
     m = torch.hub.load(
         'facebookresearch/semi-supervised-ImageNet1K-models',
         'resnext50_32x4d_swsl')
     self.enc0 = nn.Sequential(m.conv1, m.bn1, nn.ReLU(inplace=True))
     self.enc1 = nn.Sequential(
         nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1),
         m.layer1)  #256
     self.enc2 = m.layer2  #512
     self.enc3 = m.layer3  #1024
     self.enc4 = m.layer4  #2048
     #aspp with customized dilatations
     self.aspp = ASPP(
         2048,
         256,
         out_c=512,
         dilations=[stride * 1, stride * 2, stride * 3, stride * 4])
     self.drop_aspp = nn.Dropout2d(0.5)
     #decoder
     self.dec4 = UnetBlock(512, 1024, 256)
     self.dec3 = UnetBlock(256, 512, 128)
     self.dec2 = UnetBlock(128, 256, 64)
     self.dec1 = UnetBlock(64, 64, 32)
     self.fpn = FPN([512, 256, 128, 64], [16] * 4)
     self.drop = nn.Dropout2d(0.1)
     self.final_conv = ConvLayer(32 + 16 * 4,
                                 1,
                                 ks=1,
                                 norm_type=None,
                                 act_cls=None)
Ejemplo n.º 12
0
def resnet34_se():
    model = ResNet(SEBasicBlock, [3, 4, 6, 3])

    state_dict = load_state_dict_from_url(model_urls["resnet34"],
                                          progress=True)
    model.load_state_dict(state_dict, strict=False)

    return model
Ejemplo n.º 13
0
def resnet50_se():
    model = ResNet(SEBottleneck, [3, 4, 6, 3])

    state_dict = load_state_dict_from_url(model_urls["resnet50"],
                                          progress=True)
    model.load_state_dict(state_dict, strict=False)

    return model
Ejemplo n.º 14
0
def fresnet100_v3(**kwargs):
    """
    Constructs a ResNet-100 model.
    :param kwargs:
    :return:
    """
    model = ResNet(BasicBlock_v3, [3, 13, 30, 3], **kwargs)
    return model
Ejemplo n.º 15
0
def generate_model(cfg, name):
    pretrained = cfg.model.pretrained
    classes = cfg.model.classes
    model = eval(f"models.{name}(pretrained={pretrained})")
    if classes != 1000:
        in_features = model.fc.in_features
        model.fc = nn.Linear(in_features, classes, bias=False)
    return ResNet(model)
Ejemplo n.º 16
0
def get_resnext(layers, pretrained, progress, **kwargs):
    from torchvision.models.resnet import ResNet, Bottleneck
    model = ResNet(Bottleneck, layers, **kwargs)
    model.load_state_dict(
        torch.load(
            '../input/resnext-50-ssl/semi_supervised_resnext50_32x4-ddb3e555.pth'
        ))
    return model
Ejemplo n.º 17
0
    def __init__(self,
                 in_channels=1,
                 n_classes=2,
                 stride=1,
                 inplanes=64,
                 pre_ssl=True,
                 **kwargs):
        super().__init__()
        store_attr('in_channels, n_classes, inplanes, pre_ssl')
        #encoder
        if pre_ssl:
            m = torch.hub.load(
                'facebookresearch/semi-supervised-ImageNet1K-models',
                'resnext50_32x4d_ssl')
        else:
            m = ResNet(Bottleneck, [3, 4, 6, 3], groups=32, width_per_group=4)
        m.conv1.padding = (0, 0)

        if in_channels < 3:
            #print('Cutting input layer weights to', in_channels, 'channel(s).')
            with torch.no_grad():
                m.conv1.weight = nn.Parameter(m.conv1.weight[:, :in_channels,
                                                             ...])
        elif in_channels > 3:
            m.conv1 = nn.Conv2d(in_channels,
                                self.inplanes,
                                kernel_size=7,
                                stride=2,
                                bias=False)

        #self.bn1 =  m.bn1 if in_channels==3 else nn.BatchNorm2d(self.inplanes)
        self.enc0 = nn.Sequential(m.conv1, m.bn1, nn.ReLU(inplace=True))
        self.enc1 = nn.Sequential(
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1),
            m.layer1)  #256
        self.enc2 = m.layer2  #512
        self.enc3 = m.layer3  #1024
        self.enc4 = m.layer4  #2048
        #aspp with customized dilatations
        self.aspp = ASPP(
            2048,
            256,
            out_c=512,
            dilations=[stride * 1, stride * 2, stride * 3, stride * 4])
        self.drop_aspp = nn.Dropout2d(0.5)
        #decoder
        self.dec4 = UnetBlock(512, 1024, 256, padding=0)
        self.dec3 = UnetBlock(256, 512, 128, padding=0)
        self.dec2 = UnetBlock(128, 256, 64, padding=0)
        self.dec1 = UnetBlock(64, 64, 32, padding=0)
        self.fpn = FPN([512, 256, 128, 64], [16] * 4)
        self.drop = nn.Dropout2d(0.1)
        self.final_conv = ConvLayer(32 + 16 * 4,
                                    n_classes,
                                    ks=1,
                                    norm_type=None,
                                    act_cls=None)
Ejemplo n.º 18
0
def resnext50_32x4d_se(**kwargs):
    kwargs["groups"] = 32
    kwargs["width_per_group"] = 4

    model = ResNet(SEBottleneck, [3, 4, 6, 3], **kwargs)
    state_dict = load_state_dict_from_url(model_urls["resnext50_32x4d"],
                                          progress=True)
    model.load_state_dict(state_dict, strict=False)

    return model
Ejemplo n.º 19
0
def resnet18(pretrained=False, model_dir=None, **kwargs):
    """Constructs a ResNet-18 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(model_urls['resnet18'], model_dir=model_dir))
    return model
Ejemplo n.º 20
0
def fresnet50_v3_ljt(**kwargs):
    """
    Constructs a ResNet-50 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock_v3, [3, 4, 14, 3], **kwargs)
    # if pretrained:
    #     model.load_state_dict(model_zoo.load_url(model_urls['resnet50']))
    return model
Ejemplo n.º 21
0
def _resnext(arch, num_classes, block, layers, pretrained, progress, **kwargs):
    model = ResNet(block, layers, **kwargs)
    if pretrained:
        state_dict = load_state_dict_from_url(model_urls[arch],
                                              progress=progress)
        model.load_state_dict(state_dict)
    in_features = model.fc.in_features
    model.fc = nn.Linear(in_features=in_features,
                         out_features=num_classes,
                         bias=True)
    return model
Ejemplo n.º 22
0
 def __init__(self, pretrained=False, **kwargs):
     super(Mymodel, self).__init__(BasicBlock, [3, 4, 6, 3])
     pre_model = ResNet(BasicBlock, [3, 4, 6, 3], **kwargs)
     if pretrained:
         pre_model.load_state_dict(
             model_zoo.load_url(model_urls['resnet34']))  # resnet34
     fc = t.nn.Linear(in_features=1000, out_features=120)
     for param in pre_model.parameters():  # 预训练模型不需要学习参数
         param.require_grad = False
     self.model = t.nn.Sequential(pre_model, fc)
     self.model_name = str(type(self))  # 默认名字
Ejemplo n.º 23
0
def resnet34_dropout(pretrained=False, **kwargs):
    """Constructs a ResNet-34 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    block = functools.partial(BasicDropoutBlock,
                              dropout=args.dropout,
                              pre_dropout=args.pre_dropout)
    block.expansion = 1
    return ResNet(block, [3, 4, 6, 3], **kwargs)
Ejemplo n.º 24
0
def resnet152_dropout(pretrained=False, **kwargs):
    """Constructs a ResNet-152 model.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    block = functools.partial(DropoutBottleneck,
                              dropout=args.dropout,
                              pre_dropout=args.pre_dropout)
    block.expansion = 4
    return ResNet(block, [3, 8, 36, 3], **kwargs)
def _resnext(url, block, layers, pretrained, progress, **kwargs):
    # make pretrained compatible although it is not used in resnext.
    model = ResNet(block, layers, **kwargs)
    if pretrained == "imagenet" or pretrained is True:
        state_dict = load_state_dict_from_url(url, progress=progress)
        model.load_state_dict(state_dict)
    model.input_space = input_space
    model.input_range = input_range
    model.input_size = input_sizes
    model.mean = means
    model.std = stds
    return model
Ejemplo n.º 26
0
 def __init__(self, opt):
     super(ResNeXt50, self).__init__()
     self.model = ResNet(Bottleneck, [3, 4, 6, 3],
                         groups=32,
                         width_per_group=4)
     self.opt = opt
     # self.reduced_id_dim = opt.reduced_id_dim
     self.conv1x1 = nn.Conv2d(512 * Bottleneck.expansion,
                              512,
                              kernel_size=1,
                              padding=0)
     self.fc = nn.Linear(512 * Bottleneck.expansion, opt.num_classes)
Ejemplo n.º 27
0
 def __init__(self, num_classes, pretrained=False, **kwargs):
     super(FaceModel, self).__init__()
     self.model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
     if pretrained:
         parameters = model_zoo.load_url(model_urls['resnet18'])
         self.model.load_state_dict(parameters)
     self.model.avgpool = None
     self.model.fc1 = nn.Linear(512 * 3 * 4, 512)
     self.model.fc2 = nn.Linear(512, 512)
     self.model.classifier = nn.Linear(512, num_classes)
     self.register_buffer('centers', torch.zeros(num_classes, 512))
     self.num_classes = num_classes
Ejemplo n.º 28
0
def resnet18_dropout(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model with dropout.

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    # def __init__(self, inplanes, planes, stride=1,
    #              downsample=None, dropout=0.5, pre_dropout=False):
    block = functools.partial(BasicDropoutBlock,
                              dropout=args.dropout,
                              pre_dropout=args.pre_dropout)
    block.expansion = 1
    return ResNet(block, [2, 2, 2, 2], **kwargs)
Ejemplo n.º 29
0
def resnet152(pretrained=False, model_path=None, **kwargs):
    """Constructs a ResNet-152 model. top1-acc-%   parameter-60.20M

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(Bottleneck, [3, 8, 36, 3], **kwargs)
    if pretrained:
        if model_path is not None:
            model.load_state_dict(torch.load(model_path))
        else:
            model.load_state_dict(model_zoo.load_url(model_urls['resnet152']))
    return model
Ejemplo n.º 30
0
def resnet18(pretrained=False, **kwargs):
    """Constructs a ResNet-18 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet(BasicBlock, [2, 2, 2, 2], **kwargs)
    # change your path
    model_path = '/home/jiayunpei/SSDG_github/pretrained_model/resnet18-5c106cde.pth'
    if pretrained:
        model.load_state_dict(torch.load(model_path))
        print("loading model: ", model_path)
    # print(model)
    return model