File size: 1,712 Bytes
232c308
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Minimal 3D CNN for survival risk prediction — deliberately small/simple
so you can sanity-check the Cox loss pipeline before touching CMTLNet's
SFTFP/CFE/UFE/UCFC modules.

Input: single-channel (T1c) or multi-channel (e.g. T1c+T2) 3D volume,
       same preprocessing/size as the rest of your CMTLNet pipeline.
Output: scalar risk score per sample (B,)
"""

import torch
import torch.nn as nn


def conv_block(in_ch, out_ch, stride=2):
    return nn.Sequential(
        nn.Conv3d(in_ch, out_ch, kernel_size=3, stride=stride, padding=1),
        nn.BatchNorm3d(out_ch),
        nn.ReLU(inplace=True),
    )


class SurvivalNet3D(nn.Module):
    def __init__(self, in_channels: int = 1, base_ch: int = 16):
        super().__init__()
        self.encoder = nn.Sequential(
            conv_block(in_channels, base_ch),       # /2
            conv_block(base_ch, base_ch * 2),        # /4
            conv_block(base_ch * 2, base_ch * 4),    # /8
            conv_block(base_ch * 4, base_ch * 8),    # /16
            conv_block(base_ch * 8, base_ch * 8),    # /32
        )
        self.gap = nn.AdaptiveAvgPool3d(1)
        self.head = nn.Sequential(
            nn.Linear(base_ch * 8, 64),
            nn.ReLU(inplace=True),
            nn.Dropout(0.3),
            nn.Linear(64, 1),
        )

    def forward(self, x):
        # x: (B, C, D, H, W)
        feat = self.encoder(x)
        feat = self.gap(feat).flatten(1)
        risk = self.head(feat).squeeze(-1)  # (B,)
        return risk


if __name__ == "__main__":
    # quick shape sanity check
    model = SurvivalNet3D(in_channels=1)
    x = torch.randn(2, 1, 160, 192, 160)
    out = model(x)
    print("output shape:", out.shape)  # expect (2,)