Importing an ONNX model into WML for z/OS

WML for z/OS provides a DLC (deep learning compiler) model conversion utility that you can install into your Docker environment. With the utility, you can convert an ONNX model trained on your local distributed platform and save it as a WML for z/OS model. You can then import the model into WML for z/OS for deployment and management.

Before you begin

  • Download and install any release of TensorFlow Version 1 (r1.x).
  • Download and install the keras2onnx 1.6.0 package. See keras2onnx converter package for instructions.
  • Create the TENTDATA table and load the test data as described in Preparing data for a model in Db2 for z/OS.
  • Locate the following information:
    • User name and password for the WML for z/OS web user interface.
    • JDBC connection information, authorization ID, and password for the Db2 subsystem where the TENTDATA sample table is created.
    • IP address of the host system where your WML for z/OS metadata service runs.

Procedure

  1. Download iwmlz_v2.1.0.1_dlc_tool.tgz as instructed in Downloading WML for z/OS IDE installer.

    The compressed file contains the Docker image and scripts for converting your ONNX models for import and deployment in WML for z/OS.

  2. Locate the iwmlz_v2.1.0.1_dlc_tool.tgz file on your local system and run the following tar command to decompress it:
    tar -xzvf iwmlz_v2.1.0.1_dlc_tool.tgz

    You will find the following files in the iml-dlc-build-artifact directory:

    • iml-dlc-build_v2.2.0-27.tar.gz
    • onnx2dlc.sh
    • onnx2dlc.bat
  3. Run the following docker command to install the converter image:
    docker load -i iml-dlc-build_v2.2.0-27.tar.gz
  4. Create a new TensorFlow model on your local system.
    1. Import TensorFlow and keras2onnx packages:
      
      from sklearn.model_selection import train_test_split
      import sklearn.preprocessing
      import pandas as pd
      import tensorflow as tf
      import keras2onnx
      import onnx
      
      %env TF_KERAS=1
    2. Read data from the MLZ.TENTDATA table stored in your Db2 and list the first five rows from the DataFrame as a preview:
      
      from pyspark import SparkContext
      from pyspark.sql import SQLContext
      
      sc = SparkContext.getOrCreate()
      # Initialize SparkSQL Context
      sqlContext = SQLContext(sc)
      
      df = sqlContext.read.format("jdbc").options(
          driver='com.ibm.db2.jcc.DB2Driver',
          url='jdbc:db2://<url>:<port>/<location>',
          user='<userid>', password='<password>',
          dbtable='MLZ.TENTDATA').load().toPandas()
      
      print(df.head(5))

      Set <url>, <port>, <location>, <userid> and <password> to appropriate values based on the Db2 installation in your environment.

    3. Preprocess data by splitting it into training and test subsets:
      
      df = df.dropna()
      y = df.pop('label')
      
      def standardize_data(df):
          min_max_scaler = sklearn.preprocessing.MinMaxScaler()
          df['AGE'] = min_max_scaler.fit_transform(df['AGE'].values.reshape(-1, 1))
          return df
      
      def encode_data(df):
          enc = sklearn.preprocessing.OrdinalEncoder()
          df['GENDER'] = enc.fit_transform(df['GENDER'].values.reshape(-1, 1))
          df['MARITAL_STATUS'] = enc.fit_transform(df['MARITAL_STATUS'].values.reshape(-1, 1))
          df['PROFESSION'] = enc.fit_transform(df['PROFESSION'].values.reshape(-1, 1))
          return df
      
      X = df.copy()
      X = standardize_data(X)
      X = encode_data(X)
      
      X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
      print ("Training set has %d rows, test set has %d rows." %(X_train.shape[0], X_test.shape[0]))
    4. Train the TensorFlow model:
      
      X_train = X_train.as_matrix()
      X_test = X_test.as_matrix()
      
      model = tf.keras.models.Sequential([
          tf.keras.layers.Flatten(input_shape=(4, )),
          tf.keras.layers.Dense(128, activation='relu'),
          tf.keras.layers.Dense(1, activation='sigmoid')
      ])
      
      model.compile(loss='binary_crossentropy',
                optimizer='adam',
                metrics=['accuracy'])
      
      model.fit(X_train, y_train, epochs=5)
      
      model.evaluate(X_test, y_test)
  5. Convert the TensorFlow model and save it as an ONNX model on your local system.
    
    onnx_model = keras2onnx.convert_keras(model, model.name)
    
    onnx.save_model(onnx_model, "tentModel.onnx")
  6. Convert the new ONNX model and save it as a WML for z/OS model by running the following command:
    sh onnx2dlc.sh <source> <target>

    Set the <source> option to the directory (path) and the new ONNX model (tentModel.onnx) file and the <target> option to the directory and the output WML for z/OS model file (tentModel.tar.gz) from the conversion. While <source> is required, <target> is optional, which defaults to the <source> directory.

  7. Import the new model (tentModel.tar.gz) from your local system into WML for z/OS.
    1. Sign into the WML for z/OS web user interface with your user name and password.
    2. From the side bar, go to the Model Management page, select the Models tab, and click Import model.
    3. Specify a name for the new model and select MLz Model for model format.
    4. Browse, select, and upload the new model file (tentModel.tar.gz).
    5. Click Import model to import the new model.
  8. Verify that the imported model shows up on the Models tab of the Model Management page.