You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. ## MindSpore Lite 端侧目标检测demo(Android)
  2. 本示例程序演示了如何在端侧利用MindSpore Lite C++ API(Android JNI)以及MindSpore Lite 目标检测模型完成端侧推理,实现对图库或者设备摄像头捕获的内容进行检测,并在App图像预览界面中显示连续目标检测结果。
  3. ### 运行依赖
  4. - Android Studio >= 3.2 (推荐4.0以上版本)
  5. - NDK 21.3
  6. - CMake 3.10
  7. - Android SDK >= 26
  8. ### 构建与运行
  9. 1. 在Android Studio中加载本示例源码,并安装相应的SDK(指定SDK版本后,由Android Studio自动安装)。
  10. ![start_home](images/home.png)
  11. 启动Android Studio后,点击`File->Settings->System Settings->Android SDK`,勾选相应的SDK。如下图所示,勾选后,点击`OK`,Android Studio即可自动安装SDK。
  12. ![start_sdk](images/sdk_management.png)
  13. 使用过程中若出现Android Studio配置问题,可参考第5项解决。
  14. 2. 连接Android设备,运行目标检测示例应用程序。
  15. 通过USB连接Android设备调试,点击`Run 'app'`即可在你的设备上运行本示例项目。
  16. > 编译过程中Android Studio会自动下载MindSpore Lite、模型文件等相关依赖项,编译过程需做耐心等待。
  17. ![run_app](images/run_app.PNG)
  18. Android Studio连接设备调试操作,可参考<https://developer.android.com/studio/run/device?hl=zh-cn>。
  19. 3. 在Android设备上,点击“继续安装”,安装完即可查看到设备摄像头捕获的内容和推理结果。
  20. ![install](images/install.jpg)
  21. 如下图所示,检测出图中内容是鼠标。
  22. ![result](images/object_detection.png)
  23. 4. Android Studio 配置问题解决方案可参考下表:
  24. | | 报错 | 解决方案 |
  25. | ---- | ------------------------------------------------------------ | ------------------------------------------------------------ |
  26. | 1 | Gradle sync failed: NDK not configured. | 在local.properties中指定安装的ndk目录:ndk.dir={ndk的安装目录} |
  27. | 2 | Requested NDK version did not match the version requested by ndk.dir | 可手动下载相应的[NDK版本](https://developer.android.com/ndk/downloads?hl=zh-cn),并在Project Structure - Android NDK location设置中指定SDK的位置(可参考下图完成) |
  28. | 3 | This version of Android Studio cannot open this project, please retry with Android Studio or newer. | 在工具栏-help-Checkout for Updates中更新版本 |
  29. | 4 | SSL peer shut down incorrectly | 重新构建 |
  30. ![project_structure](images/project_structure.png)
  31. ## 示例程序详细说明
  32. 本端侧目标检测Android示例程序分为JAVA层和JNI层,其中,JAVA层主要通过Android Camera 2 API实现摄像头获取图像帧,以及相应的图像处理(针对推理结果画框)等功能;JNI层在[Runtime](https://www.mindspore.cn/tutorial/lite/zh-CN/master/use/runtime.html)中完成模型推理的过程。
  33. > 此处详细说明示例程序的JNI层实现,JAVA层运用Android Camera 2 API实现开启设备摄像头以及图像帧处理等功能,需读者具备一定的Android开发基础知识。
  34. ### 示例程序结构
  35. ```text
  36. app
  37. |
  38. ├── libs # 存放demo jni层编译出的库文件
  39. │ └── arm64-v8a
  40. │ │── libmlkit-label-MS.so #
  41. |
  42. ├── src/main
  43. │ ├── assets # 资源文件
  44. | | └── ssd.ms # 存放模型文件
  45. │ |
  46. │ ├── cpp # 模型加载和预测主要逻辑封装类
  47. | | ├── mindspore-lite-x.x.x-mindata-arm64-cpu # minspore源码编译出的调用包,包含demo jni层依赖的库文件及相关的头文件
  48. | | | └── ...
  49. │ | |
  50. | | ├── MindSporeNetnative.cpp # MindSpore调用相关的JNI方法
  51. │ ├── java # java层应用代码
  52. │ │ └── com.huawei.himindsporedemo
  53. │ │ ├── help # 图像处理及MindSpore JNI调用相关实现
  54. │ │ │ └── ...
  55. │ │ └── obejctdetect # 开启摄像头及绘制相关实现
  56. │ │ └── ...
  57. │ │
  58. │ ├── res # 存放Android相关的资源文件
  59. │ └── AndroidManifest.xml # Android配置文件
  60. ├── CMakeList.txt # cmake编译入口文件
  61. ├── build.gradle # 其他Android配置文件
  62. ├── download.gradle # APP构建时由gradle自动从HuaWei Server下载依赖的库文件及模型文件
  63. └── ...
  64. ```
  65. ### 配置MindSpore Lite依赖项
  66. Android JNI层调用MindSpore C++ API时,需要相关库文件支持。可通过MindSpore Lite[源码编译](https://www.mindspore.cn/tutorial/lite/zh-CN/master/use/build.html)生成`mindspore-lite-{version}-minddata-{os}-{device}.tar.gz`库文件包并解压缩(包含`libmindspore-lite.so`库文件和相关头文件),在本例中需使用生成带图像预处理模块的编译命令。
  67. > version:输出件版本号,与所编译的分支代码对应的版本一致。
  68. >
  69. > device:当前分为cpu(内置CPU算子)和gpu(内置CPU和GPU算子)。
  70. >
  71. > os:输出件应部署的操作系统。
  72. 本示例中,build过程由download.gradle文件自动下载MindSpore Lite 版本文件,并放置在`app/src/main/cpp/`目录下。
  73. > 若自动下载失败,请手动下载相关库文件,解压并放在对应位置:
  74. mindspore-lite-1.0.1-runtime-arm64-cpu.tar.gz [下载链接](https://ms-release.obs.cn-north-4.myhuaweicloud.com/1.0.1/lite/android_aarch64/mindspore-lite-1.0.1-runtime-arm64-cpu.tar.gz)
  75. 在app的`build.gradle`文件中配置CMake编译支持,以及`arm64-v8a`的编译支持,如下所示:
  76. ```text
  77. android{
  78. defaultConfig{
  79. externalNativeBuild{
  80. cmake{
  81. arguments "-DANDROID_STL=c++_shared"
  82. }
  83. }
  84. ndk{
  85. abiFilters 'arm64-v8a'
  86. }
  87. }
  88. }
  89. ```
  90. 在`app/CMakeLists.txt`文件中建立`.so`库文件链接,如下所示。
  91. ```text
  92. # Set MindSpore Lite Dependencies.
  93. set(MINDSPORELITE_VERSION mindspore-lite-1.0.1-runtime-arm64-cpu)
  94. include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION})
  95. add_library(mindspore-lite SHARED IMPORTED )
  96. add_library(minddata-lite SHARED IMPORTED )
  97. set_target_properties(mindspore-lite PROPERTIES IMPORTED_LOCATION
  98. ${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/lib/libmindspore-lite.so)
  99. set_target_properties(minddata-lite PROPERTIES IMPORTED_LOCATION
  100. ${CMAKE_SOURCE_DIR}/src/main/cpp/${MINDSPORELITE_VERSION}/lib/libminddata-lite.so)
  101. # Link target library.
  102. target_link_libraries(
  103. ...
  104. mindspore-lite
  105. minddata-lite
  106. ...
  107. )
  108. ```
  109. ### 下载及部署模型文件
  110. 从MindSpore Model Hub中下载模型文件,本示例程序中使用的目标检测模型文件为`ssd.ms`,同样通过`download.gradle`脚本在APP构建时自动下载,并放置在`app/src/main/assets`工程目录下。
  111. > 若下载失败请手动下载模型文件,ssd.ms [下载链接](https://download.mindspore.cn/model_zoo/official/lite/ssd_mobilenetv2_lite/ssd.ms)。
  112. ### 编写端侧推理代码
  113. 在JNI层调用MindSpore Lite C++ API实现端测推理。
  114. 推理代码流程如下,完整代码请参见`src/cpp/MindSporeNetnative.cpp`。
  115. 1. 加载MindSpore Lite模型文件,构建上下文、会话以及用于推理的计算图。
  116. - 加载模型文件:创建并配置用于模型推理的上下文
  117. ```cpp
  118. // Buffer is the model data passed in by the Java layer
  119. jlong bufferLen = env->GetDirectBufferCapacity(buffer);
  120. char *modelBuffer = CreateLocalModelBuffer(env, buffer);
  121. ```
  122. - 创建会话
  123. ```cpp
  124. void **labelEnv = new void *;
  125. MSNetWork *labelNet = new MSNetWork;
  126. *labelEnv = labelNet;
  127. // Create context.
  128. lite::Context *context = new lite::Context;
  129. context->cpu_bind_mode_ = lite::NO_BIND;
  130. context->device_ctx_.type = lite::DT_CPU;
  131. context->thread_num_ = numThread; //Specify the number of threads to run inference
  132. // Create the mindspore session.
  133. labelNet->CreateSessionMS(modelBuffer, bufferLen, "device label", context);
  134. delete context;
  135. ```
  136. - 加载模型文件并构建用于推理的计算图
  137. ```cpp
  138. void MSNetWork::CreateSessionMS(char* modelBuffer, size_t bufferLen, std::string name, mindspore::lite::Context* ctx)
  139. {
  140. CreateSession(modelBuffer, bufferLen, ctx);
  141. session = mindspore::session::LiteSession::CreateSession(ctx);
  142. auto model = mindspore::lite::Model::Import(modelBuffer, bufferLen);
  143. int ret = session->CompileGraph(model); // Compile Graph
  144. }
  145. ```
  146. 2. 将输入图片转换为传入MindSpore模型的Tensor格式。
  147. 将待检测图片数据转换为输入MindSpore模型的Tensor。
  148. ```cpp
  149. // Convert the Bitmap image passed in from the JAVA layer to Mat for OpenCV processing
  150. LiteMat lite_mat_bgr,lite_norm_mat_cut;
  151. if (!BitmapToLiteMat(env, srcBitmap, lite_mat_bgr)){
  152. MS_PRINT("BitmapToLiteMat error");
  153. return NULL;
  154. }
  155. int srcImageWidth = lite_mat_bgr.width_;
  156. int srcImageHeight = lite_mat_bgr.height_;
  157. if(!PreProcessImageData(lite_mat_bgr, lite_norm_mat_cut)){
  158. MS_PRINT("PreProcessImageData error");
  159. return NULL;
  160. }
  161. ImgDims inputDims;
  162. inputDims.channel =lite_norm_mat_cut.channel_;
  163. inputDims.width = lite_norm_mat_cut.width_;
  164. inputDims.height = lite_norm_mat_cut.height_;
  165. // Get the mindsore inference environment which created in loadModel().
  166. void **labelEnv = reinterpret_cast<void **>(netEnv);
  167. if (labelEnv == nullptr) {
  168. MS_PRINT("MindSpore error, labelEnv is a nullptr.");
  169. return NULL;
  170. }
  171. MSNetWork *labelNet = static_cast<MSNetWork *>(*labelEnv);
  172. auto mSession = labelNet->session;
  173. if (mSession == nullptr) {
  174. MS_PRINT("MindSpore error, Session is a nullptr.");
  175. return NULL;
  176. }
  177. MS_PRINT("MindSpore get session.");
  178. auto msInputs = mSession->GetInputs();
  179. auto inTensor = msInputs.front();
  180. float *dataHWC = reinterpret_cast<float *>(lite_norm_mat_cut.data_ptr_);
  181. // copy input Tensor
  182. memcpy(inTensor->MutableData(), dataHWC,
  183. inputDims.channel * inputDims.width * inputDims.height * sizeof(float));
  184. delete[] (dataHWC);
  185. ```
  186. 3. 进行模型推理前,输入tensor格式为 NHWC,shape为1:300:300:3,格式为RGB, 并对输入tensor做标准化处理.
  187. ```cpp
  188. bool PreProcessImageData(LiteMat &lite_mat_bgr,LiteMat &lite_norm_mat_cut) {
  189. bool ret=false;
  190. LiteMat lite_mat_resize;
  191. ret = ResizeBilinear(lite_mat_bgr, lite_mat_resize, 300, 300);
  192. if (!ret) {
  193. MS_PRINT("ResizeBilinear error");
  194. return false;
  195. }
  196. LiteMat lite_mat_convert_float;
  197. ret = ConvertTo(lite_mat_resize, lite_mat_convert_float, 1.0 / 255.0);
  198. if (!ret) {
  199. MS_PRINT("ConvertTo error");
  200. return false;
  201. }
  202. float means[3] = {0.485, 0.456, 0.406};
  203. float vars[3] = {1.0 / 0.229, 1.0 / 0.224, 1.0 / 0.225};
  204. SubStractMeanNormalize(lite_mat_convert_float, lite_norm_mat_cut, means, vars);
  205. return true;
  206. }
  207. ```
  208. 4. 对输入Tensor按照模型进行推理,获取输出Tensor,并进行后处理。
  209. - 图执行,端测推理。
  210. ```cpp
  211. // After the model and image tensor data is loaded, run inference.
  212. auto status = mSession->RunGraph();
  213. ```
  214. - 获取输出数据。
  215. ```cpp
  216. auto names = mSession->GetOutputTensorNames();
  217. typedef std::unordered_map<std::string,
  218. std::vector<mindspore::tensor::MSTensor *>> Msout;
  219. std::unordered_map<std::string,
  220. mindspore::tensor::MSTensor *> msOutputs;
  221. for (const auto &name : names) {
  222. auto temp_dat =mSession->GetOutputByTensorName(name);
  223. msOutputs.insert(std::pair<std::string, mindspore::tensor::MSTensor *> {name, temp_dat});
  224. }
  225. std::string retStr = ProcessRunnetResult(msOutputs, ret);
  226. ```
  227. - 模型有2个输出,输出1是目标的类别置信度,维度为1:1917: 81; 输出2是目标的矩形框坐标偏移量,维度为1:1917:4。 为了得出目标的实际矩形框,需要根据偏移量计算出矩形框的位置。这部分在 getDefaultBoxes中实现。
  228. ```cpp
  229. void SSDModelUtil::getDefaultBoxes() {
  230. float fk[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  231. std::vector<struct WHBox> all_sizes;
  232. struct Product mProductData[19 * 19] = {0};
  233. for (int i = 0; i < 6; i++) {
  234. fk[i] = config.model_input_height / config.steps[i];
  235. }
  236. float scale_rate =
  237. (config.max_scale - config.min_scale) / (sizeof(config.num_default) / sizeof(int) - 1);
  238. float scales[7] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
  239. for (int i = 0; i < sizeof(config.num_default) / sizeof(int); i++) {
  240. scales[i] = config.min_scale + scale_rate * i;
  241. }
  242. for (int idex = 0; idex < sizeof(config.feature_size) / sizeof(int); idex++) {
  243. float sk1 = scales[idex];
  244. float sk2 = scales[idex + 1];
  245. float sk3 = sqrt(sk1 * sk2);
  246. struct WHBox tempWHBox;
  247. all_sizes.clear();
  248. if (idex == 0) {
  249. float w = sk1 * sqrt(2);
  250. float h = sk1 / sqrt(2);
  251. tempWHBox.boxw = 0.1;
  252. tempWHBox.boxh = 0.1;
  253. all_sizes.push_back(tempWHBox);
  254. tempWHBox.boxw = w;
  255. tempWHBox.boxh = h;
  256. all_sizes.push_back(tempWHBox);
  257. tempWHBox.boxw = h;
  258. tempWHBox.boxh = w;
  259. all_sizes.push_back(tempWHBox);
  260. } else {
  261. tempWHBox.boxw = sk1;
  262. tempWHBox.boxh = sk1;
  263. all_sizes.push_back(tempWHBox);
  264. for (int j = 0; j < sizeof(config.aspect_ratios[idex]) / sizeof(int); j++) {
  265. float w = sk1 * sqrt(config.aspect_ratios[idex][j]);
  266. float h = sk1 / sqrt(config.aspect_ratios[idex][j]);
  267. tempWHBox.boxw = w;
  268. tempWHBox.boxh = h;
  269. all_sizes.push_back(tempWHBox);
  270. tempWHBox.boxw = h;
  271. tempWHBox.boxh = w;
  272. all_sizes.push_back(tempWHBox);
  273. }
  274. tempWHBox.boxw = sk3;
  275. tempWHBox.boxh = sk3;
  276. all_sizes.push_back(tempWHBox);
  277. }
  278. for (int i = 0; i < config.feature_size[idex]; i++) {
  279. for (int j = 0; j < config.feature_size[idex]; j++) {
  280. mProductData[i * config.feature_size[idex] + j].x = i;
  281. mProductData[i * config.feature_size[idex] + j].y = j;
  282. }
  283. }
  284. int productLen = config.feature_size[idex] * config.feature_size[idex];
  285. for (int i = 0; i < productLen; i++) {
  286. for (int j = 0; j < all_sizes.size(); j++) {
  287. struct NormalBox tempBox;
  288. float cx = (mProductData[i].y + 0.5) / fk[idex];
  289. float cy = (mProductData[i].x + 0.5) / fk[idex];
  290. tempBox.y = cy;
  291. tempBox.x = cx;
  292. tempBox.h = all_sizes[j].boxh;
  293. tempBox.w = all_sizes[j].boxw;
  294. mDefaultBoxes.push_back(tempBox);
  295. }
  296. }
  297. }
  298. }
  299. ```
  300. - 通过最大值抑制将目标类型置信度较高的输出筛选出来。
  301. ```cpp
  302. void SSDModelUtil::nonMaximumSuppression(const YXBoxes *const decoded_boxes,
  303. const float *const scores,
  304. const std::vector<int> &in_indexes,
  305. std::vector<int> &out_indexes, const float nmsThreshold,
  306. const int count, const int max_results) {
  307. int nR = 0; //number of results
  308. std::vector<bool> del(count, false);
  309. for (size_t i = 0; i < in_indexes.size(); i++) {
  310. if (!del[in_indexes[i]]) {
  311. out_indexes.push_back(in_indexes[i]);
  312. if (++nR == max_results) {
  313. break;
  314. }
  315. for (size_t j = i + 1; j < in_indexes.size(); j++) {
  316. const auto boxi = decoded_boxes[in_indexes[i]], boxj = decoded_boxes[in_indexes[j]];
  317. float a[4] = {boxi.xmin, boxi.ymin, boxi.xmax, boxi.ymax};
  318. float b[4] = {boxj.xmin, boxj.ymin, boxj.xmax, boxj.ymax};
  319. if (IOU(a, b) > nmsThreshold) {
  320. del[in_indexes[j]] = true;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. ```
  327. - 对每类的概率大于阈值,通过NMS算法筛选出矩形框后, 还需要将输出的矩形框恢复到原图尺寸。
  328. ```cpp
  329. std::string SSDModelUtil::getDecodeResult(float *branchScores, float *branchBoxData) {
  330. std::string result = "";
  331. NormalBox tmpBox[1917] = {0};
  332. float mScores[1917][81] = {0};
  333. float outBuff[1917][7] = {0};
  334. float scoreWithOneClass[1917] = {0};
  335. int outBoxNum = 0;
  336. YXBoxes decodedBoxes[1917] = {0};
  337. // Copy branch outputs box data to tmpBox.
  338. for (int i = 0; i < 1917; ++i) {
  339. tmpBox[i].y = branchBoxData[i * 4 + 0];
  340. tmpBox[i].x = branchBoxData[i * 4 + 1];
  341. tmpBox[i].h = branchBoxData[i * 4 + 2];
  342. tmpBox[i].w = branchBoxData[i * 4 + 3];
  343. }
  344. // Copy branch outputs score to mScores.
  345. for (int i = 0; i < 1917; ++i) {
  346. for (int j = 0; j < 81; ++j) {
  347. mScores[i][j] = branchScores[i * 81 + j];
  348. }
  349. }
  350. ssd_boxes_decode(tmpBox, decodedBoxes);
  351. const float nms_threshold = 0.3;
  352. for (int i = 1; i < 81; i++) {
  353. std::vector<int> in_indexes;
  354. for (int j = 0; j < 1917; j++) {
  355. scoreWithOneClass[j] = mScores[j][i];
  356. // if (mScores[j][i] > 0.1) {
  357. if (mScores[j][i] > g_thres_map[i]) {
  358. in_indexes.push_back(j);
  359. }
  360. }
  361. if (in_indexes.size() == 0) {
  362. continue;
  363. }
  364. sort(in_indexes.begin(), in_indexes.end(),
  365. [&](int a, int b) { return scoreWithOneClass[a] > scoreWithOneClass[b]; });
  366. std::vector<int> out_indexes;
  367. nonMaximumSuppression(decodedBoxes, scoreWithOneClass, in_indexes, out_indexes,
  368. nms_threshold);
  369. for (int k = 0; k < out_indexes.size(); k++) {
  370. outBuff[outBoxNum][0] = out_indexes[k]; //image id
  371. outBuff[outBoxNum][1] = i; //labelid
  372. outBuff[outBoxNum][2] = scoreWithOneClass[out_indexes[k]]; //scores
  373. outBuff[outBoxNum][3] =
  374. decodedBoxes[out_indexes[k]].xmin * inputImageWidth / 300;
  375. outBuff[outBoxNum][4] =
  376. decodedBoxes[out_indexes[k]].ymin * inputImageHeight / 300;
  377. outBuff[outBoxNum][5] =
  378. decodedBoxes[out_indexes[k]].xmax * inputImageWidth / 300;
  379. outBuff[outBoxNum][6] =
  380. decodedBoxes[out_indexes[k]].ymax * inputImageHeight / 300;
  381. outBoxNum++;
  382. }
  383. }
  384. MS_PRINT("outBoxNum %d", outBoxNum);
  385. for (int i = 0; i < outBoxNum; ++i) {
  386. std::string tmpid_str = std::to_string(outBuff[i][0]);
  387. result += tmpid_str; // image ID
  388. result += "_";
  389. // tmpid_str = std::to_string(outBuff[i][1]);
  390. MS_PRINT("label_classes i %d, outBuff %d",i, (int) outBuff[i][1]);
  391. tmpid_str = label_classes[(int) outBuff[i][1]];
  392. result += tmpid_str; // label id
  393. result += "_";
  394. tmpid_str = std::to_string(outBuff[i][2]);
  395. result += tmpid_str; // scores
  396. result += "_";
  397. tmpid_str = std::to_string(outBuff[i][3]);
  398. result += tmpid_str; // xmin
  399. result += "_";
  400. tmpid_str = std::to_string(outBuff[i][4]);
  401. result += tmpid_str; // ymin
  402. result += "_";
  403. tmpid_str = std::to_string(outBuff[i][5]);
  404. result += tmpid_str; // xmax
  405. result += "_";
  406. tmpid_str = std::to_string(outBuff[i][6]);
  407. result += tmpid_str; // ymax
  408. result += ";";
  409. }
  410. return result;
  411. }
  412. ```