其实在之前制作完Docker镜像之后,我就用Docker运行过一次程序,但可能最近比较忙,就没有用他来调试PyTorch,下面我记录了一次成功运行PyTorch的经历
一. cv2错误
首先我们进入Docker
docker run -it --gpus all -v /data4/wangyh:/res nvidia/cuda:v5 /bin/bash`
进入Docker之后进入到res文件夹找到docker中挂载的程序,然后用torch.distributed.launch来运行程序,如下
python -m torch.distributed.launch --nproc_per_node 8 train.py
此时服务器报了第一个错误,错误的意思是说没有安装cv2库,这里我们使用pip install opencv-python
,再次运行程序,依然报错。
Traceback (most recent call last):
File "train.py", line 2, in <module>
from utils.config import Config
File "/res/restoration/MPRNet/Deraining/utils/__init__.py", line 1, in <module>
from .image_utils import *
File "/res/restoration/MPRNet/Deraining/utils/image_utils.py", line 3, in <module>
import cv2
File "/root/miniconda3/envs/torch/lib/python3.8/site-packages/cv2/__init__.py", line 8, in <module>
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Killing subprocess 477
然后我去Google了一些答案,其中有这几个连接:链接1 链接2
总结一下就是说有下面几种解决办法
# solution 1
RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6 -y
# solution 2
RUN apt-get update && apt-get install -y python3-opencv
RUN pip install opencv-python
# solution 3
apt-get update && apt-get install libgl1
# solution 4
RUN apt-get update
RUN apt install -y libgl1-mesa-glx
但是当我运行这些命令的时候,要么安装完以后依然报错(之前的错误),要么就是无法安装,安装到最后的时候出现下面的错误,按照命令--fix-missing
之后依然报错
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/universe/f/fyba/libfyba0_4.1.1-3_amd64.deb Connection failed [IP: 91.189.88.152 80]
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/universe/f/freexl/libfreexl1_1.0.5-1_amd64.deb Connection failed [IP: 91.189.88.152 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
最终解决方案
最终我在CSDN上找到了解决办法:链接
只需要下面两行命令即可
pip uninstall opencv-python
pip install opencv-python-headless
然后运行python,import cv2就可以成功导入啦
二. distributed报错
解决cv2的问题之后,我使用python -m torch.distributed.launch --nproc_per_node 8 train.py
来训练model,然而报了以下错误
RuntimeError: NCCL error in: /opt/conda/conda-bld/pytorch_1614378083779/work/torch/lib/c10d/ProcessGroupNCCL.cpp:825, unhandled system error, NCCL version 2.7.8
解决办法:链接
即在启动容器的时候加入--ipc=host
命令
三. 启动/关闭/后台运行Docker
1. 启动
1. 正常启动
docker run -it <image:tag> /bin/bash
2. 启动并命名容器
docker run --name <container_name> -it <image:tag> /bin/bash
3. 挂载GPU
docker run --name <container_name> -it --gpus all <image:tag> /bin/bash
4. 挂载文件夹
docker run -it -v /data4/wangyh:/res <image:tag> /bin/bash`
5. 挂载多个文件夹
docker run -it -v /data4/wangyh/code:/code -v /data4/wangyh/data:/dataset <image:tag> /bin/bash`
最终版本
docker run --name wyh -p 5678:22 --ipc=host --gpus all -it -v /data4/wangyh/FSDNet:/code wangyh/cuda:10.1 /bin/bash
2. 后台运行Docker
当我们的程序正常运行时,我们需要让docker在后台运行,使用ctrl+q+p
即可退出docker,此时gpu依然被占用,说明docker在后台运行程序
查看容器: docker ps -a
删除容器: docker rm -f <id>
如果需要再次进入docker,执行下面命令即可
docker attach <name> 或者 docker attach <container id>
3. Docker内释放显存
ps -aux
找到PID直接kill即可