1 from .batchnorm
import _BatchNorm
2 from ..
import functional
as F
3 from ..._jit_internal
import weak_module, weak_script_method
7 __constants__ = [
'running_mean',
'running_var',
'weight',
'bias',
8 'track_running_stats',
'momentum',
'eps']
10 def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=False,
11 track_running_stats=
False):
12 super(_InstanceNorm, self).__init__(
13 num_features, eps, momentum, affine, track_running_stats)
16 def _check_input_dim(self, input):
17 raise NotImplementedError
19 def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict,
20 missing_keys, unexpected_keys, error_msgs):
21 version = local_metadata.get(
'version',
None)
24 if version
is None and not self.track_running_stats:
25 running_stats_keys = []
26 for name
in (
'running_mean',
'running_var'):
29 running_stats_keys.append(key)
30 if len(running_stats_keys) > 0:
32 'Unexpected running stats buffer(s) {names} for {klass} ' 33 'with track_running_stats=False. If state_dict is a ' 34 'checkpoint saved before 0.4.0, this may be expected ' 35 'because {klass} does not track running stats by default ' 36 'since 0.4.0. Please remove these keys from state_dict. If ' 37 'the running stats are actually needed, instead set ' 38 'track_running_stats=True in {klass} to enable them. See ' 39 'the documentation of {klass} for details.' 40 .format(names=
" and ".join(
'"{}"'.format(k)
for k
in running_stats_keys),
41 klass=self.__class__.__name__))
42 for key
in running_stats_keys:
45 super(_InstanceNorm, self)._load_from_state_dict(
46 state_dict, prefix, local_metadata, strict,
47 missing_keys, unexpected_keys, error_msgs)
50 def forward(self, input):
53 return F.instance_norm(
54 input, self.running_mean, self.running_var, self.weight, self.bias,
55 self.training
or not self.track_running_stats, self.momentum, self.eps)
60 r"""Applies Instance Normalization over a 2D or 3D input (a mini-batch of 1D 61 inputs with optional additional channel dimension) as described in the paper 62 `Instance Normalization: The Missing Ingredient for Fast Stylization`_ . 66 y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta 68 The mean and standard-deviation are calculated per-dimension separately 69 for each object in a mini-batch. :math:`\gamma` and :math:`\beta` are learnable parameter vectors 70 of size `C` (where `C` is the input size) if :attr:`affine` is ``True``. 72 By default, this layer uses instance statistics computed from input data in 73 both training and evaluation modes. 75 If :attr:`track_running_stats` is set to ``True``, during training this 76 layer keeps running estimates of its computed mean and variance, which are 77 then used for normalization during evaluation. The running estimates are 78 kept with a default :attr:`momentum` of 0.1. 81 This :attr:`momentum` argument is different from one used in optimizer 82 classes and the conventional notion of momentum. Mathematically, the 83 update rule for running statistics here is 84 :math:`\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momemtum} \times x_t`, 85 where :math:`\hat{x}` is the estimated statistic and :math:`x_t` is the 89 :class:`InstanceNorm1d` and :class:`LayerNorm` are very similar, but 90 have some subtle differences. :class:`InstanceNorm1d` is applied 91 on each channel of channeled data like multidimensional time series, but 92 :class:`LayerNorm` is usually applied on entire sample and often in NLP 93 tasks. Additionaly, :class:`LayerNorm` applies elementwise affine 94 transform, while :class:`InstanceNorm1d` usually don't apply affine 98 num_features: :math:`C` from an expected input of size 99 :math:`(N, C, L)` or :math:`L` from input of size :math:`(N, L)` 100 eps: a value added to the denominator for numerical stability. Default: 1e-5 101 momentum: the value used for the running_mean and running_var computation. Default: 0.1 102 affine: a boolean value that when set to ``True``, this module has 103 learnable affine parameters, initialized the same way as done for batch normalization. 105 track_running_stats: a boolean value that when set to ``True``, this 106 module tracks the running mean and variance, and when set to ``False``, 107 this module does not track such statistics and always uses batch 108 statistics in both training and eval modes. Default: ``False`` 111 - Input: :math:`(N, C, L)` 112 - Output: :math:`(N, C, L)` (same shape as input) 116 >>> # Without Learnable Parameters 117 >>> m = nn.InstanceNorm1d(100) 118 >>> # With Learnable Parameters 119 >>> m = nn.InstanceNorm1d(100, affine=True) 120 >>> input = torch.randn(20, 100, 40) 121 >>> output = m(input) 123 .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`: 124 https://arxiv.org/abs/1607.08022 128 def _check_input_dim(self, input):
129 if input.dim() != 2
and input.dim() != 3:
130 raise ValueError(
'expected 2D or 3D input (got {}D input)' 131 .format(input.dim()))
136 r"""Applies Instance Normalization over a 4D input (a mini-batch of 2D inputs 137 with additional channel dimension) as described in the paper 138 `Instance Normalization: The Missing Ingredient for Fast Stylization`_ . 142 y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta 144 The mean and standard-deviation are calculated per-dimension separately 145 for each object in a mini-batch. :math:`\gamma` and :math:`\beta` are learnable parameter vectors 146 of size `C` (where `C` is the input size) if :attr:`affine` is ``True``. 148 By default, this layer uses instance statistics computed from input data in 149 both training and evaluation modes. 151 If :attr:`track_running_stats` is set to ``True``, during training this 152 layer keeps running estimates of its computed mean and variance, which are 153 then used for normalization during evaluation. The running estimates are 154 kept with a default :attr:`momentum` of 0.1. 157 This :attr:`momentum` argument is different from one used in optimizer 158 classes and the conventional notion of momentum. Mathematically, the 159 update rule for running statistics here is 160 :math:`\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momemtum} \times x_t`, 161 where :math:`\hat{x}` is the estimated statistic and :math:`x_t` is the 165 :class:`InstanceNorm2d` and :class:`LayerNorm` are very similar, but 166 have some subtle differences. :class:`InstanceNorm2d` is applied 167 on each channel of channeled data like RGB images, but 168 :class:`LayerNorm` is usually applied on entire sample and often in NLP 169 tasks. Additionaly, :class:`LayerNorm` applies elementwise affine 170 transform, while :class:`InstanceNorm2d` usually don't apply affine 174 num_features: :math:`C` from an expected input of size 176 eps: a value added to the denominator for numerical stability. Default: 1e-5 177 momentum: the value used for the running_mean and running_var computation. Default: 0.1 178 affine: a boolean value that when set to ``True``, this module has 179 learnable affine parameters, initialized the same way as done for batch normalization. 181 track_running_stats: a boolean value that when set to ``True``, this 182 module tracks the running mean and variance, and when set to ``False``, 183 this module does not track such statistics and always uses batch 184 statistics in both training and eval modes. Default: ``False`` 187 - Input: :math:`(N, C, H, W)` 188 - Output: :math:`(N, C, H, W)` (same shape as input) 192 >>> # Without Learnable Parameters 193 >>> m = nn.InstanceNorm2d(100) 194 >>> # With Learnable Parameters 195 >>> m = nn.InstanceNorm2d(100, affine=True) 196 >>> input = torch.randn(20, 100, 35, 45) 197 >>> output = m(input) 199 .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`: 200 https://arxiv.org/abs/1607.08022 204 def _check_input_dim(self, input):
206 raise ValueError(
'expected 4D input (got {}D input)' 207 .format(input.dim()))
212 r"""Applies Instance Normalization over a 5D input (a mini-batch of 3D inputs 213 with additional channel dimension) as described in the paper 214 `Instance Normalization: The Missing Ingredient for Fast Stylization`_ . 218 y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta 220 The mean and standard-deviation are calculated per-dimension separately 221 for each object in a mini-batch. :math:`\gamma` and :math:`\beta` are learnable parameter vectors 222 of size C (where C is the input size) if :attr:`affine` is ``True``. 224 By default, this layer uses instance statistics computed from input data in 225 both training and evaluation modes. 227 If :attr:`track_running_stats` is set to ``True``, during training this 228 layer keeps running estimates of its computed mean and variance, which are 229 then used for normalization during evaluation. The running estimates are 230 kept with a default :attr:`momentum` of 0.1. 233 This :attr:`momentum` argument is different from one used in optimizer 234 classes and the conventional notion of momentum. Mathematically, the 235 update rule for running statistics here is 236 :math:`\hat{x}_\text{new} = (1 - \text{momentum}) \times \hat{x} + \text{momemtum} \times x_t`, 237 where :math:`\hat{x}` is the estimated statistic and :math:`x_t` is the 241 :class:`InstanceNorm3d` and :class:`LayerNorm` are very similar, but 242 have some subtle differences. :class:`InstanceNorm3d` is applied 243 on each channel of channeled data like 3D models with RGB color, but 244 :class:`LayerNorm` is usually applied on entire sample and often in NLP 245 tasks. Additionaly, :class:`LayerNorm` applies elementwise affine 246 transform, while :class:`InstanceNorm3d` usually don't apply affine 250 num_features: :math:`C` from an expected input of size 251 :math:`(N, C, D, H, W)` 252 eps: a value added to the denominator for numerical stability. Default: 1e-5 253 momentum: the value used for the running_mean and running_var computation. Default: 0.1 254 affine: a boolean value that when set to ``True``, this module has 255 learnable affine parameters, initialized the same way as done for batch normalization. 257 track_running_stats: a boolean value that when set to ``True``, this 258 module tracks the running mean and variance, and when set to ``False``, 259 this module does not track such statistics and always uses batch 260 statistics in both training and eval modes. Default: ``False`` 263 - Input: :math:`(N, C, D, H, W)` 264 - Output: :math:`(N, C, D, H, W)` (same shape as input) 268 >>> # Without Learnable Parameters 269 >>> m = nn.InstanceNorm3d(100) 270 >>> # With Learnable Parameters 271 >>> m = nn.InstanceNorm3d(100, affine=True) 272 >>> input = torch.randn(20, 100, 35, 45, 10) 273 >>> output = m(input) 275 .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`: 276 https://arxiv.org/abs/1607.08022 280 def _check_input_dim(self, input):
282 raise ValueError(
'expected 5D input (got {}D input)' 283 .format(input.dim()))
def _check_input_dim(self, input)