Loading a PyTorch model into my Flask app, throws this error- ModuleNotFoundError: No module named 'models'

50 Views Asked by At

The project aims to detect objects from an image and generates an audio file in which the audio plays what the object really is. For instance, if the object contains a car, the audio file contains a audio saying 'CAR'. But the problem here is that when I try to run my Flask application I run into this error:

PS E:\ObjRec> python app.py
Loading YOLOv5 model...
Traceback (most recent call last):
  File "E:\ObjRec\app.py", line 11, in <module>
    model = torch.load('best.pt')
            ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\serialization.py", line 1026, in load
    return _load(opened_zipfile,
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\serialization.py", line 1438, in _load
    result = unpickler.load()
             ^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\torch\serialization.py", line 1431, in find_class
    return super().find_class(mod_name, name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named 'models'

This is my app.py :

from flask import Flask, request, jsonify
import torch
from torchvision import transforms
from PIL import Image
import io

app = Flask(__name__)

print('Loading YOLOv5 model...')
model = torch.load('best.pt')
model.eval()

preprocess = transforms.Compose([
    transforms.Resize((416, 416)),
    transforms.ToTensor(),
])

@app.route('/detect', methods=['POST'])
def detect_objects():
    if 'image' not in request.files:
        return jsonify({'error': 'No image uploaded'}), 400
    image_file = request.files['image']
    image_bytes = image_file.read()
    image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
    image = preprocess(image)
    results = model(image.unsqueeze(0)) 
    labels = results.names
    
    return jsonify({'labels': labels}), 200

if __name__ == '__main__':
    app.run(debug=True)

I couldn't proceed further without loading my model into the project. Also I'm not sure whether the procedure I'm following is correct.

This is the actual file paths.

I tried to pip install models and again ran into metadata-generation-failed error:

PS E:\ObjRec> pip install models
Collecting models
  Using cached models-0.9.3.tar.gz (16 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [8 lines of output]
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "C:\Users\Admin\AppData\Local\Temp\pip-install-8yqo_00s\models_16df2befc82545e7a9071610e1043cde\setup.py", line 25, in <module>
          import models
        File "C:\Users\Admin\AppData\Local\Temp\pip-install-8yqo_00s\models_16df2befc82545e7a9071610e1043cde\models\__init__.py", line 23, in <module>
          from base import *
      ModuleNotFoundError: No module named 'base'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.

No idea how to proceed further from here!

0

There are 0 best solutions below