如何通过Cloud Run将flask应用程序部署到gcp?

时间:2020-05-02 09:01:35

标签: python flask keras google-cloud-run

我使用html / css / js创建了一个应用程序,其中所有此类文件都位于模板和静态目录中。然后,我执行了 gcloud构建,然后执行了 gcloud beta部署,尝试使用Cloud Run将Flask应用程序部署到GCP,但是我遇到了这个问题-

  1. ”追踪(最近一次通话结束): 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py”,行 583,在spawn_worker中 worker.init_process() 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py”, init_process中的第119行 self.load_wsgi() 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py”, 第144行,在load_wsgi中 self.wsgi = self.app.wsgi() 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/app/base.py”,行 67,在wsgi中 self.callable = self.load() 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py”,
    线 49,在负载中 返回self.load_wsgiapp() 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py”,
    线 39,在load_wsgiapp中 返回util.import_app(self.app_uri) 文件“ /usr/local/lib/python3.8/site-packages/gunicorn/util.py”,第358行, 在import_app中 mod = importlib.import_module(模块) 在import_module中的文件“ /usr/local/lib/python3.8/importlib/init.py”,第127行 返回_bootstrap._gcd_import(name [level:],包,级别) _gcd_import中的文件“”,第1014行 _find_and_load中的文件“”,第991行 _find_and_load_unlocked中的文件“”,行975 _load_unlocked中的文件“”,第671行 exec_module中的文件“”,行779 get_code中的文件“”,行916 source_to_code中的文件“”,行846 _call_with_frames_removed中的文件“”,第219行 文件“ /app/app.py”,第114行 iou_mdl_cv3 = load_model(model_in +“ keras_iou_v2.21_10foldCV_sample_3.h5”, custom_objects = {''intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})“

Flask app.py

# -*- coding: utf-8 -*-# -*- coding: utf-8 -*-

###############################################################################
###############################################################################
###############################################################################

# import numpy as np
import pandas as pd
import pydicom
import cv2
import os
import glob
from statistics import mean 
from keras.models import load_model
# import keras.backend as K
import keras.backend.tensorflow_backend as K
K._SYMBOLIC_SCOPE.value = True
import matplotlib.pyplot as plt
import matplotlib.patches as patches

from flask import Flask, request, jsonify, render_template
from flask_cors import CORS, cross_origin
from werkzeug.utils import secure_filename
# import webbrowser
import time

###############################################################################
###############################################################################
###############################################################################

uploadPath = os.path.join(os.path.abspath('.'), 'static' , 'uploads')

if os.path.exists(uploadPath):
    UPLOAD_FOLDER = uploadPath
else:
    os.mkdir(uploadPath)
    UPLOAD_FOLDER = uploadPath

base_loc = "Data" #os.path.join(os.path.abspath('.'), "Data")
model_in = "bounding_box_CV_models/" #os.path.join(os.path.abspath('.'),"bounding_box_CV_models")
label_file = "keras_iou_v_2.21_10foldCV_select_mod_v0.1.csv" #os.path.join(os.path.abspath('.'),"keras_iou_v_2.21_10foldCV_select_mod_v0.1.csv")


label_data = pd.read_csv(label_file,header=0,quotechar='"')

# ===========================================================================
# load classification model
# ===========================================================================
cls_mdl = load_model(model_in + "keras_classification_v1.00_sample2_epoc30.h5")

# =============================================================================
# Img width & height 
# =============================================================================

img_width = 1024
img_height = 1024

width_mod = int(img_width/2)
height_mod = int(img_height/2)

# =============================================================================
# session dict to store filename
# =============================================================================
session_dict = {
    'filename' : ''
    }


