diff --git a/.gitignore b/.gitignore index 547e94ea11f048c9e007996d4ee716d22a13742e..4c86068b7ee3024416094613d2f8f8d74ce89921 100644 --- a/.gitignore +++ b/.gitignore @@ -75,4 +75,5 @@ cmake-build-release demo/ios/PaddleMobileDemo/PaddleMobileDemo/googlenet_combine/ demo/ios/PaddleMobileDemo/PaddleMobileDemo/*.jpg demo/ios/PaddleMobileDemo/PaddleMobileDemo/PaddleMobile/*.a -*.xcuserstate \ No newline at end of file +*.xcuserstate +/tools/quantification/quantify diff --git a/src/io/loader.cpp b/src/io/loader.cpp index 9ed877d05d51dfbe7139ea2289fdb6480c62f88f..cdcecf02ab8af22dec0e32113052ac26e9c5fcfc 100644 --- a/src/io/loader.cpp +++ b/src/io/loader.cpp @@ -56,7 +56,8 @@ template const framework::Program Loader::Load( const std::string &model_path, const std::string ¶_path, bool optimize, bool quantification) { - auto program = this->LoadProgram(model_path, optimize); + auto program = this->LoadProgram(model_path, optimize, quantification); + program.para_path = para_path; program.combined = true; program.quantification = quantification; diff --git a/src/jni/paddle_mobile_jni.cpp b/src/jni/paddle_mobile_jni.cpp index d4eb9e0f0733814cbe367a1873e241383127340a..66150e24e0ac957773161904948c10cf4637ee42 100644 --- a/src/jni/paddle_mobile_jni.cpp +++ b/src/jni/paddle_mobile_jni.cpp @@ -61,6 +61,15 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_load(JNIEnv *env, optimize); } +JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadQualified( + JNIEnv *env, jclass thiz, jstring modelPath) { + ANDROIDLOGI("loadQualified invoked"); + bool optimize = true; + bool qualified = true; + return getPaddleMobileInstance()->Load(jstring2cppstring(env, modelPath), + optimize, qualified); +} + JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined( JNIEnv *env, jclass thiz, jstring modelPath, jstring paramPath) { ANDROIDLOGI("loadCombined invoked"); @@ -70,6 +79,16 @@ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined( optimize); } +JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombinedQualified( + JNIEnv *env, jclass thiz, jstring modelPath, jstring paramPath) { + ANDROIDLOGI("loadCombinedQualified invoked"); + bool optimize = true; + bool qualified = true; + return getPaddleMobileInstance()->Load(jstring2cppstring(env, modelPath), + jstring2cppstring(env, paramPath), + optimize, qualified); +} + JNIEXPORT jfloatArray JNICALL Java_com_baidu_paddle_PML_predictImage( JNIEnv *env, jclass thiz, jfloatArray buf, jintArray ddims) { ANDROIDLOGI("predictImage invoked"); diff --git a/src/jni/paddle_mobile_jni.h b/src/jni/paddle_mobile_jni.h index a830ab43c8ee0598fbf75e1fef5f3eb7da06c27b..06fabe04c739dfcee06110a3592a88591e3d37b9 100644 --- a/src/jni/paddle_mobile_jni.h +++ b/src/jni/paddle_mobile_jni.h @@ -27,12 +27,24 @@ namespace jni { JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_load(JNIEnv *env, jclass thiz, jstring modelPath); + +/** + * load separated qualified model for android + */ +JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadQualified( + JNIEnv *env, jclass thiz, jstring modelPath); /** * load combined model for android */ JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombined( JNIEnv *env, jclass thiz, jstring modelPath, jstring paramPath); +/** + * load combined qualified model for android + */ +JNIEXPORT jboolean JNICALL Java_com_baidu_paddle_PML_loadCombinedQualified( + JNIEnv *env, jclass thiz, jstring modelPath, jstring paramPath); + /** * object detection for anroid */ diff --git a/tools/quantification/convert.cpp b/tools/quantification/convert.cpp index 0223a3b353c4715ed0c52a1d6b3f0751e372691d..282b22073fc96ddb2ed0d421f113604aadcc4afc 100644 --- a/tools/quantification/convert.cpp +++ b/tools/quantification/convert.cpp @@ -68,60 +68,60 @@ std::shared_ptr loadParams(const std::string &model_path) { } -void LoadWithDump(const paddle_mobile::framework::VarDesc &var_desc, char *dataP, FILE *out_file) { +void LoadWithDump(const paddle_mobile::framework::VarDesc &var_desc, char **dataP, FILE *out_file) { // 1. version - uint32_t version = *reinterpret_cast(dataP); + uint32_t version = *reinterpret_cast(*dataP); // write version fwrite(&version, kSize32, 1, out_file); - dataP += kSize32; + *dataP += kSize32; // 2 Lod information auto *lod_level_ptr = new uint64_t(); - memcpy(lod_level_ptr, dataP, kSize64); + memcpy(lod_level_ptr, *dataP, kSize64); uint64_t lod_level = 0; // write lod Information fwrite(&lod_level, kSize64, 1, out_file); delete lod_level_ptr; - dataP += kSize64; + *dataP += kSize64; for (uint64_t i = 0; i < lod_level; ++i) { - uint64_t size = *reinterpret_cast(dataP); + uint64_t size = *reinterpret_cast(*dataP); // write lod size fwrite(&size, kSize64, 1, out_file); - (dataP) += kSize64; + (*dataP) += kSize64; std::vector tmp(size / sizeof(size_t)); for (unsigned long &k : tmp) { - k = *reinterpret_cast(dataP); - (dataP) += sizeof(size_t); + k = *reinterpret_cast(*dataP); + (*dataP) += sizeof(size_t); } // write lod size vector fwrite(&tmp, sizeof(size_t), tmp.size(), out_file); } // 3. tensor version - uint32_t tensor_version = *reinterpret_cast(dataP); + uint32_t tensor_version = *reinterpret_cast(*dataP); // write tensor version fwrite(&tensor_version, kSize32, 1, out_file); - (dataP) += kSize32; + (*dataP) += kSize32; // 4. tensor desc - int32_t size = *reinterpret_cast(dataP); + int32_t size = *reinterpret_cast(*dataP); // write tensor desc fwrite(&size, sizeof(int32_t), 1, out_file); - (dataP) += sizeof(int32_t); + (*dataP) += sizeof(int32_t); std::unique_ptr buf(new char[size]); for (int m = 0; m < size; ++m) { - buf.get()[m] = (dataP)[m]; + buf.get()[m] = (*dataP)[m]; } fwrite(buf.get(), sizeof(char), static_cast(size), out_file); - (dataP) += (sizeof(char) * size); + (*dataP) += (sizeof(char) * size); const paddle_mobile::framework::TensorDesc &desc = var_desc.Tensor_desc(); int memory_size = 1; @@ -158,9 +158,9 @@ void LoadWithDump(const paddle_mobile::framework::VarDesc &var_desc, char *dataP memory = new char[tensorSize]; for (int n = 0; n < tensorSize; ++n) { - static_cast(memory)[n] = (dataP)[n]; + static_cast(memory)[n] = (*dataP)[n]; } - dataP += tensorSize; + *dataP += tensorSize; // for float 32 float min_value = std::numeric_limits::max(); @@ -194,7 +194,7 @@ quantificate_combined(const std::string &model_path, const std::string ¶m_pa if (var_desc->Name() == "feed" || var_desc->Name() == "fetch") { continue; } - LoadWithDump(*var_desc, data, out_file); + LoadWithDump(*var_desc, &data, out_file); } } } @@ -220,7 +220,7 @@ void quantificate_seperated(const std::string model_dir, const std::string param FILE *out_file = fopen(file_name.c_str(), "wb"); char *origin_data = Get_binary_data(model_dir + "/" + var_desc->Name()); char *data = origin_data; - LoadWithDump(*var_desc, data, out_file); + LoadWithDump(*var_desc, &data, out_file); delete origin_data; fclose(out_file); }