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/norm.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 text_recognizer/networks/convnext/norm.py (limited to 'text_recognizer/networks/convnext/norm.py') diff --git a/text_recognizer/networks/convnext/norm.py b/text_recognizer/networks/convnext/norm.py new file mode 100644 index 0000000..2d896e5 --- /dev/null +++ b/text_recognizer/networks/convnext/norm.py @@ -0,0 +1,15 @@ +"""Layer norm for conv layers.""" +import torch +from torch import nn, Tensor + + +class LayerNorm(nn.Module): + def __init__(self, dim: int) -> None: + super().__init__() + self.gamma = nn.Parameter(torch.ones(1, dim, 1, 1)) + + def forward(self, x: Tensor) -> Tensor: + eps = 1e-5 if x.dtype == torch.float32 else 1e-3 + var = torch.var(x, dim=1, unbiased=False, keepdim=True) + mean = torch.mean(x, dim=1, keepdim=True) + return (x - mean) / (var + eps).sqrt() * self.gamma -- cgit v1.2.3-70-g09d2