在專欄之前的文章,我們介紹過ArmNN,詳情可參考被低估的ArmNN(一)如何編譯。這里,我們給大家介紹如何使用ArmNN在Android設(shè)備上進(jìn)行部署,部署的任務(wù)以Mobilenet分類器為例。關(guān)于Mobilenet回歸器的訓(xùn)練,大家可以參考如何DIY輕型的Mobilenet回歸器。我們今天的部署平臺仍然是基于RK3399嵌入式平臺,系統(tǒng)為Android-8.1。
作者:張新棟
我們知道ArmNN是一個非常高效的Inference框架,300x300的Mobilenet-SSD在depth_multiplier取1.0時inference最快可達(dá)90ms/幀。今天我們將使用ArmNN框架,用C++在RK-3399-Android-8.1中進(jìn)行Mobilenet回歸任務(wù)的部署。首先我們先進(jìn)行第一步,環(huán)境的配置。
環(huán)境配置
若想使用編譯好的ArmNN進(jìn)行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項(xiàng),由于我們在編譯ArmNN時使用的STL為c++_shared,所以這里需要使用c++_shared,另外Android.mk文件中鏈接的OpenCV庫也需要使用c++_shared的stl進(jìn)行編譯(官網(wǎng)下載的即c++_shared編譯)。
編寫C++業(yè)務(wù)代碼
在配置好依賴項(xiàng)后,我們開始使用ArmNN提供的C++API進(jìn)行業(yè)務(wù)代碼的書寫。首先第一步我們需要加載模型,ArmNN提供了解析題 ITfLiteParserPtr,我們可以使用其進(jìn)行模型的加載。另外加載好的模型我們需要使用一個網(wǎng)絡(luò)結(jié)構(gòu)進(jìn)行存儲,ArmNN提供了INetworkPtr。為了在對應(yīng)的arm嵌入式平臺中高效的執(zhí)行,ArmNN還提供了IOptimizedNetworkPtr來對網(wǎng)絡(luò)的inference進(jìn)行優(yōu)化。更多的細(xì)節(jié)大家可參考如下的業(yè)務(wù)代碼。
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;
非常簡單易懂的業(yè)務(wù)代碼就可以完成ArmNN的一次inference,注意這里我們使用的是FP16來進(jìn)行inference,相比于FP32,F(xiàn)P16具有更高的加速比,且不會損失很多精度。后續(xù)我們會給出如何使用ArmNN來做INT8的inference例子。
最后
本文我們介紹了如何使用ArmNN來進(jìn)行Mobilenet的inference(其實(shí)很容易就可以改成分類任務(wù)),并使用FP16的精度進(jìn)行inference,該網(wǎng)絡(luò)在RK3399中執(zhí)行效率非常高(約10ms)。若你想在其他設(shè)備中使用FP16,首先你要保證設(shè)備中有GPU,且支持OpenCL。歡迎大家留言討論、關(guān)注專欄,謝謝大家!
審核編輯 黃昊宇
-
ARM
+關(guān)注
關(guān)注
134文章
9121瀏覽量
368242 -
C++
+關(guān)注
關(guān)注
22文章
2113瀏覽量
73742
發(fā)布評論請先 登錄
相關(guān)推薦
評論