File size: 10,177 Bytes
fe39cc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
"""
Single Target Strategy Implementations
Handles different feature extraction strategies for single-target classification
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
from enum import Enum
from typing import Union, Tuple, Optional


class SingleTargetStrategy(Enum):
    """Enumeration of single-target classification strategies"""
    DIRECT_CLASSIFICATION_HEAD = "Direct classification head"
    CLS_TOKEN_CLASSIFICATION = "CLS token classification"
    GLOBAL_AVERAGE_POOLING = "Global average pooling"


class FeatureExtractor(nn.Module):
    """Base class for feature extraction strategies"""
    
    def __init__(self, strategy: SingleTargetStrategy):
        super().__init__()
        self.strategy = strategy
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Extract features based on the strategy"""
        raise NotImplementedError


class DirectClassificationHeadExtractor(FeatureExtractor):
    """
    Direct classification head strategy for CNN-based models
    Uses global average pooling followed by classification head
    """
    
    def __init__(self, input_dim: int, feature_dim: int = 512, dropout: float = 0.1):
        super().__init__(SingleTargetStrategy.DIRECT_CLASSIFICATION_HEAD)
        
        # Global average pooling for spatial features
        self.global_pool = nn.AdaptiveAvgPool2d((1, 1))
        
        # Feature processing layers
        self.feature_processor = nn.Sequential(
            nn.Flatten(),
            nn.Linear(input_dim, feature_dim),
            nn.ReLU(inplace=True),
            nn.Dropout(dropout),
            nn.BatchNorm1d(feature_dim)
        )
        
        self.output_dim = feature_dim
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Extract features using direct classification head approach
        
        Args:
            x: Input features [B, C, H, W] (spatial features from CNN) or [B, input_dim] (flattened features)
            
        Returns:
            Processed features [B, feature_dim]
        """
        # Check if input is spatial (4D) or flattened (2D)
        if x.dim() == 4:
            # Spatial features: apply global average pooling first
            pooled = self.global_pool(x)  # [B, C, 1, 1]
            features = self.feature_processor(pooled)  # [B, feature_dim]
        elif x.dim() == 2:
            # Flattened features: skip global pooling, go directly to feature processing
            # But we need to adjust the input dimension for the linear layer
            # The input_dim in the constructor was for spatial features, but now we have flattened features
            # We need to create a new feature processor for the flattened input
            if not hasattr(self, 'flattened_processor'):
                # Create a new processor for flattened features
                self.flattened_processor = nn.Sequential(
                    nn.Linear(x.size(1), self.output_dim),  # x.size(1) is the actual flattened dimension
                    nn.ReLU(inplace=True),
                    nn.Dropout(self.feature_processor[3].p),  # Use same dropout rate
                    nn.LayerNorm(self.output_dim)  # Use LayerNorm instead of BatchNorm to avoid single-sample issues
                )
                # Move to the same device as the input tensor
                self.flattened_processor = self.flattened_processor.to(x.device)
            features = self.flattened_processor(x)  # [B, feature_dim]
        else:
            raise ValueError(f"Expected 2D or 4D input tensor, got {x.dim()}D tensor with shape {x.shape}")
        
        return features


class CLSTokenClassificationExtractor(FeatureExtractor):
    """
    CLS token classification strategy for Transformer-based models
    Extracts the CLS token from transformer output
    """
    
    def __init__(self, feature_dim: int = 768, dropout: float = 0.1):
        super().__init__(SingleTargetStrategy.CLS_TOKEN_CLASSIFICATION)
        
        # Feature processing for CLS token
        self.feature_processor = nn.Sequential(
            nn.Linear(feature_dim, feature_dim),
            nn.ReLU(inplace=True),
            nn.Dropout(dropout),
            nn.LayerNorm(feature_dim)
        )
        
        self.output_dim = feature_dim
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Extract CLS token features from transformer output
        
        Args:
            x: Transformer output [B, seq_len, feature_dim] or [B, feature_dim]
            
        Returns:
            CLS token features [B, feature_dim]
        """
        # Handle different input shapes
        if x.dim() == 3:  # [B, seq_len, feature_dim]
            # Extract CLS token (first token)
            cls_token = x[:, 0, :]  # [B, feature_dim]
        elif x.dim() == 2:  # [B, feature_dim] - already extracted
            cls_token = x
        else:
            raise ValueError(f"Unexpected input shape: {x.shape}")
        
        # Process CLS token features
        features = self.feature_processor(cls_token)  # [B, feature_dim]
        
        return features


