예제 #1
0
 def _create(self, name, **encoder_kwargs):
     if name not in _basic_block_layers.keys():
         fn_name = getattr(resnet, name)
         model = fn_name(**encoder_kwargs)
     else:
         # special case due to prohibited dilation in the original BasicBlock
         pretrained = encoder_kwargs.pop('pretrained', False)
         progress = encoder_kwargs.pop('progress', True)
         model = resnet.ResNet(BasicBlockWithDilation,
                               _basic_block_layers[name], **encoder_kwargs)
         if pretrained:
             state_dict = load_state_dict_from_url(model_urls[name])
             model.load_state_dict(state_dict, strict=False)
         # model = resnet._resnet(
         #     name, BasicBlockWithDilation, _basic_block_layers[name], pretrained, progress, **encoder_kwargs
         # )
     replace_stride_with_dilation = encoder_kwargs.get(
         'replace_stride_with_dilation', (False, False, False))
     assert len(replace_stride_with_dilation) == 3
     if replace_stride_with_dilation[0]:
         model.layer2[0].conv2.padding = (2, 2)
         model.layer2[0].conv2.dilation = (2, 2)
     if replace_stride_with_dilation[1]:
         model.layer3[0].conv2.padding = (2, 2)
         model.layer3[0].conv2.dilation = (2, 2)
     if replace_stride_with_dilation[2]:
         model.layer4[0].conv2.padding = (2, 2)
         model.layer4[0].conv2.dilation = (2, 2)
     return model
예제 #2
0
 def __init__(self, layers=[3, 4, 6, 3]):
     block = resnet.BasicBlock
     num_classes = 7
     self.model = resnet.ResNet(block, layers, num_classes)
     if torch.cuda.is_available():
         self.model.cuda()
     self.bestaccur = 0.0
예제 #3
0
    def __init__(self, scale, classes, epochs, split_size=0):
        super(Scaled_ResNext, self).__init__()
        # Based on the imagenet normalization params.
        self.offset = 0.44900
        self.multiplier = 4.42477

        self.dev1 = torch.device('cuda:0')
        self.dev2 = torch.device('cuda:0')

        self.split_size = split_size

        # Resnext50_32x4d.
        layers = [2, 3, 5, 2]
        if scale[0] > 1:
            layers = [3, 4, 6, 3]
        self.model = Resnet.ResNet(Resnet.Bottleneck,
                                   layers,
                                   num_classes=classes,
                                   groups=4,
                                   width_per_group=32)
        self.model.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        # The first part also needs to be fixed.
        self.model.conv1 = nn.Conv2d(
            scale[0], 64, kernel_size=3, stride=1, padding=1,
            bias=False)  # Replace the harsh convolution.
        del self.model.maxpool
        self.model.maxpool = lambda x: x  # Remove the early maxpool.

        self.model = self.model.to(self.dev1)

        self.epochs = epochs
