示例#1
0
def initialize_model(model_name, embedding_dim, feature_extracting, use_pretrained=True):
    if model_name == "densenet161":
        model_ft = models.densenet161(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.classifier.in_features
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
    elif model_name == "resnet101":
        model_ft = models.resnet101(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
    elif model_name == "inceptionv3":
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext":
        model_ft = se_resnext101_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    else:
        raise ValueError

    return model_ft
示例#2
0
    def __init__(self, backbone1, backbone2, drop, pretrained=True):
        super(MultiModalNet, self).__init__()

        self.visit_model = DPN26()
        if backbone1 == 'se_resnext101_32x4d':
            self.img_encoder = se_resnext101_32x4d(9, None)
            self.img_fc = nn.Linear(2048, 256)

        elif backbone1 == 'se_resnext50_32x4d':
            self.img_encoder = se_resnext50_32x4d(9, None)

            print(
                "load pretrained model from /home/zxw/2019BaiduXJTU/se_resnext50_32x4d-a260b3a4.pth"
            )
            state_dict = torch.load(
                '/home/zxw/2019BaiduXJTU/se_resnext50_32x4d-a260b3a4.pth')

            state_dict.pop('last_linear.bias')
            state_dict.pop('last_linear.weight')
            self.img_encoder.load_state_dict(state_dict, strict=False)

            self.img_fc = nn.Linear(2048, 256)

        elif backbone1 == 'se_resnext26_32x4d':
            self.img_encoder = se_resnext26_32x4d(9, None)
            self.img_fc = nn.Linear(2048, 256)

        elif backbone1 == 'multiscale_se_resnext':
            self.img_encoder = multiscale_se_resnext(9)
            self.img_fc = nn.Linear(2048, 256)

        elif backbone1 == 'multiscale_se_resnext_cat':
            self.img_encoder = multiscale_se_resnext(9)
            self.img_fc = nn.Linear(1024, 256)

        elif backbone1 == 'multiscale_se_resnext_HR':
            self.img_encoder = multiscale_se_resnext_HR(9)
            self.img_fc = nn.Linear(2048, 256)

        elif backbone1 == 'se_resnet50':
            self.img_encoder = se_resnet50(9, None)
            print(
                "load pretrained model from /home/zxw/2019BaiduXJTU/se_resnet50-ce0d4300.pth"
            )
            state_dict = torch.load(
                '/home/zxw/2019BaiduXJTU/se_resnet50-ce0d4300.pth')

            state_dict.pop('last_linear.bias')
            state_dict.pop('last_linear.weight')
            self.img_encoder.load_state_dict(state_dict, strict=False)

            self.img_fc = nn.Linear(2048, 256)

        self.dropout = nn.Dropout(0.5)
        self.cls = nn.Linear(512, 9)
    def __init__(self,
                 num_classes=5,
                 num_filters=32,
                 pretrained=True,
                 is_deconv=False,
                 cls_only=False):

        super().__init__()
        self.num_classes = num_classes
        self.pool = nn.MaxPool2d(2, 2)
        self.encoder = se_resnext101_32x4d()

        self.relu = nn.ReLU(inplace=True)
        self.conv1 = self.encoder.layer0
        self.conv2 = self.encoder.layer1
        self.conv3 = self.encoder.layer2
        self.conv4 = self.encoder.layer3
        self.conv5 = self.encoder.layer4
        self.center = FPAv2(2048, num_filters * 8)  #

        self.dec5 = DecoderBlockBN(2048 + num_filters * 8, num_filters * 8 * 2,
                                   num_filters * 8, is_deconv)
        self.dec4 = DecoderBlockBN(1024 + num_filters * 8, num_filters * 8 * 2,
                                   num_filters * 8, is_deconv)
        self.dec3 = DecoderBlockBN(512 + num_filters * 8, num_filters * 4 * 2,
                                   num_filters * 2, is_deconv)
        self.dec2 = DecoderBlockBN(256 + num_filters * 2, num_filters * 2 * 2,
                                   num_filters * 2 * 2, is_deconv)
        self.dec1 = DecoderBlockBN(num_filters * 2 * 2, num_filters * 2 * 2,
                                   num_filters, is_deconv)
        self.dec0 = ConvReluBn(num_filters, num_filters)
        self.final = nn.Conv2d(num_filters, num_classes, kernel_size=1)
        self.drop = nn.Dropout2d(0.3)

        self.cls_only = cls_only

        self.classifer = nn.Sequential(
            se_resnext101_32x4d().layer4,
            nn.Conv2d(2048, 2, 1, stride=1, padding=1, bias=True),
            GlobalAvgPool2d())
示例#4
0
文件: model.py 项目: lzj1769/PANDA
    def __init__(self, arch, num_classes=1, pretrained=True):
        super().__init__()

        # load EfficientNet
        if arch == 'se_resnext50_32x4d':
            if pretrained:
                self.base = se_resnext50_32x4d()
            else:
                self.base = se_resnext50_32x4d(pretrained=None)
            self.nc = self.base.last_linear.in_features

        elif arch == 'se_resnext101_32x4d':
            if pretrained:
                self.base = se_resnext101_32x4d()
            else:
                self.base = se_resnext101_32x4d(pretrained=None)
            self.nc = self.base.last_linear.in_features

        elif arch == 'inceptionv4':
            if pretrained:
                self.base = inceptionv4()
            else:
                self.base = inceptionv4(pretrained=None)
            self.nc = self.base.last_linear.in_features

        elif arch == 'inceptionresnetv2':
            if pretrained:
                self.base = inceptionresnetv2()
            else:
                self.base = inceptionresnetv2(pretrained=None)
            self.nc = self.base.last_linear.in_features

        self.logit = nn.Sequential(AdaptiveConcatPool2d(1), Flatten(),
                                   nn.BatchNorm1d(2 * self.nc),
                                   nn.Dropout(0.5),
                                   nn.Linear(2 * self.nc, 512), Mish(),
                                   nn.BatchNorm1d(512), nn.Dropout(0.5),
                                   nn.Linear(512, 1))
    def __init__(self, num_classes=1):
        super(model101B_DeepSupervion, self).__init__()

        self.num_classes = num_classes

        num_filters = 32

        self.encoder = se_resnext101_32x4d()

        self.relu = nn.ReLU(inplace=True)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv1 = nn.Sequential(self.encoder.layer0.conv1,
                                   self.encoder.layer0.bn1,
                                   self.encoder.layer0.relu1)

        self.conv2 = self.encoder.layer1
        self.conv3 = self.encoder.layer2
        self.conv4 = self.encoder.layer3
        self.conv5 = self.encoder.layer4

        self.center_global_pool = nn.AdaptiveAvgPool2d([1, 1])
        self.center_conv1x1 = nn.Conv2d(2048, 64, kernel_size=1)
        self.center_fc = nn.Linear(64, 2)

        self.center_se = SELayer(512 * 4)
        self.center = ImprovedIBNaDecoderBlock(512 * 4, num_filters * 8)

        self.dec5_se = SELayer(512 * 4 + num_filters * 8)
        self.dec5 = ImprovedIBNaDecoderBlock(512 * 4 + num_filters * 8,
                                             num_filters * 8)

        self.dec4_se = SELayer(256 * 4 + num_filters * 8)
        self.dec4 = ImprovedIBNaDecoderBlock(256 * 4 + num_filters * 8,
                                             num_filters * 8)

        self.dec3_se = SELayer(128 * 4 + num_filters * 8)
        self.dec3 = ImprovedIBNaDecoderBlock(128 * 4 + num_filters * 8,
                                             num_filters * 4)

        self.dec2_se = SELayer(64 * 4 + num_filters * 4)
        self.dec2 = ImprovedIBNaDecoderBlock(64 * 4 + num_filters * 4,
                                             num_filters * 4)

        self.logits_no_empty = nn.Sequential(
            ConvRelu(num_filters * 4, num_filters, 3), nn.Dropout2d(0.5),
            nn.Conv2d(num_filters, 1, kernel_size=1, padding=0))

        self.logits_final = nn.Sequential(
            ConvRelu(num_filters * 4 + 64, num_filters, 3), nn.Dropout2d(0.5),
            nn.Conv2d(num_filters, 1, kernel_size=1, padding=0))
def initialize_model(model_name,
                     embedding_dim,
                     feature_extracting,
                     use_pretrained=True):
    if model_name == "densenet161":
        model_ft = models.densenet161(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
        if model_name == "densenet169":
            model_ft = models.densenet161(pretrained=use_pretrained,
                                          memory_efficient=True)
            set_parameter_requires_grad(model_ft, feature_extracting)

            #print(model_ft)
            num_features = model_ft.classifier.in_features
            print(num_features)
            #print(embedding_dim)
            model_ft.classifier = nn.Linear(num_features, embedding_dim)
            #print(model_ft)
    if model_name == "densenet121":
        model_ft = models.densenet121(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    if model_name == "densenet201":
        model_ft = models.densenet201(pretrained=use_pretrained,
                                      memory_efficient=True)
        set_parameter_requires_grad(model_ft, feature_extracting)

        #print(model_ft)
        num_features = model_ft.classifier.in_features
        print(num_features)
        #print(embedding_dim)
        model_ft.classifier = nn.Linear(num_features, embedding_dim)
        #print(model_ft)
    elif model_name == "resnet101":
        model_ft = models.resnet101(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "resnet34":
        model_ft = models.resnet34(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_resnet50":
        model_ft = adl_resnet50(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "inceptionv3":
        model_ft = models.inception_v3(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext":
        model_ft = se_resnext101_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "seresnext50":
        model_ft = se_resnext50_32x4d(num_classes=1000)
        set_parameter_requires_grad(model_ft, feature_extracting)
        num_features = model_ft.last_linear.in_features
        model_ft.last_linear = nn.Linear(num_features, embedding_dim)
    elif model_name == "googlenet":
        model_ft = models.googlenet(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)
        #print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    elif model_name == "mobilenet2":
        model_ft = models.MobileNetV2(pretrained=use_pretrained)
        set_parameter_requires_grad(model_ft, feature_extracting)

        num_features = model_ft.classifier[1].in_features
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)

    elif model_name == "mnasnet":
        model_ft = mnasnet1_0(pretrained=use_pretrained)
        #model_ft = MnasNet()
        set_parameter_requires_grad(model_ft, feature_extracting)

        print(model_ft.classifier[0])
        print(model_ft.classifier[1])

        num_features = model_ft.classifier[1].in_features
        print(num_features)
        print(embedding_dim)
        #model_ft.classifier[0] = nn.Dropout(p=0.2, inplace=False)
        model_ft.classifier[1] = nn.Linear(num_features, embedding_dim)
        print(model_ft)
    elif model_name == "adl_googlenet":
        model_ft = GoogLeNet()
        set_parameter_requires_grad(model_ft, feature_extracting)
        print(model_ft)
        num_features = model_ft.fc.in_features
        model_ft.fc = nn.Linear(num_features, embedding_dim)

    else:
        raise ValueError

    return model_ft