From 9732617e402a5152ba822eb1459d6ff776aba15c Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Tue, 13 Sep 2022 19:07:53 +0200 Subject: Add convnext module --- text_recognizer/networks/convnext/downsample.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 text_recognizer/networks/convnext/downsample.py (limited to 'text_recognizer/networks/convnext/downsample.py') diff --git a/text_recognizer/networks/convnext/downsample.py b/text_recognizer/networks/convnext/downsample.py new file mode 100644 index 0000000..6e4306f --- /dev/null +++ b/text_recognizer/networks/convnext/downsample.py @@ -0,0 +1,17 @@ +from typing import Tuple + +from einops.layers.torch import Rearrange +from torch import nn, Tensor + + +class Downsample(nn.Module): + def __init__(self, dim: int, dim_out: int, factors: Tuple[int, int]) -> None: + super().__init__() + s1, s2 = factors + self.fn = nn.Sequential( + Rearrange("b c (h s1) (w s2) -> b (c s1 s2) h w", s1=s1, s2=s2), + nn.Conv2d(dim * s1 * s2, dim_out, 1), + ) + + def forward(self, x: Tensor) -> Tensor: + return self.fn(x) -- cgit v1.2.3-70-g09d2