简体   繁体   English

OSError:当我尝试在 Heroku 上部署具有深度学习 model 的 Flask 应用程序时,SavedModel 文件不存在

[英]OSError: SavedModel file does not exist when I try to deploy my Flask application with deep learning model on Heroku

My flask app works fine on my local machine but when I deploy it on Heroku it gives the following error:我的 flask 应用程序在我的本地机器上运行良好,但是当我在 Heroku 上部署它时,出现以下错误:

2021-07-03T08:12:21.413990+00:00 app[web.1]: OSError: SavedModel file does not exist at: ./static/models/object_detection.h5/{saved_model.pbtxt|saved_model.pb} 2021-07-03T08:12:21.413990+00:00 app[web.1]: OSError: SavedModel 文件不存在于:./static/models/object_detection.h5/{saved_model.pbtxt|saved_model.pb}

I tried a lot but can't figure out the issue in this case.我尝试了很多但在这种情况下无法弄清楚问题所在。 Please do help if someone finds a solution to this error.如果有人找到解决此错误的方法,请提供帮助。

If someone wants to check my entire code repository, here is the GitHub link.如果有人想检查我的整个代码库, 这里是 GitHub 链接。

Here is my deeplearning.py code:这是我的deeplearning.py代码:

import numpy as np
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
# from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import pytesseract as pt
import re

model =tf.keras.models.load_model('./static/models/object_detection.h5')

def object_detection(path,filename):
    # read image
    image = load_img(path) # PIL object
    image = np.array(image,dtype=np.uint8) # 8 bit array (0,255)
    image1 = load_img(path,target_size=(224,224))
    # data preprocessing
    image_arr_224 = img_to_array(image1)/255.0  # convert into array and get the normalized output
    h,w,d = image.shape
    test_arr = image_arr_224.reshape(1,224,224,3)
    # make predictions
    coords = model.predict(test_arr)
    # denormalize the values
    denorm = np.array([w,w,h,h])
    coords = coords * denorm
    coords = coords.astype(np.int32)
    # draw bounding on top the image
    xmin, xmax,ymin,ymax = coords[0]
    pt1 =(xmin,ymin)
    pt2 =(xmax,ymax)
    print(pt1, pt2)
    cv2.rectangle(image,pt1,pt2,(0,255,0),3)
    # convert into bgr
    image_bgr = cv2.cvtColor(image,cv2.COLOR_RGB2BGR)
    cv2.imwrite('./static/predict/{}'.format(filename),image_bgr)
    return coords

def OCR(path,filename):
    img = np.array(load_img(path))
    cods = object_detection(path,filename)
    xmin ,xmax,ymin,ymax = cods[0]
    roi = img[ymin:ymax,xmin:xmax]
    roi_bgr = cv2.cvtColor(roi,cv2.COLOR_RGB2BGR)
    cv2.imwrite('./static/roi/{}'.format(filename),roi_bgr)
    configuration = ("-l eng --oem 3 --psm 11")
    text = pt.image_to_string(roi, config = configuration)
    pattern = re.compile('([^\s\w]|_)+')    
    text = pattern.sub('', text)
    print(text)
    return text

Here is my requirements.txt file:这是我的requirements.txt文件:

Flask==1.1.2
numpy==1.18.5
opencv-contrib-python-headless
matplotlib==3.3.2
tensorflow==2.3.1
pytesseract==0.3.6
gunicorn==20.1.0
h5py==2.10.0
tensorflow-gpu==2.3.1
Werkzeug==1.0.1
Jinja2==2.11.2
itsdangerous==1.1.0
MarkupSafe==1.1.1
requests==2.24.0
click==7.1.2

Here is the full logs from heroku :这是来自heroku的完整logs