# =============================================================================
# Pneumonia Detection Class
# =============================================================================
class PneumoniaDetection:

    def __init__(self):
        global width_mod, height_mod, cls_mdl, session_dict

        self.fileName = session_dict['filename']
        self.fileNmExt = self.fileName.rsplit('.',1)
        self.patientId = str(self.fileNmExt[0])
        self.fileExt = str(self.fileNmExt[1])
        self.fl_nm = uploadPath + '/' + self.patientId + '.dcm'
        self.dcm_img = pydicom.dcmread(self.fl_nm)
        self.img_mod = cv2.resize(self.dcm_img.pixel_array,(width_mod,height_mod),\
                                  interpolation=cv2.INTER_AREA).reshape(1,height_mod,width_mod,1)
        self.cls_mdl_out = cls_mdl.predict(self.img_mod)


    def intersect_metric(self, y_true, y_pred):
        AoG = K.abs(K.transpose(y_true)[2] - K.transpose(y_true)[0] + 1) * K.abs(K.transpose(y_true)[3] - K.transpose(y_true)[1] + 1)
        overlap_0 = K.maximum(K.transpose(y_true)[0], K.transpose(y_pred)[0])
        overlap_1 = K.maximum(K.transpose(y_true)[1], K.transpose(y_pred)[1])
        overlap_2 = K.minimum(K.transpose(y_true)[2], K.transpose(y_pred)[2])
        overlap_3 = K.minimum(K.transpose(y_true)[3], K.transpose(y_pred)[3])
        intersection = K.maximum((overlap_2 - overlap_0),0) * K.maximum((overlap_3 - overlap_1),0) + 1
        int_pct = intersection / AoG
        return int_pct


    def g_p_metric(self, y_true, y_pred):
        AoG = K.abs(K.transpose(y_true)[2] - K.transpose(y_true)[0] + 1) * K.abs(K.transpose(y_true)[3] - K.transpose(y_true)[1] + 1)
        AoP = K.abs(K.transpose(y_pred)[2] - K.transpose(y_pred)[0] + 1) * K.abs(K.transpose(y_pred)[3] - K.transpose(y_pred)[1] + 1)
        AoG_AoP = AoG / AoP
        return AoG_AoP


    def exec_model(self):
        if os.path.isdir(model_in):
            start_time = time.time()           
            # load models..
            iou_mdl_cv0 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_0.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv1 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_1.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv2 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_2.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv3 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_3.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv4 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_4.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv5 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_5.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv6 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_6.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv7 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_7.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv8 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_8.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    
            iou_mdl_cv9 = load_model(model_in + "keras_iou_v2.21_10foldCV_sample_9.h5", custom_objects={'intersect_metric':self.intersect_metric,'g_p_metric':self.g_p_metric})    

            time_taken = time.time() - start_time
            print('Load Model Time: ',time_taken)

            model_lst = [iou_mdl_cv0,iou_mdl_cv1,iou_mdl_cv2,iou_mdl_cv3,iou_mdl_cv4,iou_mdl_cv5,iou_mdl_cv6,iou_mdl_cv7,iou_mdl_cv8,iou_mdl_cv9]


        return model_lst




    def detect_pneumonia(self):             
        try:
            if (self.fileExt == 'dcm'):
                if self.cls_mdl_out[0][0] < 0.5: 
                    print("Pneumonia Detected ...")
                    response = {'status' : True, 'msg' : 'Pneumonia Detected!'}
                else:
                    print("Pneumonia Not Detected")

                    response = {'status' : True, 'msg' : 'Pneumonia Not Detected!'}
            else:
                response = {'status' : False, 'error': 'Please upload .dcm file!'}
        except Exception as e:
            response = {'status' : False, 'error': 'Exception occurred while pneumonia detection: {}'.format(e)}

        return response



###############################################################################
###############################################################################
###############################################################################

#app = Flask(__name__)
app = Flask(__name__,
             static_url_path='',
             static_folder='static',
             template_folder='templates')


CORS(app, support_credentials=True)
app.config['SECRET_KEY'] =  os.urandom(24)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@cross_origin(supports_credentials=True)
@app.route('/')
def index():
    return render_template("index.html")


@cross_origin(supports_credentials=True)
@app.route('/show', methods=['POST'])
def show():
    start_time = time.time()

    file = request.files['file']
    print('file : ', file)

    if file.filename != '':
        filename = secure_filename(file.filename)

        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))        

        session_dict['filename'] = filename
        dp = PneumoniaDetection()
        response = dp.detect_pneumonia()
    else:
        response = {'status' : False, 'error' : 'Please Upload File !'}

    time_taken = time.time() - start_time
    print('End Time: ',time_taken)

    return jsonify(response)
# =============================================================================
# Calling API
# =============================================================================

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=3000)

0 个答案:

没有答案