推薦:Python全棧教程痹扇!花了29980買的從入門到精通課程橡羞,分享給大家
模型部署一直是深度學(xué)習(xí)算法走向落地的重要的一環(huán)患整。隨著深度學(xué)習(xí)落地需求越來越迫切拜效,具備一定工程部署能力是算法工程師的必備能力之一。
深度學(xué)習(xí)模型一個比較關(guān)鍵的前置條件就是需要花不少時間進(jìn)行環(huán)境配置各谚,可能先需要建一個虛擬環(huán)境紧憾,然后配置深度學(xué)習(xí)框架和一些第三方庫,即時性和可移植性都比較差昌渤,總體而言可用性就不是那么強(qiáng)了赴穗。那么有沒有一種可以一鍵部署的方式?能在10分鐘內(nèi)就可以在一臺新機(jī)器上部署好我們的深度學(xué)習(xí)模型膀息?答案是肯定的望抽。本文筆者基于容器化技術(shù)的經(jīng)典工具docker,對REST API模型部署方式進(jìn)行升級履婉,讓模型部署更加高可用煤篙。
docker簡介
先簡單說一下docker。docker是一款基于Go語言開發(fā)的開源容器化技術(shù)毁腿。為了避免你寫的代碼在不同環(huán)境上表現(xiàn)不一辑奈,docker提供了一種環(huán)境隔離技術(shù)苛茂,將你的代碼和代碼所有的依賴都打包到container中去,做到once build鸠窗,run everywhere的效果妓羊。關(guān)于docker更詳細(xì)的介紹,各位讀者可以自行查找資料進(jìn)行學(xué)習(xí)稍计,不作為本文的重點(diǎn)躁绸。
docker實(shí)際要重點(diǎn)掌握幾個概念和操作如下圖所示。
類似于makefile風(fēng)格臣嚣,dockfile是用于生成image(鏡像)的文件翁潘,這個是需要我們自己編寫的骗露,編寫完dockerfile之后蚕涤,通過docker build命令生成image噪裕,這個image可以理解為可執(zhí)行程序,最后通過docker run運(yùn)行這個可執(zhí)行程序image怎虫,運(yùn)行起來的這個image就是container(容器)暑认。
關(guān)于ubuntu docker安裝可參考:https://docs.docker.com/engine/install/ubuntu/
基于Falsk的REST API實(shí)現(xiàn)
下面進(jìn)入正文,來看基于docker和Flask如何快速部署一個深度學(xué)習(xí)模型大审,模型是一個基于MobileNetV2的圖像分類器蘸际。用于啟動REST API的app.py文件代碼編寫如下:
import os
import sys
# Flask
from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer
# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications.imagenet_utils import preprocess_input, decode_predictions
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
# Some utilites
import numpy as np
from util import base64_to_pil
# Declare a flask app
app = Flask(__name__)
# You can use pretrained model from Keras
# Check https://keras.io/applications/
# or https://www.tensorflow.org/api_docs/python/tf/keras/applications
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
model = MobileNetV2(weights='imagenet')
print('Model loaded. Check http://127.0.0.1:5000/')
# Model saved with Keras model.save()
MODEL_PATH = 'models/your_model.h5'
# Load your own trained model
# model = load_model(MODEL_PATH)
# model._make_predict_function() # Necessary
# print('Model loaded. Start serving...')
def model_predict(img, model):
img = img.resize((224, 224))
# Preprocessing the image
x = image.img_to_array(img)
# x = np.true_divide(x, 255)
x = np.expand_dims(x, axis=0)
# Be careful how your trained model deals with the input
# otherwise, it won't make correct prediction!
x = preprocess_input(x, mode='tf')
preds = model.predict(x)
return preds
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
# Get the image from post request
img = base64_to_pil(request.json)
# Save the image to ./uploads
# img.save("./uploads/image.png")
# Make prediction
preds = model_predict(img, model)
# Process your result for human
pred_proba = "{:.3f}".format(np.amax(preds)) # Max probability
pred_class = decode_predictions(preds, top=1) # ImageNet Decode
result = str(pred_class[0][0][1]) # Convert to string
result = result.replace('_', ' ').capitalize()
# Serialize the result, you can add additional fields
return jsonify(result=result, probability=pred_proba)
return None
if __name__ == '__main__':
# app.run(port=5002, threaded=False)
# Serve the app with gevent
http_server = WSGIServer(('0.0.0.0', 5000), app)
http_server.serve_forever()
這里主要是基于Flask web框架實(shí)現(xiàn)一個REST API服務(wù),并添加html模板徒扶,將服務(wù)運(yùn)行到指定的IP地址上捡鱼。
為了實(shí)現(xiàn)在指定網(wǎng)頁上的效果,除了應(yīng)用html模板之外酷愧,我們還需要編寫一點(diǎn)css樣式和js代碼,這里略過缠诅,可參考文末鏈接查看完整代碼溶浴。
編寫dockerfile
接下來我們需要編寫dockerfile,這是實(shí)現(xiàn)docker一鍵部署的起始點(diǎn)和關(guān)鍵所在管引。簡單來說士败,就是通過一系列dockerfile指令將Python環(huán)境、項目所需第三方庫褥伴、腳本運(yùn)行等串起來谅将,實(shí)現(xiàn)一鍵操作。所以在本例中我們可編寫dockfile文件如下:
# 指定Python環(huán)境
FROM python:3.6-slim-stretch
# 拷貝Python依賴庫requirements文件到當(dāng)前目錄下
ADD requirements.txt /
# 安裝依賴庫
RUN pip install -r /requirements.txt
# 拷貝所有文件到app目錄下
ADD . /app
# 指定app為工作目錄
WORKDIR /app
# 聲明端口
EXPOSE 5000
# docker容器啟動
CMD [ "python" , "app.py"]
requirements.txt包括的第三方依賴庫有:
Flask==1.1.1
gevent==1.4.0
h5py==2.10.0
numpy==1.17.0
Pillow==6.1.0
tensorflow==2.3.1
Werkzeug==0.16.0
基于docker的模型部署
編寫完dockerfile文件后重慢,即可通過docker來對該深度學(xué)習(xí)分類器進(jìn)行線上部署饥臂。docker build命令建立改項目鏡像:
docker build -t keras_flask_app .
根據(jù)dockerfile中的7條指令,docker build也會經(jīng)過個步驟來生成鏡像似踱。
生成鏡像后即可運(yùn)行隅熙,使得該深度學(xué)習(xí)項目服務(wù)啟動:
服務(wù)啟動后稽煤,我們打開http://localhost:5000/即可使用該深度學(xué)習(xí)服務(wù):
我們從本地上傳一張圖像進(jìn)行測試:
這樣,通過基于Flask實(shí)現(xiàn)的REST API服務(wù)和基于docker的一鍵部署功能結(jié)合在一起囚戚,我們就可以在短時間內(nèi)快速搭建一套線上的深度學(xué)習(xí)服務(wù)酵熙。
完整項目代碼可參考:https://github.com/mtobeiyf/keras-flask-deploy-webapp