C.W.K.
Stream
Lesson 04 of 05 · published

ONNX Export and TF Hub

~11 min · onnx, tf-hub, interop

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

Crossing framework lines

Sometimes your model needs to run outside TensorFlow — in ONNX Runtime for cross-platform deployment, or TensorRT for NVIDIA GPU optimization. The tf2onnx tool converts TF/Keras models to the ONNX interchange format.

ONNX (Open Neural Network Exchange) is a vendor-neutral model format. Models in ONNX run via ONNX Runtime, TensorRT, OpenVINO, CoreML conversion, and any inference engine that supports the spec.

TensorFlow Hub is a repository of pretrained reusable model components. Hub modules can be loaded as Keras layers via hub.KerasLayer — useful for quick transfer learning experiments.

tf2onnx maintainer notice (2025): the project is seeking a new maintainer. It supports TF 2.13–2.15 and continues to function, but complex models may hit unsupported ops. For critical production use, test ONNX conversion thoroughly and have TF Serving as a fallback.

Code

Keras → ONNX 변환·python
import tensorflow as tf
import tf2onnx
import onnxruntime as rt
import numpy as np

# pip install tf2onnx onnxruntime
model = tf.keras.applications.MobileNetV2(weights='imagenet')

input_signature = [
    tf.TensorSpec([None, 224, 224, 3], tf.float32, name='input')
]
model_proto, _ = tf2onnx.convert.from_keras(
    model,
    input_signature=input_signature,
    opset=15,
    output_path='mobilenetv2.onnx',
)

# Verify with ONNX Runtime
sess = rt.InferenceSession('mobilenetv2.onnx')
input_name = sess.get_inputs()[0].name
dummy = np.random.rand(1, 224, 224, 3).astype(np.float32)
result = sess.run(None, {input_name: dummy})
print(f"ONNX output shape: {result[0].shape}")
TF Hub로 즉시 transfer learning·python
import tensorflow as tf
import tensorflow_hub as hub

feature_extractor_url = (
    "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
)

# Use as a Keras layer
feature_extractor = hub.KerasLayer(
    feature_extractor_url,
    input_shape=(224, 224, 3),
    trainable=False,
)

model = tf.keras.Sequential([
    feature_extractor,
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(5, activation='softmax'),
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

External links

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.