File size: 10,603 Bytes
d5b0af1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
fusion_model.py β€” Multi-Task Learning fusion model with dual output heads.

This is the core MTL architecture. A single model with:
  - CNN branch (ResNet-50) for spatial features
  - LSTM branch for temporal features
  - Shared FC layers fusing both representations
  - Task 1 Head: Wildfire risk heatmap (128Γ—128, sigmoid)
  - Task 2 Head: AQI forecast (72 hourly values, linear)

The shared backbone learns representations useful for BOTH tasks,
enabling knowledge transfer between wildfire risk and air quality.
"""

import logging
from typing import Dict, Optional, Tuple

import torch
import torch.nn as nn

from src.models.cnn_branch import CNNBranch
from src.models.lstm_branch import LSTMBranch
from src.training.config import (
    CNN_FEATURE_DIM, LSTM_FEATURE_DIM, FUSION_DIM,
    SHARED_FC_DIMS, DROPOUT_RATE,
    PATCH_SIZE, AQI_FORECAST_HOURS, IMAGE_CHANNELS, TIMESERIES_FEATURES,
    DEVICE,
)

logger = logging.getLogger(__name__)


class MultiTaskFusionModel(nn.Module):
    """
    Multi-Task Learning model combining CNN and LSTM branches.

    Architecture:
        CNN Branch:  (batch, 4, 128, 128) β†’ (batch, 2048)
        LSTM Branch: (batch, 7, 6) β†’ (batch, 256)
        Fusion:      concat β†’ 2304 β†’ 512 β†’ 256 (shared)
        Head 1:      256 β†’ 128Γ—128 (wildfire risk heatmap)
        Head 2:      256 β†’ 72 (AQI hourly forecast)

    This is explicitly a Multi-Task Learning (MTL) model β€” one model,
    two tasks, shared backbone, task-specific heads.
    """

    def __init__(
        self,
        pretrained_cnn: bool = True,
        freeze_cnn_early: bool = False,
    ):
        super().__init__()

        # ---- Branch 1: CNN Spatial Encoder ----
        self.cnn_branch = CNNBranch(
            in_channels=IMAGE_CHANNELS,
            out_features=CNN_FEATURE_DIM,
            pretrained=pretrained_cnn,
            freeze_early=freeze_cnn_early,
        )

        # ---- Branch 2: LSTM Temporal Encoder ----
        self.lstm_branch = LSTMBranch(
            input_size=TIMESERIES_FEATURES,
        )

        # ---- Shared Fusion Layers ----
        # Concatenate: CNN_FEATURE_DIM + LSTM_FEATURE_DIM = 2048 + 256 = 2304
        fusion_layers = []
        prev_dim = FUSION_DIM
        for dim in SHARED_FC_DIMS:
            fusion_layers.extend([
                nn.Linear(prev_dim, dim),
                nn.BatchNorm1d(dim),
                nn.ReLU(inplace=True),
                nn.Dropout(DROPOUT_RATE),
            ])
            prev_dim = dim

        self.shared_fc = nn.Sequential(*fusion_layers)

        # ---- Task 1 Head: Wildfire Risk Heatmap ----
        # 256 β†’ 128Γ—128 spatial map
        self.risk_head = nn.Sequential(
            nn.Linear(SHARED_FC_DIMS[-1], 512),
            nn.ReLU(inplace=True),
            nn.Dropout(DROPOUT_RATE),
            nn.Linear(512, 1024),
            nn.ReLU(inplace=True),
            nn.Linear(1024, PATCH_SIZE * PATCH_SIZE),
            nn.Sigmoid(),  # Output probabilities [0, 1]
        )

        # ---- Task 2 Head: AQI Forecast ----
        # (shared_dim + lstm_dim) β†’ 72 hourly values
        # Adding skip connection from lstm_features directly to prevent CNN saturation
        self.aqi_head = nn.Sequential(
            nn.Linear(SHARED_FC_DIMS[-1] + LSTM_FEATURE_DIM, 256),
            nn.ReLU(inplace=True),
            nn.Dropout(DROPOUT_RATE),
            nn.Linear(256, 128),
            nn.ReLU(inplace=True),
            nn.Linear(128, AQI_FORECAST_HOURS),
            nn.Sigmoid(),  # Bound output to [0, 1]; unscaled by Γ—500 at inference
        )

        self._log_architecture()

    def _log_architecture(self):
        """Log model architecture summary."""
        total = sum(p.numel() for p in self.parameters())
        trainable = sum(p.numel() for p in self.parameters() if p.requires_grad)
        logger.info(
            f"MultiTaskFusionModel initialized:\n"
            f"  CNN branch:  {IMAGE_CHANNELS}ch β†’ {CNN_FEATURE_DIM}d\n"
            f"  LSTM branch: {TIMESERIES_FEATURES}feat β†’ {LSTM_FEATURE_DIM}d\n"
            f"  Fusion:      {FUSION_DIM}d β†’ {SHARED_FC_DIMS}d\n"
            f"  Risk head:   {SHARED_FC_DIMS[-1]}d β†’ {PATCH_SIZE}Γ—{PATCH_SIZE}\n"
            f"  AQI head:    {SHARED_FC_DIMS[-1]}d β†’ {AQI_FORECAST_HOURS}h\n"
            f"  Parameters:  {total:,} total, {trainable:,} trainable"
        )

    def forward(
        self,
        image: torch.Tensor,
        timeseries: torch.Tensor,
    ) -> Dict[str, torch.Tensor]:
        """
        Forward pass through the full MTL model.

        Args:
            image:      (batch, 4, 128, 128) satellite image patch.
            timeseries: (batch, 7, 6) weather + AQI sequence.

        Returns:
            Dict with:
              - 'risk_map':    (batch, 128, 128) wildfire risk heatmap
              - 'aqi_forecast': (batch, 72) hourly AQI predictions
              - 'shared_features': (batch, 256) shared representation
        """
        # Extract features from both branches
        cnn_features = self.cnn_branch(image)       # (batch, 2048)
        lstm_features = self.lstm_branch(timeseries)  # (batch, 256)

        # Fuse representations
        fused = torch.cat([cnn_features, lstm_features], dim=1)  # (batch, 2304)
        shared = self.shared_fc(fused)  # (batch, 256)

        # Task-specific heads
        risk_flat = self.risk_head(shared)  # (batch, 128*128)
        risk_map = risk_flat.view(-1, PATCH_SIZE, PATCH_SIZE)  # (batch, 128, 128)

        # Skip connection: guarantee temporal structure guides regression.
        aqi_input = torch.cat([shared, lstm_features], dim=1)
        aqi_forecast = self.aqi_head(aqi_input)  # (batch, 72)

        return {
            "risk_map": risk_map,
            "aqi_forecast": aqi_forecast,
            "shared_features": shared,
        }

    def forward_cnn_only(self, image: torch.Tensor) -> Dict[str, torch.Tensor]:
        """
        Forward pass using only the CNN branch (ablation Config A).
        Uses zero-filled LSTM features.
        """
        cnn_features = self.cnn_branch(image)
        lstm_features = torch.zeros(
            image.size(0), LSTM_FEATURE_DIM, device=image.device
        )

        fused = torch.cat([cnn_features, lstm_features], dim=1)
        shared = self.shared_fc(fused)

        risk_flat = self.risk_head(shared)
        risk_map = risk_flat.view(-1, PATCH_SIZE, PATCH_SIZE)

        return {"risk_map": risk_map, "shared_features": shared}

    def forward_lstm_only(self, timeseries: torch.Tensor) -> Dict[str, torch.Tensor]:
        """
        Forward pass using only the LSTM branch (ablation Config B).
        Uses zero-filled CNN features.
        """
        cnn_features = torch.zeros(
            timeseries.size(0), CNN_FEATURE_DIM, device=timeseries.device
        )
        lstm_features = self.lstm_branch(timeseries)

        fused = torch.cat([cnn_features, lstm_features], dim=1)
        shared = self.shared_fc(fused)

        aqi_input = torch.cat([shared, lstm_features], dim=1)
        aqi_forecast = self.aqi_head(aqi_input)

        return {"aqi_forecast": aqi_forecast, "shared_features": shared}

    def get_risk_score(self, risk_map: torch.Tensor) -> Tuple[float, str]:
        """
        Compute overall risk score and level from a risk heatmap.

        Args:
            risk_map: (128, 128) risk probability map.

        Returns:
            Tuple of (risk_score, risk_level).
        """
        score = float(risk_map.mean())

        if score < 0.25:
            level = "Low"
        elif score < 0.50:
            level = "Medium"
        elif score < 0.75:
            level = "High"
        else:
            level = "Extreme"

        return score, level


def load_model(
    checkpoint_path: Optional[str] = None,
    device: str = DEVICE,
) -> MultiTaskFusionModel:
    """
    Load a MultiTaskFusionModel, optionally from a checkpoint.

    Supports both full-precision and dynamically quantized checkpoints.
    For quantized models, the architecture is first initialized, then
    dynamic quantization is applied before loading the state dict.

    Args:
        checkpoint_path: Path to .pth checkpoint file.
        device: Device to load model onto.

    Returns:
        Loaded model in eval mode.
    """
    model = MultiTaskFusionModel(pretrained_cnn=True)

    if checkpoint_path and Path(checkpoint_path).exists():
        logger.info(f"Loading checkpoint: {checkpoint_path}")
        checkpoint = torch.load(checkpoint_path, map_location=device, weights_only=False)

        if isinstance(checkpoint, dict) and "model_state_dict" in checkpoint:
            state_dict = checkpoint["model_state_dict"]
        else:
            state_dict = checkpoint

        # Detect if checkpoint is from a quantized model
        quantized_keys = [k for k in state_dict.keys() if "_packed_params" in k or "scale" in k]
        is_quantized = len(quantized_keys) > 5  # Quantized models have many such keys

        if is_quantized:
            logger.info("Detected quantized checkpoint β€” applying dynamic quantization first...")
            model = torch.quantization.quantize_dynamic(
                model,
                {torch.nn.Linear, torch.nn.LSTM},
                dtype=torch.qint8,
            )
            model.load_state_dict(state_dict, strict=False)
        else:
            model.load_state_dict(state_dict)

        logger.info("Checkpoint loaded successfully.")
    else:
        logger.info("No checkpoint provided β€” using fresh model with pretrained CNN backbone.")

    model = model.to(device)
    model.eval()
    return model


from pathlib import Path

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)

    model = MultiTaskFusionModel(pretrained_cnn=False)

    # Test full forward pass
    img = torch.randn(2, 4, 128, 128)
    ts = torch.randn(2, 7, 6)

    output = model(img, ts)
    print(f"\nFull MTL forward pass:")
    print(f"  Risk map:     {output['risk_map'].shape}")
    print(f"  AQI forecast: {output['aqi_forecast'].shape}")
    print(f"  Shared feat:  {output['shared_features'].shape}")

    # Test ablation modes
    cnn_out = model.forward_cnn_only(img)
    print(f"\nCNN-only (Config A):")
    print(f"  Risk map: {cnn_out['risk_map'].shape}")

    lstm_out = model.forward_lstm_only(ts)
    print(f"\nLSTM-only (Config B):")
    print(f"  AQI forecast: {lstm_out['aqi_forecast'].shape}")

    # Risk score
    score, level = model.get_risk_score(output["risk_map"][0])
    print(f"\nRisk score: {score:.3f} ({level})")