Return to page

BLOG

Importing, Inspecting, and Scoring With MOJO Models Inside H2O

 headshot

By H2O.ai Team | minute read | November 08, 2019

Category: H2O-3, Technical
Blog decorative banner image

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

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

An H2 O model can be saved in a binary format, which is tied to the very specific version of H2 O 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 H2 O models, please visit the official documentation. 

The second option is a MOJO. Unlike binary models, MOJOs are meant to productionize H2 O 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 H2 O MOJOs is provided in Productionizing H2O documentation .

Since H2 O release 3.26.0.8, it is possible to re-import MOJO models back into H2 O 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 H2 O 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 H2 O, doing predictions with such imported model are made available. And in case the MOJO gets lost and the H2 O cluster has it still loaded, it can be re-exported again.

This functionality is available via all H2 O 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 H2 O and editing it is optional. MOJO file key is an optional parameter, usable when a MOJO was pre-uploaded from H2 O user̈́’s local filesystem to the cluster and then imported. If the MOJO zip file saved out of reach of the H2 O 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 H2 O cluster’s filesystem to the import MOJO zip file.

By clicking on the import button, the MOJO model is actually imported and registered inside H2 O. From now on, it can be used like a normal H2 O 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 H2 O 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 H2 O 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 H2 O 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 H2 O. 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 H2 O 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 H2 O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip'). Internally, the MOJO zip file is uploaded into H2 O 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 H2 O 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 H2 O 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 H2 O 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 H2 O. 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 H2 O 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 H2 O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip'). Internally, the MOJO zip file is uploaded into H2 O 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 H2O JIRA and file an issue. Have questions? H2O offers community Gitter and Slack.

 headshot

H2O.ai Team

At H2O.ai, democratizing AI isn’t just an idea. It’s a movement. And that means that it requires action. We started out as a group of like minded individuals in the open source community, collectively driven by the idea that there should be freedom around the creation and use of AI.

Today we have evolved into a global company built by people from a variety of different backgrounds and skill sets, all driven to be part of something greater than ourselves. Our partnerships now extend beyond the open-source community to include business customers, academia, and non-profit organizations.