Reputation: 3355
I have trained LIBSVM model in WEKA (3.7.3) and now want to use it in my java code. However, I'm getting an exception.
Exception:Attempt to invoke interface method 'double weka.classifiers.Classifier.classifyInstance(weka.core.Instance)' on a null object reference
All other models work fine in this java code. Moreover, the version of WEKA jar is exactly the same as I'm using for training models. I don't have any LIBSVM jar in my app because I'm using the trained model. Do I need to place LIBSVM jar in my app ?
What am I missing here ?
inputStream = getApplicationContext().getAssets().open("svm.model");
classifier = (Classifier) weka.core.SerializationHelper.read(inputStream);
This is the content of the model:
=== Model information ===
Filename: svm.model
Scheme: weka.classifiers.functions.LibSVM -S 0 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.001 -P 0.1 -model "D:\\Program Files (x86)\\Weka-3-7" -seed 1
Relation: Sho_gsw30SVRNULL-weka.filters.unsupervised.attribute.Remove-R5-13,18-26,31-39,44-130
Attributes: 17
F1
F2
F3
F4
F14
F15
F16
F17
F27
F28
F29
F30
F40
F41
F42
F43
class
=== Classifier model ===
LibSVM wrapper, original code by Yasser EL-Manzalawy (= WLSVM)
Update: I tried WEKA SMO (support vector machine classifier) and it works fine in my code, but not the LIBSVM one.
Upvotes: 1
Views: 407
Reputation: 3355
Update: I tried WEKA SMO (support vector machine classifier) and it works fine in my code, but not the LIBSVM one.
Upvotes: 0
Reputation: 4113
If what you've shown as the content of your model is the actual content of the "svm.model" file, then that is the culprit.
Model files in WEKA are serialized Java objects. They can be created by training a classifier, which is equivalent to the model, and serializing that Java WEKA classifier object into a file. The file will probably NOT be human readable.
Here is code for serializing a model based on the WEKA site I linked:
// classifier will be your SVM classifier here
// and instances the training instances
classifier.buildClassifier(instances);
// serialize model
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("svm.model"));
oos.writeObject(classifier);
oos.flush();
oos.close();
That model can then be loaded and used by the code you've posted in your question.
Upvotes: 1