Chaquopy中不能导入64位Python训练的机器学习模型(32bit与64bit不兼容)

  我在Chaquopy中利用joblib来load一个在pycharm中写好的AdaBoost模型:

from joblib import dump, load
filename = join(dirname(__file__), 'gao1_to_gao2_1.pkl')
clf = load(filename)

  报错如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.NCEPU, PID: 18024
    com.chaquo.python.PyException: ValueError: Buffer dtype mismatch, expected 'SIZE_t' but got 'long long'
        at <python>.sklearn.tree._tree.Tree.__cinit__(_tree.pyx:607)
        at <python>.pickle.load_reduce(pickle.py:1587)
        at <python>.pickle.load(pickle.py:1210)
        at <python>.joblib.numpy_pickle._unpickle(numpy_pickle.py:504)
        at <python>.joblib.numpy_pickle.load(numpy_pickle.py:585)
        at <python>.AdaBoost.test(AdaBoost.py:76)
        at <python>.chaquopy_java.call(chaquopy_java.pyx:380)
        at <python>.chaquopy_java.Java_com_chaquo_python_PyObject_callAttrThrowsNative(chaquopy_java.pyx:352)
        at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
        at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
        at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
        at com.example.NCEPU.Student.Predict.AdaBoostActivity.callPythonCode(AdaBoostActivity.java:45)
        at com.example.NCEPU.Student.Predict.AdaBoostActivity.createList(AdaBoostActivity.java:61)
        at com.example.NCEPU.Student.Predict.AdaBoostActivity.lambda$initViews$1$AdaBoostActivity(AdaBoostActivity.java:56)
        at com.example.NCEPU.Student.Predict.-$$Lambda$AdaBoostActivity$dJAvP4dDDrPleKcJgBCESW0ea0M.onClick(Unknown Source:2)
        at android.view.View.performClick(View.java:7192)
        at android.view.View.performClickInternal(View.java:7166)
        at android.view.View.access$3500(View.java:824)
        at android.view.View$PerformClick.run(View.java:27592)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8169)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)

  翻遍*发现是因为32位Python不能导入64位训练的模型,于是在Chaquopy中输出一下:

import platform
print(platform.architecture())

输出为:

('32bit', 'WindowsPE')

  果然,Chaquopy中Python默认是32位。那没办法,只能用32位Python训练模型然后再导入了。

1.安装32位python

  在官网下载32位的python38,然后安装,这个不再叙述。
  安装后在Pycharm上配置:
Chaquopy中不能导入64位Python训练的机器学习模型(32bit与64bit不兼容)
  这个时候在pycharm上打印:

import platform
print(platform.architecture())

  输出为:

('32bit', 'WindowsPE')

  可以看到当前版本已经是32位了

2.安装skleanr和pandas

  直接在pycharm的Terminal中安装:

pip install sklearn -i https://pypi.douban.com/simple
pip install pandas -i https://pypi.douban.com/simple

  安装过程有极大概率会报错,这中间省略了很多细节。。。。因为问题实在太多了,忘记记录了。不过最后是升级了一下pip就OK了。

3.训练模型并保存

这一步是在pycharm中进行的:

clf = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=7, min_samples_leaf=5),
                             n_estimators=200,
                             algorithm='SAMME', learning_rate=0.5)
clf = clf.fit(train_x, train_y)  # 训练模型
dump(clf, 'gao1_to_gao2_1.pkl')

4.Chaquopy中加载模型

  gao1_to_gao2_1.pkl放在python文件夹下:
Chaquopy中不能导入64位Python训练的机器学习模型(32bit与64bit不兼容)

from joblib import dump, load
filename = join(dirname(__file__), 'gao1_to_gao2_1.pkl')
clf = load(filename)
上一篇:Java|获取图片rgb值


下一篇:redis事务报错No ongoing transaction. Did you forget to call multi?