TensorFlow Lite
TensorImage
Model Input
Machine Learning
Image Processing

How to input TensorImage array or a single TensorImage buffer into a tensorflow lite model?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

With TensorFlow Lite, a TensorImage is a convenient wrapper around image data and preprocessing utilities. For a model that expects one image, the normal flow is to preprocess a TensorImage and pass its underlying buffer to the interpreter. For a model that expects a batch, you usually need to build a larger input buffer that contains several images in the model’s expected shape.

The key point is that the interpreter does not consume a “TensorImage array” directly as a special high-level type. It consumes tensors. TensorImage is just a helper for preparing one image tensor at a time.

Single Image Input

For a model with input shape such as 1 x 224 x 224 x 3, the flow is simple:

  • load the image into TensorImage
  • preprocess it to the expected size and type
  • pass tensorImage.buffer to the interpreter
kotlin
1val image = TensorImage(DataType.FLOAT32)
2image.load(bitmap)
3
4val processor = ImageProcessor.Builder()
5    .add(ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR))
6    .add(NormalizeOp(127.5f, 127.5f))
7    .build()
8
9val processed = processor.process(image)
10
11val inputBuffer = processed.buffer
12interpreter.run(inputBuffer, outputBuffer.buffer)

This is the standard path for image classification and other single-image models.

What If You Have Multiple Images?

If you have several TensorImage objects, you have two possibilities.

First, if the model input shape is batch size 1, run inference once per image. That is the simplest and often the correct approach.

Second, if the model input shape supports batching such as N x 224 x 224 x 3, you need to pack the individual image buffers into one larger tensor buffer in the correct order.

Building a Batch Buffer

Suppose the model expects 4 x 224 x 224 x 3 as FLOAT32. You cannot usually hand the interpreter a Kotlin array of TensorImage objects and expect it to infer the batch tensor automatically. You need to create a tensor-shaped buffer.

kotlin
1val batchSize = 4
2val height = 224
3val width = 224
4val channels = 3
5
6val batchBuffer = TensorBuffer.createFixedSize(
7    intArrayOf(batchSize, height, width, channels),
8    DataType.FLOAT32
9)
10
11val combined = ByteBuffer.allocateDirect(batchSize * height * width * channels * 4)
12combined.order(ByteOrder.nativeOrder())
13
14for (tensorImage in images) {
15    combined.put(tensorImage.buffer)
16}
17combined.rewind()
18
19batchBuffer.loadBuffer(combined)
20interpreter.run(batchBuffer.buffer, outputBuffer.buffer)

This only works if every image was preprocessed to the exact same shape and data type.

Match the Model’s Input Details First

Before building the input, inspect the model input tensor.

kotlin
val inputTensor = interpreter.getInputTensor(0)
println(inputTensor.shape().contentToString())
println(inputTensor.dataType())

Do not guess the shape. If the model expects UINT8 and you feed FLOAT32, or expects NCHW while you assume NHWC, the inference result may be wrong even if the code runs.

Use Task Library Wrappers When Available

If you are using higher-level TFLite Task Library APIs such as image classifiers, those wrappers often expect one image at a time and handle preprocessing internally. In that case, batching may not be exposed through the same API surface at all.

So the practical rule is:

  • interpreter API for low-level tensor control
  • task library for higher-level single-item inference flows

Choose the level that matches your model execution needs.

Common Pitfalls

A common mistake is assuming a TensorImage array can be passed directly to the interpreter as if it were a batch tensor. Usually it cannot.

Another mistake is concatenating buffers without first confirming the model input shape and data type.

Developers also forget that every image in a batch must already be resized and normalized identically.

Finally, if the model batch size is fixed at 1, forcing a manual batch buffer will not help. In that case, run the model once per image unless you retrain or convert a batched model.

Summary

  • A single TensorImage is typically passed to TensorFlow Lite through its underlying buffer.
  • Multiple images require either repeated single-image inference or an explicitly built batch tensor.
  • The interpreter consumes tensors and buffers, not special “TensorImage array” input semantics.
  • Always inspect the model’s expected shape and data type before building the input.
  • High-level task-library APIs often focus on one image at a time, while the raw interpreter API gives you full tensor control.

Course illustration
Course illustration

All Rights Reserved.