Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with loading weights #5

Closed
cjwalther opened this issue Jan 28, 2023 · 3 comments
Closed

Problem with loading weights #5

cjwalther opened this issue Jan 28, 2023 · 3 comments

Comments

@cjwalther
Copy link

Using the example code in the readme.md on my workstation has shown that QReader succeeds where other python codes fail (congrats on that!). However, when moving the same small code into a venv on the server where it is meant to be included into a django project (and where hence django is installed), it fails by suddenly landing in django-land (after line 1124 in serialization.py) although django is not invoked in the example code.

This is confusing and kind of above my pay grade...

Traceback (most recent call last):
  File "/webapps/roggu/roggu/rogguapp/q1.py", line 15, in <module>
    detector = QReader()
  File "/webapps/roggu/lib/python3.10/site-packages/qreader.py", line 26, in __init__
    self.detector = QRDetector()
  File "/webapps/roggu/lib/python3.10/site-packages/qrdet.py", line 28, in __init__
    self.model = Yolov7Detector(weights=_WEIGHTS_PATH, img_size=None, agnostic_nms=True, traced=False)
  File "/webapps/roggu/lib/python3.10/site-packages/yolov7_package/model_utils.py", line 147, in __init__
    self.model = attempt_load([weights], map_location=self.device)  # load FP32 model
  File "/webapps/roggu/lib/python3.10/site-packages/yolov7_package/models/experimental.py", line 242, in attempt_load
    ckpt = torch.load(w, map_location=map_location)  # load
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 789, in load
    return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 1131, in _load
    result = unpickler.load()
  File "/webapps/roggu/lib/python3.10/site-packages/torch/serialization.py", line 1124, in find_class
    return super().find_class(mod_name, name)
  File "/webapps/roggu/roggu/rogguapp/models.py", line 3, in <module>
    class Beleg(models.Model):
  File "/webapps/roggu/lib/python3.10/site-packages/django/db/models/base.py", line 127, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/webapps/roggu/lib/python3.10/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/webapps/roggu/lib/python3.10/site-packages/django/apps/registry.py", line 137, in check_apps_ready
    settings.INSTALLED_APPS
  File "/webapps/roggu/lib/python3.10/site-packages/django/conf/__init__.py", line 92, in __getattr__
    self._setup(name)
  File "/webapps/roggu/lib/python3.10/site-packages/django/conf/__init__.py", line 72, in _setup
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
@Eric-Canas
Copy link
Owner

Wow, that's weird. I'm not really experienced with Django, so that's a bit out of my scope. It seems to be breaking inside yolov7_package library, when it tries to load the weights of the QR detector model.

I did a bit of research about the error, and it seems that you must create some reference in your settings.py. I don't really know how to manage that in Django, but Yolov7 is developed in PyTorch, so if you look for any tutorial or sample project about configuring PyTorch with Django, everything should apply without any change.

@cjwalther
Copy link
Author

Good news is: Assuming a corrupted venv, I created a fresh one and only installed QReader and Django so far. My example code mentioned previously works here as expected.

BTW: Because of limited resources on the server, I need to install torch first using

pip install torch --no-cache-dir

Maybe the pip-package for QReader could do so initially, as else it will fail.

Bad news is: I came across a document with a QR code which is correctly detected by cv2.QRCodeDetector() but not by QReader (I will make it available to you via the contact form on your website for your peruse as it pleases you.)

@Eric-Canas
Copy link
Owner

Thanks a lot!

Well, I'll add a warning about pip installing torch without cache first if you have very limited resources. Installing PyTorch is always a bit tricky, as it must be installed with the correct CUDA driver when the user plans to run models in the GPU (not really needed for most common QReader use cases). When you install QReader, torch should be directly installed, as it is a dependency of yolov7_package which is a direct QReader dependency. But PyTorch is a heavy library, and installing it at the same time that other libraries (as it happens when running pip install qreader) can overflow memory in very limited servers. As you mentioned, that's a very good workaround to install it first, without caching, and then installing the rest of the package in a second step. I'll add a warning about it. Thanks a lot!

About the image that was detected by cv2.QRCodeDetector(), thanks for sending it to me! You can directly send it to [email protected], if you feel it easier. I'll try to solve it as soon as possible.

In the background, for the QR Detection, QReader runs a Yolov7 model I trained from a mix of public datasets where I applied some data cleaning and data augmentation techniques. But, at the end of the day, there could be some limit cases or some kind of images that were not present in there. If I can find a good set of images, like that one, where QReader fails, I can add them to the dataset a re-train the detector model to make it more robust.

So, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants