How to Convert an Image to a Tensor in TensorFlow

In this post we will cover the topic of how to convert an image to a Tensor in TensorFlow.  To convert an image to a tensor in TensorFlow we use tf.convert_to_tensor(). This function accepts a Python object of various types. In this post we will read the input image using Pillow and OpenCV and convert the image to a Tensor. The image is in [H, W, C] format, where H, W and C are the height, width and number of channels of the image. And the shape of the output tensor is also in [H, W, C] format. TensorFlow is a very rich library to work on Computer Vision or Image Processing related tasks.  

Contents:

  1. Prerequisites
  2. Syntax
  3. Steps to Convert an Image to Tensor
  4. Input Image
  5. Example 1
  6. Example 2

Prerequisites:

  • Python
  • TensorFlow
  • Pillow
  • and/ or OpenCV

Syntax:

tf.convert_to_tensor(
    value, dtype=None, dtype_hint=None, name=None
)

Parameters:
  • value: An object to be converted to Tensor.
  • dtype: Data type of the returned tensor.  It's optional and if it is missing, the type is inferred from the type of value. 
  • dtype_hint: Data type for the returned tensor, used when dtype is None. Its also an optional argument. 
  • name: Name to use if a new Tensor is created. It's also an optional argument.
It converts an object to a Tensor of data type dtype.
Here in our case we want to convert an image to Tensor. value is a PIL image. The dtype of the converted tensor is tf.uint8 and the range of the tensor values is [0,255].

Steps to Convert an Image to Tensor:

To convert an image to a Tensor in tensor flow, we can follow the below given steps:

  1. Import the required libraries. Here required libraries are TensorFlow, Pillow and OpenCV.
  2. Read the input image. Here we are taking a PIL image as an input image. The image is in [H, W, C] format, where H, W and C are the height, width and number of channels of the image.
  3. Convert the image to tensor using tf.convert_to_tensor().
  4. Print the tensor values.

Input Image:

Image to convert to torch in TensorFlow
image.jpg

Example 1:

The Python3 code below shows how to convert a PIL image to Tensor using tf.convert_to_tensor(). The value is our PIL image and we are not providing other optional arguments. So using default dtype=None, dtype_hint=None, name=None.

# Import required libraries
import tensorflow as tf
from PIL import Image
# Read a PIL image  
img = Image.open('image.jpg')
print(img)
# Convert the PIL image to Tensor
img_to_tensor = tf.convert_to_tensor(img)
# print the converted Torch tensor
print(img_to_tensor)
print("dtype of tensor:",img_to_tensor.dtype)

Output:

tf.Tensor(
[[[218 225 235]
  [216 225 234]
  [216 225 232]
  ...
  [215 224 231]
  [215 224 231]
  [215 224 231]]

 [[217 224 234]
  [216 225 234]
  [216 225 232]
  ...
  [215 224 231]
  [215 224 231]
  [215 224 231]]

 [[217 224 234]
  [216 225 234]
  [216 225 232]
  ...
  [215 224 231]
  [215 224 231]
  [215 224 231]]

 ...

 [[126 120  96]
  [159 153 129]
  [153 147 123]
  ...
  [185 178 162]
  [184 177 161]
  [182 175 159]]

 [[112 105  79]
  [127 119  96]
  [139 131 108]
  ...
  [185 178 162]
  [183 176 158]
  [182 175 157]]

 [[153 146 120]
  [112 104  81]
  [102  94  71]
  ...
  [184 177 161]
  [183 176 158]
  [182 175 157]]], shape=(450, 650, 3), dtype=uint8)
dtype of tensor: <dtype: 'uint8'>

Notice that the data type of the output tensor is uint8 and the values are in range [0,255]. The shape of the image tensor is [450, 650, 3].

Example 2:

In this example we read an RGB image using OpenCV. We convert it to torch tensor using tf.convert_to_tensor(). 

The value is our RGB image and we are providing the optional argument dtype=tf.float32. Using other arguments as default , dtype_hint=None, name=None.


# Import required libraries
import tensorflow as tf
import cv2

# Read the image
image = cv2.imread('lion.jpg')
# Convert BGR image to RGB image
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Convert the image to Tensor
img_to_tensor = tf.convert_to_tensor(img, dtype=tf.float32)
# print the converted Torch tensor
print(img_to_tensor)

print("dtype of tensor:",img_to_tensor.dtype)

Output:

The above Python3 program produces the following output.

tf.Tensor(
[[[218. 225. 235.]
  [216. 225. 234.]
  [216. 225. 232.]
  ...
  [215. 224. 231.]
  [215. 224. 231.]
  [215. 224. 231.]]

 [[217. 224. 234.]
  [216. 225. 234.]
  [216. 225. 232.]
  ...
  [215. 224. 231.]
  [215. 224. 231.]
  [215. 224. 231.]]

 [[217. 224. 234.]
  [216. 225. 234.]
  [216. 225. 232.]
  ...
  [215. 224. 231.]
  [215. 224. 231.]
  [215. 224. 231.]]

 ...

 [[126. 120.  96.]
  [159. 153. 129.]
  [153. 147. 123.]
  ...
  [185. 178. 162.]
  [184. 177. 161.]
  [182. 175. 159.]]

 [[112. 105.  79.]
  [127. 119.  96.]
  [139. 131. 108.]
  ...
  [185. 178. 162.]
  [183. 176. 158.]
  [182. 175. 157.]]

 [[153. 146. 120.]
  [112. 104.  81.]
  [102.  94.  71.]
  ...
  [184. 177. 161.]
  [183. 176. 158.]
  [182. 175. 157.]]], shape=(450, 650, 3), dtype=float32)
dtype of tensor: <dtype: 'float32'>

Notice that the data type of the output tensor is tf.float32 and the values are in range [0, 255]. The shape of the image tensor is [450, 650, 3].

References:

Useful Resources:

Next Post: How to Convert an Image to a Tensor in TensorFlow

Previous Post: How to Draw Rectangle on An Image using OpenCV in Python


Comments