Loading 16 grayscale PNG in TF lite

I am a new with Python, and I have to produce a small Python script that load a 16bit PNG and run inference with TensorFlow Lite on a Coral board SDK, using an existing model with shape [224, 224, 1]. The input image is 224x224 16bit mono.

image = Image.open(‘input.png’)
CoralAdapter.set_input(interpreter, image)

ValueError: could not broadcast input array from shape (224,224) into shape (224,224,1)

I assume I need to load the PNG file as a (224,224,1) array instead of a (224,224) array.
What is the proper way to load a grayscale PNG this way?
Can TFlite use 16bit mono PNG, or does it only support 24bit RGB?


TF has native functions to decode images loaded from files like decode_image() or decode_png()
Please, see this documentation page: tf.io.decode_image  |  TensorFlow Core v2.8.0
You can specify the number of channels that you need in a decoded tensor, which can be 1, 3, 4 or the same as in the original image.

You can also set the dtype tf.image.decode_image does not support png grayscale 16bit. · Issue #15681 · tensorflow/tensorflow · GitHub

Turns out that loading the PNG was not the issue. I am now testing with the Coral USB Accelerator, and I need to re-shape the array this way for it to fit my model file:

image = Image.open(args.input)
img = np.array(image)
img.shape = (224,224,1)
common.set_input(interpreter, img)

I can do this from the linux PC I’m working on. I do not remember if I had tried exactly that on the Coral Dev board.

I notice that processing these images require ~65ms, as opposed to the ~5ms requires to process the “bird” example images, despite the example parrot image being much larger. I guess if TFlite is optimized for 8bit input values (RGB) instead of 16 bit values.

If the input image is not a gray scale image please use this line to convert to grayscale: edgetpu/engine.py at master · google-coral/edgetpu · GitHub

Here are some example scripts with pycoral to try with Coral USB Accelerator: pycoral/examples at master · google-coral/pycoral · GitHub