Search code examples
javaserializationwekalibsvmserialversionuid

how to load libsvm model in java after serialVersionUID has changed


I have trained libsvm model on weka and then I saved the model. Now I want to use this model in java.

Classifier cls = (Classifier)weka.core.SerializationHelper.read(this.modelPath);

I get this error

"java.io.InvalidClassException: libsvm.svm_model; local class incompatible: stream classdesc serialVersionUID = -8642637231196646922, local class serialVersionUID = 2709444323471798245"

when I run above code. I wanna know how i can load and use model.


Solution

  • Chances are you changed the class definition between the time you saved the model and the time you are trying to load it (see e.g. What is a serialVersionUID and why should I use it? for a good explanation). Think of serialVersionUID as a sort of checksum which makes sure you don't load an outdated version of your classes. Unless that's what you want because you know better than Java that you can still use the old models - in that case you can manually set that ID. In order to tell Java that your current classifier is still up-to-date, add

    static final long serialVersionUID = -8642637231196646922;
    

    to the code of the class.

    Now I'm however wondering: Classifier looks like a Weka built-in class. I'm not sure how easily the serialVersionUID can be changed in that case. Did you maybe update the Weka version? If you're really invested in your model file, you might wanna go into the source code of Weka and change the serialVersionUID right there.