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

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