TF 2.0 is out! Get hands-on practice at TF World, Oct 28-31. Use code TF20 for 20% off select passes. Register now

tfds.features.Image

View source

Class Image

FeatureConnector for images.

Inherits From: FeatureConnector

During _generate_examples, the feature connector accept as input any of:

  • str: path to a {bmp,gif,jpeg,png} image (ex: /path/to/img.png).
  • np.array: 3d np.uint8 array representing an image.
  • A file object containing the png or jpeg encoded image string (ex: io.BytesIO(encoded_img_bytes))

Output:

tf.Tensor of type tf.uint8 and shape [height, width, num_channels] for BMP, JPEG, and PNG images and shape [num_frames, height, width, 3] for GIF images.

Example:

features=features.FeaturesDict({
    'input': features.Image(),
    'target': features.Image(shape=(None, None, 1),
                               encoding_format='png'),
})
  • During generation:
yield {
    'input': 'path/to/img.jpg',
    'target': np.ones(shape=(64, 64, 1), dtype=np.uint8),
}

__init__

View source

__init__(
    shape=None,
    dtype=None,
    encoding_format=None
)

Construct the connector.

Args:

  • shape: tuple of ints or None, the shape of decoded image. For GIF images: (num_frames, height, width, channels=3). num_frames, height and width can be None. For other images: (height, width, channels). height and width can be None. See tf.image.encode_* for doc on channels parameter. Defaults to (None, None, 3).
  • dtype: tf.uint16 or tf.uint8 (default). tf.uint16 can be used only with png encoding_format
  • encoding_format: 'jpeg' or 'png' (default). Format to serialize np.ndarray images on disk. If image is loaded from {bmg,gif,jpeg,png} file, this parameter is ignored, and file original encoding is used.

Raises:

  • ValueError: If the shape is invalid

Properties

dtype

Return the dtype (or dict of dtype) of this FeatureConnector.

shape

Return the shape (or dict of shape) of this FeatureConnector.

Methods

decode_example

View source

decode_example(example)

Reconstruct the image from the tf example.

encode_example

View source

encode_example(image_or_path_or_fobj)

Convert the given image into a dict convertible to tf example.

get_serialized_info

View source

get_serialized_info()

get_tensor_info

View source

get_tensor_info()

load_metadata

View source

load_metadata(
    data_dir,
    feature_name=None
)

See base class for details.

save_metadata

View source

save_metadata(
    data_dir,
    feature_name=None
)

See base class for details.

set_dtype

View source

set_dtype(dtype)

Update the dtype.

set_encoding_format

View source

set_encoding_format(encoding_format)

Update the encoding format.

set_shape

View source

set_shape(shape)

Update the shape.