自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于 Docker 和 Flask 的深度學(xué)習(xí)模型部署!

人工智能 深度學(xué)習(xí)
本文基于容器化技術(shù)的經(jīng)典工具docker,對REST API模型部署方式進(jìn)行升級,讓模型部署更加高可用。

模型部署一直是深度學(xué)習(xí)算法走向落地的重要的一環(huán)。隨著深度學(xué)習(xí)落地需求越來越迫切,具備一定工程部署能力是算法工程師的必備能力之一。

深度學(xué)習(xí)模型一個比較關(guān)鍵的前置條件就是需要花不少時間進(jìn)行環(huán)境配置,可能先需要建一個虛擬環(huán)境,然后配置深度學(xué)習(xí)框架和一些第三方庫,即時性和可移植性都比較差,總體而言可用性就不是那么強了。
那么有沒有一種可以一鍵部署的方式?能在10分鐘內(nèi)就可以在一臺新機器上部署好我們的深度學(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í),不作為本文的重點。

docker實際要重點掌握幾個概念和操作如下圖所示。

類似于makefile風(fēng)格,dockfile是用于生成image(鏡像)的文件,這個是需要我們自己編寫的,編寫完dockerfile之后,通過docker build命令生成image,這個image可以理解為可執(zhí)行程序,最后通過docker run運行這個可執(zhí)行程序image,運行起來的這個image就是container(容器)。

關(guān)于ubuntu docker安裝可參考:???????https://docs.docker.com/engine/install/ubuntu/??
基于Falsk的REST API實現(xiàn)

下面進(jìn)入正文,來看基于docker和Flask如何快速部署一個深度學(xué)習(xí)模型,模型是一個基于MobileNetV2的圖像分類器。用于啟動REST API的app.py文件代碼編寫如下:

import os
import sys
# Flask
from flask import Flaskredirecturl_forrequestrender_templateResponsejsonifyredirect
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_inputdecode_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(imgmodel):   
    img = img.resize((224224))  
    # Preprocessing the image 
    x = image.img_to_array(img
    # x = np.true_divide(x, 255)  
    x = np.expand_dims(xaxis=0)  
    # Be careful how your trained model deals with the input  
    # otherwise, it won't make correct prediction!  
    x = preprocess_input(xmode='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(imgmodel)    
        # Process your result for human  
        pred_proba = "{:.3f}".format(np.amax(preds))    # Max probability     
        pred_class = decode_predictions(predstop=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=resultprobability=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框架實現(xiàn)一個REST API服務(wù),并添加html模板,將服務(wù)運行到指定的IP地址上。

為了實現(xiàn)在指定網(wǎng)頁上的效果,除了應(yīng)用html模板之外,我們還需要編寫一點css樣式和js代碼,這里略過,可參考文末鏈接查看完整代碼。

編寫dockerfile
接下來我們需要編寫dockerfile,這是實現(xiàn)docker一鍵部署的起始點和關(guān)鍵所在。簡單來說,就是通過一系列dockerfile指令將Python環(huán)境、項目所需第三方庫、腳本運行等串起來,實現(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)過個步驟來生成鏡像。

生成鏡像后即可運行,使得該深度學(xué)習(xí)項目服務(wù)啟動:

docker run -it --rm -p 5000:5000 keras_flask_app

服務(wù)啟動后,我們打開http://localhost:5000/即可使用該深度學(xué)習(xí)服務(wù):


我們從本地上傳一張圖像進(jìn)行測試:

這樣,通過基于Flask實現(xiàn)的REST API服務(wù)和基于docker的一鍵部署功能結(jié)合在一起,我們就可以在短時間內(nèi)快速搭建一套線上的深度學(xué)習(xí)服務(wù)。完整項目代碼可參考:??https://github.com/mtobeiyf/keras-flask-deploy-webapp????

責(zé)任編輯:龐桂玉 來源: Python開發(fā)者
相關(guān)推薦

2020-05-21 14:05:17

TFserving深度學(xué)習(xí)架構(gòu)

2019-10-23 08:00:00

Flask機器學(xué)習(xí)人工智能

2022-05-10 10:19:04

AI深度學(xué)習(xí)模型

2019-08-08 08:00:00

深度學(xué)習(xí)機器學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)

2024-10-12 08:00:00

機器學(xué)習(xí)Docker

2023-05-22 08:00:00

深度學(xué)習(xí)機器學(xué)習(xí)人工智能

2017-02-05 21:02:44

大數(shù)據(jù)深度學(xué)習(xí)推薦系統(tǒng)

2022-05-26 15:02:35

Docker容器云原生

2022-09-07 08:00:00

機器學(xué)習(xí)MLFlow工具

2021-09-02 08:02:50

深度學(xué)習(xí)Kubernetes集群管理

2022-10-26 15:41:38

深度學(xué)習(xí)Deepfake機器學(xué)習(xí)

2024-11-04 08:14:48

2014-12-08 11:17:51

SDNDocker部署Docker

2022-07-13 15:43:02

Docker后端api

2022-08-01 10:36:37

機器學(xué)習(xí)數(shù)據(jù)模型

2022-08-29 14:59:12

深度學(xué)習(xí)樹的模型神經(jīng)網(wǎng)絡(luò)

2022-05-06 12:13:55

模型AI

2018-11-07 09:00:00

機器學(xué)習(xí)模型Amazon Sage

2018-09-06 08:00:00

深度學(xué)習(xí)TensorFlowPython

2023-11-19 23:36:50

點贊
收藏

51CTO技術(shù)棧公眾號