An error 'Cache may be out of date, try `force_reload=True`.' comes up even though I have included `force_reload=True` in the code block?
My Heroku App gives an Internal Server Error (500) when I try to get an inference for a model.
With the command heroku logs --tail
The following error comes up ( This is part of the error received )
2021-06-25T13:13:01.052585+00:00 heroku[web.1]: State changed from up to starting
2021-06-25T13:13:02.131624+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-06-25T13:13:02.333580+00:00 app[web.1]: [2021-06-25 13:13:02 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-06-25T13:13:02.333668+00:00 app[web.1]: [2021-06-25 13:13:02 +0000] [4] [INFO] Handling signal: term
2021-06-25T13:13:02.333748+00:00 app[web.1]: [2021-06-25 13:13:02 +0000] [7] [INFO] Worker exiting (pid: 7)
2021-06-25T13:13:02.734851+00:00 app[web.1]: [2021-06-25 13:13:02 +0000] [4] [INFO] Shutting down: Master
2021-06-25T13:13:02.814358+00:00 heroku[web.1]: Process exited with status 0
2021-06-25T13:13:32.771092+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2021-06-25T13:13:37.235228+00:00 app[web.1]: [2021-06-25 13:13:37 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-06-25T13:13:37.235870+00:00 app[web.1]: [2021-06-25 13:13:37 +0000] [4] [INFO] Listening at: http://0.0.0.0:17520 (4)
2021-06-25T13:13:37.236182+00:00 app[web.1]: [2021-06-25 13:13:37 +0000] [4] [INFO] Using worker: sync
2021-06-25T13:13:37.248134+00:00 app[web.1]: [2021-06-25 13:13:37 +0000] [7] [INFO] Booting worker with pid: 7
2021-06-25T13:13:37.304799+00:00 app[web.1]: [2021-06-25 13:13:37 +0000] [8] [INFO] Booting worker with pid: 8
2021-06-25T13:13:37.739229+00:00 heroku[web.1]: State changed from starting to up
2021-06-25T13:14:08.000000+00:00 app[api]: Build succeeded
2021-06-25T13:48:08.838837+00:00 heroku[web.1]: Idling
2021-06-25T13:48:08.857160+00:00 heroku[web.1]: State changed from up to down
2021-06-25T13:48:10.249612+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-06-25T13:48:10.325914+00:00 app[web.1]: [2021-06-25 13:48:10 +0000] [4] [INFO] Handling signal: term
2021-06-25T13:48:10.336944+00:00 app[web.1]: [2021-06-25 13:48:10 +0000] [7] [INFO] Worker exiting (pid: 7)
2021-06-25T13:48:10.337795+00:00 app[web.1]: [2021-06-25 13:48:10 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-06-25T13:48:11.241652+00:00 app[web.1]: [2021-06-25 13:48:11 +0000] [4] [INFO] Shutting down: Master
2021-06-25T13:48:11.421533+00:00 heroku[web.1]: Process exited with status 0
2021-06-26T07:29:48.172006+00:00 heroku[web.1]: Unidling
2021-06-26T07:29:48.174345+00:00 heroku[web.1]: State changed from down to starting
2021-06-26T07:30:11.641635+00:00 heroku[web.1]: Starting process with command `gunicorn app:app`
2021-06-26T07:30:13.993835+00:00 app[web.1]: [2021-06-26 07:30:13 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-06-26T07:30:13.994244+00:00 app[web.1]: [2021-06-26 07:30:13 +0000] [4] [INFO] Listening at: http://0.0.0.0:34971 (4)
2021-06-26T07:30:13.994334+00:00 app[web.1]: [2021-06-26 07:30:13 +0000] [4] [INFO] Using worker: sync
2021-06-26T07:30:14.001212+00:00 app[web.1]: [2021-06-26 07:30:14 +0000] [7] [INFO] Booting worker with pid: 7
2021-06-26T07:30:14.034313+00:00 app[web.1]: [2021-06-26 07:30:14 +0000] [8] [INFO] Booting worker with pid: 8
2021-06-26T07:30:15.444809+00:00 heroku[web.1]: State changed from starting to up
2021-06-26T07:30:16.251487+00:00 heroku[router]: at=info method=GET path=&quot;/&quot; host=safety-helmet-object-detection.herokuapp.com request_id=1a73ef92-3214-4814-a6e6-0a078b49091e fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=10ms status=200 bytes=2567 protocol=https
2021-06-26T07:30:16.253872+00:00 app[web.1]: 10.43.233.218 - - [26/Jun/2021:07:30:16 +0000] &quot;GET / HTTP/1.1&quot; 200 2412 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:30:16.635010+00:00 heroku[router]: at=info method=GET path=&quot;/&quot; host=safety-helmet-object-detection.herokuapp.com request_id=8f5913f4-2fc5-4875-a490-18c10c068f80 fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=10ms status=200 bytes=2567 protocol=http
2021-06-26T07:30:16.635538+00:00 app[web.1]: 10.47.181.239 - - [26/Jun/2021:07:30:16 +0000] &quot;GET / HTTP/1.1&quot; 200 2412 &quot;-&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:30:16.913529+00:00 heroku[router]: at=info method=GET path=&quot;/static/style.css&quot; host=safety-helmet-object-detection.herokuapp.com request_id=46585aa5-e4b8-465a-b088-6026bec294e2 fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=8ms status=200 bytes=696 protocol=http
2021-06-26T07:30:16.913943+00:00 app[web.1]: 10.47.181.239 - - [26/Jun/2021:07:30:16 +0000] &quot;GET /static/style.css HTTP/1.1&quot; 200 0 &quot;http://safety-helmet-object-detection.herokuapp.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:30:17.013092+00:00 app[web.1]: 10.63.152.20 - - [26/Jun/2021:07:30:17 +0000] &quot;GET /static/pytorch.png HTTP/1.1&quot; 200 0 &quot;http://safety-helmet-object-detection.herokuapp.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:30:17.013496+00:00 heroku[router]: at=info method=GET path=&quot;/static/pytorch.png&quot; host=safety-helmet-object-detection.herokuapp.com request_id=e334b2ec-032c-4392-99f5-7ff57c368de6 fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=16ms status=200 bytes=11679 protocol=http
2021-06-26T07:30:17.371585+00:00 app[web.1]: 10.63.152.20 - - [26/Jun/2021:07:30:17 +0000] &quot;GET /favicon.ico HTTP/1.1&quot; 404 232 &quot;http://safety-helmet-object-detection.herokuapp.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:30:17.372076+00:00 heroku[router]: at=info method=GET path=&quot;/favicon.ico&quot; host=safety-helmet-object-detection.herokuapp.com request_id=5a0b6062-de2b-465c-80f9-ccc556342ef2 fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=2ms status=404 bytes=393 protocol=http
2021-06-26T07:31:30.049426+00:00 app[web.1]: Downloading: &quot;https://github.com/ultralytics/yolov5/archive/master.zip&quot; to /app/.cache/torch/hub/master.zip
2021-06-26T07:31:37.289772+00:00 app[web.1]: Exception on / [POST]
2021-06-26T07:31:37.289821+00:00 app[web.1]: Traceback (most recent call last):
2021-06-26T07:31:37.289821+00:00 app[web.1]: File &quot;/app/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py&quot;, line 40, in _create
2021-06-26T07:31:37.289822+00:00 app[web.1]: model = attempt_load(fname, map_location=torch.device('cpu'))  # download/load FP32 model
2021-06-26T07:31:37.289837+00:00 app[web.1]: File &quot;/app/.cache/torch/hub/ultralytics_yolov5_master/models/experimental.py&quot;, line 119, in attempt_load
2021-06-26T07:31:37.289838+00:00 app[web.1]: ckpt = torch.load(attempt_download(w), map_location=map_location)  # load
2021-06-26T07:31:37.289838+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/torch/serialization.py&quot;, line 579, in load
2021-06-26T07:31:37.289839+00:00 app[web.1]: with _open_file_like(f, 'rb') as opened_file:
2021-06-26T07:31:37.289839+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/torch/serialization.py&quot;, line 230, in _open_file_like
2021-06-26T07:31:37.289839+00:00 app[web.1]: return _open_file(name_or_buffer, mode)
2021-06-26T07:31:37.289840+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/torch/serialization.py&quot;, line 211, in __init__
2021-06-26T07:31:37.289840+00:00 app[web.1]: super(_open_file, self).__init__(open(name, mode))
2021-06-26T07:31:37.289840+00:00 app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: 'best.pt'
2021-06-26T07:31:37.289840+00:00 app[web.1]:
2021-06-26T07:31:37.289841+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2021-06-26T07:31:37.289841+00:00 app[web.1]:
2021-06-26T07:31:37.289841+00:00 app[web.1]: Traceback (most recent call last):
2021-06-26T07:31:37.289842+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/flask/app.py&quot;, line 2070, in wsgi_app
2021-06-26T07:31:37.289842+00:00 app[web.1]: response = self.full_dispatch_request()
2021-06-26T07:31:37.289842+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/flask/app.py&quot;, line 1515, in full_dispatch_request
2021-06-26T07:31:37.289842+00:00 app[web.1]: rv = self.handle_user_exception(e)
2021-06-26T07:31:37.289842+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/flask/app.py&quot;, line 1513, in full_dispatch_request
2021-06-26T07:31:37.289843+00:00 app[web.1]: rv = self.dispatch_request()
2021-06-26T07:31:37.289843+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/flask/app.py&quot;, line 1499, in dispatch_request
2021-06-26T07:31:37.289843+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
2021-06-26T07:31:37.289843+00:00 app[web.1]: File &quot;/app/app.py&quot;, line 23, in predict
2021-06-26T07:31:37.289844+00:00 app[web.1]: model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True).autoshape()  # force_reload = recache latest code
2021-06-26T07:31:37.289844+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/torch/hub.py&quot;, line 339, in load
2021-06-26T07:31:37.289844+00:00 app[web.1]: model = _load_local(repo_or_dir, model, *args, **kwargs)
2021-06-26T07:31:37.289845+00:00 app[web.1]: File &quot;/app/.heroku/python/lib/python3.9/site-packages/torch/hub.py&quot;, line 368, in _load_local
2021-06-26T07:31:37.289845+00:00 app[web.1]: model = entry(*args, **kwargs)
2021-06-26T07:31:37.289845+00:00 app[web.1]: File &quot;/app/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py&quot;, line 65, in custom
2021-06-26T07:31:37.289845+00:00 app[web.1]: return _create(path, autoshape=autoshape, verbose=verbose, device=device)
2021-06-26T07:31:37.289846+00:00 app[web.1]: File &quot;/app/.cache/torch/hub/ultralytics_yolov5_master/hubconf.py&quot;, line 60, in _create
2021-06-26T07:31:37.289846+00:00 app[web.1]: raise Exception(s) from e
2021-06-26T07:31:37.289851+00:00 app[web.1]: Exception: Cache may be out of date, try `force_reload=True`. See https://github.com/ultralytics/yolov5/issues/36 for help.
2021-06-26T07:31:37.290810+00:00 app[web.1]: 10.63.152.20 - - [26/Jun/2021:07:31:37 +0000] &quot;POST / HTTP/1.1&quot; 500 290 &quot;http://safety-helmet-object-detection.herokuapp.com/&quot; &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&quot;
2021-06-26T07:31:37.291579+00:00 heroku[router]: at=info method=POST path=&quot;/&quot; host=safety-helmet-object-detection.herokuapp.com request_id=15ab238a-5ef8-40e5-86e9-159f6b4de20f fwd=&quot;122.200.18.98&quot; dyno=web.1 connect=1ms service=8230ms status=500 bytes=463 protocol=http 

Main Errors are
FileNotFoundError: [Errno 2] No such file or directory: 'best.pt'
The above exception was the direct cause of the following exception:
Exception: Cache may be out of date, try force_reload=True. See https://github.com/ultralytics/yolov5/issues/36 for help.
(app.py, that is the code I am working with is in the same directory as best.pt)
The code I wrote is as follows:
&quot;&quot;&quot;
Web App to perform inference on a YOLOv5s custom model
&quot;&quot;&quot;

import io

from PIL import Image
from pathlib import Path

from flask import Flask, render_template, request, redirect

import torch

app = Flask(__name__)


@app.route(&quot;/&quot;, methods=[&quot;GET&quot;, &quot;POST&quot;])
def predict():
    if request.method == &quot;POST&quot;:
        if &quot;file&quot; not in request.files:
            return redirect(request.url)
        model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', force_reload=True).autoshape()  # force_reload = True re cache last code (But it doesn't work here)
        model.eval()
        file = request.files[&quot;file&quot;]
        if not file:
            return

        img_bytes = file.read()
        img = Image.open(io.BytesIO(img_bytes))

        results = model(img, size=640)
        results.display(save=True, save_dir = Path('static'))

        return redirect(&quot;static/image0.jpg&quot;)

    return render_template(&quot;index.html&quot;)

if __name__ == &quot;__main__&quot;:

    app.run()  


I fixed this issue,
The
FileNotFoundError: [Errno 2] No such file or directory: 'best.pt'
was the main error, Heroku couldn't figure that path out, so I included this custom model in a folder called static.
My new code block is:
model = torch.hub.load('ultralytics/yolov5', 'custom', path='static/best.pt', force_reload=True).autoshape()
where app.py and static are in the same directory.
