You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Already on GitHub?
Sign in
to your account
YOLOv8 - unable to use ultralytics when installing it as part of a docker image (cv2 dnn has no DictValue function error)
#4507
YOLOv8 - unable to use ultralytics when installing it as part of a docker image (cv2 dnn has no DictValue function error)
#4507
smandava98
opened this issue
Aug 23, 2023
· 14 comments
Detection
Hi! Thanks for the great work here. I was attempting to run YOLOv8 in my existing docker file (which already downloads a headless opencv version) and I simply appeneded 'pip install ultralytics' to my docker file.
However, when I actually call YOLOv8, I get this error:
from ultralytics import YOLO
File "/usr/local/lib/python3.8/dist-packages/ultralytics/__init__.py", line 5, in <module>
from ultralytics.hub import start
File "/usr/local/lib/python3.8/dist-packages/ultralytics/hub/__init__.py", line 5, in <module>
from ultralytics.hub.auth import Auth
File "/usr/local/lib/python3.8/dist-packages/ultralytics/hub/auth.py", line 5, in <module>
from ultralytics.hub.utils import HUB_API_ROOT, PREFIX, request_with_credentials
File "/usr/local/lib/python3.8/dist-packages/ultralytics/hub/utils.py", line 14, in <module>
from ultralytics.yolo.utils import (ENVIRONMENT, LOGGER, ONLINE, RANK, SETTINGS, TESTS_RUNNING, TQDM_BAR_FORMAT,
File "/usr/local/lib/python3.8/dist-packages/ultralytics/yolo/__init__.py", line 3, in <module>
from . import v8
File "/usr/local/lib/python3.8/dist-packages/ultralytics/yolo/v8/__init__.py", line 3, in <module>
from ultralytics.yolo.v8 import classify, detect, pose, segment
File "/usr/local/lib/python3.8/dist-packages/ultralytics/yolo/v8/classify/__init__.py", line 3, in <module>
from ultralytics.yolo.v8.classify.predict import ClassificationPredictor, predict
File "/usr/local/lib/python3.8/dist-packages/ultralytics/yolo/v8/classify/predict.py", line 5, in <module>
from ultralytics.yolo.engine.predictor import BasePredictor
File "/usr/local/lib/python3.8/dist-packages/ultralytics/yolo/engine/predictor.py", line 33, in <module>
import cv2
File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 175, in bootstrap
if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
py_module = importlib.import_module(module_name)
File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.8/dist-packages/cv2/typing/__init__.py", line 169, in <module>
LayerId = cv2.dnn.DictValue
**AttributeError: module 'cv2.dnn' has no attribute 'DictValue'**
It seems there is a bug in YOLO regarding its usage of cv2 when in docker containers. I searched everywhere and tried various solutions online but the issue still seems to exist. I would greatly appreciate any help!
👋 Hello @smandava98, thank you for your interest in YOLOv8 🚀! We recommend a visit to the YOLOv8 Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.
If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.
Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.
YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.
changed the title
YOLOv8 - unable to use ultralytics when installing it as part of a docker image
YOLOv8 - unable to use ultralytics when installing it as part of a docker image (cv2 has no dnn function error)
Aug 23, 2023
changed the title
YOLOv8 - unable to use ultralytics when installing it as part of a docker image (cv2 has no dnn function error)
YOLOv8 - unable to use ultralytics when installing it as part of a docker image (cv2 dnn has no DictValue function error)
Aug 23, 2023
@smandava98 Ultralytics works correctly in Docker, the issue seems to stem from your custom environment or usage. I'd recommend starting from one of our official Docker images:
YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
We've created a few short guidelines below to help users provide what we need in order to start investigating a possible problem.
How to create a Minimal, Reproducible Example
When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This is referred to by community members as creating a minimum reproducible example. Your code that reproduces the problem should be:
✅ Minimal – Use as little code as possible to produce the problem
✅ Complete – Provide all parts someone else needs to reproduce the problem
✅ Reproducible – Test the code you're about to provide to make sure it reproduces the problem
For Ultralytics to provide assistance your code should also be:
✅ Current – Verify that your code is up-to-date with GitHub with git pull or pip install -U ultralytics to ensure your problem has not already been solved in the latest code version.
✅ Unmodified – Your problem must be reproducible using official Ultrlaytics code without changes. Ultralytics does not provide support for custom code ⚠️.
If you believe your problem meets all the above criteria, please close this issue and raise a new one using the 🐛 Bug Reporttemplate with a minimum reproducible example to help us better understand and diagnose your problem.
Thank you! 😃
Hi @glenn-jocher thanks for the quick response! I provided my dockerfile and requirements.txt in the issue. Running that with the requirements.txt and importing cv2 gives me the problem.
I think some existing dependencies are causing the issue. I will try combining the docker files and see if that works. I thought installing ultra lyrics would work but it's not for me.
Ah, I found my issue. It is related to the nvidia base docker container (nvcr.io/nvidia/pytorch:23.04-py3). Its opencv package being pulled is not being overwritten and seems to contain an invalid opencv version.
The Ultralytics Dockerfile does not work with this image (nvcr.io/nvidia/pytorch:23.04-py3)
@smandava98 thanks for the update. The Ultralytics Dockerfile is indeed designed to work with a specific set of base images, and using a different base image like the nvcr.io/nvidia/pytorch:23.04-py3 may cause unexpected issues. In this case, it appears there might be a conflict with the OpenCV package. Please consider using the recommended base Docker image to ensure compatibility with the Ultralytics package. We hope this helps and thank you for bringing this to our attention!
@smandava98, I'm glad to hear that you've been able to resolve the issue by restructuring your dependencies! It's essential to ensure compatibility across the different components in your environment. If you encounter any other issues or have more questions about YOLOv8 in the future, please don't hesitate to open another issue. Thank you for using Ultralytics and we appreciate your support!
Thanks! I ended up not using Ultralytics because it turned out I did actually need to use that image as well as had a couple other issues so it didn't turn out to be a fit for me anyways. But hopefully this will be fixed in the future! Although I think the issue is on NVIDIA's side and not yours. The ultralytics package is fine!
@smandava98, thank you for your feedback! We're glad to hear you found our package satisfactory even though the project requirements dictated a different path. Rest assured we are continuously working on making the Ultralytics package more flexible and compatible with various environments. We appreciate your input about the potential issue with the NVIDIA image, and we will note that for future reference. Feel free to return if your needs or conditions change. We are here to support you. We wish you the best with your current setup and future coding ventures!
@Yusepp i'm glad you found a workaround to your problem. Downgrading certain packages can indeed fix compatibility issues that arise due to certain dependencies. In this case, downgrading OpenCV to 4.8.0.74 seems to resolve the conflict you were facing when running YOLOv8 under nvcr.io/nvidia/pytorch:23.08-py3. Thank you for sharing this solution. This could be very useful information for anyone who might encounter a similar issue in the future.
The downgrade to 4.8.0.74 only masks the real problem, older cv2 module is not registered with pip correctly and needs to be eradicated manually:
@zsalvet thank you for bringing this to our attention. You're correct that downgrading the package could only be a temporary solution and might simply mask the underlying issue. It is very important to ensure that older versions of packages, such as OpenCV, are properly uninstalled and removed from the working environment to avoid any potential conflicts.
Manually removing the old cv2 module and then reinstalling it is a good way to ensure a clean installation. This helps to maintain the environment's integrity and avoid any version conflicts that could potentially lead to errors further down the line.
Finally, installing libx11-6 is crucial because OpenCV has a dependency on this library. Without it, OpenCV may not work as expected.
Your contribution is very valuable and will definitely help others facing a similar situation. Thank you for your insight and we hope this solution is beneficial to our community!