Esempio n. 1
0
    def __init__(
            self,
            encoder_weights: str = None,
            encoder_freeze: bool = True,
            decoder_use_batchnorm: bool = True,
            decoder_channels: List[int] = (256, 128, 64, 32, 16),
            classes: int = 1,
            activation: Optional[Union[str, callable]] = "sigmoid",
            device=torch.device("cpu"),
    ):
        super().__init__()

        self.encoder = moco_r50(encoder_weights,
                                encoder_freeze=encoder_freeze,
                                map_location=device)

        self.decoder = UnetDecoder(
            encoder_channels=(12, 64, 256, 512, 1024, 2048),
            decoder_channels=decoder_channels,
            n_blocks=5,
            use_batchnorm=decoder_use_batchnorm,
            center=False,
            attention_type=None,
        )

        self.segmentation_head = torch.nn.Sequential(
            torch.nn.AdaptiveAvgPool2d((1, 1)),
            torch.nn.Flatten(),
            torch.nn.Linear(16, classes),
            Activation(activation),
        )

        self.classification_head = None
        self.initialize()
Esempio n. 2
0
    def __init__(self, **kwargs):
        super(Multibranch_resnet50_noclassification, self).__init__()
        self.model = smp.Unet('resnet50',
                              in_channels=in_channels,
                              classes=classes,
                              activation=activation,
                              **kwargs)

        self.distancemap_branch = UnetDecoder(
            encoder_channels=self.model.encoder.out_channels,
            decoder_channels=(256, 128, 64, 32,
                              16),  # List[int] = (256, 128, 64, 32, 16),
            n_blocks=5,  # int = 5,
            use_batchnorm=True,  # bool = True,
            center=False,  #True if encoder_name.startswith("vgg") else False,
            attention_type=None,  # Optional[str] = None,
        )
        self.distancemap_head = DistanceMapHead(
            in_channels=
            16,  # 原来此处为decoder_channels[-1],而该参数默认值为decoder_channels: List[int] = (256, 128, 64, 32, 16)
            out_channels=classes,
            activation=activation,
            kernel_size=3,
        )
        initialize_decoder(self.distancemap_branch)
        initialize_head(self.distancemap_head)
Esempio n. 3
0
    def __init__(
        self,
        encoder_name: str = "resnet34",
        encoder_depth: int = 5,
        encoder_weights: str = "imagenet",
        decoder_use_batchnorm: bool = True,
        decoder_channels: List[int] = (256, 128, 64, 32, 16),
        decoder_attention_type: Optional[str] = None,
        in_channels: int = 3,
        classes: int = 1,
        activation: Optional[Union[str, callable]] = None,
        pretext_classes=-1,
        domain_classes=2,
    ):
        super().__init__()

        self.encoder = get_encoder(
            encoder_name,
            in_channels=in_channels,
            depth=encoder_depth,
            weights=encoder_weights,
        )

        self.decoder = UnetDecoder(
            encoder_channels=self.encoder.out_channels,
            decoder_channels=decoder_channels,
            n_blocks=encoder_depth,
            use_batchnorm=decoder_use_batchnorm,
            center=True if encoder_name.startswith("vgg") else False,
            attention_type=decoder_attention_type,
        )

        self.segmentation_head = SegmentationHead(
            in_channels=decoder_channels[-1],
            out_channels=classes,
            activation=activation,
            kernel_size=3,
        )

        self.domain_layer = -2
        self.domain_classification_head = DomainClassifier(
            in_channels=self.encoder.out_channels[self.domain_layer],
            domain_classes=domain_classes)

        if pretext_classes == -1:
            raise ValueError(f'initialize pretext_classes')
        self.pretext_classification_head = PretextClassifier(
            in_channels=self.encoder.out_channels[-1],
            pretext_classes=pretext_classes)

        self.name = "u-{}".format(encoder_name)
        self.initialize()
        return
