(七)将AI样式迁移TensorFlow Lite模型添加到Android应用程序

目录

介绍

准备Android Studio环境

选择TensorFlow Lite解释器和Android支持库

导入Gradle依赖项和其他设置

在Android上部署模型

导入基本的图像处理和转换方法

创建输出对象并运行模型

开发图像处理器架构

实现量化

结论


介绍

在本系列文章中,我们将展示一个基于循环一致对抗网络(CycleGAN)的移动图像到图像转换系统。我们将构建一个CycleGAN,它可以执行不成对的图像到图像的转换,并向您展示一些有趣但具有学术深度的例子。我们还将讨论如何将这种使用TensorFlowKeras构建的训练有素的网络转换为TensorFlow Lite并用作移动设备上的应用程序。

我们假设您熟悉深度学习的概念,以及Jupyter NotebooksTensorFlow。欢迎您下载项目代码。

上一篇文章中,我们保存并加载了TensorFlow Lite (.tflite)模型。在本文中,我们会将其加载到Android Studio并选择正确的TensorFlow Lite Android支持库来运行推理。

准备Android Studio环境

构建Android应用程序的第一步是下载Android Studio。下载后,我们通过将.tflite模型导入Android Studio文件来启动所谓的机器学习(ML)和模块依赖绑定

要将模型导入Android Studio,请右键单击要使用TFLite模型的模块,或选择File > New > Other > TensorFlow Lite Model

然后,选择TFLite文件的位置。请注意,该工具将代表您使用ML模型绑定配置模块的依赖项,并且所有依赖项都会自动插入到您的Android模块的build.gradle文件中。

之后点击完成

选择TensorFlow Lite解释器和Android支持库

要解释模型输出,我们必须将TensorFlow Lite Android支持库集成到我们的应用程序中。这将提供高级API,帮助转换和预处理原始输入数据以适应模型期望的大小和格式。

这个解释器库支持常见的输入和输出数据格式,包括图像和数组。此外,该库还包含一些预处理功能,例如图像调整大小和裁剪。

导入Gradle依赖项和其他设置

要导入Gradle依赖项,我们首先将.tflite模型文件复制到我们的模型将在其中运行的Android模块的资产目录中。我们指定该文件不应被压缩,并将TensorFlow Lite库添加到模块build.gradle文件中:

dependencies {
    // Other dependencies

    // Import tflite dependencies
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly-SNAPSHOT'
    // The GPU delegate library is optional. If needed, select the second checkbox for importing TensorFlow GPU if you want to use GPU acceleration.
    implementation 'org.tensorflow:tensorflow-lite-gpu:0.0.0-nightly-SNAPSHOT'
    implementation 'org.tensorflow:tensorflow-lite-support:0.0.0-nightly-SNAPSHOT'
}

Android上部署模型

此时我们在文件中存储了一个神经网络tflite_model.tflite。如果我们想使用它,我们需要使用Android Studio创建一个新项目(例如,名为CycleGAN_App ),并将我们的模型文件放在CycleGAN_App/app/src/main/assets文件夹中。由于Android Studio出于性能原因默认压缩资源文件,我们还需要在gradle文件中使用以下行明确告诉它不要压缩神经网络:

aaptOptions {
    noCompress "tflite" 
}

其次,我们需要添加所需的依赖项:使用以下几行更新gradle文件:

implementation 'org.tensorflow:tensorflow-lite:1.13.1'

为了演示移动图像到图像的转换,我们创建了一个简单的Android应用程序,其中包含一个用于上传图像、通过我们的TensorFlow Lite模型运行并显示转换后的图像的按钮。

您可以下载项目代码以查看它的运行情况!尽管对应用程序代码的完整讨论超出了本文的范围,但我们接下来将探讨其中最重要的部分。

导入基本的图像处理和转换方法

在我们将图像输入模型之前,我们必须将其转换为模型可以使用的大小和格式。

为了使用Tensorflow Lite的图像处理和转换方法,我们必须创建一个ImagePreprocessor并添加所需的操作。要将图像转换为TensorFlow Lite解释器所需的张量格式,请创建一个TensorImage用作输入:

import org.tensorflow.lite.support.image.ImageProcessor;
import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.image.ops.ResizeOp;

// Initialization code
// Create an ImageProcessor with all ops required. For more ops,
// refer to the ImageProcessor Architecture section in this README.
ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR))
        .build();

// Create a TensorImage object. This creates the tensor of the corresponding
// tensor type (uint8 in this case) that the TensorFlow Lite interpreter needs.
TensorImage tImage = new TensorImage(DataType.UINT8);

// Analyse code for every frame
// Preprocess the image
tImage.load(bitmap);
tImage = imageProcessor.process(tImage);

创建输出对象并运行模型

要运行我们的模型,我们需要创建用于存储结果的容器对象(TensorBuffer)

import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

// Create a container for the result and specify that this is a quantized model
// Hence, the 'DataType' is defined as UINT8 (8-bit unsigned integer)
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

要加载模型并运行结论:

import org.tensorflow.lite.support.model.Model;

// Initialise the model
try{
    MappedByteBuffer tfliteModel
        = FileUtil.loadMappedFile(activity,
            "mobilenet_v1_1.0_224_quant.tflite");
    Interpreter tflite = new Interpreter(tfliteModel)
} catch (IOException e){
    Log.e("tfliteSupport", "Error reading model", e);
}

// Run inference
if(null != tflite) {
    tflite.run(tImage.getBuffer(), probabilityBuffer.getBuffer());
}

开发图像处理器架构

如上所述,ImageProcessor支持一些基本的图像处理方法;即裁剪、调整大小和旋转:

int width = bitmap.getWidth();
int height = bitmap.getHeight();

int size = height > width ? width : height;

ImageProcessor imageProcessor =
    new ImageProcessor.Builder()
        // Center crop the image to the largest square possible
        .add(new ResizeWithCropOrPadOp(size, size))
        // Resize using Bilinear or Nearest neighbour
        .add(new ResizeOp(224, 224, ResizeOp.ResizeMethod.BILINEAR));
        // Rotate counter-clockwise in 90 degree increments
        .add(new Rot90Op(rotateDegrees / 90))
        .add(new NormalizeOp(127.5, 127.5))
        .add(new QuantizeOp(128.0, 1/128.0))
        .build();

实现量化

在初始化我们的输入和输出对象(例如TensorImageTensorBuffer)期间,我们需要将对象类型指定为DataType.UINT8 DataType.FLOAT32

TensorImage tImage = new TensorImage(DataType.UINT8);
TensorBuffer probabilityBuffer =
    TensorBuffer.createFixedSize(new int[]{1, 1001}, DataType.UINT8);

TensorProcessor 用于量化输入张量或反量化输出张量:

import org.tensorflow.lite.support.common.TensorProcessor;

// Post-processor that dequantizes the result
TensorProcessor probabilityProcessor =
    new TensorProcessor.Builder().add(new DequantizeOp(0, 1/255.0)).build();
TensorBuffer dequantizedBuffer = probabilityProcessor.process(probabilityBuffer);

最终结果:一个Android应用程序,它使用图像到图像的转换来转换斑马中的驴!

结论

我们已经到了系列的结尾。我们希望阅读它可以帮助您实现自己的图像到图像转换应用程序。如果您喜欢本系列中遇到的内容,请始终记住您可以改进它!

https://www.codeproject.com/Articles/5307458/Adding-an-AI-Style-Transfer-TensorFlow-Lite-Model

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值