I’m trying to run my project in Docker via Poetry.
It runs fine on Windows and Linux when just run normally in a Poetry-created venv. However, when running with Docker, I get the following:
RuntimeError: Couldn't load custom C++ ops. This can happen if your PyTorch and torchvision versions are incompatible, or if you had errors while compiling torchvision from source. For further information on the compatible versions, check https://github.com/pytorch/vision#installation for the compatibility matrix. Please check your PyTorch version with torch.__version__ and your torchvision version with torchvision.__version__ and verify if they are compatible, and if not please reinstall torchvision so that it matches your PyTorch install.
It works if torchvision
is reinstalled via the venv’s pip, but this isn’t ideal as I want to just use Poetry. This occurs when using Docker on Linux and on Windows (via WSL2), even when the program works fine when Docker isn’t used.
The versions are compatible according to the Torchvision repo.
root@eb8f096b3563:/app# poetry run pip show torch
Name: torch
Version: 2.1.2+cpu
Summary: Tensors and Dynamic neural networks in Python with strong GPU acceleration
Home-page: https://pytorch.org/
Author: PyTorch Team
Author-email: [email protected]
License: BSD-3
Location: /root/.cache/pypoetry/virtualenvs/wyzely-detect-9TtSrW0h-py3.10/lib/python3.10/site-packages
Requires: filelock, fsspec, jinja2, networkx, sympy, typing-extensions
Required-by: thop, torchvision, ultralytics, wyzely-detect
root@eb8f096b3563:/app# poetry run pip show torchvision
Name: torchvision
Version: 0.16.2
Summary: image and video datasets and models for torch deep learning
Home-page: https://github.com/pytorch/vision
Author: PyTorch Core Team
Author-email: [email protected]
License: BSD
Location: /root/.cache/pypoetry/virtualenvs/wyzely-detect-9TtSrW0h-py3.10/lib/python3.10/site-packages
Requires: numpy, pillow, requests, torch
Required-by: ultralytics
root@eb8f096b3563:/app#
2
Answers
Seems I had to use Torch from PyPi for Docker and Torch from the Torch CPU repository on Windows.
pyproject.toml
(comments removed for clarity)Dockerfile
I’ve added the
torchvision
dependency topyproject.toml
. Just building and running the image created via theDockerfile
without other services specified indocker-compose.yml
.🗎
Dockerfile
🗎
pyproject.toml
When I build and run the image (I’ve commented out the
ENTRYPOINT
in theDockerfile
and then running Python in the container) I can import both thetorch
andtorchvision
packages. The latter gives me a warning because I don’t have a GPU on my machine. I can’t replicate the runtime error that you see though.