Esempio n. 4
0
 def __init__(self):
     super(EfficientNet_3_unet, self).__init__()
     self.model_encoder = EfficientNet_3_Encoder()
     self.model_decoder = UnetDecoder(encoder_channels=(1536, 136, 48, 32, 24),
                                      decoder_channels=(256, 128, 64, 32, 16),
                                      final_channels=1,
                                      use_batchnorm=True,
                                      center=False,
                                      )
     self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
     self.cls_head = nn.Sequential(nn.Linear(2048, 2048, bias=True), nn.Linear(2048, 1, bias=True))
     self.fea_bn = nn.BatchNorm1d(512)
     self.fea_bn.bias.requires_grad_(False)
Esempio n. 5
0
    def __init__(
        self,
        encoder_name: str = "resnet34",
        encoder_depth: int = 5,
        encoder_weights: str = "imagenet",
        decoder_use_batchnorm: bool = True,
        decoder_channels: List[int] = (256, 128, 64, 32, 16),
        in_channels: int = 3,
    ):
        super().__init__()

        self.encoder = get_encoder(
            encoder_name,
            in_channels=in_channels,
            depth=encoder_depth,
            weights=encoder_weights,
        )

        self.decoder = UnetDecoder(
            encoder_channels=self.encoder.out_channels,
            decoder_channels=decoder_channels,
            n_blocks=encoder_depth,
            use_batchnorm=decoder_use_batchnorm,
            center=True if encoder_name.startswith("vgg") else False,
            attention_type=None,
        )

        self.xydir_head = EncoderRegressionHead(
            in_channels=self.encoder.out_channels[-1],
            out_channels=2,
        )

        self.height_head = RegressionHead(
            in_channels=decoder_channels[-1],
            out_channels=1,
            kernel_size=3,
        )

        self.mag_head = RegressionHead(
            in_channels=decoder_channels[-1],
            out_channels=1,
            kernel_size=3,
        )

        self.scale_head = ScaleHead()

        self.name = "u-{}".format(encoder_name)
        self.initialize()
Esempio n. 6
0
def get_basenet_decoder(basenet, encoder_channels, decoder_channels):
    if basenet == 'fpn':
        return FPNDecoder(encoder_channels)
    elif basenet == 'psp':
        return PSPDecoder(encoder_channels)
    elif basenet == 'deeplabv3':
        return DeepLabV3Decoder(encoder_channels[-1], decoder_channels[-1])

    return UnetDecoder(
        encoder_channels=encoder_channels,
        decoder_channels=decoder_channels,
        n_blocks=len(decoder_channels),
        use_batchnorm=True,
        center=False,
        attention_type=None,
    )
Esempio n. 7
0
    def __init__(
        self,
        encoder_name: str = "resnet34",
        encoder_depth: int = 5,
        encoder_weights: str = "imagenet",
        decoder_use_batchnorm: bool = True,
        decoder_channels: List[int] = (256, 128, 64, 32, 16),
        decoder_attention_type: Optional[str] = None,
        in_channels: int = 3,
        classes: int = 1,
        activation: Optional[Union[str, callable]] = None,
        aux_params: Optional[dict] = None,
    ):
        super().__init__()

        self.encoder = get_encoder(
            encoder_name,
            in_channels=in_channels,
            depth=encoder_depth,
            weights=encoder_weights,
        )

        self.decoder = UnetDecoder(
            encoder_channels=self.encoder.out_channels,
            decoder_channels=decoder_channels,
            n_blocks=encoder_depth,
            use_batchnorm=decoder_use_batchnorm,
            center=True if encoder_name.startswith("vgg") else False,
            attention_type=decoder_attention_type,
        )

        self.segmentation_head = SegmentationHead(
            in_channels=decoder_channels[-1],
            out_channels=classes,
            activation=activation,
            kernel_size=3,
        )

        if aux_params is not None:
            self.classification_head = ClassificationHead(
                in_channels=self.encoder.out_channels[-1], **aux_params)
        else:
            self.classification_head = None

        self.name = "u-{}".format(encoder_name)
        self.initialize()
