From 729867b14754bffd98a3406b5d3a2eb9d09bc3bc Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Wed, 1 Jun 2022 23:11:05 +0200 Subject: WIP conformer --- text_recognizer/networks/conformer/attention.py | 0 text_recognizer/networks/conformer/block.py | 0 text_recognizer/networks/conformer/depth_wise_conv.py | 12 ++++++++++++ text_recognizer/networks/conformer/glu.py | 12 ++++++++++++ text_recognizer/networks/conformer/mlp.py | 0 text_recognizer/networks/conformer/scale.py | 13 +++++++++++++ 6 files changed, 37 insertions(+) delete mode 100644 text_recognizer/networks/conformer/attention.py create mode 100644 text_recognizer/networks/conformer/block.py create mode 100644 text_recognizer/networks/conformer/depth_wise_conv.py create mode 100644 text_recognizer/networks/conformer/glu.py create mode 100644 text_recognizer/networks/conformer/mlp.py create mode 100644 text_recognizer/networks/conformer/scale.py diff --git a/text_recognizer/networks/conformer/attention.py b/text_recognizer/networks/conformer/attention.py deleted file mode 100644 index e69de29..0000000 diff --git a/text_recognizer/networks/conformer/block.py b/text_recognizer/networks/conformer/block.py new file mode 100644 index 0000000..e69de29 diff --git a/text_recognizer/networks/conformer/depth_wise_conv.py b/text_recognizer/networks/conformer/depth_wise_conv.py new file mode 100644 index 0000000..9465b7c --- /dev/null +++ b/text_recognizer/networks/conformer/depth_wise_conv.py @@ -0,0 +1,12 @@ +"""Depthwise 1D convolution.""" +from torch import nn, Tensor + + +class DepthwiseConv1D(nn.Module): + def __init__(self, in_channels: int, out_channels: int, kernel_size: int) -> None: + super().__init__() + self.conv = nn.Conv1d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, groups=in_channels, + padding="same") + + def forward(self, x: Tensor) -> Tensor: + return self.conv(x) diff --git a/text_recognizer/networks/conformer/glu.py b/text_recognizer/networks/conformer/glu.py new file mode 100644 index 0000000..016b684 --- /dev/null +++ b/text_recognizer/networks/conformer/glu.py @@ -0,0 +1,12 @@ +"""GLU layer.""" +from torch import nn, Tensor + + +class GLU(nn.Module): + def __init__(self, dim: int) -> None: + super().__init__() + self.dim = dim + + def forward(self, x: Tensor) -> Tensor: + out, gate = x.chunk(2, dim=self.dim) + return out * gate.sigmoid() diff --git a/text_recognizer/networks/conformer/mlp.py b/text_recognizer/networks/conformer/mlp.py new file mode 100644 index 0000000..e69de29 diff --git a/text_recognizer/networks/conformer/scale.py b/text_recognizer/networks/conformer/scale.py new file mode 100644 index 0000000..da736a3 --- /dev/null +++ b/text_recognizer/networks/conformer/scale.py @@ -0,0 +1,13 @@ +"""Scale layer.""" +from typing import Dict +from torch import nn, Tensor + + +class Scale(nn.Module): + def __init__(self, scale: float, fn: nn.Module) -> None: + super().__init__() + self.scale = scale + self.fn = fn + + def forward(self, x: Tensor, kwargs: Dict) -> Tensor: + return self.fn(x, **kwargs) * self.scale -- cgit v1.2.3-70-g09d2