File size: 3,005 Bytes
4a9362b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Tiny CNN encoder and supervised TTC regressor."""

from __future__ import annotations

import torch
from torch import nn


class ResidualBlock(nn.Module):
    """Small residual block for dense event representations."""

    def __init__(self, channels: int) -> None:
        super().__init__()
        self.net = nn.Sequential(
            nn.Conv2d(channels, channels, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(channels),
            nn.SiLU(inplace=True),
            nn.Conv2d(channels, channels, kernel_size=3, padding=1, bias=False),
            nn.BatchNorm2d(channels),
        )
        self.activation = nn.SiLU(inplace=True)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Run the residual block."""

        return self.activation(x + self.net(x))


class TinyCNNEncoder(nn.Module):
    """Compact CNN encoder for dense event voxel grids."""

    def __init__(self, in_channels: int, width: int = 48) -> None:
        super().__init__()
        self.output_dim = width * 4
        self.backbone = nn.Sequential(
            nn.Conv2d(in_channels, width, kernel_size=5, stride=2, padding=2, bias=False),
            nn.BatchNorm2d(width),
            nn.SiLU(inplace=True),
            ResidualBlock(width),
            nn.Conv2d(width, width * 2, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(width * 2),
            nn.SiLU(inplace=True),
            ResidualBlock(width * 2),
            nn.Conv2d(width * 2, width * 4, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(width * 4),
            nn.SiLU(inplace=True),
            ResidualBlock(width * 4),
            nn.AdaptiveAvgPool2d((1, 1)),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Encode a batch into pooled latent vectors."""

        return self.backbone(x).flatten(1)

    def forward_feature_map(self, x: torch.Tensor) -> torch.Tensor:
        """Encode a batch into the final dense feature map before global pooling."""

        for layer in list(self.backbone.children())[:-1]:
            x = layer(x)
        return x

    def forward_tokens(self, x: torch.Tensor) -> torch.Tensor:
        """Encode a batch into dense spatial tokens."""

        feature_map = self.forward_feature_map(x)
        return feature_map.flatten(2).transpose(1, 2)


class TinyCNNRegressor(nn.Module):
    """Compact CNN predicting log-TTC from voxel grids."""

    def __init__(self, in_channels: int, width: int = 48) -> None:
        super().__init__()
        self.encoder = TinyCNNEncoder(in_channels=in_channels, width=width)
        self.head = nn.Sequential(
            nn.Linear(self.encoder.output_dim, width * 2),
            nn.SiLU(inplace=True),
            nn.Dropout(p=0.1),
            nn.Linear(width * 2, 1),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Predict log-TTC for a batch of voxel grids."""

        return self.head(self.encoder(x)).squeeze(-1)