Article ID: 000088667 Content Type: Troubleshooting Last Reviewed: 05/20/2022

How to convert Keras* H5 model to an Intermediate Representation (IR) format?

BUILT IN - ARTICLE INTRO SECOND COMPONENT
Summary

Steps to convert Keras H5 model to an IR format.

Description

Unable to determine steps to convert Keras* H5 model to an IR format.

Resolution

Step 1: Convert Keras H5 model to SavedModel Format

  1. Install dependencies:
    cd <INSTALL_DIR>\deployment_tools\model_optimizer\install_prerequisites
    install_prerequisites_tf2.bat
  2. Load the model using TensorFlow* 2 and serialize it in the SavedModel format.
    import tensorflow as tf
    model = tf.keras.models.load_model('model.h5')
    tf.saved_model.save(model,'model')
  3. Optional: Model with a custom layer CustomLayer from custom_layer.py:
    import tensorflow as tf
    from custom_layer import CustomLayer
    model = tf.keras.models.load_model('model.h5', custom_objects={'CustomLayer': CustomLayer})
    tf.saved_model.save(model,'model')

 

Step 2: Convert SavedModel Format to Intermediate Representation

  1. Change directory to <INSTALL_DIR>/deployment_tools/model_optimizer
  2. Run the mo_tf.py script with a path to the SavedModel directory and a writable output directory:
    python mo_tf.py --saved_model_dir <SAVED_MODEL_DIRECTORY> --output_dir <OUTPUT_MODEL_DIR> --input_shape

Related Products

This article applies to 2 products