Esempio n. 8
0
    def __init__(self, b, num_channels):
        super().__init__()
        self.model_encoder = EfficientNet_Encoder(b)
        self.model_decoder = nn.Sequential(
            UnetDecoder(
                encoder_channels=self.model_encoder.encoder_channels,
                decoder_channels=(256, 128, 64, 32, 16),
                final_channels=num_channels,
                use_batchnorm=True,
                center=False,
            ),
            nn.LogSoftmax(dim=1),
        )

        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.cls_head = nn.Sequential(nn.Linear(2048, 2048, bias=True),
                                      nn.Linear(2048, 1, bias=True))
        self.fea_bn = nn.BatchNorm1d(512)
        self.fea_bn.bias.requires_grad_(False)
Esempio n. 9
0
    def __init__(
            self,
            encoder_name='resnet34',
            encoder_weights='imagenet',
            decoder_use_batchnorm=True,
            decoder_channels=(256, 128, 64, 32, 16),
            classes=1,
            activation='sigmoid',
            center=False,  # usefull for VGG models
            attention_type=None):
        encoder = get_encoder(encoder_name, encoder_weights=encoder_weights)

        decoder = UnetDecoder(encoder_channels=encoder.out_shapes,
                              decoder_channels=decoder_channels,
                              final_channels=classes,
                              use_batchnorm=decoder_use_batchnorm,
                              center=center,
                              attention_type=attention_type)

        super().__init__(encoder, decoder, activation)

        self.name = 'u-{}'.format(encoder_name)
Esempio n. 10
0
    def __init__(
        self,
        encoder_name: str = "resnet34",
        encoder_depth: int = 5,
        encoder_weights: str = "imagenet",
        decoder_use_batchnorm: bool = True,
        decoder_channels: List[int] = (256, 128, 64, 32, 16),
        decoder_attention_type: Optional[str] = None,
        in_channels: int = 3,
        classes: int = 1,
        activation: Optional[Union[str, callable]] = None,
        pretext_classes=-1,
        domain_classes=2,
        domain_layer=-2,
        domain_classifier='DomainClassifierFlatten',
        separate=False,
        input_shape=80,
    ):
        super().__init__()

        self.encoder = get_encoder(
            encoder_name,
            in_channels=in_channels,
            depth=encoder_depth,
            weights=encoder_weights,
        ) if separate is False else \
        get_separate_encoder(
            encoder_name,
            in_channels=in_channels,
            depth=encoder_depth,
            weights=encoder_weights,
        )
        self.separate = separate

        self.decoder = UnetDecoder(
            encoder_channels=self.encoder.out_channels,
            decoder_channels=decoder_channels,
            n_blocks=encoder_depth,
            use_batchnorm=decoder_use_batchnorm,
            center=True if encoder_name.startswith("vgg") else False,
            attention_type=decoder_attention_type,
        )

        self.segmentation_head = SegmentationHead(
            in_channels=decoder_channels[-1],
            out_channels=classes,
            activation=activation,
            kernel_size=3,
        )

        if domain_classifier == 'DomainClassifier':
            domain_classifier = DomainClassifier
        elif domain_classifier == 'DomainClassifierFlatten':
            domain_classifier = DomainClassifierFlatten
        elif domain_classifier == 'DomainClassifierFlattenSimple':
            domain_classifier = DomainClassifierFlattenSimple
        elif domain_classifier == 'DomainClassifierFlattenCat':
            domain_classifier = DomainClassifierFlattenCat
        elif domain_classifier == 'DomainClassifierReduceFlatten':
            domain_classifier = DomainClassifierReduceFlatten

        self.domain_layer = domain_layer

        if type(self.domain_layer) == list:
            self.domain_classification_head = domain_classifier(
                in_channels=sum([
                    self.encoder.out_channels[idx_dl]
                    for idx_dl in self.domain_layer
                ]),
                domain_classes=domain_classes)
            if separate:
                self.encoder.set_domain_layer(self.domain_layer)
        else:
            self.domain_classification_head = domain_classifier(
                in_channels=self.encoder.out_channels[self.domain_layer],
                domain_classes=domain_classes,
                input_shape=input_shape,
            )

        if pretext_classes == -1:
            raise ValueError(f'initialize pretext_classes')
        self.pretext_classification_head = PretextClassifier(
            in_channels=self.encoder.out_channels[-1],
            pretext_classes=pretext_classes)

        self.name = "u-{}".format(encoder_name)
        self.initialize()
        return