cartoonize / models /layers.py
YANGYYYY's picture
Upload 13 files
7e4e601 verified
raw
history blame
No virus
725 Bytes
import torch
import torch.nn as nn
import torch.nn.functional as F
class LayerNorm2d(nn.LayerNorm):
""" LayerNorm for channels of '2D' spatial NCHW tensors """
def __init__(self, num_channels, eps=1e-6, affine=True):
super().__init__(num_channels, eps=eps, elementwise_affine=affine)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = x.permute(0, 2, 3, 1)
x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
x = x.permute(0, 3, 1, 2)
return x
def get_norm(norm_type):
if norm_type == "instance":
return nn.InstanceNorm2d
elif norm_type == "layer":
return LayerNorm2d
else:
raise ValueError(norm_type)