File size: 8,289 Bytes
1f2e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Model Factory - Stage 6
Factory functions for creating timm models with custom classifier heads.
"""

from typing import Optional

import torch
import torch.nn as nn
import timm


# Supported model architectures
SUPPORTED_MODELS = {
    'efficientnet_b0': 'efficientnet_b0',
    'resnet50': 'resnet50',
    'convnext_tiny': 'convnext_tiny',
}


def create_model(config: dict) -> nn.Module:
    """
    Create a timm model with custom classifier head for brain tumor classification.
    
    Loads pretrained ImageNet weights and modifies the classifier head to output
    4 classes (glioma, meningioma, notumor, pituitary). Applies dropout and
    prepares the model for training.
    
    Supported models:
        - efficientnet_b0: ~5M params, best accuracy per VRAM
        - resnet50: ~25M params, standard baseline
        - convnext_tiny: ~28M params, modern architecture
    
    Args:
        config: Configuration dictionary with structure:
            {
                'model': {
                    'name': str,           # Model architecture name
                    'pretrained': bool,    # Whether to use pretrained weights
                    'drop_rate': float     # Dropout rate for classifier head
                },
                'data': {
                    'num_classes': int     # Number of output classes
                }
            }
    
    Returns:
        Model ready for training with modified classifier head
        
    Raises:
        ValueError: If model name is not supported
        KeyError: If required config keys are missing
        
    Example:
        >>> config = {
        ...     'model': {'name': 'efficientnet_b0', 'pretrained': True, 'drop_rate': 0.2},
        ...     'data': {'num_classes': 4}
        ... }
        >>> model = create_model(config)
        >>> model = model.to('cuda')
        >>> # Optionally convert to channels_last
        >>> model = model.to(memory_format=torch.channels_last)
    """
    # Extract config parameters
    try:
        model_name = config['model']['name']
        pretrained = config['model']['pretrained']
        drop_rate = config['model'].get('drop_rate', 0.0)  # Optional, default to 0.0
        num_classes = config['data']['num_classes']
    except KeyError as e:
        raise KeyError(f"Missing required config key: {e}")
    
    # Validate model name
    if model_name not in SUPPORTED_MODELS:
        raise ValueError(
            f"Unsupported model '{model_name}'. "
            f"Supported models: {list(SUPPORTED_MODELS.keys())}"
        )
    
    # Map to timm model name
    timm_model_name = SUPPORTED_MODELS[model_name]
    
    # Create model with timm
    # For some architectures (like ConvNeXt), we need to create with num_classes directly
    # For others, we can use num_classes=0 and add a custom head
    
    # Special handling for ConvNeXt which doesn't work well with num_classes=0
    if 'convnext' in model_name.lower():
        # Create with final num_classes directly
        model = timm.create_model(
            timm_model_name,
            pretrained=pretrained,
            num_classes=num_classes,
            drop_rate=drop_rate,
            drop_path_rate=config['model'].get('drop_path_rate', 0.0)
        )
    else:
        # For other models, create without classifier and add custom head
        model = timm.create_model(
            timm_model_name,
            pretrained=pretrained,
            num_classes=0,  # Remove the classifier head
            drop_rate=drop_rate
        )
        
        # Get the number of features from the backbone
        num_features = model.num_features
        
        # Create custom classifier head
        classifier = nn.Sequential(
            nn.Dropout(p=drop_rate),
            nn.Linear(num_features, num_classes)
        )
        
        # Attach classifier to model
        # Different architectures use different attribute names for the classifier
        if hasattr(model, 'fc'):
            # ResNet uses 'fc'
            model.fc = classifier
        elif hasattr(model, 'classifier'):
            # EfficientNet and others use 'classifier'
            model.classifier = classifier
        elif hasattr(model, 'head'):
            # ConvNeXt and ViT use 'head'
            model.head = classifier
        else:
            # Fallback: try to add as 'head'
            model.head = classifier
    
    return model


def get_model_info(model: nn.Module) -> dict:
    """
    Get information about a model.
    
    Args:
        model: PyTorch model
        
    Returns:
        Dictionary with model information:
            - total_params: Total number of parameters
            - trainable_params: Number of trainable parameters
            - model_size_mb: Approximate model size in MB
    """
    total_params = sum(p.numel() for p in model.parameters())
    trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
    
    # Approximate size in MB (assuming float32)
    model_size_mb = total_params * 4 / (1024 ** 2)
    
    return {
        'total_params': total_params,
        'trainable_params': trainable_params,
        'model_size_mb': round(model_size_mb, 2)
    }


def freeze_backbone(model: nn.Module) -> nn.Module:
    """
    Freeze all parameters except the classifier head.
    
    Used for Stage 1 training (head-only).
    
    Args:
        model: Model to freeze
        
    Returns:
        Model with frozen backbone
    """
    # Freeze all parameters
    for param in model.parameters():
        param.requires_grad = False
    
    # Unfreeze classifier head
    # Check different possible attribute names
    if hasattr(model, 'fc') and model.fc is not None:
        for param in model.fc.parameters():
            param.requires_grad = True
    elif hasattr(model, 'classifier') and model.classifier is not None:
        for param in model.classifier.parameters():
            param.requires_grad = True
    elif hasattr(model, 'head') and model.head is not None:
        for param in model.head.parameters():
            param.requires_grad = True
    
    return model


def unfreeze_all(model: nn.Module) -> nn.Module:
    """
    Unfreeze all model parameters.
    
    Used for Stage 2 training (full fine-tuning).
    
    Args:
        model: Model to unfreeze
        
    Returns:
        Model with all parameters trainable
    """
    for param in model.parameters():
        param.requires_grad = True
    
    return model


def convert_to_channels_last(model: nn.Module) -> nn.Module:
    """
    Convert model to channels_last memory format for better performance.
    
    Should be called after moving model to GPU.
    
    Args:
        model: Model to convert
        
    Returns:
        Model in channels_last format
        
    Example:
        >>> model = create_model(config)
        >>> model = model.to('cuda')
        >>> model = convert_to_channels_last(model)
    """
    return model.to(memory_format=torch.channels_last)


def prepare_model_for_training(
    config: dict,
    device: str = 'cuda',
    stage: int = 1
) -> nn.Module:
    """
    Create and prepare a model for training.
    
    Convenience function that creates the model, moves it to device,
    optionally converts to channels_last, and freezes the backbone for stage 1.
    
    Args:
        config: Model configuration
        device: Device to move model to ('cuda' or 'cpu')
        stage: Training stage (1 = head only, 2 = full fine-tune)
        
    Returns:
        Model ready for training
        
    Example:
        >>> # Stage 1: head-only training
        >>> model = prepare_model_for_training(config, device='cuda', stage=1)
        >>> 
        >>> # Stage 2: full fine-tuning
        >>> model = prepare_model_for_training(config, device='cuda', stage=2)
    """
    # Create model
    model = create_model(config)
    
    # Move to device
    model = model.to(device)
    
    # Convert to channels_last if configured
    if config.get('training', {}).get('channels_last', False):
        model = convert_to_channels_last(model)
    
    # Freeze backbone for stage 1
    if stage == 1:
        model = freeze_backbone(model)
    else:
        model = unfreeze_all(model)
    
    return model