模型匯出與部署
- 1. Web與模型服務對接邏輯
- 2. 模型匯出
- 2.1 keras 模型進行TensorFlow匯出
1. Web與模型服務對接邏輯
- 目標
- 了解線上使用、部署模型的完整流程
- 掌握深度學習模型的部署架構
- 應用
- 無
完整程序為:

用到的技術:
- Tensorflow serving
- grpc+protobuf
- Flask
2. 模型匯出
- 應用
tf.saved_model.simple_save完成模型匯出
2.1 keras 模型進行TensorFlow匯出
Tensorflow Serving 使用的模型必須已固定格式匯出
from nets.ssd_net import SSD300
from keras import backend as K
import tensorflow as tf
import os
- 使用
tf.saved_model.simple_save工具進行匯出- tf.saved_model.simple_save(
session, # 會話
export_dir, # 匯出目錄
inputs, # 模型的輸入
outputs, # 模型的輸出
)
- tf.saved_model.simple_save(
模型的結構以及運行結果
Tensor("Reshape_42:0", shape=(?, 7308, 4), dtype=float32) Tensor("truediv:0", shape=(?, 7308, 9), dtype=float32) Tensor("concat_2:0", shape=(?, 7308, 8), dtype=float32)
{'concat_3:0': <tf.Tensor 'concat_3:0' shape=(?, 7308, 21) dtype=float32>}
完整帶出代碼:
def save_model_for_serving(verion=1, path="./serving_model/commodity/"):
# 2、匯出模型程序
# 路徑+模型名字:"./model/commodity/"
export_path = os.path.join(
tf.compat.as_bytes(path),
tf.compat.as_bytes(str(verion)))
print("正在匯出模型到 %s" % export_path)
# 模型獲取
model = SSD300((300, 300, 3), num_classes=9)
model.load_weights("./ckpt/fine_tuning/weights.13-5.18.hdf5")
with K.get_session() as sess:
tf.saved_model.simple_save(
sess,
export_path,
inputs={'images': model.input},
outputs={t.name: t for t in model.outputs}
)
if __name__ == '__main__':
save_model_for_serving(verion=1, path="./serving_model/commodity/")
加油!
感謝!
努力!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/294379.html
標籤:AI
上一篇:組隊學習李宏毅的深度學習-1
