Caffe2 - Python API
A deep learning, cross platform ML framework
instancenorm.py
1 from .batchnorm import _BatchNorm
2 from .. import functional as F
3 from ..._jit_internal import weak_module, weak_script_method
4 
5 
6 class _InstanceNorm(_BatchNorm):
7  __constants__ = ['running_mean', 'running_var', 'weight', 'bias',
8  'track_running_stats', 'momentum', 'eps']
9 
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)
14 
15  @weak_script_method
16  def _check_input_dim(self, input):
17  raise NotImplementedError
18 
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)
22  # at version 1: removed running_mean and running_var when
23  # track_running_stats=False (default)
24  if version is None and not self.track_running_stats:
25  running_stats_keys = []
26  for name in ('running_mean', 'running_var'):
27  key = prefix + name
28  if key in state_dict:
29  running_stats_keys.append(key)
30  if len(running_stats_keys) > 0:
31  error_msgs.append(
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:
43  state_dict.pop(key)
44 
45  super(_InstanceNorm, self)._load_from_state_dict(
46  state_dict, prefix, local_metadata, strict,
47  missing_keys, unexpected_keys, error_msgs)
48 
49  @weak_script_method
50  def forward(self, input):
51  self._check_input_dim(input)
52 
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)
56 
57 
58 @weak_module
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`_ .
63 
64  .. math::
65 
66  y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
67 
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``.
71 
72  By default, this layer uses instance statistics computed from input data in
73  both training and evaluation modes.
74 
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.
79 
80  .. note::
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
86  new observed value.
87 
88  .. note::
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
95  transform.
96 
97  Args:
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.
104  Default: ``False``.
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``
109 
110  Shape:
111  - Input: :math:`(N, C, L)`
112  - Output: :math:`(N, C, L)` (same shape as input)
113 
114  Examples::
115 
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)
122 
123  .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`:
124  https://arxiv.org/abs/1607.08022
125  """
126 
127  @weak_script_method
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()))
132 
133 
134 @weak_module
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`_ .
139 
140  .. math::
141 
142  y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
143 
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``.
147 
148  By default, this layer uses instance statistics computed from input data in
149  both training and evaluation modes.
150 
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.
155 
156  .. note::
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
162  new observed value.
163 
164  .. note::
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
171  transform.
172 
173  Args:
174  num_features: :math:`C` from an expected input of size
175  :math:`(N, C, H, W)`
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.
180  Default: ``False``.
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``
185 
186  Shape:
187  - Input: :math:`(N, C, H, W)`
188  - Output: :math:`(N, C, H, W)` (same shape as input)
189 
190  Examples::
191 
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)
198 
199  .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`:
200  https://arxiv.org/abs/1607.08022
201  """
202 
203  @weak_script_method
204  def _check_input_dim(self, input):
205  if input.dim() != 4:
206  raise ValueError('expected 4D input (got {}D input)'
207  .format(input.dim()))
208 
209 
210 @weak_module
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`_ .
215 
216  .. math::
217 
218  y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta
219 
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``.
223 
224  By default, this layer uses instance statistics computed from input data in
225  both training and evaluation modes.
226 
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.
231 
232  .. note::
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
238  new observed value.
239 
240  .. note::
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
247  transform.
248 
249  Args:
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.
256  Default: ``False``.
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``
261 
262  Shape:
263  - Input: :math:`(N, C, D, H, W)`
264  - Output: :math:`(N, C, D, H, W)` (same shape as input)
265 
266  Examples::
267 
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)
274 
275  .. _`Instance Normalization: The Missing Ingredient for Fast Stylization`:
276  https://arxiv.org/abs/1607.08022
277  """
278 
279  @weak_script_method
280  def _check_input_dim(self, input):
281  if input.dim() != 5:
282  raise ValueError('expected 5D input (got {}D input)'
283  .format(input.dim()))