-
Notifications
You must be signed in to change notification settings - Fork 1
/
ConvLSTM.py
47 lines (30 loc) · 1.28 KB
/
ConvLSTM.py
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
import torch
import torch.nn as nn
from ConvLSTMCell import ConvLSTMCell
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class ConvLSTM(nn.Module):
def __init__(self, in_channels, out_channels,
kernel_size, padding, activation, frame_size):
super(ConvLSTM, self).__init__()
self.out_channels = out_channels
# We will unroll this over time steps
self.convLSTMcell = ConvLSTMCell(in_channels, out_channels,
kernel_size, padding, activation, frame_size)
def forward(self, X):
# X is a frame sequence (batch_size, num_channels, seq_len, height, width)
# Get the dimensions
batch_size, _, seq_len, height, width = X.size()
# Initialize output
output = torch.zeros(batch_size, self.out_channels, seq_len,
height, width, device=device)
# Initialize Hidden State
H = torch.zeros(batch_size, self.out_channels,
height, width, device=device)
# Initialize Cell Input
C = torch.zeros(batch_size,self.out_channels,
height, width, device=device)
# Unroll over time steps
for time_step in range(seq_len):
H, C = self.convLSTMcell(X[:,:,time_step], H, C)
output[:,:,time_step] = H
return output