在专栏之前的文章,我们介绍过ArmNN,详情可参考被低估的ArmNN(一)如何编译。这里,我们给大家介绍如何使用ArmNN在Android设备上进行部署,部署的任务以Mobilenet分类器为例。关于Mobilenet回归器的训练,大家可以参考如何DIY轻型的Mobilenet回归器。我们今天的部署平台仍然是基于RK3399嵌入式平台,系统为Android-8.1。
首发:https://zhuanlan.zhihu.com/p/71772730
作者:张新栋
我们知道ArmNN是一个非常高效的Inference框架,300x300的Mobilenet-SSD在depth_multiplier取1.0时inference最快可达90ms/帧。今天我们将使用ArmNN框架,用C++在RK-3399-Android-8.1中进行Mobilenet回归任务的部署。首先我们先进行第一步,环境的配置。
环境配置
若想使用编译好的ArmNN进行inference,首先我们必须要先加载编译好的ArmNN库、头文件及其他依赖文件。这里我们依旧为大家提供了Android.mk及Application.mk,
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := armnn
LOCAL_SRC_FILES := $(LOCAL_PATH)/../libarmnn.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include/armnn
LOCAL_SHARED_LIBRARIES := c++_shared
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := tfliteParser
LOCAL_SRC_FILES := $(LOCAL_PATH)/../libarmnnTfLiteParser.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include/libarmnnTfLiteParser
LOCAL_SHARED_LIBRARIES := c++_shared
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := armnnSerializer
LOCAL_SRC_FILES := $(LOCAL_PATH)/../libarmnnSerializer.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../include/armnn/armnnSerializer
LOCAL_SHARED_LIBRARIES := c++_shared
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
OpenCV_INSTALL_MODULES := on
OPENCV_LIB_TYPE := STATIC
include /Users/xindongzhang/armnn-tflite/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_MODULE := face_detector
LOCAL_C_INCLUDES += $(OPENCV_INCLUDE_DIR)
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../../boost_1_64_0/
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../../third-party/stb/
LOCAL_SRC_FILES := \
face_detector.cpp
LOCAL_LDLIBS := -landroid -llog -ldl -lz
LOCAL_CFLAGS := -O2 -fvisibility=hidden -fomit-frame-pointer -fstrict-aliasing \
-ffunction-sections -fdata-sections -ffast-math -ftree-vectorize \
-fPIC -Ofast -ffast-math -w -std=c++14
LOCAL_CPPFLAGS := -O2 -fvisibility=hidden -fvisibility-inlines-hidden -fomit-frame-pointer \
-fstrict-aliasing -ffunction-sections -fdata-sections -ffast-math -fPIC \
-Ofast -ffast-math -std=c++14
LOCAL_LDFLAGS += -Wl,--gc-sections
LOCAL_CFLAGS += -fopenmp
LOCAL_CPPFLAGS += -fopenmp
LOCAL_LDFLAGS += -fopenmp
LOCAL_ARM_NEON := true
APP_ALLOW_MISSING_DEPS = true
LOCAL_SHARED_LIBRARIES := \
armnn \
tfliteParser \
armnnSerializer \
android.hardware.neuralnetworks@1.0 \
android.hidl.allocator@1.0 \
android.hidl.memory@1.0 \
libc++_shared
include $(BUILD_EXECUTABLE)
如下为Application.mk文件,
ANDROID_TOOLCHAIN=clang
APP_ABI := arm64-v8a
APP_CPPFLAGS := -frtti -fexceptions -std=c++14
APP_PLATFORM := android-27
APP_STL := c++_shared
这里需要注意的是Application.mk的APP_STL项,由于我们在编译ArmNN时使用的STL为c++_shared,所以这里需要使用c++_shared,另外Android.mk文件中链接的OpenCV库也需要使用c++_shared的stl进行编译(官网下载的即c++_shared编译)。
编写C++业务代码
在配置好依赖项后,我们开始使用ArmNN提供的C++API进行业务代码的书写。首先第一步我们需要加载模型,ArmNN提供了解析题 ITfLiteParserPtr,我们可以使用其进行模型的加载。另外加载好的模型我们需要使用一个网络结构进行存储,ArmNN提供了INetworkPtr。为了在对应的arm嵌入式平台中高效的执行,ArmNN还提供了IOptimizedNetworkPtr来对网络的inference进行优化。更多的细节大家可参考如下的业务代码。
armnnTfLiteParser::ITfLiteParserPtr parser = armnnTfLiteParser::ITfLiteParser::Create();
armnn::INetworkPtr pose_reg_network{nullptr, [](armnn::INetwork *){}};
armnn::IOptimizedNetworkPtr pose_reg_optNet{nullptr, [](armnn::IOptimizedNetwork *){}};
armnn::InputTensors pose_reg_in_tensors;
armnn::OutputTensors pose_reg_ou_tensors;
armnn::IRuntimePtr runtime{nullptr, [](armnn::IRuntime *){}};
float yaw[1];
float pose_reg_input[64*64*3];
// loading tflite model
std::string pose_reg_modelPath = "/sdcard/Algo/pose.tflite";
pose_reg_network = parser->CreateNetworkFromBinaryFile(pose_reg_modelPath.c_str());
// binding input and output
armnnTfLiteParser::BindingPointInfo pose_reg_input_bind =
parser->GetNetworkInputBindingInfo(0, "input/ImageInput");
armnnTfLiteParser::BindingPointInfo pose_reg_output_bind =
parser->GetNetworkOutputBindingInfo(0, "yaw/yangle");
// wrapping pose reg input and output
armnn::Tensor pose_reg_input_tensor(pose_reg_input_bind.second, pose_reg_input);
pose_reg_in_tensors.push_back(std::make_pair(pose_reg_input_bind.first, pose_reg_input_tensor));
armnn::Tensor pose_reg_output_tensor(pose_reg_output_bind.second, yaw);
pose_reg_ou_tensors.push_back(std::make_pair(pose_reg_output_bind.first, pose_reg_output_tensor));
// config runtime, fp16 accuracy
armnn::IRuntime::CreationOptions runtimeOptions;
runtime = armnn::IRuntime::Create(runtimeOptions);
armnn::OptimizerOptions OptimizerOptions;
OptimizerOptions.m_ReduceFp32ToFp16 = true;
this->pose_reg_optNet =
armnn::Optimize(*pose_reg_network, {armnn::Compute::GpuAcc},runtime->GetDeviceSpec(), OptimizerOptions);
runtime->LoadNetwork(this->pose_reg_identifier, std::move(this->pose_reg_optNet));
// load image
cv::Mat rgb_image = cv::imread("face.jpg", 1);
cv::resize(rgb_image, rgb_image, cv::Size(pose_reg_input_size, pose_reg_input_size));
rgb_image.convertTo(rgb_image, CV_32FC3);
rgb_image = (rgb_image - 127.5f) * 0.017f;
// preprocess image
int TOTAL = 64 * 64 * 3;
float* data = (float*) rgb_image.data;
for (int i = 0; i < TOTAL; ++i) {
pose_reg_input[i] = data[i];
}
// invoke graph forward inference
armnn::Status ret = runtime->EnqueueWorkload(
this->pose_reg_identifier,
this->pose_reg_in_tensors,
this->pose_reg_ou_tensors
);
float result = yaw[0] * 180 / 3.14;
非常简单易懂的业务代码就可以完成ArmNN的一次inference,注意这里我们使用的是FP16来进行inference,相比于FP32,FP16具有更高的加速比,且不会损失很多精度。后续我们会给出如何使用ArmNN来做INT8的inference例子。
最后
本文我们介绍了如何使用ArmNN来进行Mobilenet的inference(其实很容易就可以改成分类任务),并使用FP16的精度进行inference,该网络在RK3399中执行效率非常高(约10ms)。若你想在其他设备中使用FP16,首先你要保证设备中有GPU,且支持OpenCL。欢迎大家留言讨论、关注专栏,谢谢大家!
推荐阅读
专注嵌入式端的AI算法实现,欢迎关注作者微信公众号和知乎嵌入式AI算法实现专栏。
更多嵌入式AI相关的技术文章请关注极术嵌入式AI专栏