在Docker中使用Poetry时,PyTorch(据说)与Torchvision不兼容
我正在尝试通过 Poetry 在 Docker 中运行我的 项目。在 Windows 和 Linux 上,直接在 Poetry 创建的虚拟环境中运行时,一切都很顺利。但是,当我在 Docker 中运行时,就出现了以下问题:
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.
如果通过虚拟环境的 pip 重新安装 torchvision
,程序就能正常工作,但这并不是我想要的,因为我希望只使用 Poetry。这个问题在 Linux 和 Windows(通过 WSL2)使用 Docker 时都会出现,即使在不使用 Docker 的情况下,程序运行得很好。
根据 Torchvision 的 仓库,这些版本是兼容的。
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#
1 个回答
0
看起来我在Docker上需要使用PyPi里的Torch,而在Windows上则需要使用Torch的CPU版本。
pyproject.toml
(为了清晰起见,去掉了注释)
[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"]