From afbaee56318278afb5ef1202c18b920960eddcab Mon Sep 17 00:00:00 2001 From: lvmengsi Date: Thu, 31 Oct 2019 12:20:34 +0800 Subject: [PATCH] Revert in (#3836) * revert_in --- PaddleCV/PaddleGAN/network/base_network.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/PaddleCV/PaddleGAN/network/base_network.py b/PaddleCV/PaddleGAN/network/base_network.py index 50b2d864..e3125a32 100644 --- a/PaddleCV/PaddleGAN/network/base_network.py +++ b/PaddleCV/PaddleGAN/network/base_network.py @@ -64,6 +64,12 @@ def norm_layer(input, moving_variance_name=name + '_var') elif norm_type == 'instance_norm': + helper = fluid.layer_helper.LayerHelper("instance_norm", **locals()) + dtype = helper.input_dtype() + epsilon = 1e-5 + mean = fluid.layers.reduce_mean(input, dim=[2, 3], keep_dim=True) + var = fluid.layers.reduce_mean( + fluid.layers.square(input - mean), dim=[2, 3], keep_dim=True) if name is not None: scale_name = name + "_scale" offset_name = name + "_offset" @@ -85,8 +91,15 @@ def norm_layer(input, name=offset_name, initializer=fluid.initializer.Constant(0.0), trainable=False) - return fluid.layers.instance_norm( - input, param_attr=scale_param, bias_attr=offset_param) + scale = helper.create_parameter( + attr=scale_param, shape=input.shape[1:2], dtype=dtype) + offset = helper.create_parameter( + attr=offset_param, shape=input.shape[1:2], dtype=dtype) + + tmp = fluid.layers.elementwise_mul(x=(input - mean), y=scale, axis=1) + tmp = tmp / fluid.layers.sqrt(var + epsilon) + tmp = fluid.layers.elementwise_add(tmp, offset, axis=1) + return tmp else: raise NotImplementedError("norm type: [%s] is not support" % norm_type) -- GitLab