Skip to main content

tf-image

tf-image implements methods for image augmentation for Tensorflow 2.+ / tf.data.Dataset.

Why?

Official TensorFlow 2+ tf.image package contains just a few and simple operations for image augmentation. This is not enough if you want to augment images and using all the power of tf.data.Dataset. There is also tf-addons projects which contains more of the operations (for example rotate), but it still not enough. And on top of that, none of those two supports operation on bounding boxes and therefore is not fully usable for augmentation object detection datasets.

If you do not require the operations in graph then simply use cv2, imgaug or albumentations together with tf.py_function. They have (at the moment) much more operations and options for image augmentation.

Installation

Use pip:

pip install tf-image

For installation from source code, clone the repository and install it from code (pip install -e .). There are no dependencies specified. You have to install TensorFlow 2+ and appropriate TensorFlow Addons. Specific version is on you, we wanted to keep this library as general as possible.

Image and bounding boxes formats

We use channel last format for images. Images can be represented either in 0.0 - 1.0 or 0 - 255 range. Similar is true for bounding boxes. They can be provided either in relative coordinates with range 0.0 - 1.0 using float dtype or in absolute image coordinates using integer dtype. Internally, This is done using convert_type decorator on functions which needs it. This decorator converts the images into the type we use (tf.float and 0.0-1.1 in both cases) and after the function is done, original format is restored. If performing multiple operations, you can use this decorator on own function. (Conversions after each operation will not be needed.)

Quickstart

For your convenience, we included a simple and configurable application, which combines all the provided augmentations. They are performed in a random order to make the augmentation even more powerful.

There is also one script which uses this augmentation function and which outputs three augmented image without bounding boxes and three with bonding boxes. See example/example.py for more information.

If you want to use the functions alone, here is how:

import tensorflow as tf
import tensorflow_addons as tfa

from tf_image.core.random import random_function
from tf_image.core.colors import rgb_shift, channel_drop
from tf_image.core.convert_type_decorator import convert_type


@convert_type
def augment_image(image):
    # use TensorFlow library
    image = tf.image.random_flip_left_right(image)
    image = tf.image.random_flip_up_down(image)

    # use tf-image library
    image = random_function(
        image, rgb_shift, 0.1, None, **{"r_shift": 0.1, "g_shift": 0.1, "b_shift": 0.1}
    )  # do rgb shift with 10 % prob
    image = random_function(image, channel_drop, 0.1, None)
    # and whatever else you want

    # use TensorFlow Addons library
    image = tfa.image.rotate(image, 10)

    return image


def map_function(image_file, label):
    image = tf.io.read_file(image_file)
    image = tf.image.decode_jpeg(image)
    image = augment_image(image)

    return image, label


def return_dataset(image_files, labels):
    dataset = (
        tf.data.Dataset.from_tensor_slices((image_files, labels))
        .cache()
        .shuffle(len(image_files))
        .map(map_function)
        .batch(20)
        .prefetch(tf.data.experimental.AUTOTUNE)
    )

    return dataset

return_dataset(["images/ximilar-similar.jpg"], [[1,2,3]])

Supported operations

Image augmentations:

  • aspect ration deformations (inc. bounding boxes)
  • channel drop
  • channel swap
  • erase, see [https://arxiv.org/abs/1708.04552] (repeated, inc. bounding boxes)
  • flip up-down, left-right (inc. bounding boxes)
  • grayscale
  • gaussian noise
  • clip (inc. bounding boxes)
  • rgb shift
  • resize with different methods (inc. bounding boxes)
  • rotate (inc. bounding boxes)

Random operations:

  • random_function: calls function on image with some probability [0.0, 0.1]
  • random_function_bboxes: calls function on image and bounding boxes with some probability [0.0, 0.1]

Feel free to improve and add more functions. We are looking forward to your merge requests! (Please only plain tensorflow2+, no opencv.)

Metadata

Release files for tf-image 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tf-image 0.2.0
File Size Uploaded Provenance
tf-image-0.2.0.tar.gz 16.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tf-image 0.2.0
File Interpreter ABI Platform
tf_image-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.4 kB

Release files / tf-image-0.2.0.tar.gz

Download URL tf-image-0.2.0.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a9167e9f18be9075bcf5e09c650e66cdc492250c40874c10b11f62a2701ac4c6
BLAKE2b-256 checksum
How to use checksums
be40ac1baba453fa8bed9542c160302c952f27b96009ca1ed951436bd1bf49b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release files / tf_image-0.2.0-py3-none-any.whl

Download URL tf_image-0.2.0-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1e218ffa20183c534d57f0db653dbd79106ba6996e8c5edd273d8f68824ff0c
BLAKE2b-256 checksum
How to use checksums
c4bed4b0a17b209d75369eff55ae0084bc8c2a12573022b2843f95734f23b32a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Security for tf-image 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page