diff options
| author | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2023-09-03 22:54:09 +0200 |
|---|---|---|
| committer | Gustaf Rydholm <gustaf.rydholm@gmail.com> | 2023-09-03 22:54:09 +0200 |
| commit | 1732ed564a738a42c1bf6e8127ae810f5658cb06 (patch) | |
| tree | 8d7f67793fec820850b4f8fd92f762f7f5e4b9f6 /text_recognizer/network/convnext/downsample.py | |
| parent | 53cfc21cffa4e877ad0959170b47b690d2fdb40f (diff) | |
Revert "Delete convnext"
This reverts commit 7239bce214607c70a7a91358586f265b2f74de7b.
Diffstat (limited to 'text_recognizer/network/convnext/downsample.py')
| -rw-r--r-- | text_recognizer/network/convnext/downsample.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/text_recognizer/network/convnext/downsample.py b/text_recognizer/network/convnext/downsample.py new file mode 100644 index 0000000..a8a0466 --- /dev/null +++ b/text_recognizer/network/convnext/downsample.py @@ -0,0 +1,21 @@ +"""Convnext downsample module.""" +from typing import Tuple + +from einops.layers.torch import Rearrange +from torch import Tensor, nn + + +class Downsample(nn.Module): + """Downsamples feature maps by patches.""" + + 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: + """Applies patch function.""" + return self.fn(x) |