mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-08-26 18:24:18 +00:00
86 lines
2.6 KiB
Python
86 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright 2021 Xiaomi Corp. (authors: Mingshuang Luo)
|
|
#
|
|
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
|
|
|
|
class VisualNet(torch.nn.Module):
|
|
def __init__(self, num_classes: int) -> None:
|
|
"""
|
|
Args:
|
|
num_classes:
|
|
The output dimension of the visualnet model.
|
|
"""
|
|
super().__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 = 0.5
|
|
|
|
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
|