【问题标题】:Tensorflow lite and its deployment to android studioTensorflow lite 及其对 android studio 的部署
【发布时间】:2019-10-20 17:26:04
【问题描述】:

我已经将我的 keras 模型转换为 *.tflite 文件,在试图弄清楚如何在 android studio 上使用 tensorflow lite 之后,我在从我的 android 手机(或我的相机)预处理数据时遇到了困难) 然后进行分类(因为它是一个具有 4 个 softmax 输出节点的 cnn 模型,输入图像大小为 (1,256,256,3))。由于 tensorflow 和其他网站没有提到更多关于 tflite.run(input, output) 的输入和输出(它们的类型,...等)的信息,以从可能来自手机图库或相机的图像生成预测,我也是Java应用程序开发的新手,希望你们能帮助我弄清楚并完成应用程序,谢谢。 (对不起我的语法不好)

我已包含 tflite 模型,从图库中打开图像但不知道如何预处理它,因为 Java 对我来说是新手。

【问题讨论】:

    标签: java android tensorflow keras


    【解决方案1】:

    您可以通过 TFLite Android 示例 here 更好地了解如何对图像文件(至少对于 ImageNet 样式的模型)执行预处理。

    他们使用这个函数转换位图:

    private void convertBitmapToByteBuffer(Bitmap bitmap) {
        if (imgData == null) {
          return;
        }
        imgData.rewind();
        bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        // Convert the image to floating point.
        int pixel = 0;
        long startTime = SystemClock.uptimeMillis();
        for (int i = 0; i < getImageSizeX(); ++i) {
          for (int j = 0; j < getImageSizeY(); ++j) {
            final int val = intValues[pixel++];
            addPixelValue(val);
          }
        }
        long endTime = SystemClock.uptimeMillis();
        LOGGER.v("Timecost to put values into ByteBuffer: " + (endTime - startTime));
      }
    

    精确的像素预处理如下:

    protected void addPixelValue(int pixelValue) {
        imgData.putFloat((((pixelValue >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        imgData.putFloat((((pixelValue >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        imgData.putFloat(((pixelValue & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
      }
    

    【讨论】:

    • 感谢您的回复,顺便问一下,Image mean 和 image std 是什么 :(( 我昨天看到这段代码决定不接受它,而是像你一样接受位操作上面并除以 255.f,它似乎运行良好。
    猜你喜欢
    • 1970-01-01
    • 2019-11-12
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 2015-09-10
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多