summaryrefslogtreecommitdiff
path: root/text_recognizer/networks/conformer/conformer.py
blob: 8d0e98ef5fd72700fea14d38fd7e5485c1761043 (plain)
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
"""Conformer module."""
from copy import deepcopy
from typing import Type

from torch import nn, Tensor

from text_recognizer.networks.conformer.block import ConformerBlock


class Conformer(nn.Module):
    def __init__(
        self,
        subsampler: Type[nn.Module],
        block: ConformerBlock,
        depth: int,
    ) -> None:
        super().__init__()
        self.subsampler = subsampler
        self.blocks = nn.ModuleList([deepcopy(block) for _ in range(depth)])

    def forward(self, x: Tensor) -> Tensor:
        x = self.subsampler(x)
        for fn in self.blocks:
            x = fn(x)
        return x