Я создал приложение, используя html / css / js со всеми такими файлами в шаблонах и stati c dir. Затем я выполнил gcloud build , а затем выполнил gcloud beta deploy , пытаясь развернуть приложение flask на GCP с помощью Cloud Run, но я столкнулся с этой проблемой -
"Трассировка (последний последний вызов): файл" /usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py ", строка 583, в работнике spawn_worker. Файл init_process () "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", строка 119, в файле init_process self.load_wsgi () "/ usr / local / lib / python3 .8 / site-packages / gunicorn / worker / 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-pack ages / gunicorn / util.py ", строка 358, в файле import_app mod = importlib.import_module (модуль) Файл" /usr/local/lib/python3.8/importlib/init.py ", строка 127, в файле import_module возвращает _ bootstrap ._ gcd_import (имя [уровень:], пакет, уровень) Файл "", строка 1014, в файле _gcd_import "", строка 991, в файле _find_and_load "", строка 975, в _find_and_load_unlocked Файл "", строка 671, в _load_unlocked файле "", строка 779, в файле exec_module "", строка 916, в файле get_code "", строка 846, в файле source_to_code "", строка 219, в файле _call_with_frames_removed "/ app / app.py ", строка 114 iou_mdl_cv3 = load_model (model_in +" keras_iou_v2.21_10foldCV_sample_3.h5 ", custom_objects = {'intersect_metri c': self.intersect_metri c, 'g_p_metri * 10) c 1037 * "
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)