Mask RCNN目標檢測源碼運行筆記(Demo模式)

1. 論文基本信息




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

運行時,源碼會先後顯示原圖和檢測/分割後的圖,只有關閉了第一個窗口,程序纔會繼續運行,如下圖所示:
在這裏插入圖片描述
在這裏插入圖片描述
注: 如果上述圖像的url難以訪問,網速緩慢,也可以自己換一個圖片鏈接,比如:https://inews.gtimg.com/newsapp_ls/0/10947814918_580328/0,運行的效果如下圖所示:

在這裏插入圖片描述





參考資料:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章