-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
arch
committed
Nov 26, 2021
1 parent
5dd9532
commit ce300b2
Showing
8 changed files
with
272 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import torch | ||
import torch.nn as nn | ||
from utils.config import CONFIG | ||
|
||
from modules.convlstm import ConvLSTM | ||
from modules.convtransformer import CvT | ||
|
||
MODEL='model3' | ||
|
||
class Flatten(torch.nn.Module): | ||
def forward(self, input): | ||
b, seq_len, _, h, w = input.size() | ||
return input.view(b, seq_len, -1) | ||
|
||
|
||
class Model3(nn.Module): | ||
""" FunPos Model """ | ||
def __init__(self): | ||
super().__init__() | ||
|
||
reduce = 1 | ||
self.conv1 = CvT( | ||
image_size=CONFIG[MODEL]['img_height'], | ||
in_channels = CONFIG[MODEL]['img_channels'] | ||
) | ||
|
||
reduce += 1 | ||
self.conv2 = CvT( | ||
image_size=CONFIG[MODEL]['img_height'], | ||
in_channels = 16, | ||
dim = 32 | ||
) | ||
|
||
self.convlstm1 = ConvLSTM( | ||
img_size = (int(CONFIG[MODEL]['img_height']), int(CONFIG[MODEL]['img_width'])), | ||
input_dim = 32, | ||
hidden_dim = CONFIG[MODEL]['convlstm_hidden_dim']*2, | ||
kernel_size = (3,3), | ||
cnn_dropout = 0.1, | ||
rnn_dropout = 0.1, | ||
batch_first = True, | ||
bias = False, | ||
layer_norm = True, | ||
return_sequence = True, | ||
bidirectional = True | ||
) | ||
|
||
self.convlstm2 = ConvLSTM( | ||
img_size = (int(CONFIG[MODEL]['img_height']), int(CONFIG[MODEL]['img_width'])), | ||
input_dim = 256, | ||
hidden_dim = CONFIG[MODEL]['convlstm_hidden_dim']*2, | ||
kernel_size = (3,3), | ||
cnn_dropout = 0.1, | ||
rnn_dropout = 0.1, | ||
batch_first = True, | ||
bias = False, | ||
layer_norm = False, | ||
return_sequence = True, | ||
bidirectional = True | ||
) | ||
|
||
self.flatten = Flatten() | ||
|
||
self.fc1 = nn.Linear( | ||
int(2*CONFIG[MODEL]['img_width'])*int(2*CONFIG[MODEL]['img_height'])*CONFIG[MODEL]['convlstm_hidden_dim'], | ||
128 | ||
) | ||
self.fc2 = nn.Linear(128, 1) | ||
|
||
|
||
def forward(self, x, hidden_state=None): | ||
""" Forward pass | ||
Args: | ||
x (torch.tensor): 5-D Tensor of shape (batch, time, channel, height, width) | ||
Returns: | ||
tensor: prediction | ||
""" | ||
|
||
b, seq_len, _, h, w = x.size() | ||
x_new = [] | ||
for t in range(CONFIG[MODEL]['seq_len']): | ||
a = self.conv1(x[:,t,:,:,:]) | ||
a = self.conv2(a) | ||
x_new.append(a) | ||
x = torch.stack(x_new, dim=1) | ||
|
||
x, last_state, last_state_inv = self.convlstm1(x) | ||
x, last_state, last_state_inv = self.convlstm2(x) | ||
|
||
x = self.flatten(x) | ||
x = self.fc1(x) | ||
x = self.fc2(x) | ||
|
||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import torch | ||
from torch import nn, einsum | ||
from einops import rearrange | ||
from einops.layers.torch import Rearrange | ||
|
||
|
||
class SepConv2d(torch.nn.Module): | ||
def __init__(self, | ||
in_channels, | ||
out_channels, | ||
kernel_size, | ||
stride=1, | ||
padding=0, | ||
dilation=1,): | ||
super(SepConv2d, self).__init__() | ||
self.depthwise = torch.nn.Conv2d(in_channels, | ||
in_channels, | ||
kernel_size=kernel_size, | ||
stride=stride, | ||
padding=padding, | ||
dilation=dilation, | ||
groups=in_channels) | ||
self.bn = torch.nn.BatchNorm2d(in_channels) | ||
self.pointwise = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1) | ||
|
||
def forward(self, x): | ||
x = self.depthwise(x) | ||
x = self.bn(x) | ||
x = self.pointwise(x) | ||
return x | ||
|
||
|
||
class PreNorm(nn.Module): | ||
def __init__(self, dim, fn): | ||
super().__init__() | ||
self.norm = nn.LayerNorm(dim) | ||
self.fn = fn | ||
def forward(self, x, **kwargs): | ||
return self.fn(self.norm(x), **kwargs) | ||
|
||
|
||
class FeedForward(nn.Module): | ||
def __init__(self, dim, hidden_dim, dropout = 0.): | ||
super().__init__() | ||
self.net = nn.Sequential( | ||
nn.Linear(dim, hidden_dim), | ||
nn.GELU(), | ||
nn.Dropout(dropout), | ||
nn.Linear(hidden_dim, dim), | ||
nn.Dropout(dropout) | ||
) | ||
def forward(self, x): | ||
return self.net(x) | ||
|
||
|
||
class ConvAttention(nn.Module): | ||
def __init__(self, dim, img_size, heads = 8, dim_head = 64, kernel_size=3, q_stride=1, k_stride=1, v_stride=1, dropout = 0., | ||
last_stage=False): | ||
|
||
super().__init__() | ||
self.last_stage = last_stage | ||
self.img_size = img_size | ||
inner_dim = dim_head * heads | ||
project_out = not (heads == 1 and dim_head == dim) | ||
|
||
self.heads = heads | ||
self.scale = dim_head ** -0.5 | ||
pad = (kernel_size - q_stride)//2 | ||
self.to_q = SepConv2d(dim, inner_dim, kernel_size, q_stride, pad) | ||
self.to_k = SepConv2d(dim, inner_dim, kernel_size, k_stride, pad) | ||
self.to_v = SepConv2d(dim, inner_dim, kernel_size, v_stride, pad) | ||
|
||
self.to_out = nn.Sequential( | ||
nn.Linear(inner_dim, dim), | ||
nn.Dropout(dropout) | ||
) if project_out else nn.Identity() | ||
|
||
def forward(self, x): | ||
b, n, _, h = *x.shape, self.heads | ||
x = rearrange(x, 'b (l w) n -> b n l w', l=self.img_size, w=self.img_size) | ||
q = self.to_q(x) | ||
q = rearrange(q, 'b (h d) l w -> b h (l w) d', h=h) | ||
|
||
v = self.to_v(x) | ||
v = rearrange(v, 'b (h d) l w -> b h (l w) d', h=h) | ||
|
||
k = self.to_k(x) | ||
k = rearrange(k, 'b (h d) l w -> b h (l w) d', h=h) | ||
|
||
dots = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale | ||
|
||
attn = dots.softmax(dim=-1) | ||
|
||
out = einsum('b h i j, b h j d -> b h i d', attn, v) | ||
out = rearrange(out, 'b h n d -> b n (h d)') | ||
out = self.to_out(out) | ||
return out | ||
|
||
|
||
class Transformer(nn.Module): | ||
def __init__(self, dim, img_size, depth, heads, dim_head, mlp_dim, dropout=0., last_stage=False): | ||
super().__init__() | ||
self.layers = nn.ModuleList([]) | ||
for _ in range(depth): | ||
self.layers.append(nn.ModuleList([ | ||
PreNorm(dim, ConvAttention(dim, img_size, heads=heads, dim_head=dim_head, dropout=dropout, last_stage=last_stage)), | ||
PreNorm(dim, FeedForward(dim, mlp_dim, dropout=dropout)) | ||
])) | ||
|
||
def forward(self, x): | ||
for attn, ff in self.layers: | ||
x = attn(x) + x | ||
x = ff(x) + x | ||
return x | ||
|
||
|
||
class CvT(nn.Module): | ||
def __init__(self, | ||
image_size: int, | ||
in_channels: int, | ||
dim: int = 16, | ||
kernel_size: int = 3, | ||
stride: int = 1, | ||
depth: int = 6, | ||
heads: int = 6, | ||
dropout: float = 0.01, | ||
scale_dim: int = 4): | ||
super().__init__() | ||
|
||
self.conv_embed = nn.Sequential( | ||
nn.Conv2d(in_channels, dim, kernel_size, stride, 1), | ||
Rearrange('b c h w -> b (h w) c', h = image_size, w = image_size), | ||
nn.LayerNorm(dim) | ||
) | ||
self.transformer = nn.Sequential( | ||
Transformer(dim=dim, img_size=image_size, depth=depth, heads=heads, dim_head=dim, mlp_dim=dim * scale_dim, dropout=dropout), | ||
Rearrange('b (h w) c -> b c h w', h = image_size, w = image_size) | ||
) | ||
|
||
|
||
def forward(self, x): | ||
x = self.conv_embed(x) | ||
x = self.transformer(x) | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters