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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "common/jni_utils.h"
  20. #include "include/model.h"
  21. extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModel(JNIEnv *env, jobject thiz, jobject buffer) {
  22. MS_LOGD("Start getting buffer from java");
  23. if (buffer == nullptr) {
  24. MS_LOGE("Buffer from java is nullptr");
  25. return reinterpret_cast<jlong>(nullptr);
  26. }
  27. jlong buffer_len = env->GetDirectBufferCapacity(buffer);
  28. auto *model_buffer = static_cast<char *>(env->GetDirectBufferAddress(buffer));
  29. MS_LOGD("Start Loading model");
  30. auto model = mindspore::lite::Model::Import(model_buffer, buffer_len);
  31. // env->DeleteLocalRef(*(jobject *)model_buffer);
  32. if (model == nullptr) {
  33. MS_LOGE("Import model failed");
  34. return reinterpret_cast<jlong>(nullptr);
  35. }
  36. return reinterpret_cast<jlong>(model);
  37. }
  38. extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_Model_loadModelByPath(JNIEnv *env, jobject thiz,
  39. jstring model_path) {
  40. auto model_path_char = JstringToChar(env, model_path);
  41. if (nullptr == model_path_char) {
  42. MS_LOGE("model_path_char is nullptr");
  43. return reinterpret_cast<jlong>(nullptr);
  44. }
  45. std::ifstream ifs(model_path_char);
  46. if (!ifs.good()) {
  47. MS_LOGE("file: %s is not exist", model_path_char);
  48. return reinterpret_cast<jlong>(nullptr);
  49. }
  50. if (!ifs.is_open()) {
  51. MS_LOGE("file: %s open failed", model_path_char);
  52. return reinterpret_cast<jlong>(nullptr);
  53. }
  54. ifs.seekg(0, std::ios::end);
  55. auto size = ifs.tellg();
  56. std::unique_ptr<char[]> buf(new (std::nothrow) char[size]);
  57. if (buf == nullptr) {
  58. MS_LOGE("malloc buf failed, file: %s", model_path_char);
  59. ifs.close();
  60. return reinterpret_cast<jlong>(nullptr);
  61. }
  62. ifs.seekg(0, std::ios::beg);
  63. ifs.read(buf.get(), size);
  64. ifs.close();
  65. delete[](model_path_char);
  66. MS_LOGD("Start Loading model");
  67. auto model = mindspore::lite::Model::Import(buf.get(), size);
  68. if (model == nullptr) {
  69. MS_LOGE("Import model failed");
  70. return reinterpret_cast<jlong>(nullptr);
  71. }
  72. return reinterpret_cast<jlong>(model);
  73. }
  74. extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_Model_free(JNIEnv *env, jobject thiz, jlong model_ptr) {
  75. auto *pointer = reinterpret_cast<void *>(model_ptr);
  76. if (pointer == nullptr) {
  77. MS_LOGE("Model pointer from java is nullptr");
  78. return;
  79. }
  80. auto *lite_model_ptr = static_cast<mindspore::lite::Model *>(pointer);
  81. delete (lite_model_ptr);
  82. }