天天看點

Mask RCNN目标檢測源碼運作筆記(Demo模式)1. 論文基本資訊2. 運作環境3. 準備4. Demo

1. 論文基本資訊

  • 論文标題:Mask R-CNN
  • 論文作者:Kaiming He(Facebook AI Research (FAIR))等人
  • 論文出處:ICCV 2017
  • 線上閱讀:https://arxiv.org/pdf/1703.06870.pdf
  • 源碼連結:https://github.com/facebookresearch/maskrcnn-benchmark

2. 運作環境

  • NVIDIA GTX 1070
  • Ubuntu 18.04 x64
  • gcc/g++ 7.4.0
  • CUDA 10.0 (cuda_10.0.130_410.48_linux.run)
  • Anaconda 3
  • Python 3.7
  • PyTorch 1.3.0 for CUDA 10.0

隻需要預先安裝好gcc/g++ 7.4.0、CUDA 10.0和Anaconda即可,其他依賴包(包括PyTorch)會在下一節會的詳細環境搭建步驟進行安裝。

3. 準備

S1. 運作如下指令安裝matplotlib後端:

sudo apt-get install tcl-dev tk-dev python-tk python3-tk
           

S2. 在自己的工作區目錄中,運作如下指令下載下傳源碼包,也可以手工複制連結在浏覽器下載下傳:

git clone https://github.com/facebookresearch/maskrcnn-benchmark.git
           

S3. cd到

maskrcnn-benchmark

目錄中,然後運作如下指令建立虛拟環境并激活:

conda create --name maskrcnn_benchmark python=3.7
conda activate maskrcnn_benchmark
           

S4. 繼續保持在

maskrcnn-benchmark

目錄和剛剛激活的虛拟環境中,然後運作如下指令編寫簡易安裝腳本:

gedit install.sh
           

在打開的gedit空白編輯界面中,粘貼如下文本:

# this installs the right pip and dependencies for the fresh python
conda install ipython pip

# maskrcnn_benchmark and coco api dependencies
pip install ninja yacs cython matplotlib tqdm opencv-python requests

# follow PyTorch installation in https://pytorch.org/get-started/locally/
# we give the instructions for CUDA 10.0
conda install -c pytorch pytorch-nightly torchvision cudatoolkit=10.0

export INSTALL_DIR=$PWD

# install pycocotools
cd $INSTALL_DIR
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py build_ext install

# install cityscapesScripts
cd $INSTALL_DIR
git clone https://github.com/mcordts/cityscapesScripts.git
cd cityscapesScripts/
python setup.py build_ext install

# install apex
cd $INSTALL_DIR
git clone https://github.com/NVIDIA/apex.git
cd apex
python setup.py install --cuda_ext --cpp_ext

# install PyTorch Detection
cd $INSTALL_DIR
git clone https://github.com/facebookresearch/maskrcnn-benchmark.git
cd maskrcnn-benchmark

# the following will install the lib with
# symbolic links, so that you can modify
# the files if you want and won't need to
# re-build it
python setup.py build develop



unset INSTALL_DIR

# or if you are on macOS
# MACOSX_DEPLOYMENT_TARGET=10.9 CC=clang CXX=clang++ python setup.py build develop
           

粘貼完成後,儲存并關閉gedit,回到終端。

S5. 在終端中運作:

chmod 777 install.sh
./install.sh
           

在執行install.sh的過程中,中間會詢問是否繼續,輸入

y

即可繼續安裝。

注 1: 若install過程中出現網絡問題導緻中斷,可以重新執行

./install.sh

注 2: 若install過程中提示形如

socket.timeout: The read operation timed out

的錯誤,多半是網絡不好導緻某個檔案沒有下載下傳下來,這時直接copy連結手工下載下傳安裝相應的包即可。

4. Demo

S1. 在終端中cd到源碼内部的

demo

目錄。

S2. 運作如下指令建立一個示例源碼:

gedit demo2.py
           

在文本編輯器中粘貼如下内容:

import matplotlib
matplotlib.use('TkAgg')

import matplotlib.pyplot as plt
import matplotlib.pylab as pylab

import requests
from io import BytesIO
from PIL import Image
import numpy as np

# this makes our figures bigger
pylab.rcParams['figure.figsize'] = 20, 12

from maskrcnn_benchmark.config import cfg
from predictor import COCODemo

config_file = "../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"

# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
# cpu or cuda
cfg.merge_from_list(["MODEL.DEVICE", "cuda"])

coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)

def load(url):
    """
    Given an url of an image, downloads the image and
    returns a PIL image
    """
    response = requests.get(url)
    pil_image = Image.open(BytesIO(response.content)).convert("RGB")
    # convert to BGR format
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image

def imshow(img):
    plt.imshow(img[:, :, [2, 1, 0]])
    plt.axis("off")

# from http://cocodataset.org/#explore?id=345434
image = load("https://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")
image2 = image[:, :, [2, 1, 0]]
plt.imshow(image2)
plt.show()

# compute predictions
predictions = coco_demo.run_on_opencv_image(image)
imshow(predictions)
plt.show()
           

S3. 粘貼完畢,關閉gedit,回到終端。

S4. 在終端中運作:

python demo2.py
           

運作時,源碼會先後顯示原圖和檢測/分割後的圖,隻有關閉了第一個視窗,程式才會繼續運作,如下圖所示:

Mask RCNN目标檢測源碼運作筆記(Demo模式)1. 論文基本資訊2. 運作環境3. 準備4. Demo
Mask RCNN目标檢測源碼運作筆記(Demo模式)1. 論文基本資訊2. 運作環境3. 準備4. Demo

注: 如果上述圖像的url難以通路,網速緩慢,也可以自己換一個圖檔連結,比如:https://inews.gtimg.com/newsapp_ls/0/10947814918_580328/0,運作的效果如下圖所示:

Mask RCNN目标檢測源碼運作筆記(Demo模式)1. 論文基本資訊2. 運作環境3. 準備4. Demo

參考資料:

https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/INSTALL.md