Once an algorithm has converged after hours or days of training, you must save its learned mathematical weights to persistent storage. Model Serialization converts in-memory model objects into standardized byte streams that can be stored, versioned, and transferred across network boundaries for inference.
Standard Python uses the built-in pickle module (and its wrapper joblib) to serialize Python objects. However, pickle suffers from a catastrophic security vulnerability:
Pickle is an executable virtual machine. When calling pickle.load(), arbitrary Python code embedded inside the file executes with full server permissions. A malicious actor can craft a payload that executes shell commands, steals database credentials, or installs malware upon unpickling. Never load pickle files from public sources or untrusted user uploads!
Created by Microsoft, Meta, and Amazon, ONNX is an open-source format for representing machine learning models. Instead of storing arbitrary Python bytecode, ONNX compiles the model into a strict, static Computational Graph consisting of standard mathematical operators (MatMul, Add, Relu, Conv).
| Serialization Format | Language Portability | Security Profile | Inference Execution Speed |
|---|---|---|---|
| Python Pickle / Joblib | Locked strictly to Python with identical library versions. | Dangerous; vulnerable to arbitrary Remote Code Execution. | Standard interpreted Python execution speed. |
ONNX Format (.onnx) |
Universal: Export in PyTorch/Scikit, run in C++, C#, Java, Go, Rust, or JavaScript! | 100% Safe: Static computational graph containing only numerical tensors. | 3x to 10x Faster: Hardware accelerated via C++ SIMD, AVX-512, CUDA, and TensorRT. |
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
from skl2onnx import convert_sklearn
from skl2onnx.common.data_types import FloatTensorType
import onnxruntime as rt
import numpy as np
# 1. Train Model
iris = load_iris()
clf = RandomForestClassifier(n_estimators=50).fit(iris.data, iris.target)
# 2. Convert to ONNX format
initial_type = [('float_input', FloatTensorType([None, 4]))] # [batch_size, 4 features]
onnx_model = convert_sklearn(clf, initial_types=initial_type)
with open("model.onnx", "wb") as f:
f.write(onnx_model.SerializeToString())
print("Model successfully compiled to model.onnx!")
# 3. High-Speed Inference with ONNX Runtime (Zero Python overhead!)
session = rt.InferenceSession("model.onnx", providers=['CPUExecutionProvider'])
input_name = session.get_inputs()[0].name
query = np.array([[5.1, 3.5, 1.4, 0.2]], dtype=np.float32)
pred_onx = session.run(None, {input_name: query})
print("Predicted Class:", pred_onx[0][0])
print("Class Probabilities:", pred_onx[1][0])
Broaden your foundational and advanced Machine Learning knowledge across our curriculum:
Load ONNX sessions into memory for sub-millisecond API execution.
Execute compiled models directly in user browsers via WebAssembly/WebGPU.
Prevent remote code execution vulnerabilities and Python version mismatch.
Hardware acceleration, cross-language portability, and compilation benefits.