class GlobalAveragePoolingExtractor(FeatureExtractor):
    """
    Global average pooling strategy for models with spatial feature maps
    Used for VAE encoders and other models that output spatial features
    """
    
    def __init__(self, input_dim: int, feature_dim: int = 512, dropout: float = 0.1):
        super().__init__(SingleTargetStrategy.GLOBAL_AVERAGE_POOLING)
        
        # Global average pooling
        self.global_pool = nn.AdaptiveAvgPool2d((1, 1))
        
        # Feature processing layers
        self.feature_processor = nn.Sequential(
            nn.Flatten(),
            nn.Linear(input_dim, feature_dim),
            nn.ReLU(inplace=True),
            nn.Dropout(dropout),
            nn.BatchNorm1d(feature_dim)
        )
        
        self.output_dim = feature_dim
    
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        Extract features using global average pooling
        
        Args:
            x: Input features [B, C, H, W] (spatial features)
            
        Returns:
            Processed features [B, feature_dim]
        """
        # Apply global average pooling
        pooled = self.global_pool(x)  # [B, C, 1, 1]
        
        # Process through feature layers
        features = self.feature_processor(pooled)  # [B, feature_dim]
        
        return features


def create_feature_extractor(
    strategy: Union[str, SingleTargetStrategy], 
    input_dim: int, 
    feature_dim: int = 512, 
    dropout: float = 0.1
) -> FeatureExtractor:
    """
    Factory function to create appropriate feature extractor
    
    Args:
        strategy: Single-target strategy (string or enum)
        input_dim: Input feature dimension
        feature_dim: Output feature dimension
        dropout: Dropout rate
        
    Returns:
        Appropriate FeatureExtractor instance
    """
    # Convert string to enum if needed
    if isinstance(strategy, str):
        strategy = SingleTargetStrategy(strategy)
    
    if strategy == SingleTargetStrategy.DIRECT_CLASSIFICATION_HEAD:
        return DirectClassificationHeadExtractor(input_dim, feature_dim, dropout)
    
    elif strategy == SingleTargetStrategy.CLS_TOKEN_CLASSIFICATION:
        return CLSTokenClassificationExtractor(feature_dim, dropout)
    
    elif strategy == SingleTargetStrategy.GLOBAL_AVERAGE_POOLING:
        return GlobalAveragePoolingExtractor(input_dim, feature_dim, dropout)
    
    else:
        raise ValueError(f"Unknown single-target strategy: {strategy}")


def extract_features_from_model_output(
    model_output: torch.Tensor, 
    strategy: Union[str, SingleTargetStrategy],
    input_dim: Optional[int] = None,
    feature_dim: int = 512,
    dropout: float = 0.1
) -> torch.Tensor:
    """
    Extract features from model output based on strategy
    
    Args:
        model_output: Raw output from backbone model
        strategy: Single-target strategy to use
        input_dim: Input dimension (required for some strategies)
        feature_dim: Output feature dimension
        dropout: Dropout rate
        
    Returns:
        Extracted features [B, feature_dim]
    """
    # Convert string to enum if needed
    if isinstance(strategy, str):
        strategy = SingleTargetStrategy(strategy)
    
    if strategy == SingleTargetStrategy.DIRECT_CLASSIFICATION_HEAD:
        if input_dim is None:
            raise ValueError("input_dim required for DIRECT_CLASSIFICATION_HEAD strategy")
        extractor = DirectClassificationHeadExtractor(input_dim, feature_dim, dropout)
        return extractor(model_output)
    
    elif strategy == SingleTargetStrategy.CLS_TOKEN_CLASSIFICATION:
        extractor = CLSTokenClassificationExtractor(feature_dim, dropout)
        return extractor(model_output)
    
    elif strategy == SingleTargetStrategy.GLOBAL_AVERAGE_POOLING:
        if input_dim is None:
            raise ValueError("input_dim required for GLOBAL_AVERAGE_POOLING strategy")
        extractor = GlobalAveragePoolingExtractor(input_dim, feature_dim, dropout)
        return extractor(model_output)
    
    else:
        raise ValueError(f"Unknown single-target strategy: {strategy}")


# Strategy mapping from config string values to enum values
STRATEGY_MAPPING = {
    "Direct classification head": SingleTargetStrategy.DIRECT_CLASSIFICATION_HEAD,
    "CLS token classification": SingleTargetStrategy.CLS_TOKEN_CLASSIFICATION,
    "Global average pooling": SingleTargetStrategy.GLOBAL_AVERAGE_POOLING,
}


def get_strategy_from_name(strategy_name: str) -> SingleTargetStrategy:
    """
    Convert strategy string value to SingleTargetStrategy enum.
    
    Args:
        strategy_name: Strategy string from config/checkpoint
        
    Returns:
        SingleTargetStrategy enum value
    """
    if strategy_name not in STRATEGY_MAPPING:
        raise ValueError(
            f"Unknown strategy: {strategy_name}. Available: {list(STRATEGY_MAPPING.keys())}"
        )
    
    return STRATEGY_MAPPING[strategy_name]