Skip to main content

An implementation of novel data augmentation AugMix (2020) in TensorFlow. It runs on TPU.

Project description

augmix-tf

Augmix-tf is an implementation of novel data augmentation AugMix (2020) in TensorFlow. It runs on TPU.

AugMix utilizes simple augmentation operations which are stochastically sampled and layered to produce a high diversity of augmented images. The process of mixing basic tranformations into augmented image is shown below (picture taken from the original paper). This augmentation performs better when used in concert with Jensen-Shannon Divergence Consistency Loss. AugMix pipeline

Installation

pip install augmix-tf

Usage

AugMix

The main function, which does the augmentation is AugMix.transform, let's print a docstring of it.

from augmix import AugMix
print(AugMix.transform.__doc__)
	Performs AugMix data augmentation on given image.

	Parameters:
	image (tf tensor): an image tensor with shape (x, x, 3) and values scaled to range [0, 1]
	severity (int): level of a strength of transformations (integer from 1 to 10)
	width (int): number of different chains of transformations to be mixed
	depth (int): number of transformations in one chain, -1 means random from 1 to 3

	Returns:
	tensor: augmented image

Example 1 - transforming a single image

from PIL import Image
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from augmix import AugMix

# precalculated means and stds of the dataset (in RGB order)
means = [0.44892993872313053, 0.4148519066242368, 0.301880284715257]
stds = [0.24393544875614917, 0.2108791383467354, 0.220427056859487]
ag = AugMix(means, stds)

# preprocess
image = np.asarray(Image.open('geranium.jpg'))
image = tf.convert_to_tensor(image)
image = tf.cast(image, dtype=tf.float32)
image = tf.image.resize(image, (331, 331)) # resize to square
image /=  255  # scale to [0, 1]

# augment
augmented = ag.transform(image)

# visualize
comparison = tf.concat([image, augmented], axis=1)
plt.imshow(comparison.numpy())
plt.title("Original image (left) and augmented image (right).")
plt.show()

result of example 1)

Example 2 - transforming a dataset of images

# here a dataset is a tf.data.Dataset object
# assuming images are properly preprocessed (see example 1)
dataset = dataset.map(lambda  img: ag.transform(img))

Example 3 - transforming a dataset to use with the Jensen-Shannon loss

# here a dataset is a tf.data.Dataset object
# assuming images are properly preprocessed (see example 1)
dataset = dataset.map(lambda  img: (img, ag.transform(img), ag.transform(img)))

Visualization

AugMix

original images original images

augmented visualization of augmix

Simple transformations

AugMix mixes images transformed by simple augmentations defined in transformations.py file. Every transformation function takes an image and level parameter that determines a strength of this transformation. This level parameter has the same value as severity parameter in AugMix.transform function, so again it is the integer between 1 and 10, where 10 means the strongest augmentation. These functions can be used by itself. Below is a visualization what every simple augmentation does to a batch of images (at level 10).

translate_x, translate_y translate

rotate rotate

shear_x, shear_y shear

solarize solarize

solarize_add solarize add

posterize posterize

autocontrast autocontrast

contrast contrast

equalize equalize

brightness brightness

color color

More information

TODO

  • batch implementation of AugMix
  • possibility to choose basic transformations easily
  • appendix
    • calculation of mean and standard devation on a dataset
    • implementation of Jensen-Shannon Divergence Consistency Loss

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

augmix-tf-1.1.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

augmix_tf-1.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file augmix-tf-1.1.0.tar.gz.

File metadata

  • Download URL: augmix-tf-1.1.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for augmix-tf-1.1.0.tar.gz
Algorithm Hash digest
SHA256 af1154aa7f982149621ea97d0d7b3d694a1ba5b426fa234815d2abea14dafa7a
MD5 08b26a36b0ca57e7dc98d9dd84e890af
BLAKE2b-256 d23e8524d952410f554b6027d07267f25aab25808263bfce208d51d1d7928f83

See more details on using hashes here.

File details

Details for the file augmix_tf-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: augmix_tf-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for augmix_tf-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f9cce2f7b6d7904f5e38b1f775344c3d4b0351b1262c0417961813061b547f0
MD5 0a3abcd85c17cadd3472e464daea159f
BLAKE2b-256 8247b8839be5eea477cdc50ccbbc43e821505e005ac9e334dc1f0fdff62b39a1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page