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

Viewed 5093

I'm catching up on h2o'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 mymodel.zip or mymodel.java 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/)

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

ERROR MESSAGE:

Missing magic number 0x1CED at stream start
3 Answers

Newer versions of H2O have the ability to import MOJOs via the python API:

# re-import saved MOJO
imported_model = h2o.import_mojo(path)

new_observations = h2o.import_file(path='new_observations.csv')
predictions = imported_model.predict(new_observations)

Caution: MOJO cannot be re-imported into python in older H2O versions, which lack the h2o.import_mojo() function.

So h2o.save_model() seems to have lost its role - we can use just my_model.save_mojo() (notice it's not a h2o method, but a property of the model object), as these files can be used not just for Java apps deployment, but also in python as well (in fact they still use a python-Java bridge for that internally).

Related