Hi @ptrblck,
I’m trying to build a DANN model from this paper. The architecture is given in the figure below.
For the feature extractor (green part) I’d like to use a pretrained model, e.g. resnet50. I’m just wondering if my code is correct.
class ReverseLayerF(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, alpha):
        ctx.alpha = alpha
        return x.view_as(x)

    @staticmethod
    def backward(ctx, grad_output):
        output = grad_output.neg() * ctx.alpha
        return output, None


class DANN(torch.nn.Module):
    def __init__(self,architecture,pretrained=True):
        super(DANN, self).__init__()
        assert architecture in ['shuffle05', 'mobile', 'resnet50']
        if architecture == 'shuffle05':
            model = models.shufflenet_v2_x0_5(pretrained=pretrained)
        elif architecture == 'mobile':
            model = models.mobilenet_v2(pretrained=pretrained)
        else:
            model = models.resnet50(pretrained=pretrained)
        self.feature_extractor = torch.nn.Sequential(*list(model.children())[:-1])

        self.label_classifier = torch.nn.Sequential(
            torch.nn.Linear(2048,128)
            ,torch.nn.BatchNorm1d(128)
            ,torch.nn.Dropout2d()
            ,torch.nn.ReLU(True)
            ,torch.nn.Linear(128,64)
            ,torch.nn.BatchNorm1d(64)
            ,torch.nn.ReLU(True)
            ,torch.nn.Linear(64,1)
        )

        self.domain_classifier = torch.nn.Sequential(
            torch.nn.Linear(2048, 128)
            ,torch.nn.BatchNorm1d(128)
            ,torch.nn.ReLU(True)
            ,torch.nn.Linear(128,1)
        )

    def forward(self,x,alpha):
        feature = self.feature_extractor(x)
        feature = feature.view(feature.shape[0],-1)
        reverse_feature = ReverseLayerF.apply(feature, alpha)
        class_logit = self.label_classifier(feature)
        domain_logit = self.domain_classifier(reverse_feature)
        return class_logit,domain_logit

my_model = DANN('resnet50')

image980×428 123 KB