Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XGBoost4J: Exporting to, then importing a model from stream results in wrong model format exception #7168

Closed
PossessedC0bra opened this issue Aug 11, 2021 · 2 comments

Comments

@PossessedC0bra
Copy link

PossessedC0bra commented Aug 11, 2021

Hi there!

I'm currently having issues with exporting and importing an XGBoost model to/from a Stream. I export the model to a ByteArrayOutputStream with the saveModel method on ml.dmlc.xgboost4j.java.Booster :

public void saveModel(OutputStream out) throws XGBoostError, IOException {
out.write(this.toByteArray());
out.close();
}

At a later point i try to import the previously exported model again from an InputStream using the loadModel method on ml.dmlc.xgboost4j.java.XGBoost:

public static Booster loadModel(InputStream in) throws XGBoostError, IOException {
int size;
byte[] buf = new byte[1<<20];
ByteArrayOutputStream os = new ByteArrayOutputStream();
while ((size = in.read(buf)) != -1) {
os.write(buf, 0, size);
}
in.close();
return Booster.loadModel(buf);
}

Altough the Stream contains the exact same data the import fails with the following exception:

Caused by: ml.dmlc.xgboost4j.java.XGBoostError: [16:24:54] D:\a\xgboost\xgboost\src\learner.cc:785: Check failed: fi->Read(&tparam_.objective): BoostLearner: wrong model format
	at ml.dmlc.xgboost4j.java.XGBoostJNI.checkCall(XGBoostJNI.java:48)
	at ml.dmlc.xgboost4j.java.Booster.loadModel(Booster.java:81)
	at ml.dmlc.xgboost4j.java.XGBoost.loadModel(XGBoost.java:64)
	...

I recently upgraded from version 1.3.1 to 1.4.1. I never encountered this problem with version 1.3.1. I noticed an entry in the changelog mentioning changes to the export/importing of models ([jvm] Add ability to load booster direct from byte array (#6655)). Might i be using the new API wrong?

Library version: XGBoost4J 1.4.1

@PossessedC0bra PossessedC0bra changed the title Exporting to, then importing a model from stream results in wrong model format exception XGBoost4J: Exporting to, then importing a model from stream results in wrong model format exception Aug 11, 2021
@trivialfis
Copy link
Member

I think this might be relevant? #7067

@PossessedC0bra
Copy link
Author

I think this might be relevant? #7067

I can confirm the mentioned pull request solves my issue. Thanks for your quick response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants