#BN
def BN(input, bn_training, name):
# assert isinstance(is_training, (ops.Tensor, variables.Variable)) and is_training.dtype == tf.bool
def bn(input, bn_training, name, reuse=None):
with tf.variable_scope('bn', reuse=reuse):
output = tf.layers.batch_normalization(input, training=bn_training, name=name)
return output
return tf.cond(
bn_training,
lambda: bn(input, bn_training=True, name=name, reuse=None),
lambda: bn(input, bn_training=False, name=name, reuse=True),
)
#LN
def Layernorm(x, gamma, beta):
# x_shape:[B, H, W, C]
results = 0.
eps = 1e-5
x = tf.transpose(x, [0, 3, 1, 2]) # [B,C,H,W]
x_mean = np.mean(x, axis=(1, 2, 3), keepdims=True)
x_var = np.var(x, axis=(1, 2, 3), keepdims=True0)
x_normalized = (x - x_mean) / np.sqrt(x_var + eps)
results = gamma * x_normalized + beta
results = tf.transpose(results, [0, 2, 3, 1])
return results
#IN
def Instancenorm(x, gam
BN,GN,IN,LN的TensorFlow代码实现

最新推荐文章于 2020-05-07 14:38:09 发布