繁体   English   中英

在 Flask 服务器启动时运行 mask-rcnn 模型

[英]Running a mask-rcnn model on Flask server startup

这是我当前工作正常的 Flask 代码,它从客户端接收带有图像的 POST 请求,通过模型运行它(基于这个 GH: https : //github.com/matterport/Mask_RCNN ),并发送一个掩码图像返回给客户端。

但是,它是从Configuration文件加载模型并为每个请求加载权重,这需要很长时间。 我想在服务器启动时加载模型和权重并将其传递给索引函数。 我已经尝试了其他问题的解决方案,但没有运气。 我想知道是不是因为我正在加载一个模型,然后是权重,而不是仅仅加载一个 h5 模型文件?

如何在烧瓶应用程序中初始化时加载文件 在烧瓶应用程序启动后运行代码

烧瓶应用程序:

from flask import Flask, jsonify, request
import base64
import cv2
import numpy as np
from Configuration import create_model


app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        # Load the image sent from the client
        imagefile = request.files['image'].read()  # Type: bytes
        jpg_as_np = np.frombuffer(imagefile, dtype=np.uint8) # Convert to numpy array
        img = cv2.imdecode(jpg_as_np, flags=1) # Decode from numpy array to opencv object - This is an array

        ### Enter OpenCV/Tensorflow below ###

        model = create_model()

        image = img[..., ::-1]

        # Detect objects
        r = model.detect([image], verbose=0)[0]

        REDACTED VISUALIATION CODE

        ### ###

        string = base64.b64encode(cv2.imencode('.jpg', masked_image)[1]).decode() # Convert back to b64 string ready for json.

        return jsonify({"count": str(r["masks"].shape[2]), 'image': string})

if __name__ == "__main__":
    app.run()

配置:

def create_model():
    device = "/cpu:0"
    weights_path = "weights.h5"
    with tf.device(device):
        model = modellib.MaskRCNN(mode="inference", model_dir=weights_path, config=InferenceConfig())
    model.load_weights(weights_path, by_name=True)
    print("Weights Loaded")
    return model

我使用before_first_request装饰器解决了这个问题。 下面是一般结构:

app = Flask(__name__)

@app.before_first_request
def before_first_request_func():
    MOODEL WEIGHT LOADING CODE
    return model

@app.route('/', methods=['POST'])
def index():
    if request.method == "POST":
        REDACTED LOADING CODE
        # Detect objects
        r = model.detect([image], verbose=0)[0]

        REDACTED VISUALISATION CODE

        string = base64.b64encode(cv2.imencode('.jpg', masked_image)[1]).decode() # Convert back to b64 string ready for json.

        return jsonify({"count": str(r["masks"].shape[2]), 'image': string})

if __name__ == "__main__":
    app.run()

model存储在内存中,以后可以在检测函数中引用。 它可用于每个 POST 请求,不需要重新加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM