tflite.run()针对不同的输入值返回相同的输出

时间:2020-03-11 07:03:30

标签: android tensorflow tensorflow-lite python-3.7

我正在尝试制作一个用于纪念碑识别的Android应用程序。输入每次运行都会更改,但返回的输出始终相同。

下面是代码段

加载存储在资产目录中的tflite模型

private ByteBuffer loadModelFile(String filename) throws IOException {
        AssetFileDescriptor fileDescriptor = this.getAssets().openFd(filename);
        FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
        FileChannel fileChannel = inputStream.getChannel();
        long startOffset = fileDescriptor.getStartOffset();
        long declaredLength = fileDescriptor.getDeclaredLength();
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
    }

初始化tflite解释器

predict.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                try {
                    tflite = new Interpreter(loadModelFile("converted_model.tflite"));
                    Log.println(7,"tflite", "tflite init");
                    doInference(picFile);

                } catch (Exception e) {
                    System.out.println(e);
                }

            }
        });

运行模型

@RequiresApi(api = Build.VERSION_CODES.O)
    public void doInference(File photo) throws IOException {
        img = findViewById(R.id.imgToDisp);
        Bitmap bitmapImg = BitmapFactory.decodeFile(pathToFile);
        img.setImageBitmap(bitmapImg);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmapImg.compress(Bitmap.CompressFormat.JPEG, 50, stream);
        byte[] arr = stream.toByteArray();

        changedim = new float[1][150][150][3];
        outputval = new float[1][28];

        int m = 0;
        for (int i = 0; i < 1; i++) {
            for (int j = 0; j < 150; j++) {
                for (int k = 0; k < 150; k++) {
                    for (int l = 0; l < 3; l++) {
                        byte a = arr[m++];
                        changedim[i][j][k][l] = Byte.toUnsignedLong(a);
                    }
                }
            }
        }

        tflite.run(changedim, outputval);

        for(int i=0;i<28;i++) {
            Log.println(7,"outputval",i+" "+outputval[0][i]);
        }

        path = findViewById(R.id.path);
        String out = "";

        float[] op = outputval[0];
        int ind = 0;

        float max = op[0];

        while (op[ind] != 1) {
            ind++;
            //Log.println(7,"op", " "+op[ind]+" "+ind);
        }

        for (float f : op) {
            out += Float.toString(f) + ",";
        }

        predict.setText("result: " + labels.get(ind));
        Log.println(7, "label", ind + " " + labels.get(ind));
        //path.setText(""+pathToFile);
    }

模型输入必须是大小为150 * 150的图像,并转换为形状为1 * 150 * 150 * 3的4d float32数组

1 个答案:

答案 0 :(得分:0)

模型的输入是各个像素的颜色值。 可以使用

提取
CustomEvent

更改该设置,您的模型将正常工作!