mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-08-27 10:44:19 +00:00
64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
|
|
class LipNet(torch.nn.Module):
|
|
def __init__(self, num_classes, dropout_p=0.1):
|
|
super(LipNet, self).__init__()
|
|
self.num_classes = num_classes
|
|
self.conv1 = nn.Conv3d(3, 32, (3, 5, 5), (1, 2, 2), (1, 2, 2))
|
|
self.pool1 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
|
|
|
|
self.conv2 = nn.Conv3d(32, 64, (3, 5, 5), (1, 1, 1), (1, 2, 2))
|
|
self.pool2 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
|
|
|
|
self.conv3 = nn.Conv3d(64, 96, (3, 3, 3), (1, 1, 1), (1, 1, 1))
|
|
self.pool3 = nn.MaxPool3d((1, 2, 2), (1, 2, 2))
|
|
|
|
self.gru1 = nn.GRU(96 * 4 * 8, 256, 1, bidirectional=True)
|
|
self.gru2 = nn.GRU(512, 256, 1, bidirectional=True)
|
|
|
|
self.FC = nn.Linear(512, self.num_classes)
|
|
self.dropout_p = dropout_p
|
|
|
|
self.relu = nn.ReLU(inplace=True)
|
|
self.dropout = nn.Dropout(self.dropout_p)
|
|
self.dropout3d = nn.Dropout3d(self.dropout_p)
|
|
|
|
def forward(self, x):
|
|
|
|
x = self.conv1(x)
|
|
x = self.relu(x)
|
|
x = self.dropout3d(x)
|
|
x = self.pool1(x)
|
|
|
|
x = self.conv2(x)
|
|
x = self.relu(x)
|
|
x = self.dropout3d(x)
|
|
x = self.pool2(x)
|
|
|
|
x = self.conv3(x)
|
|
x = self.relu(x)
|
|
x = self.dropout3d(x)
|
|
x = self.pool3(x)
|
|
|
|
# (B, C, T, H, W)->(T, B, C, H, W)
|
|
x = x.permute(2, 0, 1, 3, 4).contiguous()
|
|
# (B, C, T, H, W)->(T, B, C*H*W)
|
|
x = x.view(x.size(0), x.size(1), -1)
|
|
|
|
self.gru1.flatten_parameters()
|
|
self.gru2.flatten_parameters()
|
|
|
|
x, h = self.gru1(x)
|
|
x = self.dropout(x)
|
|
x, h = self.gru2(x)
|
|
x = self.dropout(x)
|
|
|
|
x = x.permute(1, 0, 2).contiguous()
|
|
x = self.FC(x)
|
|
x = nn.functional.log_softmax(x, dim=-1)
|
|
|
|
return x
|