Skip to content

NoiseInjector

Description

Info

Parent class: Module

Devired classes: -

This module implements the function of introducing noise into the data tensor.

This operation can be used both for data augmentation, as well as to increase the noise immunity of the model when training on prepared intentionally noisy data.

Additional sources

Initializing

def __init__(self, mode="add", noisetype="uniform", params=(0.0, 1.0), rng=globalRng, inplace=False, slicing=None,
                 name=None):

Parameters

Parameter Allowed types Description Default
mode str Noise adding mode "add"
noisetype str Noise type "uniform"
params tuple Noise model parameters (0.0, 1.0)
rng str Random number generator object (backend-dependent) globalRng
inplace bool If True, then the output tensor will be written in the place of the input tensor in memory False
slicing slice To which slice the operation is applied None
name str Layer name None

Explanations

mode - noise adding mode. It can take values "add" and "mul";


noisetype - type of added noise. It can take values "gaussian" and "uniform";


rng - random number generator, which depends on the platform where the calculations are performed, i.e. the generator is backend-dependent: for example, when using Nvidia-GPU and CUDA, the generator is formed using the curand library;


params - params - tuple of noise parameters: (a, b) for "uniform" and (mean, sigma) for "gaussian";


inplace - flag showing whether additional memory resources should be allocated for the result. If True, then the output tensor will be written in the place of the input tensor in memory, which can negatively affect the network, if the input tensor takes part in calculations on other branches of the graph;


slicing - slice is set through the Python built-in slice object. Please have in mind that the slice indexes are set for the flattened data tensor;

Examples

Necessary imports.

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Modules import NoiseInjector

Info

gpuarray is required to properly place the tensor in the GPU

Take the normally distributed noise and the summing noise values mode with data values:

np.random.seed(123)
data = gpuarray.to_gpu(np.random.randint(0, 9, (1, 3, 4, 4)).astype(np.float32))
print(data)
[[[[2. 2. 6. 1.]
   [3. 6. 1. 0.]
   [1. 0. 0. 3.]
   [4. 0. 0. 4.]]

  [[1. 7. 3. 2.]
   [4. 7. 2. 4.]
   [8. 0. 7. 3.]
   [4. 6. 1. 5.]]

  [[6. 2. 1. 8.]
   [3. 5. 0. 2.]
   [6. 2. 4. 4.]
   [6. 3. 0. 6.]]]]
injector = NoiseInjector(mode="add", noisetype="gaussian", params=(0.0, 10.0))
print(injector(data))
[[[[ -4.3309712    2.08201     16.573734     8.57326   ]
   [ 21.06646      3.758148    14.312004    -6.5693135 ]
   [  9.087677    -9.566303    -6.01458      0.16788411]
   [ -1.3159542    0.38866973 -11.592874    23.892897  ]]

  [[  3.4345775    8.709841    -4.4099264   11.415678  ]
   [  2.3037925    5.180489     5.9057817    6.890024  ]
   [ -9.175585    16.199835    22.050224     9.048243  ]
   [  0.18314695   9.058003     0.78339946   1.8816617 ]]

  [[ 13.431959     8.329308    -7.429866    20.178535  ]
   [  5.6269503    2.4493244    4.348013    11.406551  ]
   [ 10.589058    21.149744    15.779724     2.0710073 ]
   [  8.479838    12.597048    11.60874    -19.87299   ]]]]

Let us change the distribution to uniform and choose the mode of multiplying noise values by data values; this time the noise will be introduced through slicing:

injector = NoiseInjector(mode="mul", noisetype="uniform", params=(0.0, 10.0), slicing = slice(1*2*3*2, 1*3*3*2))
print(injector(data))

[[[[ 2.         2.         6.         1.       ]
   [ 3.         6.         1.         0.       ]
   [ 1.         0.         0.         3.       ]
   [ 6.6883607  0.         0.        36.555717 ]]

  [[ 9.848382  65.91567    3.         2.       ]
   [ 4.         7.         2.         4.       ]
   [ 8.         0.         7.         3.       ]
   [ 4.         6.         1.         5.       ]]

  [[ 6.         2.         1.         8.       ]
   [ 3.         5.         0.         2.       ]
   [ 6.         2.         4.         4.       ]
   [ 6.         3.         0.         6.       ]]]]