File size: 1,987 Bytes
1423dc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
from .sinc_conv import TimeSincExtractor, FreqSincExtractor
from .patchify import Patchify
from .csp_tiny_layer import CSPTinyLayer

class TinyVAD(nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels, patch_size, num_blocks, sinc_conv, ssm):
        super(TinyVAD, self).__init__()

        self.sinc_conv = sinc_conv

        if self.sinc_conv:
            # self.extractor = TimeSincExtractor(out_channels=64, kernel_size=101, range_constraint=True, stride=2)
            self.extractor = FreqSincExtractor(out_channels=64, kernel_size=101, range_constraint=True, stride=2)

        self.patchify = Patchify(in_channels, hidden_channels, patch_size)

        self.csp_tiny_layer1 = CSPTinyLayer(hidden_channels, hidden_channels, num_blocks, ssm)
        self.csp_tiny_layer2 = CSPTinyLayer(hidden_channels, hidden_channels, num_blocks, ssm)
        self.csp_tiny_layer3 = CSPTinyLayer(hidden_channels, out_channels, num_blocks, ssm)

        self.avg_pool = nn.AdaptiveAvgPool2d(1)

        self.classifier = nn.Sequential(
            nn.Linear(out_channels, 1),
            # nn.Sigmoid()
        )

    def forward(self, x):
        if self.sinc_conv: 
            x = self.extractor(x, None)
            x = x[0]  # Untuple

        x = self.patchify(x)

        x = self.csp_tiny_layer1(x)
        x = self.csp_tiny_layer2(x)
        x = self.csp_tiny_layer3(x)

        x = self.avg_pool(x).view(x.size(0), -1)

        x = self.classifier(x)

        return x

    def predict(self, inputs):
        logits = self.forward(inputs)
        probs = torch.sigmoid(logits)

        return probs

if __name__ == "__main__":
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"Using device: {device}")

    model = TinyVAD(1, 32, 64, 2, 2, False, False).to(device)
    print(model)
    dummy_input = torch.randn(1, 1, 64, 16).to(device)
    output = model(dummy_input)
    print(output)