Exception when trying to use tensorflow classify android example on a model trained from scratch
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you train a TensorFlow model from scratch and try to run it with the TensorFlow Classify Android example, you will often hit runtime exceptions like IllegalArgumentException, IOException, or input/output tensor shape mismatches. These errors happen because the example app expects a specific model format, input shape, and label file structure that a custom-trained model may not match. This article walks through the most common causes and provides concrete fixes.
Understanding the Android Classify Example
The TensorFlow Android classify example (and its TensorFlow Lite successor) is built to work with models like MobileNet or Inception. These models expect a fixed input size (commonly 224x224x3 or 299x299x3), produce a fixed number of output classes, and ship with a labels file that maps output indices to human-readable names.
When you substitute your own model, every one of these assumptions can break. The example code does not dynamically adapt to a different model's requirements.
Common Exceptions and Their Causes
1. Input Tensor Shape Mismatch
The most frequent exception looks like this:
This happens when the input image buffer size does not match what the model expects. The example app might prepare a 224x224x3 float buffer (224 * 224 * 3 * 4 = 602,112 bytes), but your model expects a different resolution.
Fix: Update the image preprocessing to match your model's input dimensions.
You can verify what your model expects by inspecting it with Python before deploying:
2. Output Label Count Mismatch
If your model classifies 10 categories but the labels file contains 1,000 entries (or vice versa), the app crashes or produces garbage results.
Fix: Make sure your labels.txt file has exactly as many lines as your model has output classes, in the correct order.
Place this file in the assets folder of your Android project and update the code to reference it:
3. Model File Not Found or Corrupted
This error usually means the .tflite file is not in the correct location, the filename in code does not match the actual file, or the file was corrupted during transfer.
Fix: Place the model file in app/src/main/assets/ and verify the path in your code:
Check the file size after copying. If it is zero bytes or significantly smaller than expected, the file was not copied correctly.
4. Quantization Type Mismatch
The example app may use a quantized model (uint8 inputs) while your custom model uses float32 inputs, or the reverse.
Fix: Either quantize your model to match, or update the app to use float inputs.
To convert a SavedModel to a float TFLite model:
To apply post-training quantization:
Then update the Android code to use the correct input type. For float models, the input buffer should be a ByteBuffer of floats. For quantized models, it should be a ByteBuffer of unsigned bytes.
5. Normalization Mismatch
Even when shapes and types match, incorrect normalization produces wildly wrong predictions without throwing an exception. If your model was trained with pixel values in the range [0, 1] but the app feeds values in [0, 255], every prediction will be wrong.
Check how your training pipeline preprocessed images and replicate the same normalization in the Android app.
Common Pitfalls
- Training with one version of TensorFlow and converting with another. Version mismatches between the training framework and the TFLite converter can produce invalid models. Use the same TensorFlow version for both.
- Forgetting to include all custom ops when converting. If your model uses operations not built into TFLite by default, you need to add them through the Select TF Ops package.
- Not testing the converted model in Python before deploying to Android. Always run inference in Python first to confirm the model produces correct results after conversion.
- Hardcoding image dimensions from the original example without updating them for your model.
- Ignoring the difference between NHWC and NCHW tensor layouts. TFLite typically uses NHWC (batch, height, width, channels), but some training configurations produce NCHW models.
Summary
Most exceptions when running a custom TensorFlow model in the Android classify example come from mismatches between what the app sends and what the model expects. Inspect your model's input and output tensors with the TFLite Interpreter in Python. Match the image dimensions, normalization range, data type (float vs quantized), and label count in your Android code. Test inference in Python before deploying to mobile, and use the same TensorFlow version for training and conversion.

