Paddle-Lite setModelFromBuffer内存加载方法?

ocebsuys  于 2021-12-01  发布在  Java
关注(0)|答案(12)|浏览(585)

部署到手机端,我把setModelFromFile换成setModelFromBuffer方法,一直报错,这是为什么,是so库不支持还是什么原因

jum4pzuy

jum4pzuy1#

请问具体错误是什么,还有您使用的so库是什么版本的?以下是2.7/2.8版本上测试过的参考代码

需要注意read_file函数的实现,确认加载的std::string包含了完整的modle.nb文件的所有内容

paddle::lite_api::MobileConfig mobile_config;
  // Read model file to buffer
  std::string model_buffer = read_file(model_path+".nb");
  // Check the model buffer length to confirm the model size is correct
  std::cout << "model_buffer length is " << model_buffer.length() << std::endl;
  // load model from buffer by set_model_from_buffer API
  mobile_config.set_model_from_buffer(model_buffer);
csbfibhn

csbfibhn2#

使用的so库版本2.6.3 ,MobileConfig config = new MobileConfig();
config.setModelFromFile(realPath + File.separator + "model.nb");
config.setThreads(cpuThreadNum);
我把setModelFromFile换成setModelFromBuffer方法,File路径转成String缓存,一直报错。

niwlg2el

niwlg2el3#

2020-12-31 14:25:13.345 589-631/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
2020-12-31 14:25:13.348 1671-2106/? E/ActivityTrigger: activityStartTrigger: not whiteListedcom.baidu.paddle.lite.demo.human_segmentation/com.baidu.paddle.lite.demo.segmentation.MainActivity/1
2020-12-31 14:25:13.350 1671-2106/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.baidu.paddle.lite.demo.human_segmentation/com.baidu.paddle.lite.demo.segmentation.MainActivity/1
2020-12-31 14:25:13.362 1671-2106/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.baidu.paddle.lite.demo.human_segmentation/com.baidu.paddle.lite.demo.segmentation.MainActivity/1
2020-12-31 14:25:13.364 589-631/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:25:13.364 589-631/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:25:13.364 589-631/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:25:13.364 589-631/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:25:13.365 589-631/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:25:13.365 589-631/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:25:13.382 470-470/? E/SELinux: avc: denied { find } for service=miui.contentcatcher.ContentCatcherService pid=6891 uid=13122 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_app_service:s0 tclass=service_manager permissive=0
2020-12-31 14:25:13.383 470-470/? E/SELinux: avc: denied { find } for service=miui.contentcatcher.ContentCatcherService pid=6891 uid=13122 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_app_service:s0 tclass=service_manager permissive=0
2020-12-31 14:25:13.765 589-631/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/mmc_host/mmc0/clk_scaling/enable with 1
return value 2
2020-12-31 14:25:13.891 1671-1741/? E/ANDR-PERF-JNI: Iop tryGetService failed
2020-12-31 14:25:13.912 6891-6954/? E/libc++abi: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc

--------- beginning of crash

