ST-DTKD / src /binaryforecast /core /model /backbones.py
yellowwt's picture
Release binary forecasting code and seed42 checkpoints
21419ae verified
Raw History Blame Contribute Delete
1.89 kB
"""Shared 2D/3D feature extractors used by both CNNs and MDT teachers."""
import torch
from torch import nn
class SimpleLightCNNBackbone(nn.Module):
"""2D CNN backbone for full-disk images; output shape is (B, 512)."""
def __init__(self, in_channels: int):
super().__init__()
self.features = nn.Sequential(nn.Conv2d(in_channels, 32, kernel_size=3, padding=1), nn.BatchNorm2d(32), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(32, 64, kernel_size=3, padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(64, 128, kernel_size=3, padding=1), nn.BatchNorm2d(128), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(128, 256, kernel_size=3, padding=1), nn.BatchNorm2d(256), nn.ReLU(inplace=True), nn.MaxPool2d(2), nn.Conv2d(256, 512, kernel_size=3, padding=1), nn.BatchNorm2d(512), nn.ReLU(inplace=True), nn.AdaptiveAvgPool2d((1, 1)))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.flatten(self.features(x), 1)
class SimpleLight3DCNNBackbone(nn.Module):
"""3D CNN backbone for inputs shaped (B, C, T, H, W)."""
def __init__(self, in_channels: int):
super().__init__()
self.features = nn.Sequential(nn.Conv3d(in_channels, 32, 3, padding=1), nn.BatchNorm3d(32), nn.ReLU(inplace=True), nn.MaxPool3d((1, 2, 2), (1, 2, 2)), nn.Conv3d(32, 64, 3, padding=1), nn.BatchNorm3d(64), nn.ReLU(inplace=True), nn.MaxPool3d((1, 2, 2), (1, 2, 2)), nn.Conv3d(64, 128, 3, padding=1), nn.BatchNorm3d(128), nn.ReLU(inplace=True), nn.MaxPool3d((1, 2, 2), (1, 2, 2)), nn.Conv3d(128, 256, 3, padding=1), nn.BatchNorm3d(256), nn.ReLU(inplace=True), nn.MaxPool3d((1, 2, 2), (1, 2, 2)), nn.Conv3d(256, 512, 3, padding=1), nn.BatchNorm3d(512), nn.ReLU(inplace=True), nn.AdaptiveAvgPool3d((1, 1, 1)))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.flatten(self.features(x), 1)