Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/core/leras/layers/FRNorm2D.py
628 views
1
from core.leras import nn
2
tf = nn.tf
3
4
class FRNorm2D(nn.LayerBase):
5
"""
6
Tensorflow implementation of
7
Filter Response Normalization Layer: Eliminating Batch Dependence in theTraining of Deep Neural Networks
8
https://arxiv.org/pdf/1911.09737.pdf
9
"""
10
def __init__(self, in_ch, dtype=None, **kwargs):
11
self.in_ch = in_ch
12
13
if dtype is None:
14
dtype = nn.floatx
15
self.dtype = dtype
16
17
super().__init__(**kwargs)
18
19
def build_weights(self):
20
self.weight = tf.get_variable("weight", (self.in_ch,), dtype=self.dtype, initializer=tf.initializers.ones() )
21
self.bias = tf.get_variable("bias", (self.in_ch,), dtype=self.dtype, initializer=tf.initializers.zeros() )
22
self.eps = tf.get_variable("eps", (1,), dtype=self.dtype, initializer=tf.initializers.constant(1e-6) )
23
24
def get_weights(self):
25
return [self.weight, self.bias, self.eps]
26
27
def forward(self, x):
28
if nn.data_format == "NHWC":
29
shape = (1,1,1,self.in_ch)
30
else:
31
shape = (1,self.in_ch,1,1)
32
weight = tf.reshape ( self.weight, shape )
33
bias = tf.reshape ( self.bias , shape )
34
nu2 = tf.reduce_mean(tf.square(x), axis=nn.conv2d_spatial_axes, keepdims=True)
35
x = x * ( 1.0/tf.sqrt(nu2 + tf.abs(self.eps) ) )
36
37
return x*weight + bias
38
nn.FRNorm2D = FRNorm2D
39