实例
数据集
笔记本
笔记本

如何在实例中使用 Docker (使用 Docker 安装 Detectron2)
🎉 Featurize 支持 Docker 了!点击右上角的保存至云盘自己试试吧!🎉
笔记本内容
如何在实例中使用 Docker (使用 Docker 安装 Detectron2) #
🎉 Featurize 支持 Docker 了!点击右上角的保存至云盘自己试试吧!🎉 #
!pip install opencv-python matplotlib
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Requirement already satisfied: opencv-python in /environment/python/versions/3.7.12/lib/python3.7/site-packages (4.5.4.58) Requirement already satisfied: matplotlib in /environment/python/versions/3.7.12/lib/python3.7/site-packages (3.5.0) Requirement already satisfied: numpy>=1.14.5 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from opencv-python) (1.21.4) Requirement already satisfied: fonttools>=4.22.0 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (4.28.1) Requirement already satisfied: python-dateutil>=2.7 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (2.8.2) Requirement already satisfied: kiwisolver>=1.0.1 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (1.3.2) Requirement already satisfied: pyparsing>=2.2.1 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (2.4.7) Requirement already satisfied: packaging>=20.0 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (21.2) Requirement already satisfied: cycler>=0.10 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (0.11.0) Requirement already satisfied: pillow>=6.2.0 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (8.4.0) Requirement already satisfied: setuptools-scm>=4 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from matplotlib) (6.3.2) Requirement already satisfied: six>=1.5 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0) Requirement already satisfied: tomli>=1.0.0 in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from setuptools-scm>=4->matplotlib) (1.2.2) Requirement already satisfied: setuptools in /environment/python/versions/3.7.12/lib/python3.7/site-packages (from setuptools-scm>=4->matplotlib) (47.1.0) [33mWARNING: You are using pip version 20.1.1; however, version 21.3.1 is available. You should consider upgrading via the '/environment/python/versions/3.7.12/bin/python3.7 -m pip install --upgrade pip' command.[0m
制作 Detectron2 的 Docker 镜像 #
!git clone https://github.com/facebookresearch/detectron2.git
Cloning into 'detectron2'... remote: Enumerating objects: 13559, done.[K remote: Counting objects: 100% (1060/1060), done.[K remote: Compressing objects: 100% (563/563), done.[K remote: Total 13559 (delta 657), reused 819 (delta 488), pack-reused 12499[K Receiving objects: 100% (13559/13559), 5.80 MiB | 3.53 MiB/s, done. Resolving deltas: 100% (9613/9613), done.
!cd detectron2/docker;docker build -t detectron2 .
Sending build context to Docker daemon 10.24kB Step 1/21 : FROM nvidia/cuda:11.1.1-cudnn8-devel-ubuntu18.04 ---> 8adcfd968b3c Step 2/21 : ENV DEBIAN_FRONTEND noninteractive ---> Using cache ---> 63b8fa145f4e Step 3/21 : RUN apt-get update && apt-get install -y python3-opencv ca-certificates python3-dev git wget sudo ninja-build ---> Using cache ---> 0b7c9b77a9e2 Step 4/21 : RUN ln -sv /usr/bin/python3 /usr/bin/python ---> Using cache ---> 50fe8866582d Step 5/21 : ARG USER_ID=1000 ---> Using cache ---> 9e403f0d924f Step 6/21 : RUN useradd -m --no-log-init --system --uid ${USER_ID} appuser -g sudo ---> Using cache ---> 8d9e00522844 Step 7/21 : RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers ---> Using cache ---> 7850f43709b3 Step 8/21 : USER appuser ---> Using cache ---> 8b27f829e79d Step 9/21 : WORKDIR /home/appuser ---> Using cache ---> 43a7752ba3fc Step 10/21 : ENV PATH="/home/appuser/.local/bin:${PATH}" ---> Using cache ---> 7115d64cb8a0 Step 11/21 : RUN wget https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py --user && rm get-pip.py ---> Using cache ---> 750f1b3cd0c8 Step 12/21 : RUN pip install --user tensorboard cmake # cmake from apt-get is too old ---> Using cache ---> 51cb57d71ad7 Step 13/21 : RUN pip install --user torch==1.10 torchvision==0.11.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html ---> Using cache ---> 695f325472d7 Step 14/21 : RUN pip install --user 'git+https://github.com/facebookresearch/fvcore' ---> Using cache ---> 84511ad8931e Step 15/21 : RUN git clone https://github.com/facebookresearch/detectron2 detectron2_repo ---> Using cache ---> e8728de1fbfa Step 16/21 : ENV FORCE_CUDA="1" ---> Using cache ---> 7c2130142e5e Step 17/21 : ARG TORCH_CUDA_ARCH_LIST="Kepler;Kepler+Tesla;Maxwell;Maxwell+Tegra;Pascal;Volta;Turing" ---> Using cache ---> a02d84cae889 Step 18/21 : ENV TORCH_CUDA_ARCH_LIST="${TORCH_CUDA_ARCH_LIST}" ---> Using cache ---> 3b19dd28aaf7 Step 19/21 : RUN pip install --user -e detectron2_repo ---> Using cache ---> 8a5856dfb4e3 Step 20/21 : ENV FVCORE_CACHE="/tmp" ---> Using cache ---> 8b5f74eb569b Step 21/21 : WORKDIR /home/appuser/detectron2_repo ---> Using cache ---> 66db9d906763 Successfully built 66db9d906763 Successfully tagged detectron2:latest
创建临时目录用于存放输入文件 #
!mkdir /home/featurize/tmp
下载测试图片和视频(可以自己选择) #
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/slomo.mp4 -O /home/featurize/tmp/slomo.mp4
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/input.jpg -O /home/featurize/tmp/input.jpg
--2021-11-20 16:37:16-- https://featurize.oss-cn-chengdu.aliyuncs.com/slomo.mp4 Resolving featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)... 47.108.6.63 Connecting to featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)|47.108.6.63|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 2654268 (2.5M) [video/mp4] Saving to: ‘/home/featurize/tmp/slomo.mp4’ /home/featurize/tmp 100%[===================>] 2.53M --.-KB/s in 0.08s 2021-11-20 16:37:17 (31.6 MB/s) - ‘/home/featurize/tmp/slomo.mp4’ saved [2654268/2654268] --2021-11-20 16:37:17-- https://featurize.oss-cn-chengdu.aliyuncs.com/input.jpg Resolving featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)... 47.108.6.63 Connecting to featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)|47.108.6.63|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 268407 (262K) [image/jpeg] Saving to: ‘/home/featurize/tmp/input.jpg’ /home/featurize/tmp 100%[===================>] 262.12K --.-KB/s in 0.02s 2021-11-20 16:37:17 (11.6 MB/s) - ‘/home/featurize/tmp/input.jpg’ saved [268407/268407]
可视化视频文件 #
from IPython.display import Video
Video("/home/featurize/tmp/slomo.mp4", embed=True)
下载 Detectron2 的预训练模型(当然也可以自己选择) #
!wget https://featurize.oss-cn-chengdu.aliyuncs.com/model_final_cafdb1.pkl -O /home/featurize/tmp/model_final_cafdb1.pkl
--2021-11-20 16:37:07-- https://featurize.oss-cn-chengdu.aliyuncs.com/model_final_cafdb1.pkl Resolving featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)... 47.108.6.63 Connecting to featurize.oss-cn-chengdu.aliyuncs.com (featurize.oss-cn-chengdu.aliyuncs.com)|47.108.6.63|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 260581693 (249M) [application/octet-stream] Saving to: ‘/home/featurize/tmp/model_final_cafdb1.pkl’ /home/featurize/tmp 100%[===================>] 248.51M 67.5MB/s in 3.7s 2021-11-20 16:37:11 (66.8 MB/s) - ‘/home/featurize/tmp/model_final_cafdb1.pkl’ saved [260581693/260581693]
运行 Docker (需要一点推断时间) #
!docker run -it \
--volume="/home/featurize/tmp:/home/appuser/detectron2_repo/tmp" \
--gpus all \
--shm-size=8gb \
--env="DISPLAY" \
detectron2 python demo/demo.py \
--input tmp/input.jpg \
--output tmp/output.jpg \
--config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml \
--opts MODEL.WEIGHTS ./tmp/model_final_cafdb1.pkl
Failed to load OpenCL runtime [32m[11/20 08:37:27 detectron2]: [0mArguments: Namespace(confidence_threshold=0.5, config_file='./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml', input=['tmp/input.jpg'], opts=['MODEL.WEIGHTS', './tmp/model_final_cafdb1.pkl'], output='tmp/output.jpg', video_input=None, webcam=False) [32m[11/20 08:37:32 fvcore.common.checkpoint]: [0m[Checkpointer] Loading from ./tmp/model_final_cafdb1.pkl ... [32m[11/20 08:37:33 fvcore.common.checkpoint]: [0mReading a file from 'Detectron2 Model Zoo' 0%| | 0/1 [00:00<?, ?it/s]/home/appuser/.local/lib/python3.6/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2157.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined] [32m[11/20 08:37:33 detectron2]: [0mtmp/input.jpg: detected 10 instances in 0.37s 100%|#############################################| 1/1 [00:00<00:00, 1.45it/s]
看看 Docker 运行结果 #
import cv2
import matplotlib.pyplot as plt
f, axs = plt.subplots(1,2,figsize=(20,20))
axs[0].imshow(cv2.cvtColor(cv2.imread('/home/featurize/tmp/input.jpg'), cv2.COLOR_BGR2RGB));
axs[1].imshow(cv2.cvtColor(cv2.imread('/home/featurize/tmp/output.jpg'), cv2.COLOR_BGR2RGB));
将输入替换成视频 #
!docker run -it \
--volume="/home/featurize/tmp:/home/appuser/detectron2_repo/tmp" \
--gpus all \
--shm-size=8gb \
--env="DISPLAY" \
detectron2 python demo/demo.py \
--video-input tmp/slomo.mp4 \
--output tmp/output.mp4 \
--config-file ./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml \
--opts MODEL.WEIGHTS ./tmp/model_final_cafdb1.pkl
Failed to load OpenCL runtime [32m[11/20 08:11:36 detectron2]: [0mArguments: Namespace(confidence_threshold=0.5, config_file='./configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml', input=None, opts=['MODEL.WEIGHTS', './tmp/model_final_cafdb1.pkl'], output='tmp/output.mp4', video_input='tmp/slomo.mp4', webcam=False) [32m[11/20 08:11:41 fvcore.common.checkpoint]: [0m[Checkpointer] Loading from ./tmp/model_final_cafdb1.pkl ... [32m[11/20 08:11:41 fvcore.common.checkpoint]: [0mReading a file from 'Detectron2 Model Zoo' OpenCV: FFMPEG: tag 0x34363278/'x264' is not supported with codec id 28 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: fallback to use tag 0x31637661/'avc1' 0%| | 0/342 [00:00<?, ?it/s]/home/appuser/.local/lib/python3.6/site-packages/torch/functional.py:445: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2157.) return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined] 100%|#########################################| 342/342 [03:12<00:00, 1.77it/s]
from IPython.display import Video
Video("/home/featurize/tmp/output.mp4", embed=True)
评论(0条)