Methods to Serialize and Deserialize Scikit Learn and Tensorflow models for production

This article briefs about the various methods to serialize and deserialize Scikit Learn and Tensorflow models for production
Listen to this story

There are various methods to put trained machine learning models into production, and one such way to check the readiness of developed machine learning models for production is only Serialization and Deserialization. This technique enables certain parameters of the developed machine learning models to be altered accordingly in the production phase and proceed with model deployment. So this article basically briefs about the best methods to serialize and deserialize machine learning and deep learning models for production or transmission.

Table of Contents

  1. Overview and the necessity for serialization and deserialization
  2. Case study for scikit learn models
    1. Pickle format
    2. Joblib format
  3. Case study for TensorFlow models
    1. HDF5 (h5) format
    2. JSON format
  4. Summary

Let’s first discuss why we need serialization and deserialization while deploying the model.

Overview and Necessity of serialization and deserialization

Serialization is the process of transforming the overall data structure or the trained model in the form of flexible formats of retrieval to later facilitate the decomposition of the serialized model into production. The method of decomposing the trained model in the production environment can also be termed Deserialization

So the process of serialization and deserialization is necessary to obtain models ready for production or final deployments and also to obtain reliable models delivered in production. There are various formats in which machine learning and deep learning models respectively can be serialized and deserialized. 

Some of the standard forms for machine learning models include pickle, joblib, and JSON (JavaScript Object Notation) primarily, and with respect to deep learning models, there are various types such as  HDF5(h5) (Hierarchical Data Format), YAML (Yet Another Markup Language) and JSON supported from Keras. 

So according to the computational requirements and ease of access and storage respective data formats are opted where the JSON and the YAML formats turn out to be more human interpretable and flexible.

Case Study for scikit learn models

Serialization and Deserialization of machine learning models can be done in three forms basically as mentioned earlier. They are the pickle format, joblib format, and JSON format. Let’s explore each of the types individually.

Steps involved in Pickle Serialization and Deserialization

Usage of the pickle module is generally termed pickling, wherein the trained model parameters will be converted into a byte stream and later deserialized in the production end. The steps involved in pickling machine learning models are shown below.

Import the required module for pickling into the working environment as shown below.

import pickle

If the module is not available in the working environment it can easily be installed using the pip command in the same working environment as shown below and later import the same module to the working environment.

!pip install pickle
import pickle

Later a random name can be given for the pickle filename and a print statement can be mentioned to verify for successful serialization of the machine learning model to the working environment as shown below.

dtc_pkl_filename="DTC_Model.pkl"

print('Pickle model serialized to the working environment')

If the serialization of the trained model is successful we would yield the below output.

The inbuilt function of dump has to be used as shown below to open the serialized pickle file and perform the required write operations, here mainly write binary (wb)  is used to ensure that the file is accessible to perform write operations in it in binary format as pickling process also supports binary format storage. The code for the same is given below.

with open(dtc_pkl_filename, 'wb') as file: #wb stands for write binary and open is used to serialize the model
   pickle.dump(dtc_model, file) # dump is used to serialize the ML model

Now the load module of pickle is used to deserialize the dumped pickle file in the same working environment as shown below where read binary (rb) is used for deserialization of the trained model and also to evaluate various parameters of the model. The complete steps of deserialization and the usage of the deserialized model for evaluating certain parameters are shown below.

with open(dtc_pkl_filename, 'rb') as file: ## rb stands for read binary which is used to deserialize the ML model from the working environment
   des_dtc_model = pickle.load(file)  ## load is used to bring the ML model from the pickle format to the working environment

Successful deserialization of the model to the working environment can be done using a print statement as shown below.

print('Pickle model deserialized to the working environment')

The above print statement generates the following output.

Later using the deserialized model the accuracy score can be computed as shown below.

acc_score=des_dtc_model.score(X_test,Y_test)

The deserialized model in this case study has yielded an accuracy score of 70.99% as shown below.

print('Accuracy score of Deserialized Model is',acc_score*100)

Pickling of machine learning models is easier but sometimes due to the inability of easier human interpretation pickling process is condemned in production and other formats are opted.

Steps involved in joblib serialization and deserialization

Joblib is an extension of the pickle module and it is used instead of a pickle due to its lighter data type transformation in the pipeline and its inbuilt faster processing for various data types.

The joblib module can be made available in the working environment as shown below.

pip install joblib
import joblib

Later a random name for the joblib file can be declared as shown below.

dtc_joblib_file = "DTC_Model.pkl" 

Now the process of serializing the joblib file can be done by using the inbuilt function of dump similar to pickle to serialize the file format to the working environment and the successful serialization of the joblib file can be evaluated as shown below.

joblib.dump(dtc_model, dtc_joblib_file)
print('Joblib model serialized to working environment')

