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.

model.cpp 3.4 kB

4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <jni.h>
  17. #include <fstream>
  18. #include "common/ms_log.h"
  19. #include "include/model.h"
  20. #include "common/jni_utils.h"
  21. extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEnv *env, jobject thiz, jobject buffer) {
  22. if (buffer == nullptr) {
  23. MS_LOGE("Buffer from java is nullptr");
  24. return reinterpret_cast<jlong>(nullptr);
  25. }
  26. jlong buffer_len = env->GetDirectBufferCapacity(buffer);
  27. auto *model_buffer = static_cast<char *>(env->GetDirectBufferAddress(buffer));
  28. auto model = mindspore::lite::Model::Import(model_buffer, buffer_len);
  29. if (model == nullptr) {
  30. MS_LOGE("Import model failed");
  31. return reinterpret_cast<jlong>(nullptr);
  32. }
  33. return reinterpret_cast<jlong>(model);
  34. }
  35. extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath(JNIEnv *env, jobject thiz,
  36. jstring model_path) {
  37. auto model_path_char = RealPath(env->GetStringUTFChars(model_path, JNI_FALSE));
  38. if (model_path_char.empty()) {
  39. MS_LOGE("model_path_char is nullptr");
  40. return reinterpret_cast<jlong>(nullptr);
  41. }
  42. std::ifstream ifs(model_path_char);
  43. if (!ifs.good()) {
  44. MS_LOGE("file: %s is not exist", model_path_char.c_str());
  45. return reinterpret_cast<jlong>(nullptr);
  46. }
  47. if (!ifs.is_open()) {
  48. MS_LOGE("file: %s open failed", model_path_char.c_str());
  49. return reinterpret_cast<jlong>(nullptr);
  50. }
  51. ifs.seekg(0, std::ios::end);
  52. auto size = ifs.tellg();
  53. auto buf = new (std::nothrow) char[size];
  54. if (buf == nullptr) {
  55. MS_LOGE("malloc buf failed, file: %s", model_path_char.c_str());
  56. ifs.close();
  57. return reinterpret_cast<jlong>(nullptr);
  58. }
  59. ifs.seekg(0, std::ios::beg);
  60. ifs.read(buf, size);
  61. ifs.close();
  62. auto model = mindspore::lite::Model::Import(buf, size);
  63. delete[] buf;
  64. if (model == nullptr) {
  65. MS_LOGE("Import model failed");
  66. return reinterpret_cast<jlong>(nullptr);
  67. }
  68. return reinterpret_cast<jlong>(model);
  69. }
  70. extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_Model_free(JNIEnv *env, jobject thiz, jlong model_ptr) {
  71. auto *pointer = reinterpret_cast<void *>(model_ptr);
  72. if (pointer == nullptr) {
  73. MS_LOGE("Model pointer from java is nullptr");
  74. return;
  75. }
  76. auto *lite_model_ptr = static_cast<mindspore::lite::Model *>(pointer);
  77. delete (lite_model_ptr);
  78. }
  79. extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_Model_freeBuffer(JNIEnv *env, jobject thiz, jlong model_ptr) {
  80. auto *pointer = reinterpret_cast<void *>(model_ptr);
  81. if (pointer == nullptr) {
  82. MS_LOGE("Model pointer from java is nullptr");
  83. return;
  84. }
  85. auto *lite_model_ptr = static_cast<mindspore::lite::Model *>(pointer);
  86. lite_model_ptr->Free();
  87. }