gpt4 book ai didi

java - 如何在 Android Studio 中使用预训练的 .model 文件进行预测?

转载 作者:太空狗 更新时间:2023-10-29 14:36:53 24 4
gpt4 key购买 nike

我在 Weka 3.8 Dekstop 版本中训练了一个决策表 ML 模型。我已将模型相应地保存在我的 Assets 文件夹中,并配置了 wekaSTRIPPED.jar 文件。此时,构建似乎在 Android Studio 上运行良好。但是,当我尝试在 Java 中使用分类模型时,我的代码出现了一些红色高亮显示 - 错误 - 我无法自动修复。

我访问了一些在线指南here并访问了这个常用的 weka-android 引用 herehere .它们遵循与我在代码中所做的相同的结构(使用不同的模型),但我的代码无法在 Android Studio 中编译。

package com.example.owner.introductoryapplication;

import android.support.v7.app.AppCompatActivity;

import weka.classifiers.Classifier;
import weka.classifiers.rules.DecisionTable;
import weka.core.Attribute;
import weka.core.DenseInstance;
import weka.core.Instances;

import java.util.ArrayList;

public class Test extends AppCompatActivity {
public static void main(String[] args) throws Exception {
Test test = new Test();
test.start();
}
public void start() throws Exception {
//LOADS THE MODEL...
String rootPath = "/assets/";
String fileName = "PGBD_DecisionTableUPD.model";
//Classifier cls = null;
Classifier cls = (Classifier) weka.core.SerializationHelper.read(rootPath + fileName);

我希望输出能够简单构建;我不期望任何预测输出,因为我没有指定测试数据集。但是,当我单击编译时,屏幕上没有任何术语以红色突出显示。

具体来说,在我运行它之后,我立即得到 Compilation failed;有关详细信息,请参阅编译器错误输出。 在构建控制台中,后跟:

"C:\Program Files\Android\Android Studio\jre\bin\java.exe" -Didea.launcher.port=64163 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Owner\AppData\Local\Android\Sdk\platforms\android-28\android.jar;C:\Users\Owner\AppData\Local\Android\Sdk\platforms\android-28\data\res;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\intermediates\javac\debug\compileDebugJavaWithJavac\classes;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\generated\res\rs\debug;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\build\generated\res\resValues\debug;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\customview-28.0.0.aar\14e09720fc7f657365dec6e786490d47\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\localbroadcastmanager-28.0.0.aar\d791f241c61f09475b5d43561b131f77\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-28.0.0.aar\c8f91860fd6811b7040ad585c1bb749f\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\interpolator-28.0.0.aar\73289dbc6711e8292415efe83a859a43\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-core-utils-28.0.0.aar\2dc7d30dd7abd1c4b0846462e1137cc4\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-core-ui-28.0.0.aar\b5199754cf7a7bf403b3073b69edcfe3\jars\classes.jar;C:\Users\Owner\AndroidStudioProjects\IntroductoryApplication\app\libs\wekaSTRIPPED.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\slidingpanelayout-28.0.0.aar\32aa91f4149120a8a428b24c5291b432\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\viewmodel-1.1.1.aar\3477f32913f9b9934a8185f031c05533\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\drawerlayout-28.0.0.aar\bfb4eb97f205f1db9a43db5a200619a5\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\aba273524bbeff5bfab162bc52fb07b9\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\coordinatorlayout-28.0.0.aar\aba273524bbeff5bfab162bc52fb07b9\res;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support\collections\28.0.0\c1bcdade4d3cc2836130424a3f3e4182c666a745\collections-28.0.0.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\documentfile-28.0.0.aar\e49f57095504cc48cc7ba2b26757daf1\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\96728e03169a340c5e642682fbd18ff8\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\96728e03169a340c5e642682fbd18ff8\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\swiperefreshlayout-28.0.0.aar\a560a21fcde6d062081d9c09634c92a8\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\cursoradapter-28.0.0.aar\733ca7f2a61e46bdb2164e238fe8c4b6\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\asynclayoutinflater-28.0.0.aar\f950d05bc8679714fb0d7642259e29eb\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\livedata-1.1.1.aar\400a5420ad5e4bcef8ebbebf2123e101\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.1.1\e55b70d1f5620db124b3e85a7f4bdc7bd48d9f95\common-1.1.1.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\versionedparcelable-28.0.0.aar\12c3dd068050bf0422d8455749982d52\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\68e9f88e3e623f899ee79dd4aa8966f1\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\print-28.0.0.aar\f12fdac753fb8cbf34b55eced23a29c7\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\loader-28.0.0.aar\0f1fa1caaba4127fdcb1d7bb76f1c144\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\viewpager-28.0.0.aar\bdf7976216feddc6d17d8052bf1807db\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-fragment-28.0.0.aar\df9931f8c3d84779819dccebe1aa6282\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\28.0.0\ed73f5337a002d1fd24339d5fb08c2c9d9ca60d8\support-annotations-28.0.0.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.1.1\207a6efae6a3555e326de41f76bdadd9a239cbce\common-1.1.1.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\livedata-core-1.1.1.aar\f5b8b9e2bbbf6e16caa00d091d538f2f\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\0fefdb1435b453663f8b867197d75a41\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\support-compat-28.0.0.aar\0fefdb1435b453663f8b867197d75a41\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-28.0.0.aar\7eca742b05d3ca93c5f22855deb68690\jars\classes.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\b5690672012030cd411a187af3fc56e5\res;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\appcompat-v7-28.0.0.aar\b5690672012030cd411a187af3fc56e5\jars\classes.jar;C:\Users\Owner\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.3\bde0667d7414c16ed62d3cfe993cff7f9d732373\constraint-layout-solver-1.1.3.jar;C:\Users\Owner\.gradle\caches\transforms-1\files-1.1\runtime-1.1.1.aar\b33f18eb28c72c82424fc1a72a917596\jars\classes.jar;C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 com.example.owner.introductoryapplication.Test
Exception in thread "main" java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:67)
at android.content.ContextWrapper.<init>(ContextWrapper.java:30)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:40)
at android.app.Activity.<init>(Activity.java:643)
at android.support.v4.app.SupportActivity.<init>(ComponentActivity.java:46)
at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:68)
at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:62)
at com.example.owner.introductoryapplication.Test.<init>(Test.java:13)
at com.example.owner.introductoryapplication.Test.main(Test.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

Process finished with exit code 1

不过,据我所知,我已经将 .model 文件包含在我的 Assets 文件夹中;它应该识别文件路径。

因此这引出了问题:我如何让 Android Studio 处理我的 PGBD_DecisionTable.model 文件,以及我如何将它用于测试数据(直接在手机上)?

最佳答案

使用 .open("PGBD_DecisionTable.model") 代替 .open(PGBD_DecisionTable.model)

您正在尝试从常量 String 表达式加载文件,而不是名为 PGBD_DecisionTable 的类的静态属性。

关于java - 如何在 Android Studio 中使用预训练的 .model 文件进行预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53983231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com