File size: 1,702 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""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