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

时间:2018-12-31 03:11:45

标签: java android machine-learning classification weka

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

我访问了一些在线指南here,并访问了此常用的weka-android参考文献herehere。它们遵循与我在代码中所做的相同的结构(具有不同的模型),但是我的代码无法在Android Studio中进行编译。

books:Book[] = [{author:"John Doe",title:"Javascript"},{author:"John Doe",title:"Javascript"}]
selectedBook:Book = Object.assign({},Books[1]);

我希望输出可以简单地构建;我没有任何预测输出,因为我没有指定测试数据集。但是,当我单击“编译”时,这些术语都不会在屏幕上突出显示为红色。

具体来说,在我运行它之后,我立即在构建控制台中得到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; see the compiler error output for details.

但是,据我了解,我已经在资产文件夹中包含了.model文件;它应该可以识别文件路径。

所以这引出了一个问题:我将如何使Android Studio处理我的"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 文件,以及如何将其用于测试数据(直接在电话上)?

1 个答案:

答案 0 :(得分:0)

Use .open("PGBD_DecisionTable.model") instead of .open(PGBD_DecisionTable.model).

You are trying to load a file from a constant String expression, not a static property of a class named PGBD_DecisionTable.