robustness package

Submodules

robustness.all_in_one module

Implements generic modules to apply robustness functions to generic training processses.

robustness.all_in_one.general_test(testprocess, filename, robustdatasets, encoder=False, multi_measure=False)

Test model on noisy data.

Parameters:
  • testprocess – Testing function.

  • filename – Filename of the saved model.

  • robustdatasets – A list of dataloaders of noisy test data.

  • encoder – True if the model has an encoder. ( default: False )

  • multi_measure – True if multiple evaluation metrics are used. ( default: False )

robustness.all_in_one.general_train(trainprocess, algorithm, encoder=False)

Train model on data.

Parameters:
  • trainprocess – Training function.

  • algorithm – Algorithm name.

  • encoder – True if the model has an encoder. ( default: False )

Returns:

Filename of the best saved model trained on training data.

robustness.all_in_one.stocks_test(num_training, models, noise_range, testprocess, encoder=False)

Test model on noisy stocks data.

Parameters:
  • num_training – >1 to enable multiple training to reduce model variance.

  • models – Models loaded with pre-trained weights.

  • noisy_range – A range of noise level, e.g. 0 (no noise) - 1 (completely noisy).

  • testprocess – Testing function.

  • encoder – True if the model has an encoder. ( default: False )

robustness.all_in_one.stocks_train(num_training, trainprocess, algorithm, encoder=False)

Train model on stocks data.

Parameters:
  • num_training – >1 to enable multiple training to reduce model variance.

  • trainprocess – Training function.

  • algorithm – Algorithm name.

  • encoder – True if the model has an encoder. ( default: False )

Returns:

A list of filenames of the best saved models trained on stocks data.

robustness.audio_robust module

Implements audio transformations.

robustness.audio_robust.add_audio_noise(tests, noise_level=0.3, noises=None)

Add various types of noise to audio data.

Parameters:
  • noise_level – Probability of randomly applying noise to each audio signal, and standard deviation for gaussian noise, and structured dropout probability.

  • noises – list of noises to add. # TODO: Change this to use either a list of enums or if statements.

robustness.audio_robust.additive_white_gaussian_noise(signal, noise_level)

Add gaussian white noise to audio signal.

Parameters:
  • signal – Audio signal to permute.

  • noise_level – standard deviation of the gaussian noise.

robustness.audio_robust.audio_random_dropout(sig, p)

Randomly drop the signal for a single time step.

Parameters:
  • signal – Audio signal to transform.

  • p – Dropout probability.

robustness.audio_robust.audio_structured_dropout(signal, p, step=10)

Randomly drop signal for step time steps.

Parameters:
  • signal – Audio signal to permute.

  • p – Dropout probability.

  • step – Number of time steps to drop the signal.

robustness.tabular_robust module

Implements tabular data transformations.

robustness.tabular_robust.add_tabular_noise(tests, noise_level=0.3, drop=True, swap=True)

Add various types of noise to tabular data.

Parameters:
  • noise_level – Probability of randomly applying noise to each element.

  • drop – Drop elements with probability noise_level

  • swap – Swap elements with probability noise_level

robustness.tabular_robust.drop_entry(data, p)

Randomly drop elements in data with probability p

Parameters:
  • data – Data to drop elements from.

  • p – Probability of dropping elements.

robustness.tabular_robust.swap_entry(data, p)

Randomly swap adjacent elements in data with probability p.

Parameters:
  • data – Data to swap elems.

  • p – Probability of swapping elements.

robustness.text_robust module

Implements text transformations.

robustness.text_robust.add_text_noise(tests, noise_level=0.3, swap=True, rand_mid=True, typo=True, sticky=True, omit=True)

Add various types of noise to text data.

Parameters:
  • noise_level – Probability of randomly applying noise to a word. ( default: 0.1)

  • swap – Swap two adjacent letters. ( default: True )

  • rand_mid – Randomly permute the middle section of the word, except for the first and last letters. ( default: True )

  • typo – Simulate keyboard typos for the word. ( default: True )

  • sticky – Randomly repeat letters inside a word. ( default: True )

  • omit – Randomly omit some letters from a word ( default: True )

robustness.text_robust.omission(word, num_omit=1)

Randomly omit num_omit number of letters of a word.

Parameters:
  • word – word to apply transformations to.

  • num_sticky – Number of letters to randomly omit.

