TensorFlow
Android
Machine Learning
Mobile Development
Custom Model

Tensorflow Android demo load a custom graph in?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Loading a custom model into the old TensorFlow Android demo is mostly a matter of matching the demo code to your model's actual files, tensor names, and input preprocessing. The key point is that the demo does not magically understand a new graph just because you copied the model file into the project. You have to tell the app what the input and output nodes are and how to feed them.

Old Demo Flow Versus Modern TensorFlow Lite

Historically, the TensorFlow Android demo often used frozen graphs, label files, and explicit input or output operation names. Modern Android deployments more commonly use TensorFlow Lite and the Interpreter API.

If you are modifying the old demo, check which style it expects:

  • frozen .pb graph with explicit tensor names
  • '.tflite model with TFLite-specific classifier code'

The setup differs between the two.

What Must Match Your Model

For a custom model, these pieces must agree with the training export:

  • model file path in Android assets
  • label file path if classification labels are used
  • input tensor name
  • output tensor name or indices
  • input image size and color format
  • output interpretation logic

If any of those are wrong, inference may compile and still return nonsense.

TensorFlow Lite Example on Android

A modern Android example looks like this:

kotlin
1import android.content.res.AssetFileDescriptor
2import java.io.FileInputStream
3import java.nio.MappedByteBuffer
4import java.nio.channels.FileChannel
5import org.tensorflow.lite.Interpreter
6
7fun loadModelFile(fd: AssetFileDescriptor): MappedByteBuffer {
8    FileInputStream(fd.fileDescriptor).channel.use { channel ->
9        return channel.map(
10            FileChannel.MapMode.READ_ONLY,
11            fd.startOffset,
12            fd.declaredLength
13        )
14    }
15}
16
17val model = context.assets.openFd("model.tflite")
18val interpreter = Interpreter(loadModelFile(model))

Then prepare the input tensor in the shape your model expects and call run or runForMultipleInputsOutputs.

If You Are Replacing the Old Demo Model

The old demo code usually hardcodes assumptions such as:

  • expected image width and height
  • normalization strategy
  • labels file name
  • output format such as top-k classification scores

So after copying your model into assets, update the classifier code accordingly.

For example, a model trained on 224 x 224 RGB images cannot be fed with the preprocessing used for a different input shape unless you adapt the code.

Frozen-Graph Style Tensor Names

If the demo still uses a frozen graph, the input and output names must match the exported graph exactly. For example, a classifier might expect something like:

  • input: input
  • output: Softmax

Those names are model-specific. If the Android code refers to the wrong tensor name, inference fails or returns empty results.

Preprocessing Is Usually the Real Problem

Many "my custom graph does not work" issues are not graph-loading issues at all. They are preprocessing mismatches.

Examples:

  • model expects normalized floats, but app sends raw bytes
  • model expects grayscale, but app sends RGB
  • model expects a different image size
  • label order in the app does not match training order

That is why you should document the model contract when exporting it.

Test the Same Input Outside Android First

Before blaming the Android app, verify the exported model on desktop Python or a small host-side script using the exact same input preprocessing. If the result is already wrong there, the problem is the exported model or preprocessing logic, not Android.

Common Pitfalls

A common mistake is replacing only the model file and leaving the old tensor names and preprocessing code untouched.

Another mistake is confusing the old frozen-graph demo with the newer TensorFlow Lite workflow. They are related historically, but not interchangeable.

Developers also often overlook label ordering, which can make a working model appear broken because the predicted index is mapped to the wrong class name.

Summary

  • A custom Android model must match the app's tensor names, preprocessing, and output parsing.
  • Determine first whether the demo expects a frozen graph or a .tflite model.
  • Updating the model file alone is not enough.
  • Input size, normalization, and label order are common failure points.
  • Validate the exported model outside Android before debugging the mobile integration layer.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.