paddle_mobile_jni.cpp 6.6 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#ifdef ANDROID

D
dolphin8 已提交
17
#include <cmath>
W
wangliu 已提交
18
#include "paddle_mobile_jni.h"
19 20 21 22
#include "common/log.h"
#include "framework/tensor.h"
#include "io/paddle_mobile.h"

W
wangliu 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
#ifdef __cplusplus
extern "C" {
#endif
namespace paddle_mobile {
namespace jni {
using framework::DDim;
using framework::Program;
using framework::Tensor;
using paddle_mobile::CPU;
using std::string;

extern const char *ANDROID_LOG_TAG =
    "paddle_mobile LOG built on " __DATE__ " " __TIME__;
36
static PaddleMobile<CPU> *shared_paddle_mobile_instance = nullptr;
W
wangliu 已提交
37 38 39 40

// toDo mutex lock
// static std::mutex shared_mutex;

41 42 43
PaddleMobile<CPU> *getPaddleMobileInstance() {
  if (nullptr == shared_paddle_mobile_instance) {
    shared_paddle_mobile_instance = new PaddleMobile<CPU>();
W
wangliu 已提交
44
  }
45
  return shared_paddle_mobile_instance;
W
wangliu 已提交
46 47 48 49 50 51 52 53 54 55 56 57
}

string jstring2cppstring(JNIEnv *env, jstring jstr) {
  const char *cstr = env->GetStringUTFChars(jstr, 0);
  string cppstr(cstr);
  env->ReleaseStringUTFChars(jstr, cstr);
  return cppstr;
}

JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_load(JNIEnv *env,
                                                          jclass thiz,
                                                          jstring modelPath) {
58
  ANDROIDLOGI("load invoked");
W
wangliu 已提交
59
  bool optimize = true;
60 61
  return getPaddleMobileInstance()->Load(jstring2cppstring(env, modelPath),
                                         optimize);
W
wangliu 已提交
62 63
}

64 65
JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined(
    JNIEnv *env, jclass thiz, jstring modelPath, jstring paramPath) {
66
  ANDROIDLOGI("loadCombined invoked");
67 68 69 70 71 72
  bool optimize = true;
  return getPaddleMobileInstance()->Load(jstring2cppstring(env, modelPath),
                                         jstring2cppstring(env, paramPath),
                                         optimize);
}

W
wangliu 已提交
73 74
JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictImage(
    JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims) {
75 76
  ANDROIDLOGI("predictImage invoked");
  jsize ddim_size = env->GetArrayLength(ddims);
W
wangliu 已提交
77 78
  if (ddim_size != 4) {
    ANDROIDLOGE("ddims size not equal to 4");
79 80
  }
  jint *ddim_ptr = env->GetIntArrayElements(ddims, NULL);
W
wangliu 已提交
81 82
  framework::DDim ddim = framework::make_ddim(
      {ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]});
83
  int length = framework::product(ddim);
W
wangliu 已提交
84 85 86 87 88 89 90 91 92
  jfloatArray result = NULL;
  int count = 0;
  float *dataPointer = nullptr;
  if (nullptr != buf) {
    dataPointer = env->GetFloatArrayElements(buf, NULL);
  }
  framework::Tensor input;
  input.Resize(ddim);
  auto input_ptr = input.mutable_data<float>();
93
  for (int i = 0; i < length; i++) {
W
wangliu 已提交
94 95
    input_ptr[i] = dataPointer[i];
  }
96
  auto output = shared_paddle_mobile_instance->Predict(input);
W
wangliu 已提交
97 98 99
  count = output->numel();
  result = env->NewFloatArray(count);
  env->SetFloatArrayRegion(result, 0, count, output->data<float>());
W
wangliu 已提交
100
  env->ReleaseIntArrayElements(ddims, ddim_ptr, 0);
101
  ANDROIDLOGI("predictImage finished");
W
wangliu 已提交
102 103 104
  return result;
}

W
wangliu 已提交
105 106 107 108
inline int yuv_to_rgb(int y, int u, int v, float *r, float *g, float *b) {
  int r1 = (int)(y + 1.370705 * (v - 128));
  int g1 = (int)(y - 0.698001 * (u - 128) - 0.703125 * (v - 128));
  int b1 = (int)(y + 1.732446 * (u - 128));
109

W
wangliu 已提交
110 111 112 113 114 115
  r1 = (int)fminf(255, fmaxf(0, r1));
  g1 = (int)fminf(255, fmaxf(0, g1));
  b1 = (int)fminf(255, fmaxf(0, b1));
  *r = r1;
  *g = g1;
  *b = b1;
116

W
wangliu 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
  return 0;
}
void convert_nv21_to_matrix(uint8_t *nv21, float *matrix, int width, int height,
                            int targetWidth, int targetHeight, float *means) {
  const uint8_t *yData = nv21;
  const uint8_t *vuData = nv21 + width * height;

  const int yRowStride = width;
  const int vuRowStride = width;

  float scale_x = width * 1.0 / targetWidth;
  float scale_y = height * 1.0 / targetHeight;

  for (int j = 0; j < targetHeight; ++j) {
    int y = j * scale_y;
    const uint8_t *pY = yData + y * yRowStride;
    const uint8_t *pVU = vuData + (y >> 1) * vuRowStride;
    for (int i = 0; i < targetWidth; ++i) {
      int x = i * scale_x;
      const int offset = ((x >> 1) << 1);
      float r = 0;
      float g = 0;
      float b = 0;
      yuv_to_rgb(pY[x], pVU[offset + 1], pVU[offset], &r, &g, &b);
      int r_index = j * targetWidth + i;
      int g_index = r_index + targetWidth * targetHeight;
      int b_index = g_index + targetWidth * targetHeight;
      matrix[r_index] = r - means[0];
      matrix[g_index] = g - means[1];
      matrix[b_index] = b - means[2];
147
    }
W
wangliu 已提交
148 149
  }
}
150

W
wangliu 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictYuv(
    JNIEnv *env, jclass thiz, jbyteArray yuv_, jint imgwidth, jint imgHeight,
    jintArray ddims, jfloatArray meanValues) {
  ANDROIDLOGI("predictYuv invoked");
  jsize ddim_size = env->GetArrayLength(ddims);
  if (ddim_size != 4) {
    ANDROIDLOGE("ddims size not equal to 4");
  }
  jint *ddim_ptr = env->GetIntArrayElements(ddims, NULL);
  framework::DDim ddim = framework::make_ddim(
      {ddim_ptr[0], ddim_ptr[1], ddim_ptr[2], ddim_ptr[3]});
  int length = framework::product(ddim);
  float matrix[length];
  jbyte *yuv = env->GetByteArrayElements(yuv_, NULL);
  float *meansPointer = nullptr;
  if (nullptr != meanValues) {
    meansPointer = env->GetFloatArrayElements(meanValues, NULL);
  }
  convert_nv21_to_matrix((uint8_t *)yuv, matrix, imgwidth, imgHeight, ddim[3],
                         ddim[2], meansPointer);
  jfloatArray result = NULL;
  int count = 0;
  framework::Tensor input;
  input.Resize(ddim);
  auto input_ptr = input.mutable_data<float>();
  for (int i = 0; i < length; i++) {
    input_ptr[i] = matrix[i];
  }
  auto output = shared_paddle_mobile_instance->Predict(input);
  count = output->numel();
  result = env->NewFloatArray(count);
  env->SetFloatArrayRegion(result, 0, count, output->data<float>());
  env->ReleaseByteArrayElements(yuv_, yuv, 0);
  env->ReleaseIntArrayElements(ddims, ddim_ptr, 0);
  env->ReleaseFloatArrayElements(meanValues, meansPointer, 0);
  ANDROIDLOGI("predictYuv finished");
  return result;
188 189
}

W
wangliu 已提交
190
JNIEXPORT void JNICALL Java_com_baidu_paddle_PML_clear(JNIEnv *env,
191 192 193
                                                       jclass thiz) {
  getPaddleMobileInstance()->Clear();
}
W
wangliu 已提交
194 195 196 197 198 199 200 201 202

}  // namespace jni
}  // namespace paddle_mobile

#ifdef __cplusplus
}
#endif

#endif