MCPcopy Create free account
hub / github.com/Elsaam2y/DINet_optimized / SynchronizedBatchNorm2d

Class SynchronizedBatchNorm2d

sync_batchnorm/batchnorm.py:280–338  ·  view source on GitHub ↗

r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch of 3d inputs .. math:: y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta This module differs from the built-in PyTorch BatchNorm2d as the mean and standard-deviation are reduced acr

Source from the content-addressed store, hash-verified

278
279
280class SynchronizedBatchNorm2d(_SynchronizedBatchNorm):
281 r"""Applies Batch Normalization over a 4d input that is seen as a mini-batch
282 of 3d inputs
283
284 .. math::
285
286 y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta
287
288 This module differs from the built-in PyTorch BatchNorm2d as the mean and
289 standard-deviation are reduced across all devices during training.
290
291 For example, when one uses `nn.DataParallel` to wrap the network during
292 training, PyTorch's implementation normalize the tensor on each device using
293 the statistics only on that device, which accelerated the computation and
294 is also easy to implement, but the statistics might be inaccurate.
295 Instead, in this synchronized version, the statistics will be computed
296 over all training samples distributed on multiple devices.
297
298 Note that, for one-GPU or CPU-only case, this module behaves exactly same
299 as the built-in PyTorch implementation.
300
301 The mean and standard-deviation are calculated per-dimension over
302 the mini-batches and gamma and beta are learnable parameter vectors
303 of size C (where C is the input size).
304
305 During training, this layer keeps a running estimate of its computed mean
306 and variance. The running sum is kept with a default momentum of 0.1.
307
308 During evaluation, this running mean/variance is used for normalization.
309
310 Because the BatchNorm is done over the `C` dimension, computing statistics
311 on `(N, H, W)` slices, it's common terminology to call this Spatial BatchNorm
312
313 Args:
314 num_features: num_features from an expected input of
315 size batch_size x num_features x height x width
316 eps: a value added to the denominator for numerical stability.
317 Default: 1e-5
318 momentum: the value used for the running_mean and running_var
319 computation. Default: 0.1
320 affine: a boolean value that when set to ``True``, gives the layer learnable
321 affine parameters. Default: ``True``
322
323 Shape::
324 - Input: :math:`(N, C, H, W)`
325 - Output: :math:`(N, C, H, W)` (same shape as input)
326
327 Examples:
328 >>> # With Learnable Parameters
329 >>> m = SynchronizedBatchNorm2d(100)
330 >>> # Without Learnable Parameters
331 >>> m = SynchronizedBatchNorm2d(100, affine=False)
332 >>> input = torch.autograd.Variable(torch.randn(20, 100, 35, 45))
333 >>> output = m(input)
334 """
335
336 def _check_input_dim(self, input):
337 if input.dim() != 4:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected