0

I'm trying to load my weka trained model file to generate a prediction. But I get a error of java.io.eofexception when trying to do this. I'm sure this is got to do with my model file being not correctly formed. But I have used weka tool to create the model file and don't understand what's wrong.

Code

public Classifier loadModel() throws Exception {
    this.readConfFile();
    Classifier classifier;
    FileInputStream fis = new FileInputStream(
            prop.getProperty("Output_Model_Dir") + "/best3.model");
    ObjectInputStream ois = new ObjectInputStream(fis);

    Log.write.info("Load Classifier Successfully => "
            + prop.getProperty("Output_Model_Dir") + "/best3.model");
    classifier = (Classifier) weka.core.SerializationHelper.read(ois);
    Log.write.info("1");
    ois.close();
    Log.write.fine("Read Classifier Successfully");
    return classifier;
}

Log

INFO: Load Classifier Successfully => C:/Users/CRY$TAL/workspace/flysafews/system_files/best.model
java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at weka.core.SerializationHelper.read(SerializationHelper.java:285)
    at flysafe.predict.support.FileHandler.loadModel(FileHandler.java:104)
    at flysafe.predict.core.PredictionManager.predict(PredictionManager.java:189)
    at flysafe.webservice.WebServiceHandler.predictQuery(WebServiceHandler.java:62)

PS: I have used a different model file to this. when using that file, code works except for the error of arff structure being different.

4
  • @wero 49kb . The one that works is only 36kb Commented May 29, 2016 at 12:17
  • the problem is that the ObjectInputStream can't even read the stream header which consists of four bytes only. An access problem? What happens if you do fis.read() four times? Does it also return -1 meaning EOF reached? Commented May 29, 2016 at 12:26
  • sorry it's somewhat unclear to me. What seems to be the problem here? Commented May 29, 2016 at 12:31
  • it shows 'first byte 172' Commented May 29, 2016 at 12:46

1 Answer 1

0

You pass a ObjectInputStream to weka.core.SerializationHelper.read which expects only a InputStream and which wraps that ObjectInputStream in another ObjectInputStream which then fails.

Therefore simply pass your FileInputStream fis to SerializationHelper.read:

FileInputStream fis = new FileInputStream(...);            
classifier = (Classifier) weka.core.SerializationHelper.read(fis);
Sign up to request clarification or add additional context in comments.

2 Comments

Then I get this error. java.io.StreamCorruptedException: invalid stream header: ED000573
@CraZyDroiD do you still create your own ObjectInputStream ois = new ObjectInputStream(fis)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.