horaceT horaceT - 1 year ago 445
R Question

R h2o load a saved model from disk in MOJO or POJO format

I'm catching up on

's MOJO and POJO model format. I'm able to save a model in MOJO/POJO with

h2o.download_mojo(model, path = "/media/somewhere/tmp") # ok
h2o.download_pojo(model, path = "/media/somewhere/tmp") # ok

which writes an object with name like
to the directory.

However, it's not clear to me how to read it back into the server in R. I tried,

saved_model2 <- h2o.loadModel("/media/somewhere/tmp/mymodel.java") # not work
saved_model3 <- h2o.loadModel("/media/somewhere/tmp/mymodel.zip") # not work

but got error msg like this,

ERROR: Unexpected HTTP Status code: 400 Bad Request (url = http://localhost:54321/99/Models.bin/)

[1] "java.lang.IllegalArgumentException: Missing magic number 0x1CED at stream start"
Error in .h2o.doSafeREST(h2oRestApiVersion = h2oRestApiVersion, urlSuffix = page, :


Missing magic number 0x1CED at stream start

Answer Source

h2o.loadModel is meant to be used with h2o.saveModel. If you want to compile and run a MOJO you need to do the following:

first let's say you created a MOJO from a GBM:

path = "http://h2o-public-test-data.s3.amazonaws.com/smalldata/prostate/prostate.csv"
h2o_df = h2o.importFile(path)
h2o_df$RACE = as.factor(h2o_df$RACE)
model = h2o.gbm(y="CAPSULE",
        x=c("AGE", "RACE", "PSA", "GLEASON"),

and then downloaded the MOJO and the resulting h2o-genmodel.jar file to a new experiment folder. Note that the h2o-genmodel.jar file is a library that supports scoring and contains the required readers and interpreters. This file is required when MOJO models are deployed to production.

modelfile = model.download_mojo(path="~/experiment/", get_genmodel_jar=True)
print("Model saved to " + modelfile)
Model saved to /Users/user/GBM_model_R_1475248925871_74.zip"

Then you would open a new terminal window and change into the experiment directory where you have have the MOJO files .zip and .jar.

$ cd experiment

Then you would create your main program in the experiment folder by creating a new file called main.java (for example, using "vim main.java"). Include the following contents. Note that this file is referencing the GBM model created above using R.

import java.io.*;
import hex.genmodel.easy.RowData;
import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.prediction.*;
import hex.genmodel.MojoModel;

public class main {
  public static void main(String[] args) throws Exception {
    EasyPredictModelWrapper model = new EasyPredictModelWrapper(MojoModel.load("GBM_model_R_1475248925871_74.zip"));

    RowData row = new RowData();
    row.put("AGE", "68");
    row.put("RACE", "2");
    row.put("DCAPS", "2");
    row.put("VOL", "0");
    row.put("GLEASON", "6");

    BinomialModelPrediction p = model.predictBinomial(row);
    System.out.println("Has penetrated the prostatic capsule (1=yes; 0=no): " + p.label);
    System.out.print("Class probabilities: ");
    for (int i = 0; i < p.classProbabilities.length; i++) {
      if (i > 0) {

Then compile and run in terminal window 2 to get a display of predicted probabilities

$ javac -cp h2o-genmodel.jar -J-Xms2g -J-XX:MaxPermSize=128m main.java
$ java -cp .:h2o-genmodel.jar main  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download