tensorflow: ByteBuffer is not a valid flatbuffer model

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 18.04.5 LTS [Google Colab]
  • TensorFlow installed from (source or binary): nightly
  • TensorFlow version (or github SHA if from source): 2.5.0-dev20201029

Command used to run the converter or code if you’re using the Python API I am trying to covert custom efficientdet d2 object detection model that I trained for Android. I successfully trained it and also was able to convert it to tflite by using various github issue threads. I followed this colab file to convert and was able to convert also used tf_convert and was able to convert

# did ran this after running export_tflite_graph_tf2.py
tflite_convert --saved_model_dir /content/model_nightly/saved_model --output_file /content/tflite-conversion.tflite

and also

import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model('/content/model_nightly/saved_model/',signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

tflite_model = converter.convert()

with tf.io.gfile.GFile('/content/model.tflite', 'wb') as f:
  f.write(tflite_model)

The output from the converter invocation from command 1

W1030 22:40:38.686931 139654416934784 function_deserialization.py:416] Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
W1030 22:40:38.687373 139654416934784 function_deserialization.py:416] Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
W1030 22:40:38.687821 139654416934784 function_deserialization.py:416] Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
2020-10-30 22:40:56.738889: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:316] Ignored output_format.
2020-10-30 22:40:56.738961: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:319] Ignored drop_control_dependency.
2020-10-30 22:40:56.738973: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:325] Ignored change_concat_input_ranges.
2020-10-30 22:40:56.740050: I tensorflow/cc/saved_model/reader.cc:32] Reading SavedModel from: /content/model_graph_100K_nightly/saved_model
2020-10-30 22:40:57.034144: I tensorflow/cc/saved_model/reader.cc:55] Reading meta graph with tags { serve }
2020-10-30 22:40:57.034227: I tensorflow/cc/saved_model/reader.cc:93] Reading SavedModel debug info (if present) from: /content/model_graph_100K_nightly/saved_model
2020-10-30 22:40:57.034311: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2020-10-30 22:40:57.901184: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:196] None of the MLIR optimization passes are enabled (registered 0 passes)
2020-10-30 22:40:58.067181: I tensorflow/cc/saved_model/loader.cc:206] Restoring SavedModel bundle.
2020-10-30 22:40:58.215865: I tensorflow/core/platform/profile_utils/cpu_utils.cc:112] CPU Frequency: 2300000000 Hz
2020-10-30 22:41:00.181931: I tensorflow/cc/saved_model/loader.cc:190] Running initialization op on SavedModel bundle at path: /content/model_graph_100K_nightly/saved_model
2020-10-30 22:41:01.017615: I tensorflow/cc/saved_model/loader.cc:277] SavedModel load for tags { serve }; Status: success: OK. Took 4277564 microseconds.
2020-10-30 22:41:04.164837: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:194] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
2020-10-30 22:41:06.474280: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
Exception ignored in: <bound method Buckets.__del__ of <tensorflow.python.eager.monitoring.ExponentialBuckets object at 0x7f037e83e798>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/monitoring.py", line 407, in __del__
AttributeError: 'NoneType' object has no attribute 'TFE_MonitoringDeleteBuckets'

from command 2

WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.
WARNING:absl:Importing a function (__inference_EfficientDet-D2_layer_call_and_return_conditional_losses_131625) with ops with custom gradients. Will likely fail if a gradient is requested.

Also, please include a link to the saved model or GraphDef

# Put link here or attach to the issue.

Failure details

  • While using it in the android application from the repository here, I get the following error.
E/tensorflow: CameraActivity: Exception!
    java.lang.RuntimeException: java.lang.IllegalArgumentException: ByteBuffer is not a valid flatbuffer model
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:153)
        at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:99)
        at org.tensorflow.lite.examples.detection.CameraActivity.onPreviewFrame(CameraActivity.java:200)
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1221)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalArgumentException: ByteBuffer is not a valid flatbuffer model
        at org.tensorflow.lite.NativeInterpreterWrapper.createModelWithBuffer(Native Method)
        at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:62)
        at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:277)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:148)
        at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:99) 
        at org.tensorflow.lite.examples.detection.CameraActivity.onPreviewFrame(CameraActivity.java:200) 
        at android.hardware.Camera$EventHandler.handleMessage(Camera.java:1221) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

I am trying to do this for my college project.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (7 by maintainers)

Most upvoted comments

Am also having this issue as well

It seems that I get that error only when I load the module on a separate thread and only for some devices.