Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android端reload分类模型后预测改变且多个测试图片汇聚在同一类 #10492

Open
XinyeYang opened this issue Apr 9, 2024 · 2 comments

Comments

@XinyeYang
Copy link

  • 版本、预测库信息:
       1)Paddle Lite 版本:2.13-rc
       2)Host 环境:Mac OS 14.4.1
       3)运行设备环境:小米12S Ultra
       4)预测后端信息:安卓端侧部署
  • 预测信息
       1)预测 API:SDK二次开发包、原生so包
       2)预测选项信息:armv8,单线程
       3)预测库来源:官网下载
  • 问题描述:
  • 目前需求是在APP中切换制定模型预测,于是参考官方demo中onResume的写法抽离了加载模型函数。App刚启动首次加载模型、预测结果没有问题。一旦调用dynamicLoadModel切换了其他模型(即便重新加载一遍原模型),预测结果就会改变,并且我自己拿了几十张完全不同的图片测试,预测分数几乎相同(汇聚在错误的类)。

private void dynamicLoadModel(String modelPath, String yamlPath) {
predictButton.setText("正在加载模型");
predictButton.setEnabled(false);
radioGroup.setEnabled(false);
tvOutputResult.setText("");
tvInferenceTime.setText("模型预测耗时: -- 毫秒");

    // 由于切换过程中加载模型UI会卡住,使用线程保证用户体验,创建后台单线程池
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(new Runnable() {
        @Override
        public void run() {
            // 执行耗时操作
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
            int cpuThreadNum = Integer.parseInt(sharedPreferences.getString(getString(R.string.CPU_THREAD_NUM_KEY),
                    getString(R.string.CPU_THREAD_NUM_DEFAULT)));
            String cpuPowerMode = sharedPreferences.getString(getString(R.string.CPU_POWER_MODE_KEY),
                    getString(R.string.CPU_POWER_MODE_DEFAULT));
            try {
                String realModelPath = modelPath;
                if (!modelPath.substring(0, 1).equals("/")) {
                    String modelFileName = Utils.getFileNameFromString(modelPath);
                    realModelPath = MainActivity.this.getCacheDir() + File.separator + modelFileName;
                    Utils.copyFileFromAssets(MainActivity.this, modelPath, realModelPath);
                }
                String realYamlPath = yamlPath;
                if (!yamlPath.substring(0, 1).equals("/")) {
                    String yamlFileName = Utils.getFileNameFromString(yamlPath);
                    realYamlPath = MainActivity.this.getCacheDir() + File.separator + yamlFileName;
                    Utils.copyFileFromAssets(MainActivity.this, yamlPath, realYamlPath);
                }
                configParser.init(realModelPath, realYamlPath, cpuThreadNum, cpuPowerMode);
                visualize.init(configParser.getNumClasses());
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "配置加载失败!", Toast.LENGTH_SHORT).show();
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // reload model if configure has been changed
                    loadModel();
                }
            });
        }
    });
    // 关闭线程池
    executorService.shutdown();
}
@lishicheng1996
Copy link
Contributor

已收到您的issue,等内部同学看一下

@qili93
Copy link
Collaborator

qili93 commented Apr 15, 2024

建议先用CPP Demo跑一下 (参考 https://www.paddlepaddle.org.cn/lite/v2.12/demo_guides/arm_cpu.html#yunxingtuxiangfenleishilichengxu ),脱离 APP 确认下二次加载是否有问题,先排除一下APP集成本身的问题,在SHELL Demo中运行确认下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants