简体   繁体   中英

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:

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}

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.

Here is my deeplearning.py code:

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:

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 :

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. On how I solved this issue myself, I recommend trying to debug the gunicorn locally before deploying to Heroku:

gunicorn --bind 0.0.0.0:$PORT app:app

For $PORT choose 5000 as this command is what is used in the Heroku PROCFILE.

Make sure your app.py is in the main folder. 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.

In my case, the model was in ./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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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