替换自定义训练的TensorFlow Lite模型后Android应用崩溃求助
Let’s walk through the most likely causes and fixes for this crash—since your model works locally with label_image, the issue almost certainly lies in how the model is converted for TensorFlow Lite or how the Android app is configured to use it.
Ensure You Properly Converted the
.pbModel to.tflite
The TensorFlow Lite Android app requires a.tflitefile, not the raw.pbgraph. If you skipped this step or did it incorrectly, that’s the first thing to fix. Use thetflite_converttool with parameters matching your model’s specs:tflite_convert \ --graph_def_file=tf_files/retrained_graph.pb \ --output_file=tf_files/retrained_graph.tflite \ --input_arrays=input \ --output_arrays=final_result \ --input_shapes=1,224,224,3- Double-check the input/output array names: You can verify these by loading the
.pbmodel in TensorBoard or usingsaved_model_clito inspect the graph. - Confirm the input shape matches your
mobilenet_0.50_224model (224x224x3).
- Double-check the input/output array names: You can verify these by loading the
Verify Android App Input Preprocessing Matches Your Model
MobileNet models expect specific input normalization, and mismatched preprocessing can cause memory access errors (like SIGBUS):- Open the
ImageClassifier(orClassifierQuantizedMobileNet) class in the Android demo. - Update these constants to match your model:
INPUT_SIZE = 224(matches your model’s input dimension)IMAGE_MEAN = 127.5IMAGE_STD = 127.5(these values normalize pixel data from 0-255 to [-1, 1], which MobileNet expects)
- Ensure the number of output classes (
OUTPUT_CLASSES) matches the number of flower categories in your training dataset.
- Open the
Check for Corrupted Model Files
SIGBUS errors often happen when the app tries to read an incomplete or corrupted.tflitefile:- Re-copy the converted
.tflitefile into your Android project’sassetsdirectory. - Compare the file size of the
.tflitefile in your project with the one on your local machine to confirm they’re identical.
- Re-copy the converted
Validate the TFLite Model Locally
Test the converted.tflitemodel on your computer first to rule out conversion issues:import tensorflow as tf # Load the TFLite model interpreter = tf.lite.Interpreter(model_path="tf_files/retrained_graph.tflite") interpreter.allocate_tensors() # Check input/output specs input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() print(f"Expected input shape: {input_details[0]['shape']}") # Should be [1, 224, 224, 3] print(f"Expected input dtype: {input_details[0]['dtype']}") # Typically float32 for unquantized MobileNetIf this throws errors, your conversion step was faulty—go back to the first point and adjust the
tflite_convertparameters.Match TensorFlow Versions
Make sure the TensorFlow version used to train the model is compatible with the TensorFlow Lite version in your Android app:- If you trained with TensorFlow 1.x, use a 1.x-compatible TFLite dependency in your
build.gradle(e.g.,org.tensorflow:tensorflow-lite:1.15.0). - If using TensorFlow 2.x, ensure you used the correct conversion workflow (e.g., using
tf.lite.TFLiteConverterinstead of the oldtflite_converttool).
- If you trained with TensorFlow 1.x, use a 1.x-compatible TFLite dependency in your
Start with the first two checks—these are the most common culprits when moving a working local model to Android. If the crash persists, share more details about your conversion command and Android code adjustments, and we can dig deeper.
内容的提问来源于stack exchange,提问作者Mandar

