November 8th, 2019

Importing, Inspecting, and Scoring With MOJO Models Inside H2O

RSS icon RSS Category: H2O, Technical

Machine-learning models created with H2O may be exported in two basic ways:

  1. Binary format,
  2. Model Object, Optimized (MOJO).

An H2O model can be saved in a binary format, which is tied to the very specific version of H2O it has been created with. There are multiple reasons for such a restriction. One of the important reasons is that model-building algorithms may evolve in time. The algorithm’s hyperparameters, as well as the “behavior” of the algorithm itself, may change. To obtain more information about H2O models, please visit the official documentation.

The second option is a MOJO. Unlike binary models, MOJOs are meant to productionize H2O models. Those are self-contained models, deployable into a production environment. Typically, once a model is well-performing, a MOJO is exported and given to engineers to be deployed into production, bridging the gap between engineering and data science. An in-depth description of H2O MOJOs is provided in Productionizing H2O documentation.

Since H2O release 3.26.0.8, it is possible to re-import MOJO models back into H2O and:

  1. Inspect hyperparameters used to train the model
  2. See the scoring history
  3. Predict
  4. Display variable importances
  5. Use it exactly as native H2O model, except for checkpointing

With the new MOJO import functionality, all the information about the model is available for the H2O user to inspect. Also, there is no need to use the GenModel for scoring a dataset if only the MOJO model is available — by importing it back into H2O, doing predictions with such imported model are made available. And in case the MOJO gets lost and the H2O cluster has it still loaded, it can be re-exported again.

This functionality is available via all H2O interfaces: Flow, Python & R.

Note: Besides MOJO, a similar functionality named POJO used to exist in H2O as well. POJOs are now deprecated and the functionality described in this article does not apply to POJOs.

Flow

There are two ways to import a MOJO using Flow:

  1. Use MOJO import functionality directly
  2. Pre-upload the MOJO to the H2O cluster and then use MOJO import functionality

To access MOJO import, in the upmost menu of Flow, select the “Model” option and in the bottom part of the menu, then click on “Import MOJO Model”. A dialogue appears, asking for:

  1. Model ID
  2. MOJO file key
  3. Path to the MOJO

Model ID is already pre-generated by H2O and editing it is optional. MOJO file key is an optional parameter, usable when a MOJO was pre-uploaded from H2O user̈́’s local filesystem to the cluster and then imported. If the MOJO zip file saved out of reach of the H2O cluster, clicking on the “Data” option in Flow’s upmost menu and then using the “Upload file” dialogue to upload the MOJO first makes it possible for a MOJO to be imported.

By default, only the last input box named path is filled by the user. It represents path on the H2O cluster’s filesystem to the import MOJO zip file.

By clicking on the import button, the MOJO model is actually imported and registered inside H2O. From now on, it can be used like a normal H2O model, with a few restrictions listed above. By clicking on the View button, import MOJO model’s details can be displayed.

Notice the predict button is active — users are able to make predictions with imported MOJO models.

Python

As in Flow and R, there are two ways to import a MOJO using Flow:

  1. Use MOJO import functionality directly
  2. Pre-upload the MOJO to the H2O cluster, then use MOJO import functionality

The pre-upload functionality is useful when the MOJO model can not be imported directly, being out of reach of H2O cluster’s filesystem, e.g. residing on the user’s local filesystem. Simply uploading the MOJO using h2o.upload_file('/some/path/to/mojo.zip') and then using the import functionality solves this problem. However, uploading the file manually and then calling the H2OGenericEstimator’s constructor introduces a lot of overhead. Therefore, we’ve introduced h2o.upload_mojo('/path/to/some/mojo.zip') convenience function. However, for the simplest of use cases, there is a function named h2o.import_mojo('/some/path/to/mojo.zip'). This function takes a path accessible by the H2O cluster, imports the MOJO and creates an H2OGenericEstimator. Such H2OGenericEstimator can hold any model, including all kinds of imported MOJO models, hence the name Generic.