robustness.text_robust.qwerty_typo(word)

Randomly replace num_typo number of letters of a word to a one adjacent to it on qwerty keyboard.

Parameters:

word – word to apply transformations to.:

robustness.text_robust.random_mid(word)

Randomly permute the middle chunk of a word (all letters except the first and last letter).

Parameters:

word – word to apply transformations to.

robustness.text_robust.sticky_keys(word, num_sticky=1)

Randomly repeat letters of a word once.

Parameters:
  • word – word to apply transformations to.

  • num_sticky – Number of letters to randomly repeat once.

robustness.text_robust.swap_letter(word)

Swap two random adjacent letters.

Parameters:

word – word to apply transformations to.

robustness.timeseries_robust module

Implements timeseries transformations.

robustness.timeseries_robust.add_timeseries_noise(tests, noise_level=0.3, gaussian_noise=True, rand_drop=True, struct_drop=True)

Add various types of noise to timeseries data.

Parameters:
  • noise_level – Standard deviation of gaussian noise, and drop probability in random drop and structural drop

  • gauss_noise – Add Gaussian noise to the time series ( default: True )

  • rand_drop – Add randomized dropout to the time series ( default: True )

  • struct_drop – Add randomized structural dropout to the time series ( default: True )

robustness.timeseries_robust.random_drop(data, p)

Drop each time series entry independently with probability p.

Parameters:
  • data – Data to process.

  • p – Probability to drop feature.

robustness.timeseries_robust.structured_drop(data, p)

Drop each time series entry independently with probability p, but drop all modalities if you drop an element.

Parameters:
  • data – Data to process.

  • p – Probability to drop entire element of time series.

robustness.timeseries_robust.white_noise(data, p)

Add noise sampled from zero-mean Gaussian with standard deviation p at every time step.

Parameters:
  • data – Data to process.

  • p – Standard deviation of added Gaussian noise.

robustness.visual_robust module

Implements visual transformations.

robustness.visual_robust.WB(img, p)

Randomly change the white-black balance / temperature of an image.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.add_visual_noise(tests, noise_level=0.3, gray=True, contrast=True, inv=True, temp=True, color=True, s_and_p=True, gaus=True, rot=True, flip=True, crop=True)

Add various types of noise to visual data.

Parameters:
  • noise_level – Probability of randomly applying noise to each audio signal, and standard deviation for gaussian noise, and structured dropout probability.

  • gray – Boolean flag denoting if grayscale should be applied as a noise type.

  • contrast – Boolean flag denoting if lowering the contrast should be applied as a noise type.

  • inv – Boolean flag denoting if inverting the image should be applied as a noise type.

  • temp – Boolean flag denoting if randomly changing the image’s color balance should be applied as a noise type.

  • color – Boolean flag denoting if randomly tinting the image should be applied as a noise type.

  • s_and_p – Boolean flag denoting if applying salt and pepper noise should be applied as a noise type.

  • gaus – Boolean flag denoting if applying Gaussian noise should be applied as a noise type.

  • rot – Boolean flag denoting if randomly rotating the image should be applied as a noise type.

  • flip – Boolean flag denoting if randomly flipping the image should be applied as a noise type.

  • crop – Boolean flag denoting if randomly cropping the image should be applied as a noise type.

robustness.visual_robust.colorize(img, p)

Randomly tint the color of an image using an existing RGB channel.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.gaussian(img, p)

Randomly add salt-and-pepper noise to the image.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.grayscale(img, p)

Randomly make an image grayscale.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.horizontal_flip(img, p)

Randomly flip the image horizontally.

robustness.visual_robust.inversion(img, p)

Randomly invert an image.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.low_contrast(img, p)

Randomly reduce the contract of an image.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

robustness.visual_robust.periodic(img, periodic_noise_filename='periodic_noise')

Randomly expose the image to periodic pattern/noise.

robustness.visual_robust.random_crop(img, p)

Randomly apply cropping changes.

robustness.visual_robust.rotate(img, p)

Randomly rotate the image by a random angle in [20, 40].

robustness.visual_robust.salt_and_pepper(img, p)

Randomly add salt-and-pepper noise to the image.

Parameters:
  • img – Input to add noise to.

  • p – Probability of applying transformation.

Module contents