【问题标题】:"How to pass single image to Tensorflow lite"“如何将单个图像传递给 Tensorflow lite”
【发布时间】:2020-05-11 11:23:43
【问题描述】:

我想制作一个应用程序,使用移动相机捕获图像并将此图像传递给 tensorflow lite 以对图像中的内容进行分类。

【问题讨论】:

    标签: tensorflow-lite tensorflow-android


    【解决方案1】:

    您可以使用TensorImage(来自 org.tensorflow.lite.support.image)。它具有 TensorBufferBitmapint []float [] 的构造函数。

    因此,假设您有一个名为myImage 的位图图像,同时在myContext 上下文中运行,那么您可以使用以下代码在myModel.tflite 模型上运行tflite 解释器:

    // create tflite options (currently empty) and load the tf model
    Interpreter.Options tfliteOptions = (new Interpreter.Options());
    tfliteModel = FileUtil.loadMappedFile(myContext, "myModel.tflite");
    
    // create tflite interpreter
    tfliteInterpreter = new Interpreter(tfliteModel, tfliteOptions);
    
    // get model parameters (index tensor is 0 for a single image)
    DataType myImageDataType = tfliteInterpreter.getInputTensor(0).dataType();
    myTensorImage = new TensorImage(myImageDataType);
    
    // load bitmap
    myTensorImage.load(myImage);
    
    // run inference
    tfliteInterpreter.run(myTensorImage.getBuffer(), output);
    

    如果你的图片输入是以字节形式给出的,你可以使用TensorBufferByteBuffer,其余的都一样。

    请注意,我没有指定解释器output

    您也可以使用TensorBuffer作为输出,它可以很容易地为您提供ByteBufferint []float []数组。

    希望这会有所帮助:)

    【讨论】:

      【解决方案2】:

      如果你有经验,你可以按照@somethingorange 提到的方法。

      如果您是初学者并想在移动设备上开发图像分类应用程序,请遵循以下方法。例如,您正在尝试开发一个模型来分类给定图像是Cat 还是Dogs

      1. 收集班级数据(CatsDogs 的图片)
      2. 创建两个文件夹,一个用于猫的图像,另一个用于狗的图像
      3. 使用任何预训练模型通过迁移学习方法开发分类模型
      4. 训练模型并保存模型
      5. 将模型转换为 tflite 格式 (model.tflite)
      6. 使用类名创建label.txt
      7. model.tflitelabel.txt 移动到Android Studio 中的assets 文件夹中。

      TFLite 团队在this tutorial 中提到了上述所有步骤,这是一个很好的起点。

      希望这些步骤对初学者有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-03
        • 1970-01-01
        • 2018-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多