Such a model can then be used to do predictions, just like any H2O model with mojo_model.predict(airlines). In fact, all the parameters, the scoring history, it’s all there! The listing below shows a basic use-case where a GBM model is created, saved into a temporary MOJO zip file and then loaded back into H2O. Once the model is imported, making predictions with the imported MOJO model is demonstrated using h2o.predict function.

    airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
    ## Create a GBM model, only to later export it as a MOJO
    from h2o.estimators import H2OGradientBoostingEstimator
    original_model = H2OGradientBoostingEstimator(ntrees = 1)
    original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
    #Save the previously created model into a temporary file
    import tempfile
    original_model_filename = tempfile.mkdtemp()
    original_model_filename = original_model.download_mojo(original_model_filename)
    # Load the model from the temporary file
    mojo_model = h2o.import_mojo(original_model_filename)
    predictions = mojo_model.predict(airlines_data)

As an alternative to the h2o.import_mojo('/some/path/to/mojo.zip') function, creating a generic model directly is possible as well with the H2OGenericEstimator.from_file('/some/path/to/mojo.zip') function. The result is exactly the same as with the h2o.import_mojo function. See the runnable example below for comparison.

airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
    ## Create a GBM model, only to later export it as a MOJO
    from h2o.estimators import H2OGradientBoostingEstimator
    original_model = H2OGradientBoostingEstimator(ntrees = 1)
    original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
    #Save the previously created model into a temporary file
    import tempfile
    original_model_filename = tempfile.mkdtemp()
    original_model_filename = original_model.download_mojo(original_model_filename)
    # Load the model from the temporary file using H2OGenericEstimator
    from h2o.estimators import H2OGenericEstimator
    mojo_model = H2OGenericEstimator.from_file(original_model_filename)
    predictions = mojo_model.predict(airlines_data)

Upload a MOJO in Python

If the MOJO zip file is not reachable by the H2O cluster, it would need to be uploaded first with h2o.upload_file('path/to/some/mojo.zip') and then, the key to the uploaded file would be required to be supplied to the H2OGenericEstimator’s constructor. However, for uploading a MOJO not reachable directly by the H2O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip'). Internally, the MOJO zip file is uploaded into H2O and represented as a Frame of bytes. Afterwards, the key of such byte Frame is supplied to the H2OGenericEstimator, creating a Generic model by using the provided frame, instead of trying to import a file from cluster’s filesystem.

A fully reproducible example is to be found in the following example.

 airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
    ## Create a GBM model, only to later export it as a MOJO
    from h2o.estimators import H2OGradientBoostingEstimator
    original_model = H2OGradientBoostingEstimator(ntrees = 1)
    original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
    #Save the previously created model into a temporary file
    import tempfile
    original_model_filename = tempfile.mkdtemp()
    original_model_filename = original_model.download_mojo(original_model_filename)
    # Upload a MOJO model and create a Generic model out of it
    mojo_model = h2o.upload_mojo(original_model_filename)
    predictions = mojo_model.predict(airlines_data)

R

As in Flow and Python, there are two ways to import a MOJO using Flow:

  1. Use MOJO import functionality directly,
  2. Pre-upload the MOJO to the H2O cluster, then use MOJO import functionality.

