读取Weka训练的模型文件时出现java

读取Weka训练的模型文件时出现java

本文介绍了读取Weka训练的模型文件时出现java.io.EOFException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试加载经过weka训练的模型文件以生成预测.但是尝试执行此操作时出现java.io.eofexception错误.我确定这与我的模型文件格式不正确有关.但是我已经使用了weka工具来创建模型文件,并且不知道出了什么问题.

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.

代码

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;
}

日志

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:我为此使用了另一个模型文件.使用该文件时,代码可以正常工作,只是arff结构的错误有所不同.

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.

推荐答案

您将ObjectInputStream传递给 weka.core.SerializationHelper.read ,它只需要一个InputStream并将该ObjectInputStream包裹在另一个ObjectInputStream中,然后失败.

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.

因此,只需将您的FileInputStream fis传递给SerializationHelper.read:

Therefore simply pass your FileInputStream fis to SerializationHelper.read:

FileInputStream fis = new FileInputStream(...);
classifier = (Classifier) weka.core.SerializationHelper.read(fis);

这篇关于读取Weka训练的模型文件时出现java.io.EOFException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:38