File size: 4,841 Bytes
961cf0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Author: André Pacheco
E-mail: pacheco.comp@gmail.com

This file implements the Context Guided Cell (GCell)
and a full MetaNet + ResNet-50 model.

Paper:
Fusing Metadata and Dermoscopy Images for Skin Disease Diagnosis
IEEE Journal of Biomedical and Health Informatics, 2020
https://ieeexplore.ieee.org/document/9098645
"""

import timm
import torch
import torch.nn as nn
import torch.nn.functional as F


# =====================================================
# MetaNet block (Context Guided Cell)
# =====================================================
class MetaNet(nn.Module):
    """
    Metadata-driven channel attention (MetaNet / GCell)

    metadata (B, meta_dim)  →  (B, C, 1, 1)
    feat_maps (B, C, H, W)  →  gated feature maps
    """

    def __init__(self, in_channels: int, middle_channels: int, out_channels: int):
        super().__init__()
        self.metanet = nn.Sequential(
            nn.Conv2d(in_channels, middle_channels, kernel_size=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(middle_channels, out_channels, kernel_size=1),
            nn.Sigmoid()
        )

    def forward(self, feat_maps: torch.Tensor, metadata: torch.Tensor) -> torch.Tensor:
        """
        feat_maps: (B, C, H, W)
        metadata:  (B, meta_dim)
        """
        m = metadata.unsqueeze(-1).unsqueeze(-1)  # (B, meta_dim, 1, 1)
        attn = self.metanet(m)                    # (B, C, 1, 1)
        return feat_maps * attn


# =====================================================
# MetaNet + ResNet-50 model
# =====================================================
class MetaNetModel(nn.Module):
    """
    MetaNet + ResNet-50 (faithful to IEEE JBHI paper)
    """

    def __init__(
        self,
        meta_dim: int,
        num_classes: int = 6,
        dropout_fraction: float = 0.3,
        image_encoder: str = "resnet50",
        pretrained: bool = True,
        unfreeze_weights: bool = False
    ):
        super().__init__()

        self.meta_dim = meta_dim
        self.num_classes = num_classes
        self.dropout_fraction = dropout_fraction
        self.image_encoder = image_encoder
        self.pretrained = pretrained
        self.unfreeze_weights = unfreeze_weights

        # =====================================================
        # 1) CNN backbone (conv features only)
        # =====================================================
        self.backbone = timm.create_model(
            self.image_encoder,
            pretrained=self.pretrained,
            num_classes=0,      # ❗ remove FC
            global_pool=""      # ❗ remove GAP → retorna (B,C,H,W)
        )

        self.feat_dim = self.backbone.num_features  # 2048 for resnet50

        if not self.unfreeze_weights:
            for p in self.backbone.parameters():
                p.requires_grad = False

        # =====================================================
        # 2) MetaNet attention (metadata → channel gates)
        # =====================================================
        self.metanet = MetaNet(
            in_channels=self.meta_dim,
            middle_channels=128,
            out_channels=self.feat_dim
        )

        # =====================================================
        # 3) Classifier (after GAP)
        # =====================================================
        self.classifier = self.fc_mlp_module(self.feat_dim)

    # -----------------------------------------------------
    # MLP classifier (stronger than single FC)
    # -----------------------------------------------------
    def fc_mlp_module(self, input_dim: int) -> nn.Module:
        return nn.Sequential(
            nn.Linear(input_dim, input_dim),
            nn.LayerNorm(input_dim),
            nn.ReLU(inplace=True),
            nn.Dropout(self.dropout_fraction),

            nn.Linear(input_dim, input_dim // 2),
            nn.LayerNorm(input_dim // 2),
            nn.ReLU(inplace=True),
            nn.Dropout(self.dropout_fraction),

            nn.Linear(input_dim // 2, self.num_classes)
        )

    # =====================================================
    # Forward
    # =====================================================
    def forward(self, image: torch.Tensor, metadata: torch.Tensor) -> torch.Tensor:
        """
        image:    (B, 3, 224, 224)
        metadata: (B, meta_dim)
        """

        # 1) CNN feature maps
        feat_maps = self.backbone(image)          # (B, 2048, H, W)

        # 2) Metadata-guided channel attention
        feat_maps = self.metanet(feat_maps, metadata)

        # 3) Global Average Pooling
        pooled = F.adaptive_avg_pool2d(feat_maps, 1).flatten(1)  # (B, 2048)

        # 4) Classification
        logits = self.classifier(pooled)
        return logits