The pre-upload functionality is useful when the MOJO model can not be imported directly, being out of reach of H2O cluster’s filesystem, e.g. residing on user’s local filesystem. Simply uploading the MOJO using h2o.upload_file('/some/path/to/mojo.zip') and then using the h2o.generic(model_key = 'some_model_key') functionality solves this problem but is a lot of work to do. Therefore, there is a convenience function named h2o.upload_mojo('/path/to/some/mojo.zip), which does everything in a single call. MOJO upload in R has its dedicated section below, named “Upload a MOJO in R”. However, for the simplest of use cases, there is a function named h2o.import_mojo('/some/path/to/mojo.zip'). This function takes a path accessible by the H2O cluster, imports the MOJO and creates an H2OGenericEstimator. Such H2OGenericEstimator can hold any model, including all kinds of imported MOJO models, hence the name Generic.

Such a model can then be used to do predictions, just like any H2O model with mojo_model.predict(airlines). In fact, all the parameters, the scoring history, it’s all there! The listing below shows a basic use-case where a GBM model is created, saved into a temporary MOJO zip file and then loaded back into H2O. Once the model is imported, making predictions with the imported MOJO model is demonstrated using h2o.predict function.

airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.import_mojo(original_mojo_path)
predictions  <- h2o.predict(mojo_model, airlines_data)

As an alternative to the h2o.import_mojo('/some/path/to/mojo.zip') function, creating a generic model is also possible by calling h2o.genericModel('/some/path/to/mojo.zip') function. The result is exactly the same as with the h2o.import_mojo function. See the runnable example below for comparison.

airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.genericModel(original_mojo_path)
predictions  <- h2o.predict(mojo_model, airlines_data)

Upload a MOJO in R

If the MOJO zip file is not reachable by the H2O cluster, it would need to be uploaded first with h2o.upload_file('path/to/some/mojo.zip') and then, the key to the uploaded file would be required to be supplied to the h2o.generic function. However, for uploading a MOJO not reachable directly by the H2O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip'). Internally, the MOJO zip file is uploaded into H2O and represented as a Frame of bytes. Afterward, the key of such byte Frame is supplied to the h2o.generic(model_key = 'some_h2o_key'), creating a Generic model by using the provided frame, instead of trying to import a file from cluster’s filesystem.

A fully reproducible example is to be found in the following example.

airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.upload_mojo(original_mojo_path)
predictions  <- h2o.predict(mojo_model, airlines_data)

Documentation and Final Thoughts

The H2O MOJO import functionality evolves over time. To explore all of the functionality and possible limitations, please visit H2O MOJO Import official documentation.

Remember, H2O.ai is open-source and can be found on GitHub. Found a bug? Head to H2JIRA and file an issue. Have questions? H2O offers community Gitter and Slack.

About the Author

Pavel
Pavel Pscheidl

Pavel is a machine learning engineer at H2O. Holding a master's degree in Applied Informatics, his main focus during his studies was applied statistics & stochastic methods, agent-based simulations and optimization. He joined a research team as a Ph.D. candidate while working on various problems like the effectiveness of fraud detection methods in highly-distributed systems. Due to his roots in computer science, his commercial focus was on enterprise Java systems and related standards. He also wrote a book in this field. In 2017, Pavel joined H2O's awesome team, abandoning all other activities, including research at the university. At H2O, he is proud of being able to leverage his passion for algorithms and optimization while diving deeper into statistics every single day.

Leave a Reply

Scalable AutoML in H2O

Note: I’m grateful to Dr. Erin LeDell for the suggestions, corrections with the writeup. All of

November 27, 2019 - by Sanyam Bhutani
Meet Yauhen Babakhin: The first and the only Kaggle Grandmaster from Belarus

There is more to competitive Data Science than simply applying algorithms to get the best

November 22, 2019 - by Parul Pandey
Climbing the AI and ML Maturity Model Curve

AI/ML Maturity Model Curve/Steps AI/ML Maturity models are published and updated periodically by a lot of

November 19, 2019 - by Karthik Guruswamy
How to write a Transformer Recipe for Driverless AI

What is a transformer recipe? A transformer (or feature) recipe is a collection of programmatic steps,

November 18, 2019 - by Ashrith Barthur
Novel Ways To Use Driverless AI

I am biased when I write that Driverless AI is amazing, but what's more amazing

November 14, 2019 - by Thomas Ott
Useful Machine Learning Sessions from the H2O World New York

Conferences not only help us learn new skills but also enable us to build brand

November 13, 2019 - by Parul Pandey

Join the AI Revolution

Subscribe, read the documentation, download or contact us.

Subscribe to the Newsletter

Start Your 21-Day Free Trial Today

Get It Now
Desktop img