问题
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.
回答1:
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);
来源:https://stackoverflow.com/questions/37507890/java-io-eofexception-when-reading-weka-trained-model-file