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

ONNX export와 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

Framework 경계 넘기

가끔 model이 TensorFlow 밖에서 돌아야 해 — 크로스 플랫폼 배포는 ONNX Runtime, NVIDIA GPU 최적화는 TensorRT. tf2onnx 도구가 TF/Keras model을 ONNX 교환 포맷으로 변환.

ONNX (Open Neural Network Exchange)는 벤더 중립 model 포맷. ONNX 형태 model은 ONNX Runtime, TensorRT, OpenVINO, CoreML 변환, 명세 지원하는 어떤 inference engine에서도 돌아.

TensorFlow Hub는 pretrained 재사용 model 컴포넌트 저장소. Hub 모듈은 hub.KerasLayer로 Keras layer처럼 로드 — 빠른 transfer learning 실험에 유용.

tf2onnx 메인테이너 공지 (2025): 프로젝트가 새 메인테이너 찾는 중. TF 2.13–2.15 지원하고 계속 작동하지만 복잡한 model은 미지원 op 만날 수 있음. 핵심 production 용도엔 ONNX 변환 철저히 테스트하고 fallback으로 TF Serving 준비.

Code

Keras → ONNX conversion·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}")
Instant transfer learning via TF Hub·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.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.