A problem that I needed to solve today at work was how to import a single color image in OpenCV format with variable X-, Y-size dimensions into a TensorFlow Inception V4 model with dimensions (x, 299, 299, 1) for prediction. The dimension (299, 299, 3) is a color image, while (299, 299, 1) is in gray scale. This post is a very short post with a few lines of Python code.

First our imports.

import cv2
import tensorflow as tf

OpenCV uses BGR format, therefore the original image must read and converted to RGB.

img_bgr = cv2.imread(filename='/path/to/file')
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

Once this is complete, the image can be placed into a TensorFlow tensor.

img_tensor = tf.convert_to_tensor(img_rgb, dtype=tf.float32)

Now the image can be converted to gray-scale using the TensorFlow API. A note of caution is necessary here. Some PIL and OpenCV routines will output a gray-scale image, but still retain 3 channels in the image, leaving the image appearing gray but unflattened. Such an image is still color, in spite of its visual appearance. The Inception v4 model that I am using requires a single-channel image, which is true gray-scale.

img_gray = tf.image.rgb_to_grayscale(img_tensor)

Now the image must be resized to 299×299. If the image is smaller than 299×299 the pad will add “0”s to the additional pixels. If larger, the resize will shrink the image accordingly.

img_resized = tf.image.resize_with_pad(img_gray, 299, 299)

At this point our tensor dimension is (299, 299, 1). Another dimension must be added to the tensor prior to feeding into the prediction algorithm.

img_final = tf.expand_dims(img_resized, 0)

This adds a dimension to highest level of the tensor, giving it dimensions (1, 299, 299, 1). After this step the tensor is now prepared for undergoing prediction operations.