File size: 1,888 Bytes
21419ae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | """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)
|