【Linux】使用docker安装detectron2

在Linux下使用docker安装detectron2

主要参考:
docker安装detectron2
其他参考:
【官方】detectron2 docker安装

mkdir env
cd env
vim Dockerfile


编写Dockerfile

【官方】detectron2 Dockerfile

FROM nvidia/cuda:11.1.1-cudnn8-devel

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install -y \
	python3-opencv ca-certificates python3-dev git wget sudo ninja-build
RUN ln -sv /usr/bin/python3 /usr/bin/python

# create a non-root user
ARG USER_ID=1000
RUN useradd -m --no-log-init --system  --uid ${USER_ID} appuser -g sudo
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
USER appuser
WORKDIR /home/appuser

ENV PATH="/home/appuser/.local/bin:${
     
     PATH}"
RUN wget https://bootstrap.pypa.io/get-pip.py && \
	python3 get-pip.py --user && \
	rm get-pip.py

# install dependencies
# See https://pytorch.org/ for other options if you use a different version of CUDA
RUN pip install --user tensorboard cmake   # cmake from apt-get is too old
RUN pip install --user torch==1.9 torchvision==0.10 -f https://download.pytorch.org/whl/cu111/torch_stable.html

RUN pip install --user 'git+https://github.com/facebookresearch/fvcore'
# install detectron2
RUN git clone https://github.com/facebookresearch/detectron2 detectron2_repo
# set FORCE_CUDA because during `docker build` cuda is not accessible
ENV FORCE_CUDA="1"
# This will by default build detectron2 for all common cuda architectures and take a lot more time,
# because inside `docker build`, there is no way to tell which architecture will be used.
ARG TORCH_CUDA_ARCH_LIST="Kepler;Kepler+Tesla;Maxwell;Maxwell+Tegra;Pascal;Volta;Turing"
ENV TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST}"

RUN pip install --user -e detectron2_repo

# Set a fixed model cache directory.
ENV FVCORE_CACHE="/tmp"
WORKDIR /home/appuser/detectron2_repo

# run detectron2 under user "appuser":
# wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg
# python3 demo/demo.py  \
	#--config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
	#--input input.jpg --output outputs/ \
	#--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

运行docker创建环境
docker build -t detectron2 .

其中,"."表示当前文件夹。

一些错误

安装torch请认准

安装torch请认准

安装torch请认准

# cuda10.1
pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html

否则,安装到只有cpu版本的torch 会出现 很尴尬的情况。

包括不限于以下错误发生,如:

1. cuda runtime no found
2. nvidia driver is too old
3.
$python
>>> import torch
>>> print(torch.cuda.is_available())
>>> False

重新安装detectron2

如果重装了torch之后,detectron2也需要重新安装。

【官方】detectron2 Installation

cd ~/detectron2_repo
rm -rf ./build/ **/*.so
cd ~
python -m pip install -e detectron2_repo # 时间较长,稍微等一下

其他

torch对应的torchvision版本

torch对应的torchvision版本

查看显卡型号

nvidia-smi -L

docker 命令

从images中启动容器
sudo docker run --name my_docker -v /home/usrname/workspace:/root/workspace -p 2000:22 -itd image_name /bin/bash

docker run 从images建立容器
--name 为容器起一个名字,不然就要用id,有点麻烦
-v 宿主机 到 容器 的目录 映射,即【共享】
-p 宿主机 到 容器 的端口映射
-i 交互
-t 终端
-d 后台
image_name 使用的【映像】名
/bin/bash 使用的【指令】
删除容器
sudo docker rm container_id

container_id 不一定要输入完整,能够唯一区分即可

sudo docker ps -a
可以看到container_id
进入容器(UP状态)

docker exec -it container_name /bin/bash

如何在docker和宿主机之间复制文件

从主机复制到容器sudo docker cp host_path containerID:container_path

从容器复制到主机sudo docker cp containerID:container_path host_path

docker启动容器之后马上又自动关闭了

docker启动容器之后马上又自动关闭了
cp containerID:container_path host_path`

docker启动容器之后马上又自动关闭了

docker启动容器之后马上又自动关闭了
Docker run 命令 【-dit】

猜你喜欢

转载自blog.csdn.net/LittleSeedling/article/details/119792316