InstanceNorm2D

class paddle.nn. InstanceNorm2D ( num_features, epsilon=1e-05, momentum=0.9, weight_attr=None, bias_attr=None, data_format='NCHW', name=None ) [source]

Create a callable object of InstanceNorm2D. Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs with additional channel dimension) as described in the paper Instance Normalization: The Missing Ingredient for Fast Stylization .

DataLayout: NCHW [batch, in_channels, in_height, in_width]

\(input\) is the input features over a mini-batch.

\[\begin{split}\mu_{\beta} &\gets \frac{1}{HW} \sum_{i=1}^{HW} x_i \qquad &//\ \ mean\ of\ one\ feature\ map\ in\ mini-batch \\ \sigma_{\beta}^{2} &\gets \frac{1}{HW} \sum_{i=1}^{HW}(x_i - \ \mu_{\beta})^2 \qquad &//\ variance\ of\ one\ feature\ map\ in\ mini-batch \\ \hat{x_i} &\gets \frac{x_i - \mu_\beta} {\sqrt{\ \sigma_{\beta}^{2} + \epsilon}} \qquad &//\ normalize \\ y_i &\gets \gamma \hat{x_i} + \beta \qquad &//\ scale\ and\ shift\end{split}\]

Where H means height of feature map, W means width of feature map.

Parameters
  • num_features (int) – Indicate the number of channels of the input Tensor.

  • epsilon (float, optional) – A value added to the denominator for numerical stability. Default is 1e-5.

  • momentum (float, optional) – The value used for the moving_mean and moving_var computation. Default: 0.9.

  • weight_attr (ParamAttr|bool, optional) – The parameter attribute for Parameter scale of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm will create ParamAttr as weight_attr, the name of scale can be set in ParamAttr. If the Initializer of the weight_attr is not set, the parameter is initialized one. If it is set to False, will not create weight_attr. Default: None. For more information, please refer to ParamAttr .

  • bias_attr (ParamAttr|bool, optional) – The parameter attribute for the bias of instance_norm. If it is set to None or one attribute of ParamAttr, instance_norm will create ParamAttr as bias_attr, the name of bias can be set in ParamAttr. If the Initializer of the bias_attr is not set, the bias is initialized zero. If it is set to False, will not create bias_attr. Default: None. For more information, please refer to ParamAttr .

  • data_format (str, optional) – Specify the input data format, could be “NCHW”. Default: NCHW.

  • name (str, optional) – Name for the InstanceNorm, default is None. For more information, please refer to Name .

Shape:
  • x: 4-D tensor with shape: (batch, num_features, height, weight).

  • output: 4-D tensor with same shape as input x.

Returns

None.

Examples

>>> import paddle
>>> paddle.seed(100)
>>> x = paddle.rand((2, 2, 2, 3))
>>> instance_norm = paddle.nn.InstanceNorm2D(2)
>>> instance_norm_out = instance_norm(x)

>>> print(instance_norm_out)
Tensor(shape=[2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
[[[[ 1.26652932, -0.60229748, -1.65705574],
   [ 1.06272733,  0.24229208, -0.31219524]],
  [[-0.85414171,  0.31684181, -1.42204332],
   [ 1.00412714, -0.43966094,  1.39487720]]],
 [[[ 0.83324969,  1.25046813, -0.79470295],
   [-1.38446140,  0.81851846, -0.72307163]],
  [[-0.33560610,  0.95346332,  0.45585334],
   [-0.53483474,  1.20336461, -1.74224067]]]])