2020-12-31 14:25:13.912 6891-6954/? A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 6954 (Predictor Worke), pid 6891 (an_segmentation)
2020-12-31 14:25:13.987 6973-6973/? A/DEBUG:************************************************
2020-12-31 14:25:13.988 6973-6973/? A/DEBUG: Build fingerprint: 'xiaomi/sakura/sakura:8.1.0/OPM1.171019.019/V9.6.15.0.ODICNFD:user/release-keys'
2020-12-31 14:25:13.988 6973-6973/? A/DEBUG: Revision: '0'
2020-12-31 14:25:13.988 6973-6973/? A/DEBUG: ABI: 'arm64'
2020-12-31 14:25:13.988 6973-6973/? A/DEBUG: pid: 6891, tid: 6954, name: Predictor Worke >>> com.baidu.paddle.lite.demo.human_segmentation <<<
2020-12-31 14:25:13.988 6973-6973/? A/DEBUG: signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: Abort message: 'terminating with uncaught exception of type std::bad_alloc: std::bad_alloc'
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x0 0000000000000000 x1 0000000000001b2a x2 0000000000000006 x3 0000000000000008
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x4 fefeff7ce2f16d43 x5 fefeff7ce2f16d43 x6 fefeff7ce2f16d43 x7 7f7f7f7fffff7f7f
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x8 0000000000000083 x9 db972315aa1eaf8c x10 0000000000000000 x11 0000000000000001
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x12 ffffffffffffffff x13 000000005fed6ec9 x14 0035b4299670a400 x15 0000b1ff385431b4
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x16 0000005d4dc84fa8 x17 0000007e84d6864c x18 000000006f8e0660 x19 0000000000001aeb
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x20 0000000000001b2a x21 0000007de72ff248 x22 00000000ffffff80 x23 00000000ffffffc8
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x24 0000007de72ff310 x25 0000007de72ff1e0 x26 0000007de72ff220 x27 0000000000000001
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: x28 0000000000000004 x29 0000007de72ff150 x30 0000007e84d1deac
2020-12-31 14:25:13.993 6973-6973/? A/DEBUG: sp 0000007de72ff110 pc 0000007e84d1dec8 pstate 0000000060000000
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: backtrace:
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #0 pc 000000000001dec8 /system/lib64/libc.so (abort+104)
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #1 pc 00000000002bd67c /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #2 pc 00000000002bd7dc /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #3 pc 00000000002ba600 /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #4 pc 00000000002b9d24 /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #5 pc 00000000002b9ca4 /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so (__cxa_throw+120)
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #6 pc 00000000002bcf3c /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so (operator new(unsigned long)+84)
2020-12-31 14:25:13.998 6973-6973/? A/DEBUG: #7 pc 000000000023c820 /data/app/com.baidu.paddle.lite.demo.human_segmentation-J6V23WEBvtmga2YZqdWQVw==/lib/arm64/libpaddle_lite_jni.so
2020-12-31 14:25:15.367 953-953/? E//system/bin/tombstoned: Tombstone written to: /data/tombstones/tombstone_06
2020-12-31 14:25:13.383 6891-6928/? E/ContentCatcherManager: failed to get ContentCatcherService.
2020-12-31 14:25:15.421 1671-1805/? E/InputDispatcher: channel '8be5aa1 com.baidu.paddle.lite.demo.human_segmentation/com.baidu.paddle.lite.demo.segmentation.HomeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-12-31 14:25:15.428 1671-1805/? E/InputDispatcher: channel 'ec66e38 com.baidu.paddle.lite.demo.human_segmentation/com.baidu.paddle.lite.demo.segmentation.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-12-31 14:25:15.474 1671-6661/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.miui.home/com.miui.home.launcher.Launcher/3030326
2020-12-31 14:25:15.475 589-631/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
2020-12-31 14:25:15.482 589-631/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:25:15.482 589-631/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:25:15.483 589-631/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:25:15.483 589-631/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:25:15.525 3960-3960/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:25:15.526 3960-3960/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:25:15.884 589-631/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/mmc_host/mmc0/clk_scaling/enable with 1

gab6jxml

gab6jxml4#

