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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
- """
- EPA Sequence-to-Sequence Model
- see https://sladewinter.medium.com/video-frame-prediction-using-convlstm-network-in-pytorch-b5210a6ce582
- """
- import torch
- import torch.nn as nn
- # Original ConvLSTM cell as proposed by Shi et al.
- class ConvLSTMCell(nn.Module):
- def __init__(self, in_channels, out_channels,
- kernel_size, padding, frame_size, activation='tanh'):
- super(ConvLSTMCell, self).__init__()
- if activation == "tanh":
- self.activation = torch.tanh
- elif activation == "relu":
- self.activation = torch.relu
-
- # Idea adapted from https://github.com/ndrplz/ConvLSTM_pytorch
- self.conv = nn.Conv2d(
- in_channels=in_channels + out_channels,
- out_channels=4 * out_channels,
- kernel_size=kernel_size,
- padding=padding)
- # Initialize weights for Hadamard Products
- self.W_ci = nn.Parameter(torch.Tensor(out_channels, *frame_size))
- self.W_co = nn.Parameter(torch.Tensor(out_channels, *frame_size))
- self.W_cf = nn.Parameter(torch.Tensor(out_channels, *frame_size))
- def forward(self, X, H_prev, C_prev):
- # Idea adapted from https://github.com/ndrplz/ConvLSTM_pytorch
- conv_output = self.conv(torch.cat([X, H_prev], dim=1))
- # Idea adapted from https://github.com/ndrplz/ConvLSTM_pytorch
- i_conv, f_conv, C_conv, o_conv = torch.chunk(conv_output, chunks=4, dim=1)
- input_gate = torch.sigmoid(i_conv + self.W_ci * C_prev )
- forget_gate = torch.sigmoid(f_conv + self.W_cf * C_prev )
- # Current Cell output
- C = forget_gate*C_prev + input_gate * self.activation(C_conv)
- output_gate = torch.sigmoid(o_conv + self.W_co * C )
- # Current Hidden State
- H = output_gate * self.activation(C)
- return H, C
- class ConvLSTM(nn.Module):
- def __init__(self, in_channels, out_channels,
- kernel_size, padding, frame_size,
- activation='tanh', device='cuda'):
- super(ConvLSTM, self).__init__()
- self.device = device
- self.out_channels = out_channels
- # We will unroll this over time steps
- self.convLSTMcell = ConvLSTMCell(
- in_channels, out_channels,
- kernel_size, padding, frame_size, activation
- )
- 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=self.device)
-
- # Initialize Hidden State
- H = torch.zeros(batch_size, self.out_channels,
- height, width, device=self.device)
- # Initialize Cell Input
- C = torch.zeros(batch_size,self.out_channels,
- height, width, device=self.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
- class EpaSeq2Seq(nn.Module):
- def __init__(self, in_channels, out_channels, frame_size,
- num_kernels=64, kernel_size=(3, 3), padding=(1, 1),
- num_layers=2, activation='tanh', device='cuda'):
- super(EpaSeq2Seq, self).__init__()
- self.sequential = nn.Sequential()
- # Add First layer (Different in_channels than the rest)
- self.sequential.add_module(
- "convlstm1", ConvLSTM(
- in_channels=in_channels, out_channels=num_kernels,
- kernel_size=kernel_size, padding=padding,
- frame_size=frame_size, activation=activation, device=device)
- )
- self.sequential.add_module(
- "batchnorm1", nn.BatchNorm3d(num_features=num_kernels)
- )
- # Add rest of the layers
- for l in range(2, num_layers+1):
- self.sequential.add_module(
- f"convlstm{l}", ConvLSTM(
- in_channels=num_kernels, out_channels=num_kernels,
- kernel_size=kernel_size, padding=padding,
- frame_size=frame_size, activation=activation,
- device=device)
- )
-
- self.sequential.add_module(
- f"batchnorm{l}", nn.BatchNorm3d(num_features=num_kernels)
- )
- # Add Convolutional Layer to predict output frame
- self.conv = nn.Conv2d(
- in_channels=num_kernels, out_channels=out_channels,
- kernel_size=kernel_size, padding=padding)
- def forward(self, X):
- # Forward propagation through all the layers
- seq_output = self.sequential(X)
- _, _, seq_len, _, _ = seq_output.size()
- # Apply convolutional layer to each element of series
- return torch.stack([
- self.conv(seq_output[:, :, time_step])
- for time_step in range(seq_len)
- ], dim=2)
|