【Android Studio】将 MobileNet v3(pt格式->量化) 模型部署在APP上

一、Android Studio 创建项目

Gradle 版本

SDK & JDK 版本

二、Android Studio 界面设置

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/photoButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Photo"
        android:textAllCaps="false"
        app:layout_constraintEnd_toStartOf="@+id/detectButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/detectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:text="Detect"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/photoButton"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

<ImageView
    android:id="@+id/imageView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="180dp"
    android:background="#FFFFFF"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="120dp"
    android:textSize="18sp"
    android:background="#80000000"
    android:textColor="@android:color/white"
    app:layout_constraintTop_toTopOf="parent" 
    tools:ignore="MissingConstraints"
    android:gravity="center" />

三、模型转换

device = "cpu"

infer_model = mobilenet_v3_large(num_classes=3).to(device)
infer_model.load_state_dict(torch.load("./weights/019.ckpt", map_location=device))
infer_model.eval()

example = torch.zeros((1, 3, 400, 400), device=device)
traced_script_module = torch.jit.trace(infer_model, example)
optimized_traced_model = optimize_for_mobile(traced_script_module)
optimized_traced_model._save_for_lite_interpreter("./model.pt")

注意:在这里我使用的 torch 的版本为 1.10.0。这里的转换的版本要与Android studio 中依赖的 pytorch_android 和 pytorch_android_torchvision 版本一致。如果不一致就会导致加载失败。

四、Android Studio 关键代码

模型加载

module = Module.load(assetFilePath(modelName));

图片处理

final Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,
        TensorImageUtils.TORCHVISION_NORM_MEAN_RGB, TensorImageUtils.TORCHVISION_NORM_STD_RGB, MemoryFormat.CHANNELS_LAST);

模型推理

final Tensor outputTensor = module.forward(IValue.from(inputTensor)).toTensor();

推理结果

String className = ImageNetClasses.IMAGENET_CLASSES[maxScoreIdx];

五、Android Studio 真机测试

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