Model Serialization: Pickle, Joblib, and ONNX formats
Learn how to save and export machine learning models. Master Pickle, Joblib, TorchScript, and ONNX formats for deployment.
Introduction
When you train a machine learning model, its learned weights, coefficients, and structure exist inside the computer's RAM. If you turn off your Jupyter Notebook, that model is lost forever.
To deploy your model, you must save it to your hard drive in a file format that can be loaded later by a web server or mobile application. This process is called Model Serialization (saving/exporting) and Deserialization (loading/importing). Depending on whether you are deploying a simple linear regression model or a deep neural network, you must choose the correct format to balance loading speed, compatibility, and safety.
What You Will Learn
- What serialization and deserialization are.
- The differences between Pickle and Joblib for classical ML.
- How to export PyTorch models using TorchScript.
- The role of ONNX (Open Neural Network Exchange) in cross-language deployments.
- Important security warnings when loading model files.
Why This Topic Matters
In production, models must load in milliseconds and consume minimal RAM. If your API server tries to load a raw PyTorch training class directly, it will fail due to dependency mismatches. By exporting models to serialized formats like ONNX, you can run Python-trained models in C++, Go, or JavaScript environments directly, boosting speed and scalability.
Prerequisites
Detailed Explanation
Different frameworks and models require different serialization formats:
1. Pickle vs. Joblib (For Scikit-Learn)
For classical machine learning models (SVM, Random Forests, Linear Regression), Python provides two main modules:
- Pickle: The standard Python module for serializing objects.
- Joblib: A replacement for Pickle optimized for objects containing large NumPy arrays. Scikit-learn estimators often store huge arrays for tree paths. Joblib is much faster than Pickle and compresses files on disk.
Joblib advantage:
- Stores arrays in separate memory maps.
- Avoids memory copying when loading.
2. TorchScript (For PyTorch)
PyTorch models are written in dynamic Python code. However, web servers running in C++ cannot parse Python. PyTorch solves this with TorchScript—a way to compile your model into a static representation:
- Tracing: Pass a dummy input tensor through the model to record the operations executed.
- Scripting: Analyze the Python source code directly to compile it (handles conditional loops
if/else).
3. ONNX (Open Neural Network Exchange)
ONNX is a cross-platform open format for machine learning models.
- The Concept: Train your model in PyTorch or TensorFlow, export it to
.onnxformat, and run it using the optimized ONNX Runtime engine in a C++ server or inside a browser via JavaScript. - Advantage: Bypasses Python interpreter constraints, making inference extremely fast.
Security Warning: Pickle/Joblib Vulnerability
> [!CAUTION] > Never load a pickle or joblib file from an untrusted source. > During deserialization, Python executes whatever code is stored inside the file. A hacker can construct a malicious model file that runs command-line scripts to download viruses or steal database passwords once loaded onto your server.
Visual Diagram (Mermaid)
graph TD
A[Trained Model in RAM] --> B{Choose Format}
B -->|Scikit-learn / Numpy| C[Joblib: Optimized for arrays]
B -->|PyTorch Production| D[TorchScript: Static C++ runtime]
B -->|Cross-Language / Browser| E[ONNX: Global runtime format]
C -->|Output| F[.joblib file]
D -->|Output| G[.pt / .ptl file]
E -->|Output| H[.onnx file]
style E fill:#10B981,stroke:#fff,color:#fff
Python Code Examples
We will write a complete Python script to serialize a simple model using Joblib and export a PyTorch model to ONNX.
import numpy as np
from sklearn.linear_model import LinearRegression
import joblib
# 1. Train and save a Scikit-Learn Model using Joblib
X = np.array([[1], [2], [3], [4]])
y = np.array([3, 5, 7, 9]) # y = 2x + 1
model = LinearRegression()
model.fit(X, y)
# Serialize to file
model_filename = "linear_model.joblib"
joblib.dump(model, model_filename)
print(f"Scikit-learn model saved successfully to: {model_filename}")
# Deserialize (Load) from file
loaded_model = joblib.load(model_filename)
print("Prediction from loaded model for X=5:", loaded_model.predict([[5]]))
# 2. Export a PyTorch Module to ONNX (Conceptual Setup)
import torch
import torch.nn as nn
class MiniNet(nn.Module):
def __init__(self):
super(MiniNet, self).__init__()
self.fc = nn.Linear(10, 2)
def forward(self, x):
return self.fc(x)
torch_model = MiniNet()
torch_model.eval() # Must set model to evaluation mode before export
# Create dummy input vector matching model shape
dummy_input = torch.randn(1, 10)
# Export to ONNX file
onnx_filename = "mini_net.onnx"
torch.onnx.export(
torch_model,
dummy_input,
onnx_filename,
export_params=True,
opset_version=11, # Target ONNX operator set version
do_constant_folding=True
)
print(f"PyTorch model successfully exported to ONNX format: {onnx_filename}")
Industry Use Cases
- Mobile AI Deployments: Exporting PyTorch models to ONNX/TorchScript to run real-time face tracking inside iOS and Android apps.
- Embedded Systems (Edge Computing): Running model files directly on microchips in smart home devices.
- Enterprise Web Pipelines: Deploying Python models into C# or Java corporate servers using ONNX execution runtimes.
Summary
Model serialization converts runtime model states into static disk files. While Joblib represents the standard for Scikit-Learn structures due to numpy array compressions, TorchScript compiles PyTorch for C++ serves, and ONNX offers cross-platform runtimes. Security precautions must be observed to block arbitrary script executions when reloading serialized files.
Next Topic
Once the model is exported to disk, how do we wrap it in a web service so users can query it? Let's check: Deploying Models as APIs using FastAPI and Pydantic.