V2.7版本
2020-12-31 14:36:24.996 592-627/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
2020-12-31 14:36:25.000 1673-2818/? E/ActivityTrigger: activityStartTrigger: not whiteListedcom.example.human_demo/com.example.humanaar.HumanAARActivity/1
2020-12-31 14:36:25.002 1673-2818/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.human_demo/com.example.humanaar.HumanAARActivity/1
2020-12-31 14:36:25.009 1673-2818/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.example.human_demo/com.example.humanaar.HumanAARActivity/1
2020-12-31 14:36:25.016 592-627/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:36:25.016 592-627/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:36:25.017 592-627/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:36:25.017 592-627/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:36:25.017 592-627/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:36:25.017 592-627/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:36:25.052 3832-3832/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:36:25.053 3832-3832/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:36:25.092 472-472/? E/SELinux: avc: denied { find } for service=miui.contentcatcher.ContentCatcherService pid=6382 uid=13123 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_app_service:s0 tclass=service_manager permissive=0
2020-12-31 14:36:25.093 472-472/? E/SELinux: avc: denied { find } for service=miui.contentcatcher.ContentCatcherService pid=6382 uid=13123 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:system_app_service:s0 tclass=service_manager permissive=0
2020-12-31 14:36:25.418 592-627/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/mmc_host/mmc0/clk_scaling/enable with 1
return value 2
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 ...e-Lite/lite/model_parser/model_parser.cc:1021 LoadModelNaiveFromMemory] The model format cannot be recognized. Please make sure you use the correct interface and model file.
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 ...e-Lite/lite/model_parser/model_parser.cc:1021 LoadModelNaiveFromMemory] The model format cannot be recognized. Please make sure you use the correct interface and model file.
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 ...island/Paddle-Lite/lite/api/light_api.cc:146 BuildRuntimeProgram] Check failed: block_size:
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 ...island/Paddle-Lite/lite/api/light_api.cc:146 BuildRuntimeProgram] Check failed: block_size:
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 /island/Paddle-Lite/lite/core/program.cc RuntimeProgram:173] Check failed: block_size: No block found!
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 /island/Paddle-Lite/lite/core/program.cc RuntimeProgram:173] Check failed: block_size: No block found!
2020-12-31 14:36:25.620 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 /island/Paddle-Lite/lite/core/program.cc RuntimeProgram:174] Check failed: block_idx >= 0 && block_idx < block_size: Invalid block index, expected [0,4294967295] but got 0
2020-12-31 14:36:25.621 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.620 /island/Paddle-Lite/lite/core/program.cc RuntimeProgram:174] Check failed: block_idx >= 0 && block_idx < block_size: Invalid block index, expected [0,4294967295] but got 0
2020-12-31 14:36:25.621 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.621 ...model_parser/flatbuffers/program_desc.cc:31 GetBlockpaddle::lite::fbs::BlockDescView] Check failed: (idx < static_cast<int32_t>(BlocksSize())): 0!<0 idx >= blocks.size()
2020-12-31 14:36:25.621 6382-6426/? A/Paddle-Lite: [F 12/31 14:36:25.621 ...model_parser/flatbuffers/program_desc.cc:31 GetBlockpaddle::lite::fbs::BlockDescView] Check failed: (idx < static_cast<int32_t>(BlocksSize())): 0!<0 idx >= blocks.size()

--------- beginning of crash

2020-12-31 14:36:25.621 6382-6426/? A/libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 6426 (Predictor Worke), pid 6382 (mple.human_demo)
2020-12-31 14:36:25.686 6447-6447/? A/DEBUG:************************************************
2020-12-31 14:36:25.686 6447-6447/? A/DEBUG: Build fingerprint: 'xiaomi/sakura/sakura:8.1.0/OPM1.171019.019/V9.6.15.0.ODICNFD:user/release-keys'
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: Revision: '0'
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: ABI: 'arm'
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: pid: 6382, tid: 6426, name: Predictor Worke >>> com.example.human_demo <<<
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: Cause: null pointer dereference
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: r0 00000000 r1 e9f83ed0 r2 e9f83ed0 r3 00000000
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: r4 e9f83edc r5 0000000c r6 00000000 r7 00000000
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: r8 e9f8e790 r9 d5dfdf18 sl d5dfde88 fp e9f83ec0
2020-12-31 14:36:25.687 6447-6447/? A/DEBUG: ip f4264d5c sp d5dfdc78 lr 00000000 pc d23b6108 cpsr 60010030
2020-12-31 14:36:25.688 6447-6447/? A/DEBUG: backtrace:
2020-12-31 14:36:25.688 6447-6447/? A/DEBUG: #0 pc 000ae108 /data/app/com.example.human_demo-otc88PPOPJd1aHBwIBoQiQ==/lib/arm/libpaddle_lite_jni.so (paddle::lite::RuntimeProgram::RuntimeProgram(std::__ndk1::shared_ptr<paddle::lite::fbs::ProgramDescView const> const&, paddle::lite::Scope*, int)+515)
2020-12-31 14:36:27.328 900-900/? E//system/bin/tombstoned: Tombstone written to: /data/tombstones/tombstone_09
2020-12-31 14:36:25.093 6382-6401/? E/ContentCatcherManager: failed to get ContentCatcherService.
2020-12-31 14:36:27.386 1673-1811/? E/InputDispatcher: channel 'f4ae859 com.example.human_demo/com.example.human_demo.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-12-31 14:36:27.398 1673-2052/? E/ActivityTrigger: activityResumeTrigger: not whiteListedcom.miui.home/com.miui.home.launcher.Launcher/3030326
2020-12-31 14:36:27.408 592-627/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:36:27.408 592-627/? E/ANDR-PERF-UTIL: Failed to read
2020-12-31 14:36:27.409 592-627/? E/ANDR-PERF-OPTSHANDLER: Failed to read /sys/class/kgsl/kgsl-3d0/force_no_nap
2020-12-31 14:36:27.409 592-627/? E/ANDR-PERF-RESOURCEQS: Failed to apply optimization [10, 7]
2020-12-31 14:36:27.450 2820-2820/? E/RecyclerView: No adapter attached; skipping layout
2020-12-31 14:36:27.811 592-627/? E/ANDR-PERF-OPTSHANDLER: perf_lock_rel: updated /sys/class/mmc_host/mmc0/clk_scaling/enable with 1
return value 2
2020-12-31 14:36:27.825 3832-3832/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:36:27.827 3832-3832/? E/FontFamily: Error mapping font file system/fonts/MiLanProVF.ttf
2020-12-31 14:36:27.852 2820-2820/? E/RecyclerView: No adapter attached; skipping layout

