"""Single-modality classifiers for HMI/AIA baselines, CTL and KD students.""" import torch from torch import nn from .backbones import SimpleLightCNNBackbone, SimpleLight3DCNNBackbone class SimpleLightCNN(nn.Module): """2D CNN classifier using the same feature/classifier names as the baseline.""" def __init__(self, in_channels: int, num_classes: int=2, dropout: float=0.5): super().__init__() self.features = SimpleLightCNNBackbone(in_channels).features self.classifier = nn.Sequential(nn.Linear(512, 128), nn.ReLU(inplace=True), nn.Dropout(dropout), nn.Linear(128, num_classes)) def extract_features(self, x: torch.Tensor) -> torch.Tensor: return torch.flatten(self.features(x), 1) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.classifier(self.extract_features(x)) class SimpleLight3DCNN(nn.Module): def __init__(self, in_channels: int, num_classes: int=2, dropout: float=0.5): super().__init__() self.features = SimpleLight3DCNNBackbone(in_channels).features self.classifier = nn.Sequential(nn.Linear(512, 128), nn.ReLU(inplace=True), nn.Dropout(dropout), nn.Linear(128, num_classes)) def extract_features(self, x: torch.Tensor) -> torch.Tensor: return torch.flatten(self.features(x), 1) def forward(self, x: torch.Tensor) -> torch.Tensor: return self.classifier(self.extract_features(x)) class SimpleLight3DCNNWithFeature(SimpleLight3DCNN): def forward(self, x: torch.Tensor, return_features: bool=True): features = self.extract_features(x) logits = self.classifier(features) return (logits, features) if return_features else logits