任意版本的pytorch、cuda的gpu版本与detectron2、jupyter notebook安装
任意版本的pytorch、cuda的gpu版本与detectron2、jupyter notebook安装
1. 简介
本文主要介绍pytorch cuda gpu版本与detectron2、jupyter notebook安装, 主要是基于docker构建AI开发/预测用的环境.
优势与不足
优势
- 一键安装, 无需手动安装pytorch/cuda/detectron2/jupyter notebook等,尤其是在Windows系统下安装detectron2, 安装过程中会遇到各种问题, 本文避免了这些问题.
- docker运行GPU任务与直接在宿主机器上运行GPU任务性能相当,方便后续移植交付.
不足
- 目前只支持NVIDIA的cuda以及amd架构处理器,缺乏arm架构版本,也未找到opencl版本的base镜像
2. 快速启动
-
下载镜像
docker pull zzc932/pytorch:1.13.0-py3.9.12-cuda11.7.1-ubuntu20.04 -
运行容器
docker run --rm --gpus all -it zzc932/pytorch:1.13.0-py3.9.12-cuda11.7.1-ubuntu20.04 bash
参数说明:
- --rm: 容器退出后自动删除
- --gpus all: 使用所有GPU,如果只使用部分GPU,可以使用--gpus device=0,1
- -it: 交互式运行
- zzc932/pytorch:1.13.0-py3.9.12-cuda11.7.1-ubuntu20.04: 镜像名称
进入容器后执行nvidia-smi确认GPU是否正常使用.
3. 容器内使用
主要思路为启动时挂载工程目录, 启动jupyter notebook, 然后在宿主机器上通过浏览器访问jupyter notebook, 从而实现在宿主机器上开发代码, 在容器内运行代码.
# 初始化jupyter密码
mkdir ~/.jupyter
docker run --rm -it -v ~/.jupyter:/root/.jupyter zzc932/pytorch:1.13.0-py3.9.12-cuda11.7.1-ubuntu20.04 "jupyter notebook password"
# 警告: --ip=0.0.0.0 --allow-root 会导致jupyter notebook不安全, 在生产环境使用时请去掉
jupyter notebook password jupyter notebook --port 8999 --ip=0.0.0.0 --allow-root
4. dockerfile内容
本章节给出dockerfile, 有需要修改特定CPU架构/GPU驱动的需求,可以修改对应命令.
FROM cnstark/pytorch:1.13.0-py3.9.12-cuda11.7.1-ubuntu20.04
# gcc & g++ ≥ 5.4 are required. ninja
RUN pip install ninja
RUN cd /tmp && apt-get update && apt-get install -y git && git clone https://github.com/facebookresearch/detectron2.git && python -m pip install -e detectron2 jupyter
5. 参考
- 自动化构建镜像
- jupyter安装与启动命令
- more

浙公网安备 33010602011771号