Search code examples
pythondockerdockerfilepython-poetrypyproject.toml

PyTorch (supposedly) Incompatible with Torchvision in Docker when using Poetry


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: packages@pytorch.org
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: soumith@pytorch.org
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# 

Solution

  • 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)

    [tool.poetry]
    name = "wyzely-detect"
    version = "0.2.1"
    description = "Recognize faces/objects in a video stream (from a webcam or a security camera) and send notifications to your devices"
    authors = ["slashtechno <77907286+slashtechno@users.noreply.github.com>"]
    repository = "https://github.com/slashtechno/wyzely-detect"
    keywords = ["object-detection", "face-detection", "wyze", "security", "yolov8", "unified-push"]
    license = "MIT"
    readme = "README.md"
    packages = [{include = "wyzely_detect"}]
    
    [tool.poetry.dependencies]
    
    python = ">=3.10, <3.12"
    
    python-dotenv = "^1.0.0"
    httpx = "^0.25.0"
    opencv-python = "^4.8.1.78"
    ultralytics = "^8.0.190"
    hjson = "^3.1.0"
    numpy = "^1.23.2"
    
    torch = [
        {version = "^2.2.1", markers = "extra!='cuda' and platform_system=='Linux'"},
        {version = "^2.2.1", source = "pytorch-cpu", markers = "extra!='cuda' and platform_system!='Linux'"},
        ]
    
    absl-py = "^2.1.0"
    tensorflow = {version = "^2.13.0", markers = "extra!='cuda'"}
    tensorflow-macos = { version = "^2.13.0", platform = "darwin", markers = "platform_machine=='arm64'" }
    tensorflow-intel = { version = "^2.13.0", platform = "win32" }
    tensorflow-io-gcs-filesystem = [
        { version = "< 0.32.0", markers = "platform_system == 'Windows'" }
    ]
    
    deepface = "^0.0.79"
    prettytable = "^3.9.0"
    
    [tool.poetry.group.gpu]
    optional = true
    
    [tool.poetry.group.gpu.dependencies]
    torch = {version = "^2.2.1", source = "pytorch-cu121", markers = "extra=='cuda'"}
    tensorflow = {version = "^2.14.0", extras = ["and-cuda"], markers = "extra=='cuda'"}
    
    [tool.poetry.extras]
    
    cuda = []
    
    [[tool.poetry.source]]
    name = "pytorch-cpu"
    url = "https://download.pytorch.org/whl/cpu"
    priority = "explicit"
    
    [[tool.poetry.source]]
    name = "pytorch-cu121"
    url = "https://download.pytorch.org/whl/cu121"
    priority = "explicit"
    
    [tool.poetry.group.dev.dependencies]
    black = "^23.9.1"
    ruff = "^0.0.291"
    ipykernel = "^6.25.2"
    nbconvert = "^7.9.2"
    
    [build-system]
    requires = ["poetry-core"]
    build-backend = "poetry.core.masonry.api"
    
    [tool.ruff]
    
    line-length = 135
    extend-select= ["FIX002"]
    
    [tool.poetry.scripts]
    wyzely-detect = "wyzely_detect.__main__:main"
    

    Dockerfile

    FROM python:3.10.5-buster
    
    LABEL org.opencontainers.image.description "Docker image for running wyzely-detect"
    LABEL org.opencontainers.image.source "https://github.com/slashtechno/wyzely-detect"
    
    RUN apt update && apt install libgl1 -y
    RUN pip install poetry
    
    
    WORKDIR /app
    
    COPY . .
    
    RUN poetry install
    
    # RUN poetry run pip uninstall -y torchvision 
    # RUN poetry run pip install torchvision 
    
    ENTRYPOINT ["poetry", "run", "python", "-m", "--", "wyzely_detect", "--no-display"]
    

    All file changes: https://github.com/slashtechno/wyzely-detect/compare/771154cbefdb09c7328c49ded9b675a38b628426..ee66a2f42845be512b65fb398c75ba06e94cea23