idfiyjo8

idfiyjo85#

@zyyuanf999 注意“File路径转成String缓存”,这个不是把路径改成缓存,是要把整个.nb模型文件缓存,例如以下实现

std::string read_file(std::string filename) {
  std::ifstream file(filename);
  return std::string((std::istreambuf_iterator<char>(file)),
                     std::istreambuf_iterator<char>());
}

std::string model_buffer = read_file(model_path+".nb");
mobile_config.set_model_from_buffer(model_buffer);
oxosxuxt

oxosxuxt6#

我用下面的方法把整个.nb模型文件缓存了,path=“路径/模型.nb";
public String FiletoString(String path){
String str = null;
try{
File file = new File(path)
FileInpuStream inpus = new FileInpuStream(file);
byte[] buffer = new byte[(int)file.length()];
inpus.read(buffer);
inous.close();
str = Base64.encodeToString(buffer,Base64.DEFAULT);
} catch(Exception e){
str = null;
}
return str;
}

yruzcnhs

yruzcnhs7#

我是使用Human_segmentation_demo测试的

tct7dpnv

tct7dpnv8#

@zyyuanf999 一般这类问题都是nb模型缓存读取不对造成的,之前用ifstream读取.nb文件在MAC/Linux上读到的文件缓存大小都是1M,但是同样的函数在WIindows上就只能读到10K,明显是文件读取失败了,后来Windows上改成用C的FILE类读取才读到正常的1M的文件缓存。建议在set_model_from_buffer API之前先用类似这样的代码检查一下你的buffer大小,一定是要和你的.nb文件的大小对得上才行。

paddle::lite_api::MobileConfig mobile_config;
  std::string model_buffer = read_file(model_path+".nb");
  // 就这下面这一行,把模型大小打印出来,看看是不是和.nb文件的大小一样,如果不一致就是文件读取的问题
  std::cout << "model_buffer length is " << model_buffer.length() << std::endl;
mwyxok5s

mwyxok5s9#

贴一个我WINDOWS上可以成功读取的代码,供你参考,之前贴的用ifstream方法读取的那个在WINDOWS上就没办法成功,不同平台是会有差异的,还是需要先检查model_buffer的大小是对的

static std::string read_file(std::string filename) {
  FILE *file = fopen(filename.c_str(), "rb");
  if (file == nullptr) {
    std::cout << "Failed to open file: " << filename << std::endl;
    return nullptr;
  }
  fseek(file, 0, SEEK_END);
  int64_t size = ftell(file);
  if (size == 0) {
    std::cout << "File should not be empty: " << size << std::endl;
    return nullptr;
  }
  rewind(file);
  char * data = new char[size];
  size_t bytes_read = fread(data, 1, size, file);
  if (bytes_read != size) {
    std::cout << "Read binary file bytes do not match with fseek: " << bytes_read << std::endl;
    return nullptr;
  }
  fclose(file);
  std::string file_data(data, size);
  return file_data;
}
8yparm6h

8yparm6h10#

@qili93 好的,谢谢,我去试试

dm7nw8vv

dm7nw8vv11#

@qili93 您好,有Java的方法吗,我这是部署在Android端的

v2g6jxz6

v2g6jxz612#

@zyyuanf999 java的方法我没有现成的,建议搜索一下java的文件读取方法

相关问题