File size: 2,593 Bytes
cc80b10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""SNN preditora: K event frames -> posicoes futuras do objeto.



Encoder conv-LIF sobre cada event frame; a membrana final da camada

de leitura alimenta uma head que regrediza M posicoes (x,y) futuras

normalizadas em [0,1].

"""
import torch
import torch.nn as nn
import torch.nn.functional as F


class SpikeFn(torch.autograd.Function):
    @staticmethod
    def forward(ctx, v_mem, v_th=1.0):
        ctx.save_for_backward(v_mem)
        ctx.v_th = v_th
        return (v_mem >= v_th).float()

    @staticmethod
    def backward(ctx, grad_out):
        (v_mem,) = ctx.saved_tensors
        x = torch.pi * (v_mem - ctx.v_th)
        return grad_out / (1.0 + x * x), None


class LIF(nn.Module):
    def __init__(self, beta=0.85, v_th=1.0):
        super().__init__()
        self.beta, self.v_th = beta, v_th

    def forward(self, x, mem):
        mem = self.beta * mem + x
        s = SpikeFn.apply(mem, self.v_th)
        return s, mem * (1.0 - s)


class ForecastSNN(nn.Module):
    """(B,K,2,64,64) -> (B, 2*n_pred) posicoes futuras."""

    def __init__(self, n_pred=5, steps_per_frame=2):
        super().__init__()
        self.steps_per_frame = steps_per_frame
        self.conv1 = nn.Conv2d(2, 16, 3, stride=2, padding=1)   # 32
        self.lif1 = LIF()
        self.conv2 = nn.Conv2d(16, 32, 3, stride=2, padding=1)  # 16
        self.lif2 = LIF(v_th=0.8)
        self.conv3 = nn.Conv2d(32, 48, 3, stride=2, padding=1)  # 8
        self.lif3 = LIF(v_th=0.6)
        self.fc1 = nn.Linear(48 * 8 * 8, 128)
        self.lif4 = LIF(v_th=0.5)
        self.head = nn.Linear(128, 2 * n_pred)
        self.lif5 = LIF(v_th=1e9)
        nn.init.normal_(self.fc1.weight, std=0.06)

    def forward(self, clip):
        B, K = clip.shape[:2]
        m1 = torch.zeros(B, 16, 32, 32, device=clip.device)
        m2 = torch.zeros(B, 32, 16, 16, device=clip.device)
        m3 = torch.zeros(B, 48, 8, 8, device=clip.device)
        m4 = torch.zeros(B, 128, device=clip.device)
        m5 = torch.zeros(B, self.head.out_features, device=clip.device)
        acc = 0.0
        for k in range(K):
            x = clip[:, k]
            for _ in range(self.steps_per_frame):
                s, m1 = self.lif1(self.conv1(x), m1)
                s, m2 = self.lif2(self.conv2(s), m2)
                s, m3 = self.lif3(self.conv3(s), m3)
                s4, m4 = self.lif4(self.fc1(s.flatten(1)), m4)
                _, m5 = self.lif5(self.head(s4), m5)
                acc = acc + m5
        return acc / (K * self.steps_per_frame)