2021-07-03T08:12:21.413181+00:00 app[web.1]: self.load_wsgi()
2021-07-03T08:12:21.413181+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-07-03T08:12:21.413181+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-07-03T08:12:21.413182+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-07-03T08:12:21.413182+00:00 app[web.1]: self.callable = self.load()
2021-07-03T08:12:21.413183+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-07-03T08:12:21.413183+00:00 app[web.1]: return self.load_wsgiapp()
2021-07-03T08:12:21.413183+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-07-03T08:12:21.413184+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-07-03T08:12:21.413184+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
2021-07-03T08:12:21.413185+00:00 app[web.1]: mod = importlib.import_module(module)
2021-07-03T08:12:21.413185+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2021-07-03T08:12:21.413187+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-07-03T08:12:21.413187+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-07-03T08:12:21.413188+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-07-03T08:12:21.413188+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-07-03T08:12:21.413188+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-07-03T08:12:21.413189+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-07-03T08:12:21.413189+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-07-03T08:12:21.413189+00:00 app[web.1]: File "/app/app.py", line 3, in <module>
2021-07-03T08:12:21.413190+00:00 app[web.1]: from deeplearning import OCR
2021-07-03T08:12:21.413190+00:00 app[web.1]: File "/app/deeplearning.py", line 10, in <module>
2021-07-03T08:12:21.413191+00:00 app[web.1]: model =tf.keras.models.load_model('./static/models/object_detection.h5')
2021-07-03T08:12:21.413202+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 186, in load_model
2021-07-03T08:12:21.413203+00:00 app[web.1]: loader_impl.parse_saved_model(filepath)
2021-07-03T08:12:21.413204+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/tensorflow/python/saved_model/loader_impl.py", line 113, in parse_saved_model
2021-07-03T08:12:21.413205+00:00 app[web.1]: constants.SAVED_MODEL_FILENAME_PB))
2021-07-03T08:12:21.413205+00:00 app[web.1]: OSError: SavedModel file does not exist at: ./static/models/object_detection.h5/{saved_model.pbtxt|saved_model.pb}
2021-07-03T08:12:21.413703+00:00 app[web.1]: [2021-07-03 08:12:21 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-07-03T08:12:21.413968+00:00 app[web.1]: [2021-07-03 08:12:21 +0000] [10] [ERROR] Exception in worker process
2021-07-03T08:12:21.413969+00:00 app[web.1]: Traceback (most recent call last):
2021-07-03T08:12:21.413971+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-07-03T08:12:21.413972+00:00 app[web.1]: worker.init_process()
2021-07-03T08:12:21.413972+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-07-03T08:12:21.413973+00:00 app[web.1]: self.load_wsgi()
2021-07-03T08:12:21.413973+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-07-03T08:12:21.413973+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-07-03T08:12:21.413974+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-07-03T08:12:21.413975+00:00 app[web.1]: self.callable = self.load()
2021-07-03T08:12:21.413975+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-07-03T08:12:21.413975+00:00 app[web.1]: return self.load_wsgiapp()
2021-07-03T08:12:21.413976+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-07-03T08:12:21.413976+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-07-03T08:12:21.413976+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
2021-07-03T08:12:21.413977+00:00 app[web.1]: mod = importlib.import_module(module)
2021-07-03T08:12:21.413977+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
2021-07-03T08:12:21.413978+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-07-03T08:12:21.413978+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-07-03T08:12:21.413979+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-07-03T08:12:21.413979+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-07-03T08:12:21.413980+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-07-03T08:12:21.413980+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-07-03T08:12:21.413980+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-07-03T08:12:21.413981+00:00 app[web.1]: File "/app/app.py", line 3, in <module>
2021-07-03T08:12:21.413981+00:00 app[web.1]: from deeplearning import OCR
2021-07-03T08:12:21.413982+00:00 app[web.1]: File "/app/deeplearning.py", line 10, in <module>
2021-07-03T08:12:21.413982+00:00 app[web.1]: model =tf.keras.models.load_model('./static/models/object_detection.h5')
2021-07-03T08:12:21.413983+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 186, in load_model
2021-07-03T08:12:21.413983+00:00 app[web.1]: loader_impl.parse_saved_model(filepath)
2021-07-03T08:12:21.413989+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/tensorflow/python/saved_model/loader_impl.py", line 113, in parse_saved_model
2021-07-03T08:12:21.413989+00:00 app[web.1]: constants.SAVED_MODEL_FILENAME_PB))
2021-07-03T08:12:21.413990+00:00 app[web.1]: OSError: SavedModel file does not exist at: ./static/models/object_detection.h5/{saved_model.pbtxt|saved_model.pb}
2021-07-03T08:12:21.414552+00:00 app[web.1]: [2021-07-03 08:12:21 +0000] [10] [INFO] Worker exiting (pid: 10)
2021-07-03T08:12:21.846509+00:00 app[web.1]: Traceback (most recent call last):
2021-07-03T08:12:21.846533+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 209, in run
2021-07-03T08:12:21.846851+00:00 app[web.1]: self.sleep()
2021-07-03T08:12:21.846869+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 357, in sleep
2021-07-03T08:12:21.847070+00:00 app[web.1]: ready = select.select([self.PIPE[0]], [], [], 1.0)
2021-07-03T08:12:21.847086+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2021-07-03T08:12:21.847237+00:00 app[web.1]: self.reap_workers()
2021-07-03T08:12:21.847253+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-07-03T08:12:21.847468+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-07-03T08:12:21.847540+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-07-03T08:12:21.847541+00:00 app[web.1]:
2021-07-03T08:12:21.847542+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-07-03T08:12:21.847542+00:00 app[web.1]:
2021-07-03T08:12:21.847542+00:00 app[web.1]: Traceback (most recent call last):
2021-07-03T08:12:21.847543+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 8, in <module>
2021-07-03T08:12:21.847665+00:00 app[web.1]: sys.exit(run())
2021-07-03T08:12:21.847682+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
2021-07-03T08:12:21.847820+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2021-07-03T08:12:21.847824+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 231, in run
2021-07-03T08:12:21.847998+00:00 app[web.1]: super().run()
2021-07-03T08:12:21.848003+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/app/base.py", line 72, in run
2021-07-03T08:12:21.848137+00:00 app[web.1]: Arbiter(self).run()
2021-07-03T08:12:21.848160+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 229, in run
2021-07-03T08:12:21.848330+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2021-07-03T08:12:21.848355+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 342, in halt
2021-07-03T08:12:21.848592+00:00 app[web.1]: self.stop()
2021-07-03T08:12:21.848596+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 393, in stop
2021-07-03T08:12:21.848809+00:00 app[web.1]: time.sleep(0.1)
2021-07-03T08:12:21.848812+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2021-07-03T08:12:21.848979+00:00 app[web.1]: self.reap_workers()
2021-07-03T08:12:21.848982+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-07-03T08:12:21.849271+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-07-03T08:12:21.849330+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-07-03T08:12:21.929600+00:00 heroku[web.1]: Process exited with status 1
2021-07-03T08:12:22.010880+00:00 heroku[web.1]: State changed from up to crashed