예제 #4
0
def resnet101(pretrained=False, model_dir=None, **kwargs):
    """Constructs a ResNet-101 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = res.ResNet(res.Bottleneck, [3, 4, 23, 3], **kwargs)
    if pretrained:
        model.load_state_dict(model_zoo.load_url(res.model_urls['resnet101'], model_dir=model_dir))
    return model
예제 #5
0
def ResNet18(low_dim=128):
    net = resnet.ResNet(resnet.BasicBlock, [2, 2, 2, 2], low_dim)
    net.conv1 = nn.Conv2d(3,
                          64,
                          kernel_size=3,
                          stride=1,
                          padding=1,
                          bias=False)
    net.maxpool = nn.Identity()
    return net
예제 #6
0
    def __init__(self):
        super(TinyImagenet_Resnet, self).__init__()

        # Based on the imagenet normalization params.
        self.offset = 0.44900
        self.multiplier = 4.42477

        # Resnet50.
        self.model = Resnet.ResNet(Resnet.Bottleneck, [3, 4, 6, 3], num_classes=200)

        # TinyImagenet would have a different sized feature map.
        self.model.avgpool = nn.AdaptiveAvgPool2d((1,1))
        # The first part also needs to be fixed.
        self.model.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False) # Replace the harsh convolution.
예제 #7
0
 def __init__(self):
     super(CIFAR10_LIGHTNING, self).__init__()
     self.model = Resnet.ResNet(Resnet.Bottleneck, [3, 4, 6, 3],
                                num_classes=10)
     self.model.inplanes = 64
     self.model.conv1 = nn.Conv2d(3,
                                  64,
                                  kernel_size=3,
                                  stride=1,
                                  padding=1,
                                  bias=False)
     self.model.bn1 = nn.BatchNorm2d(64)
     self.model.linear = nn.Linear(512 * Resnet.Bottleneck.expansion, 10)
     del self.model.maxpool
     self.model.maxpool = lambda x: x
예제 #8
0
    def __init__(self):
        super(MNIST_Resnet, self).__init__()

        # Based on the imagenet normalization params.
        self.offset = 0.44900
        self.multiplier = 4.42477

        # Resnet50.
        self.model = Resnet.ResNet(Resnet.Bottleneck, [2, 3, 5, 2], num_classes=10)

        # MNIST would have a different sized feature map.
        self.model.avgpool = nn.AdaptiveAvgPool2d((1,1))
        # The first part also needs to be fixed.
        self.model.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1, bias=False) # Replace the harsh convolution.
        del self.model.maxpool
        self.model.maxpool = lambda x: x # Remove the early maxpool.
예제 #9
0
    def __init__(self, arch, pool="avg"):
        assert (arch == "resnet10" or arch == "resnet18" or arch == "resnet34"
                or arch == "resnet50" or arch == "resnet101"
                or arch == "resnet152")

        if arch == "resnet10":
            net = resnet_utils.ResNet(block=resnet_utils.BasicBlock,
                                      layers=[1, 1, 1, 1],
                                      num_classes=10)
        else:
            net = models.__dict__[arch](num_classes=10)

        all_feat_names = []
        feature_blocks = []

        # 1st conv before any network block
        conv1 = nn.Sequential()
        conv1.add_module("Conv", net.conv1)
        conv1.add_module("bn", net.bn1)
        conv1.add_module("relu", net.relu)
        conv1.add_module("maxpool", net.maxpool)
        feature_blocks.append(conv1)
        all_feat_names.append("conv1")

        # 1st block.
        feature_blocks.append(net.layer1)
        all_feat_names.append("block1")

        # 2nd block.
        feature_blocks.append(net.layer2)
        all_feat_names.append("block2")

        # 3rd block.
        feature_blocks.append(net.layer3)
        all_feat_names.append("block3")

        # 4th block.
        feature_blocks.append(net.layer4)
        all_feat_names.append("block4")

        assert pool == "none" or pool == "avg" or pool == "max"
        if pool == "max" or pool == "avg":
            feature_blocks.append(tools.GlobalPooling(pool_type=pool))
            all_feat_names.append("GlobalPooling")

        super().__init__(all_feat_names, feature_blocks)
예제 #10
0
def resnet26(pretrained=False, remote=True):
    """Constructs a ResNet-26 model.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
    """
    model = ResNet_C5(Bottleneck, [2, 2, 2, 2])

    if pretrained:

        # Define ResNet26 ImageNet
        model_IN = resnet.ResNet(block=Bottleneck,
                                 layers=[2, 2, 2, 2],
                                 num_classes=1000)

        # Load checkpoint
        if remote:
            checkpoint = load_state_dict_from_url(model_urls['resnet26'],
                                                  map_location='cpu',
                                                  progress=True)
        else:
            checkpoint = torch.load(os.path.join(Path.models_dir(),
                                                 'resnet26.pth'),
                                    map_location=lambda storage, loc: storage)
        checkpoint = checkpoint['model_state']

        # Handle DataParallel
        if 'module.' in list(checkpoint.keys())[0]:
            new_state_dict = OrderedDict()
            for k, v in checkpoint.items():
                name = k.replace('module.', '')  # remove `module.`
                new_state_dict[name] = v
        else:
            new_state_dict = checkpoint

        # Load pre-trained IN model
        model_IN.load_state_dict(new_state_dict)

        # Load weights to dense-labelling network
        model.load_pretrained(model_IN)

    return model
예제 #11
0
 def __init__(self):
     super(SVHN_Resnet, self).__init__()
     self.model = Resnet.ResNet(Resnet.Bottleneck, [3, 4, 6, 3],
                                num_classes=10)
     self.model.inplanes = 64
     self.model.conv1 = nn.Conv2d(3,
                                  64,
                                  kernel_size=3,
                                  stride=1,
                                  padding=1,
                                  bias=False)
     self.model.bn1 = nn.BatchNorm2d(64)
     self.model.linear = nn.Linear(512 * Resnet.Bottleneck.expansion, 10)
     del self.model.maxpool
     self.model.maxpool = lambda x: x
     self.model.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
     self.model.features = nn.Sequential(self.model.conv1, self.model.bn1,
                                         self.model.relu, self.model.layer1,
                                         self.model.layer2,
                                         self.model.layer3,
                                         self.model.layer4,
                                         self.model.avgpool)
예제 #12
0
 def __init__(self, hidden_size):
     super().__init__(hidden_size)
     
     self._models = resnet.ResNet(resnet.BasicBlock, [2, 2, 2, 2], num_classes=2)
     self._models.train()
예제 #13
0
    def __init__(self,
                 num_classes,
                 num_features=0,
                 dropout=0.6,
                 set_pooling='max'):
        super(CelebNet, self).__init__()
        self.dropout = dropout
        self.num_class = num_classes
        self.num_features = num_features
        self.has_embedding = num_features > 0
        self.set_pooling = set_pooling

        img_base = resnet.resnet50(pretrained=True)  # image branch
        mask_base = resnet.ResNet(resnet.Bottleneck, [1, 1, 1, 1],
                                  num_classes=1000,
                                  zero_init_residual=True)
        mask_base.load_state_dict(model_zoo.load_url(
            'https://download.pytorch.org/models/resnet50-19c8e357.pth'),
                                  strict=False)

        # fixed_names = []
        # for name, module in img_base._modules.items():
        #     if name == "layer3":
        #         print("break at layer3...")
        #         break
        #     fixed_names.append(name)
        #     for param in module.parameters():
        #         param.requires_grad = False
        #
        # fixed_names = []
        # for name, module in mask_base._modules.items():
        #     if name == "layer3":
        #         print("break at layer3...")
        #         break
        #     fixed_names.append(name)
        #     for param in module.parameters():
        #         param.requires_grad = False

        img_modules = list(img_base.children())[:-2]
        self.img_base = nn.Sequential(*img_modules)

        mask_modules = list(mask_base.children())[:-2]
        self.mask_base = nn.Sequential(*mask_modules)

        num_ftrs = img_base.fc.in_features
        self.attenblock0 = CrossAttention(in_channels=num_ftrs,
                                          out_channels=num_ftrs // 8)
        # self.attenblock0 = CrossAttentionShort(in_channels=num_ftrs, out_channels=num_ftrs)
        self.conv_layer1 = BasicResLayer(num_ftrs, num_ftrs // 2,
                                         stride=2)  # 1024
        self.mask_layer1 = MaskConvBlock(num_ftrs, num_ftrs // 2,
                                         padding=1)  # 1024
        self.glob_layer1 = BasicResLayer(num_ftrs, num_ftrs // 2, stride=2)
        self.attenblock1 = CrossAttention(in_channels=num_ftrs // 2,
                                          out_channels=num_ftrs // 8)
        # self.attenblock1 = CrossAttentionShort(in_channels=num_ftrs // 2, out_channels=num_ftrs//2)

        num_ftrs = num_ftrs // 2
        self.conv_layer2 = BasicResLayer(num_ftrs, num_ftrs // 2,
                                         stride=2)  # 512
        self.mask_layer2 = MaskConvBlock(num_ftrs, num_ftrs // 2,
                                         padding=1)  # 512
        self.glob_layer2 = BasicResLayer(num_ftrs, num_ftrs // 2, stride=2)
        self.attenblock2 = CrossAttention(in_channels=num_ftrs // 2,
                                          out_channels=num_ftrs // 8)
        # self.attenblock2 = CrossAttentionShort(in_channels=num_ftrs//2, out_channels=num_ftrs//2)
        num_ftrs = num_ftrs // 2

        if self.has_embedding:
            self.fc_img = nn.Linear(num_ftrs, self.num_features)
            self.img_feat_bn = nn.BatchNorm1d(self.num_features)
            self.fc_mask = nn.Linear(num_ftrs, self.num_features)
            self.mask_feat_bn = nn.BatchNorm1d(self.num_features)
            self.fc_glob = nn.Linear(num_ftrs, self.num_features)
            self.glob_feat_bn = nn.BatchNorm1d(self.num_features)

            nn.init.kaiming_normal_(self.fc_img.weight, mode='fan_out')
            nn.init.constant_(self.fc_img.bias, 0)
            nn.init.constant_(self.img_feat_bn.weight, 1)
            nn.init.constant_(self.img_feat_bn.bias, 0)

            nn.init.kaiming_normal_(self.fc_mask.weight, mode='fan_out')
            nn.init.constant_(self.fc_mask.bias, 0)
            nn.init.constant_(self.mask_feat_bn.weight, 1)
            nn.init.constant_(self.mask_feat_bn.bias, 0)

            nn.init.kaiming_normal_(self.fc_glob.weight, mode='fan_out')
            nn.init.constant_(self.fc_glob.bias, 0)
            nn.init.constant_(self.glob_feat_bn.weight, 1)
            nn.init.constant_(self.glob_feat_bn.bias, 0)
        else:
            self.num_features = num_ftrs

        # if self.dropout:
        #     self.concat_drop = nn.Dropout(self.dropout)
        # if self.num_class:
        #     self.concat_cls = nn.Linear(self.num_features*3, self.num_class)
        #     nn.init.normal_(self.concat_cls.weight, std=0.001)
        #     nn.init.constant_(self.concat_cls.bias, 0)

        if self.dropout > 0:
            self.conv_drop = nn.Dropout(self.dropout)
            self.mask_drop = nn.Dropout(self.dropout)
            self.glob_drop = nn.Dropout(self.dropout)
        if self.num_class > 0:
            self.conv_cls = nn.Linear(self.num_features, self.num_class)
            self.mask_cls = nn.Linear(self.num_features, self.num_class)
            self.glob_cls = nn.Linear(self.num_features, self.num_class)

            nn.init.normal_(self.conv_cls.weight, std=0.001)
            nn.init.constant_(self.conv_cls.bias, 0)
            nn.init.normal_(self.mask_cls.weight, std=0.001)
            nn.init.constant_(self.mask_cls.bias, 0)
            nn.init.normal_(self.glob_cls.weight, std=0.001)
            nn.init.constant_(self.glob_cls.bias, 0)