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

Class SynchronizedBatchNorm3d

sync_batchnorm/batchnorm.py:341–400  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

339
340
341class SynchronizedBatchNorm3d(_SynchronizedBatchNorm):
342 r"""Applies Batch Normalization over a 5d input that is seen as a mini-batch
343 of 4d inputs
344
345 .. math::
346
347 y = \frac{x - mean[x]}{ \sqrt{Var[x] + \epsilon}} * gamma + beta
348
349 This module differs from the built-in PyTorch BatchNorm3d as the mean and
350 standard-deviation are reduced across all devices during training.
351
352 For example, when one uses `nn.DataParallel` to wrap the network during
353 training, PyTorch's implementation normalize the tensor on each device using
354 the statistics only on that device, which accelerated the computation and
355 is also easy to implement, but the statistics might be inaccurate.
356 Instead, in this synchronized version, the statistics will be computed
357 over all training samples distributed on multiple devices.
358
359 Note that, for one-GPU or CPU-only case, this module behaves exactly same
360 as the built-in PyTorch implementation.
361
362 The mean and standard-deviation are calculated per-dimension over
363 the mini-batches and gamma and beta are learnable parameter vectors
364 of size C (where C is the input size).
365
366 During training, this layer keeps a running estimate of its computed mean
367 and variance. The running sum is kept with a default momentum of 0.1.
368
369 During evaluation, this running mean/variance is used for normalization.
370
371 Because the BatchNorm is done over the `C` dimension, computing statistics
372 on `(N, D, H, W)` slices, it's common terminology to call this Volumetric BatchNorm
373 or Spatio-temporal BatchNorm
374
375 Args:
376 num_features: num_features from an expected input of
377 size batch_size x num_features x depth x height x width
378 eps: a value added to the denominator for numerical stability.
379 Default: 1e-5
380 momentum: the value used for the running_mean and running_var
381 computation. Default: 0.1
382 affine: a boolean value that when set to ``True``, gives the layer learnable
383 affine parameters. Default: ``True``
384
385 Shape::
386 - Input: :math:`(N, C, D, H, W)`
387 - Output: :math:`(N, C, D, H, W)` (same shape as input)
388
389 Examples:
390 >>> # With Learnable Parameters
391 >>> m = SynchronizedBatchNorm3d(100)
392 >>> # Without Learnable Parameters
393 >>> m = SynchronizedBatchNorm3d(100, affine=False)
394 >>> input = torch.autograd.Variable(torch.randn(20, 100, 35, 45, 10))
395 >>> output = m(input)
396 """
397
398 def _check_input_dim(self, input):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected