You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

替换自定义训练的TensorFlow Lite模型后Android应用崩溃求助

Troubleshooting SIGBUS Crash with Custom Retrained MobileNet in TensorFlow Lite Android Demo

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 .pb Model to .tflite
    The TensorFlow Lite Android app requires a .tflite file, not the raw .pb graph. If you skipped this step or did it incorrectly, that’s the first thing to fix. Use the tflite_convert tool 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 .pb model in TensorBoard or using saved_model_cli to inspect the graph.
    • Confirm the input shape matches your mobilenet_0.50_224 model (224x224x3).
  • 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 (or ClassifierQuantizedMobileNet) class in the Android demo.
    • Update these constants to match your model:
      • INPUT_SIZE = 224 (matches your model’s input dimension)
      • IMAGE_MEAN = 127.5
      • IMAGE_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.
  • Check for Corrupted Model Files
    SIGBUS errors often happen when the app tries to read an incomplete or corrupted .tflite file:

    • Re-copy the converted .tflite file into your Android project’s assets directory.
    • Compare the file size of the .tflite file in your project with the one on your local machine to confirm they’re identical.
  • Validate the TFLite Model Locally
    Test the converted .tflite model 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 MobileNet
    

    If this throws errors, your conversion step was faulty—go back to the first point and adjust the tflite_convert parameters.

  • 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.TFLiteConverter instead of the old tflite_convert tool).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:54:32