For those who encountered this error, it is clear: the path of the model is incorrect.对于遇到这个错误的人来说,很明显:model的路径不正确。 On how I solved this issue myself, I recommend trying to debug the gunicorn locally before deploying to Heroku:关于我自己是如何解决这个问题的,我建议在部署到 Heroku 之前尝试在本地调试 gunicorn:

gunicorn --bind 0.0.0.0:$PORT app:app

For $PORT choose 5000 as this command is what is used in the Heroku PROCFILE.对于 $PORT 选择 5000,因为此命令是 Heroku PROCFILE 中使用的命令。

Make sure your app.py is in the main folder.确保您的app.py位于主文件夹中。 It was the original problem for me when I wanted to make app.py within an internal folder, and the lesson is to make it simple.当我想在内部文件夹中制作 app.py 时,这对我来说是最初的问题,教训是让它变得简单。

In my case, the model was in ./chatbot/chatbot_model.h5在我的例子中,model 在./chatbot/chatbot_model.h5

This answer is a general process to deal with this error.这个答案是处理这个错误的一般过程。 I hope it will be very helpful for anyone who started deploying their DL and ML models using Flask and Heroku.我希望它对开始使用 Flask 和 Heroku 部署他们的 DL 和 ML 模型的任何人都有帮助。

暂无
暂无

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

相关问题 OSError:当我尝试在 Heroku 上部署我的 Flask 应用程序时,SavedModel 文件不存在 - OSError: SavedModel file does not exist when I try to deploy my Flask application on Heroku 如何使用 Flask、GitHub 和 Heroku 将我的 Tensorflow 深度学习模型部署为 Web 应用程序 - How can I deploy my Tensorflow Deep Learning model as a web application using Flask, GitHub and Heroku OSError: SavedModel 文件不存在 - OSError: SavedModel file does not exist OSError:SavedModel 文件不存在 tflite - OSError: SavedModel file does not exist tflite OSError:SavedModel 文件不存在于:/content\model\2016/{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: /content\model\2016/{saved_model.pbtxt|saved_model.pb} OSError:SavedModel 文件不存在于:C:/User/A/model/saved_model.pb - OSError: SavedModel file does not exist at: C:/User/A/model/saved_model.pb OSError:SavedModel 文件不存在于:cnnCat2.h5\{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: cnnCat2.h5\{saved_model.pbtxt|saved_model.pb} OSError:SavedModel 文件不存在于:model/CPN_Model.h5\{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: model/CPN_Model.h5\{saved_model.pbtxt|saved_model.pb} OSError:SavedModel 文件不存在于:model\mymodel.h5/{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: model\mymodel.h5/{saved_model.pbtxt|saved_model.pb} OSError:SavedModel 文件不存在于:../dnn/mpg_model.h5/{saved_model.pbtxt|saved_model.pb} - OSError: SavedModel file does not exist at: ../dnn/mpg_model.h5/{saved_model.pbtxt|saved_model.pb}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM