googlecodelabs / tensorflow-for-poets-2

Apache License 2.0
509 stars 463 forks source link

Cannot convert between a TensorFlowLite buffer with X bytes and a ByteBuffer with Y bytes. #105

Open sachin-ranka opened 5 years ago

sachin-ranka commented 5 years ago

Replaced default graph.lite with my custom trained model, followed steps as mentioned in Codelabs

Error log:

12-26 15:46:58.164 12196-12196/? I/Zygote: seccomp disabled by setenforce 0
12-26 15:46:58.171 12196-12196/? I/flitecameradem: Late-enabling -Xcheck:jni
12-26 15:46:58.580 12196-12196/android.example.com.tflitecamerademo W/flitecameradem: JIT profile information will not be recorded: profile file does not exits.
12-26 15:46:58.585 12196-12196/android.example.com.tflitecamerademo I/chatty: uid=10125(android.example.com.tflitecamerademo) identical 9 lines
12-26 15:46:58.586 12196-12196/android.example.com.tflitecamerademo W/flitecameradem: JIT profile information will not be recorded: profile file does not exits.
12-26 15:46:58.634 12196-12196/android.example.com.tflitecamerademo I/InstantRun: starting instant run server: is main process
12-26 15:46:58.808 12196-12196/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Created a Tensorflow Lite Image Classifier.
12-26 15:46:58.827 12196-12196/android.example.com.tflitecamerademo D/OpenGLRenderer: Skia GL Pipeline
12-26 15:46:58.842 12196-12205/android.example.com.tflitecamerademo W/System: A resource failed to call close. 
12-26 15:46:58.843 12196-12205/android.example.com.tflitecamerademo W/System: A resource failed to call close. 
12-26 15:46:58.895 12196-12219/android.example.com.tflitecamerademo I/Adreno: QUALCOMM build                   : a5b4970, If5818605d9
    Build Date                       : 10/12/16
    OpenGL ES Shader Compiler Version: XE031.09.00.04
    Local Branch                     : N24D
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
12-26 15:46:58.902 12196-12219/android.example.com.tflitecamerademo I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
    android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
12-26 15:46:58.903 12196-12219/android.example.com.tflitecamerademo I/OpenGLRenderer: Initialized EGL, version 1.4
12-26 15:46:58.903 12196-12219/android.example.com.tflitecamerademo D/OpenGLRenderer: Swap behavior 1
12-26 15:46:58.960 12196-12196/android.example.com.tflitecamerademo I/CameraManagerGlobal: Connecting to camera service
12-26 15:46:59.127 12196-12219/android.example.com.tflitecamerademo D/vndksupport: Loading /vendor/lib/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
12-26 15:46:59.128 12196-12219/android.example.com.tflitecamerademo D/vndksupport: Loading /vendor/lib/hw/gralloc.msm8084.so from current namespace instead of sphal namespace.
12-26 15:46:59.268 12196-12218/android.example.com.tflitecamerademo D/TfLiteCameraDemo: Timecost to put values into ByteBuffer: 102
12-26 15:46:59.282 12196-12218/android.example.com.tflitecamerademo E/AndroidRuntime: FATAL EXCEPTION: CameraBackground
    Process: android.example.com.tflitecamerademo, PID: 12196
    java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 1080000 bytes and a ByteBuffer with 602112 bytes.
        at org.tensorflow.lite.Tensor.throwExceptionIfTypeIsIncompatible(Tensor.java:221)
        at org.tensorflow.lite.Tensor.setTo(Tensor.java:93)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:136)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:216)
        at org.tensorflow.lite.Interpreter.run(Interpreter.java:195)
        at com.example.android.tflitecamerademo.ImageClassifier.classifyFrame(ImageClassifier.java:117)
        at com.example.android.tflitecamerademo.Camera2BasicFragment.classifyFrame(Camera2BasicFragment.java:663)
        at com.example.android.tflitecamerademo.Camera2BasicFragment.access$900(Camera2BasicFragment.java:69)
        at com.example.android.tflitecamerademo.Camera2BasicFragment$5.run(Camera2BasicFragment.java:558)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
12-26 15:46:59.303 12196-12218/android.example.com.tflitecamerademo I/Process: Sending signal. PID: 12196 SIG: 9

Unable to run the model in android.

AkshaykumarA commented 5 years ago

Maybe your model is a float model.

Try to set the QUANT as FALSE and give a try & Instead of using byte[][] use the Float[][] in your Classifier class. I had a similar issue and its fixed now. i.e; private static final boolean QUANT = false; & float[][] result = new float[1][labelList.size()]; interpreter.run(byteBuffer, result); return getSortedResultFloat(result);

The input type is INT32 so its multiplied with 4. byteBuffer = ByteBuffer.allocateDirect(4 BATCH_SIZE inputSize inputSize PIXEL_SIZE);