Now the trained model can be deserialized to the working environment as shown below using the load module similar to pickle. The steps involved in deserialization are shown below.

DTC_joblib_model = joblib.load(dtc_joblib_file) ## load is used to deserialize the ML model to the working environment

The successful deserialization can be validated as shown below with the corresponding output generated.

print('Joblib model deserialized to working environment')
DTC_joblib_model

Later the deserialized model was used to evaluate the accuracy score along with the output generated as shown below.

acc_score_joblib=DTC_joblib_model.score(X_test,Y_test)
print('Accuracy score of Deserialized Joblib Model is',acc_score_joblib*100)

However, the joblib module is an extension of the pickle but it is opted over pickle for its computational and storage benefits. In the next section, let’s explore the steps involved in serializing and deserializing ML models in JSON format.

Case study for Tensorflow models

Unlike the process of serialization and deserialization of machine learning models, deep learning models have their respective formats of serialization and deserialization. For deep learning models, substantial support is provided by Keras for formats like hdf5 (h5), YAML, and JSON. The steps involved for the same are briefly discussed below.

HDF5 (h5) format of serialization and deserialization

First, the necessary module has to be imported from Keras as shown below.

from tensorflow.keras.models import load_model

Now the deep learning model developed can be serialized into the working environment and the serialization of the deep learning model also can be checked as shown below.

model.save('serialized_dl_model.h5') ## Serializing the model
print('Model serialized to the disk')

Now the model can be deserializable into the working environment as shown below.

des_model=load_model('serialized_dl_model.h5')  ## Deserializing the model
print('Model deserialized to the disk')

Now the deserialized deep learning model can be used for the evaluation of certain parameters. The steps involved to evaluate the model testing loss and the testing accuracy is shown below.

print('Testing loss {} \n Testing accuracy {}'.format(des_model.evaluate(X_test,Y_test)[0],des_model.evaluate(X_test,Y_test)[1]))

From the implementation end, hdf5 (h5) format based implementation is easier, but as h5 format stores complex data in hierarchical format it sometimes has concerns with respect to optimal convergence and faster processing.

JSON format of serialization and deserialization

Now let us explore the steps involved in serialization and deserialization of deep learning models in JSON format. Unlike other formats, the necessary library has to be imported from the Keras module as shown below.

from tensorflow.keras.models import model_from_json

Once the necessary module is imported the deep learning model is serialized using the to_json() module as shown below.

json_model=model.to_json() ## Serializing the deep learning model to the disk

Now the model has to be deserialized by first creating the JSON file in the readable format as shown below.

json_file=open('json_model','r') ## r is used to read the json_file opened

Now the created JSON file has to be loaded by utilizing the read() inbuilt function and closed accordingly as shown below.

loaded_json_model=json_file.read()
json_file.close()

Now the deserialized model is loaded into the working environment using the library model_from_json of Keras as shown below.

des_json_model=model_from_json(loaded_json_model)

Now the deserialized deep learning model has to be compiled again in the same working environment accordingly and later proceeded to evaluate the needed parameters. Here the steps involved in evaluating the deserialized models’ test performance are shown below.

print('Testing loss {} \n Testing accuracy {}'.format(des_json_model.evaluate(X_test,Y_test)[0],des_json_model.evaluate(X_test,Y_test)[1]))

In the above image we can see that post deserialization of the deep learning model the models loss has increased and the accuracy has decreased in the test phase which is showing signs of unreliable model along with poor performance for changing data and this is how the process of serialization and deserialization helps us to cross-validate the model’s performance in productions.

Summary

As mentioned earlier, machine learning and deep learning models have their respective formats of serialization and deserialization along with some associated pros and cons. The below table gives a glimpse of certain parameters and concerns associated with the individual types.

Scikit-learn model concerns and parametersTensorflow model concerns and parameters
The pickle format is easy to implement but it is not human interpretable and shows signs of slower convergence in production.The h5 format is easy to implement. But due to its hierarchy of storage, it may show concerns for complex models and huge data processing.
The joblib format is an extension of pickle and opted over pickle due to its faster convergence along with various data types.The YAML format serialization appears to be no longer supported by Keras due to security concerns.
The JSON format is prone to yield attribute errors and it is hard to deserialize certain attributes and it is easily manipulable and interpretable by humans.The JSON format is easy to implement but the deserialization model showed higher loss and lower accuracy in testing which would be a concern during production.

References

Download our Mobile App

Subscribe to our newsletter

Join our editors every weekday evening as they steer you through the most significant news of the day.
Your newsletter subscriptions are subject to AIM Privacy Policy and Terms and Conditions.

Our Recent Stories

Our Upcoming Events

3 Ways to Join our Community

Telegram group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our Daily newsletter

Get our daily awesome stories & videos in your inbox
MOST POPULAR