Download src/binaryforecast/core/model/cnn.py from SPARK-LAB/ST-DTKD: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/SPARK-LAB/ST-DTKD/resolve/main/src/binaryforecast/core/model/cnn.py
- Command line
-
hf download hf://SPARK-LAB/ST-DTKD/src/binaryforecast/core/model/cnn.py
-
curl -L -o cnn.py https://huggingface.co/SPARK-LAB/ST-DTKD/resolve/main/src/binaryforecast/core/model/cnn.py
1.7 kB
| """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 | |