tf.image.convert_image_dtype
Stay organized with collections
Save and categorize content based on your preferences.
Convert image
to dtype
, scaling its values if needed.
tf.image.convert_image_dtype(
image, dtype, saturate=False, name=None
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
The operation supports data types (for image
and dtype
) of
uint8
, uint16
, uint32
, uint64
, int8
, int16
, int32
, int64
,
float16
, float32
, float64
, bfloat16
.
Images that are represented using floating point values are expected to have
values in the range [0,1). Image data stored in integer data types are
expected to have values in the range [0,MAX]
, where MAX
is the largest
positive representable number for the data type.
This op converts between data types, scaling the values appropriately before
casting.
Usage Example:
x = [[[1, 2, 3], [4, 5, 6]],
[[7, 8, 9], [10, 11, 12]]]
x_int8 = tf.convert_to_tensor(x, dtype=tf.int8)
tf.image.convert_image_dtype(x_int8, dtype=tf.float16, saturate=False)
<tf.Tensor: shape=(2, 2, 3), dtype=float16, numpy=
array([[[0.00787, 0.01575, 0.02362],
[0.0315 , 0.03937, 0.04724]],
[[0.0551 , 0.063 , 0.07086],
[0.07874, 0.0866 , 0.0945 ]]], dtype=float16)>
Converting integer types to floating point types returns normalized floating
point values in the range [0, 1); the values are normalized by the MAX
value
of the input dtype. Consider the following two examples:
a = [[[1], [2]], [[3], [4]]]
a_int8 = tf.convert_to_tensor(a, dtype=tf.int8)
tf.image.convert_image_dtype(a_int8, dtype=tf.float32)
<tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
array([[[0.00787402],
[0.01574803]],
[[0.02362205],
[0.03149606]]], dtype=float32)>
a_int32 = tf.convert_to_tensor(a, dtype=tf.int32)
tf.image.convert_image_dtype(a_int32, dtype=tf.float32)
<tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
array([[[4.6566129e-10],
[9.3132257e-10]],
[[1.3969839e-09],
[1.8626451e-09]]], dtype=float32)>
Despite having identical values of a
and output dtype of float32
, the
outputs differ due to the different input dtypes (int8
vs. int32
). This
is, again, because the values are normalized by the MAX
value of the input
dtype.
Note that converting floating point values to integer type may lose precision.
In the example below, an image tensor b
of dtype float32
is converted to
int8
and back to float32
. The final output, however, is different from
the original input b
due to precision loss.
b = [[[0.12], [0.34]], [[0.56], [0.78]]]
b_float32 = tf.convert_to_tensor(b, dtype=tf.float32)
b_int8 = tf.image.convert_image_dtype(b_float32, dtype=tf.int8)
tf.image.convert_image_dtype(b_int8, dtype=tf.float32)
<tf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=
array([[[0.11811024],
[0.33858266]],
[[0.5590551 ],
[0.77952754]]], dtype=float32)>
Scaling up from an integer type (input dtype) to another integer type (output
dtype) will not map input dtype's MAX
to output dtype's MAX
but converting
back and forth should result in no change. For example, as shown below, the
MAX
value of int8 (=127) is not mapped to the MAX
value of int16 (=32,767)
but, when scaled back, we get the same, original values of c
.
c = [[[1], [2]], [[127], [127]]]
c_int8 = tf.convert_to_tensor(c, dtype=tf.int8)
c_int16 = tf.image.convert_image_dtype(c_int8, dtype=tf.int16)
print(c_int16)
tf.Tensor(
[[[ 256]
[ 512]]
[[32512]
[32512]]], shape=(2, 2, 1), dtype=int16)
c_int8_back = tf.image.convert_image_dtype(c_int16, dtype=tf.int8)
print(c_int8_back)
tf.Tensor(
[[[ 1]
[ 2]]
[[127]
[127]]], shape=(2, 2, 1), dtype=int8)
Scaling down from an integer type to another integer type can be a lossy
conversion. Notice in the example below that converting int16
to uint8
and
back to int16
has lost precision.
d = [[[1000], [2000]], [[3000], [4000]]]
d_int16 = tf.convert_to_tensor(d, dtype=tf.int16)
d_uint8 = tf.image.convert_image_dtype(d_int16, dtype=tf.uint8)
d_int16_back = tf.image.convert_image_dtype(d_uint8, dtype=tf.int16)
print(d_int16_back)
tf.Tensor(
[[[ 896]
[1920]]
[[2944]
[3968]]], shape=(2, 2, 1), dtype=int16)
Note that converting from floating point inputs to integer types may lead to
over/underflow problems. Set saturate to True
to avoid such problem in
problematic conversions. If enabled, saturation will clip the output into the
allowed range before performing a potentially dangerous cast (and only before
performing such a cast, i.e., when casting from a floating point to an integer
type, and when casting from a signed to an unsigned type; saturate
has no
effect on casts between floats, or on casts that increase the type's range).
Args |
image
|
An image.
|
dtype
|
A DType to convert image to.
|
saturate
|
If True , clip the input before casting (if necessary).
|
name
|
A name for this operation (optional).
|
Returns |
image , converted to dtype .
|
Raises |
AttributeError
|
Raises an attribute error when dtype is neither
float nor integer.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-04-26 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.image.convert_image_dtype\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/image_ops_impl.py#L2378-L2560) |\n\nConvert `image` to `dtype`, scaling its values if needed.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.image.convert_image_dtype`](https://www.tensorflow.org/api_docs/python/tf/image/convert_image_dtype)\n\n\u003cbr /\u003e\n\n tf.image.convert_image_dtype(\n image, dtype, saturate=False, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Pruning for on-device inference w/ XNNPACK](https://www.tensorflow.org/model_optimization/guide/pruning/pruning_for_on_device_inference) | - [DeepDream](https://www.tensorflow.org/tutorials/generative/deepdream) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) - [Integrated gradients](https://www.tensorflow.org/tutorials/interpretability/integrated_gradients) - [Load video data](https://www.tensorflow.org/tutorials/load_data/video) - [Transfer learning for video classification with MoViNet](https://www.tensorflow.org/tutorials/video/transfer_learning_with_movinet) |\n\nThe operation supports data types (for `image` and `dtype`) of\n`uint8`, `uint16`, `uint32`, `uint64`, `int8`, `int16`, `int32`, `int64`,\n`float16`, `float32`, `float64`, `bfloat16`.\n\nImages that are represented using floating point values are expected to have\nvalues in the range \\[0,1). Image data stored in integer data types are\nexpected to have values in the range `[0,MAX]`, where `MAX` is the largest\npositive representable number for the data type.\n\nThis op converts between data types, scaling the values appropriately before\ncasting.\n\n#### Usage Example:\n\n x = [[[1, 2, 3], [4, 5, 6]],\n [[7, 8, 9], [10, 11, 12]]]\n x_int8 = tf.convert_to_tensor(x, dtype=tf.int8)\n tf.image.convert_image_dtype(x_int8, dtype=tf.float16, saturate=False)\n \u003ctf.Tensor: shape=(2, 2, 3), dtype=float16, numpy=\n array([[[0.00787, 0.01575, 0.02362],\n [0.0315 , 0.03937, 0.04724]],\n [[0.0551 , 0.063 , 0.07086],\n [0.07874, 0.0866 , 0.0945 ]]], dtype=float16)\u003e\n\nConverting integer types to floating point types returns normalized floating\npoint values in the range \\[0, 1); the values are normalized by the `MAX` value\nof the input dtype. Consider the following two examples: \n\n a = [[[1], [2]], [[3], [4]]]\n a_int8 = tf.convert_to_tensor(a, dtype=tf.int8)\n tf.image.convert_image_dtype(a_int8, dtype=tf.float32)\n \u003ctf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=\n array([[[0.00787402],\n [0.01574803]],\n [[0.02362205],\n [0.03149606]]], dtype=float32)\u003e\n\n a_int32 = tf.convert_to_tensor(a, dtype=tf.int32)\n tf.image.convert_image_dtype(a_int32, dtype=tf.float32)\n \u003ctf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=\n array([[[4.6566129e-10],\n [9.3132257e-10]],\n [[1.3969839e-09],\n [1.8626451e-09]]], dtype=float32)\u003e\n\nDespite having identical values of `a` and output dtype of `float32`, the\noutputs differ due to the different input dtypes (`int8` vs. `int32`). This\nis, again, because the values are normalized by the `MAX` value of the input\ndtype.\n\nNote that converting floating point values to integer type may lose precision.\nIn the example below, an image tensor `b` of dtype `float32` is converted to\n`int8` and back to `float32`. The final output, however, is different from\nthe original input `b` due to precision loss. \n\n b = [[[0.12], [0.34]], [[0.56], [0.78]]]\n b_float32 = tf.convert_to_tensor(b, dtype=tf.float32)\n b_int8 = tf.image.convert_image_dtype(b_float32, dtype=tf.int8)\n tf.image.convert_image_dtype(b_int8, dtype=tf.float32)\n \u003ctf.Tensor: shape=(2, 2, 1), dtype=float32, numpy=\n array([[[0.11811024],\n [0.33858266]],\n [[0.5590551 ],\n [0.77952754]]], dtype=float32)\u003e\n\nScaling up from an integer type (input dtype) to another integer type (output\ndtype) will not map input dtype's `MAX` to output dtype's `MAX` but converting\nback and forth should result in no change. For example, as shown below, the\n`MAX` value of int8 (=127) is not mapped to the `MAX` value of int16 (=32,767)\nbut, when scaled back, we get the same, original values of `c`. \n\n c = [[[1], [2]], [[127], [127]]]\n c_int8 = tf.convert_to_tensor(c, dtype=tf.int8)\n c_int16 = tf.image.convert_image_dtype(c_int8, dtype=tf.int16)\n print(c_int16)\n tf.Tensor(\n [[[ 256]\n [ 512]]\n [[32512]\n [32512]]], shape=(2, 2, 1), dtype=int16)\n c_int8_back = tf.image.convert_image_dtype(c_int16, dtype=tf.int8)\n print(c_int8_back)\n tf.Tensor(\n [[[ 1]\n [ 2]]\n [[127]\n [127]]], shape=(2, 2, 1), dtype=int8)\n\nScaling down from an integer type to another integer type can be a lossy\nconversion. Notice in the example below that converting `int16` to `uint8` and\nback to `int16` has lost precision. \n\n d = [[[1000], [2000]], [[3000], [4000]]]\n d_int16 = tf.convert_to_tensor(d, dtype=tf.int16)\n d_uint8 = tf.image.convert_image_dtype(d_int16, dtype=tf.uint8)\n d_int16_back = tf.image.convert_image_dtype(d_uint8, dtype=tf.int16)\n print(d_int16_back)\n tf.Tensor(\n [[[ 896]\n [1920]]\n [[2944]\n [3968]]], shape=(2, 2, 1), dtype=int16)\n\nNote that converting from floating point inputs to integer types may lead to\nover/underflow problems. Set saturate to `True` to avoid such problem in\nproblematic conversions. If enabled, saturation will clip the output into the\nallowed range before performing a potentially dangerous cast (and only before\nperforming such a cast, i.e., when casting from a floating point to an integer\ntype, and when casting from a signed to an unsigned type; `saturate` has no\neffect on casts between floats, or on casts that increase the type's range).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|----------------------------------------------------------|\n| `image` | An image. |\n| `dtype` | A `DType` to convert `image` to. |\n| `saturate` | If `True`, clip the input before casting (if necessary). |\n| `name` | A name for this operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| `image`, converted to `dtype`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|------------------|--------------------------------------------------------------------|\n| `AttributeError` | Raises an attribute error when dtype is neither float nor integer. |\n\n\u003cbr /\u003e"]]