java - 将自定义TensorFlowLite模型添加到就绪的Android应用程序时出现问题

标签 java python android customization tensorflow-lite

我正在尝试使用以下仓库添加我的自定义tflite模型:https://github.com/amitshekhariitbhu/Android-TensorFlow-Lite-Example。我的.tflite模型参数是:

== Input details ==
name: x shape: [  1 256 256   3]
type: <class 'numpy.float32'>
 == Output details ==
name: Identity shape: [ 1 30]
type: <class 'numpy.float32'>


我以.tflite格式和labels.txt添加了模型。

这是我的错误:

java.lang.IllegalArgumentException: Cannot convert between a TensorFlowLite buffer with 786432 bytes and a ByteBuffer with 196608 bytes.
 at org.tensorflow.lite.Tensor.throwIfShapeIsIncompatible(Tensor.java:272)
 at org.tensorflow.lite.Tensor.throwIfDataIsIncompatible(Tensor.java:249)
 at org.tensorflow.lite.Tensor.setTo(Tensor.java:110)
 at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:145)
 at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:275)
 at org.tensorflow.lite.Interpreter.run(Interpreter.java:249)
 at com.amitshekhar.tflite.TensorFlowImageClassifier.recognizeImage(TensorFlowImageClassifier.java:66)
 at com.amitshekhar.tflite.MainActivity$1.onImage(MainActivity.java:70)
 at com.wonderkiln.camerakit.EventDispatcher$1.run(EventDispatcher.java:42)
 at android.os.Handler.handleCallback(Handler.java:873)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:201)
 at android.app.ActivityThread.main(ActivityThread.java:6810)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)


我需要为真正的工作应用程序更改哪些参数?

最佳答案

我在TensorFlowImageClassifier.java中更改了以下字符串:

private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
        ByteBuffer byteBuffer;

        if(quant) {
            byteBuffer = ByteBuffer.allocateDirect(BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE);
        } else {
            byteBuffer = ByteBuffer.allocateDirect(4 * BATCH_SIZE * inputSize * inputSize * PIXEL_SIZE);
        }

        byteBuffer.order(ByteOrder.nativeOrder());
        int[] intValues = new int[inputSize * inputSize];
        bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        int pixel = 0;
        for (int i = 0; i < inputSize; ++i) {
            for (int j = 0; j < inputSize; ++j) {
                final int val = intValues[pixel++];
                if(quant){
                    byteBuffer.put((byte) ((val >> 16) & 0xFF));
                    byteBuffer.put((byte) ((val >> 8) & 0xFF));
                    byteBuffer.put((byte) (val & 0xFF));
                } else {
                    byteBuffer.put((byte) ((((val >> 16) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
                    byteBuffer.put((byte) ((((val >> 8) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
                    byteBuffer.put((byte) ((((val) & 0xFF)-IMAGE_MEAN)/IMAGE_STD));
                }

            }
        }
        return byteBuffer;
    }


@Override
    public List<Recognition> recognizeImage(Bitmap bitmap) {
        ByteBuffer byteBuffer = convertBitmapToByteBuffer(bitmap);
        if(quant){
            byte[][] result = new byte[1][labelList.size()];
            interpreter.run(byteBuffer, result);
            return getSortedResultByte(result);
        } else {
            byte [][] result = new byte[1][labelList.size()];
            interpreter.run(byteBuffer, result);
            return getSortedResultByte(result);


而MainActivity.java中的这个字符串:

private static final boolean QUANT = true;
private static final int INPUT_SIZE = 256;


但是它不能以正确的方式工作...
另外,我认为,其中一些参数必须更改:

private static final int MAX_RESULTS = 3;
    private static final int BATCH_SIZE = 1;
    private static final int PIXEL_SIZE = 3;
    private static final float THRESHOLD = 0.1f;

    private static final int IMAGE_MEAN = 128;
    private static final float IMAGE_STD = 128;


在我的情况下,哪些参数也很重要?

关于java - 将自定义TensorFlowLite模型添加到就绪的Android应用程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187298/

相关文章:

java - 使用 JRE 8 时 UI 抽搐和丢失字母

java - 较短版本的计算

java - 如何将可序列化类发送到具有所有字段的其他 Activity ?

android - MVVMCross Binding 导致 Android 应用程序崩溃

android - 多次执行AsyncTask

java - 用户刷新页面时重用 OAuth 请求 token - GAE 上的 Twitter4j

java - 如何在java中保存文件

python - 当没有 activate.bat 并且我是凡人时,如何在虚拟环境中的任务调度程序中运行 python 脚本

python - 收到 ValueError : Unknown level when running GoogleScraper

python - 如何在具有多对多关系的 Django 中反向序列化