mmdetection 用自定義 coco 數(shù)據(jù)集做目標(biāo)檢測

1.1 mmdetction 安裝

1.1.1 系統(tǒng)環(huán)境需求

參考 mmdetection 官方文檔 https://mmdetection.readthedocs.io/en/latest/INSTALL.html,系統(tǒng)環(huán)境需求如下:

  • Linux (Windows is not officially supported)
  • Python 3.5+
  • PyTorch 1.1 or higher
  • CUDA 9.0 or higher
  • NCCL 2
  • GCC 4.9 or higher
  • mmcv

我的系統(tǒng)環(huán)境:

  • CentOS 7.2
  • Python 3.7
  • PyTorch 1.1.0
  • CUDA 10.0
  • NCCL 2
  • GCC 7.4.0
  • mmcv

安裝 mmcv 時(shí)的依賴項(xiàng)如下:

addict
numpy
pyyaml
six

其中,我的環(huán)境中缺少 addict 和 pyyaml,從 https://pypi.org/ 中下載源碼離線安裝。

附:查看深度學(xué)習(xí)軟件/庫/工具的命令速查表:

https://tech.amikelive.com/node-841/command-cheatsheet-checking-versions-of-installed-software-libraries-tools-for-deep-learning-on-ubuntu-16-04/

1.1.2 安裝 mmdetection

官方文檔的安裝說明如下,適合網(wǎng)絡(luò)環(huán)境好的條件下進(jìn)行在線安裝,

# Clone the mmdetection repository
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

# Install build requirements and then install mmdetection.
pip install -r requirements/build.txt
pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI"
pip install -v -e .  # or "python setup.py develop"

這里采用離線安裝方式,
(1)檢查 mmdetection 的依賴項(xiàng),滿足要求。

cat requirements/build.txt
# These must be installed before building mmdetection
numpy
torch>=1.1

(2)安裝 cocoapi,

git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py install

(3)安裝 mmdetection,依賴項(xiàng)檢查結(jié)果,terminaltables 沒有,采用源碼離線安裝。

安裝完成以后,打開 Python 進(jìn)行驗(yàn)證,

from mmdet.apis import init_detector, inference_detector, show_result

1.2 訓(xùn)練自定義數(shù)據(jù)集 CatDog

1.2.1 準(zhǔn)備數(shù)據(jù)集

創(chuàng)建 data 文件夾,軟鏈接到數(shù)據(jù)集根目錄,其中標(biāo)記好的數(shù)據(jù)集采用 coco 數(shù)據(jù)格式,

cd mmdetection
mkdir data
export COCO_ROOT=/data1/Projects/datasets/coco
ln -s $COCO_ROOT data

mmdet/datasets/cat_dog.py:

from .coco import CocoDataset
from .registry import DATASETS

@DATASETS.register_module
class CatDog(CocoDataset):
    CLASSES = ('dog', 'cat')

mmdet/datasets/__init__.py:

from .cat_dog import CatDog

1.2.2 修改 faster_rcnn 模型配置

下載 resnet50 的預(yù)訓(xùn)練模型,放入 $TORCH_HOME

export TORCH_HOME=/data1/Projects/pretrained_models
echo $TORCH_HOME
mkdir -p /data1/Projects/pretrained_models/checkpoints/
mv resnet50-19c8e357.pth /data1/Projects/pretrained_models/checkpoints/

用 faster_rcnn 做為模型進(jìn)行目標(biāo)檢測,拷貝一份 configs/faster_rcnn_r50_fpn_1x.pyconfigs/cat_dog_faster_rcnn_r50_fpn_1x.py,在 cat_dog_faster_rcnn_r50_fpn_1x.py,加入預(yù)訓(xùn)練的 resnet50 加載路徑,

# model settings
import os

os.environ['TORCH_HOME'] = '/data1/Projects/pretrained_models'

在 config 文件 configs/cat_dog_faster_rcnn_r50_fpn_1x.py 使用 CatDog 數(shù)據(jù)集,分類數(shù) 3 包括狗,貓和背景,

num_classes=3,

CatDog 數(shù)據(jù)集為類 coco 數(shù)據(jù)集,針對 coco 數(shù)據(jù)集修改:

  • 定義數(shù)據(jù)種類,修改 mmdetection/mmdet/datasets/coco.py,把 CLASSES 的 tuple 改為自己數(shù)據(jù)集對應(yīng)的種類。
CLASSES = ('dog', 'cat')
  • mmdetection/mmdet/core/evaluation/class_names.py 修改 coco_classes 數(shù)據(jù)集類別,這個(gè)關(guān)系到后面 test 的時(shí)候結(jié)果圖中顯示的類別名稱。
def coco_classes():
    return [ 'dog', 'cat']

1.2.3 訓(xùn)練模型

使用單個(gè) GPU 進(jìn)行訓(xùn)練,指定 --work_dir 保存模型結(jié)果,

python tools/train.py configs/cat_dog_faster_rcnn_r50_fpn_1x.py \
--gpus 1 \
--work_dir './work_dirs/cat_dog_faster_rcnn_r50_fpn_1x'

1.2.4 測試圖片

1.2.4.1 測試單張圖片

import numpy as np
from mmdet.apis import init_detector, inference_detector
import mmcv
import cv2

threshold = 0.9  # confidence score
config_file = './configs/cat_dog_faster_rcnn_r50_fpn_1x.py'
checkpoint_file = './work_dirs/cat_dog_faster_rcnn_r50_fpn_1x/latest.pth'
# 通過配置文件(config file)和模型文件(checkpoint file)構(gòu)建檢測模型
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# 測試單張圖片并展示結(jié)果
img_path = '/data1/Projects/datasets/cat_dog_single/cat.12176.jpg'
result = inference_detector(model, img_path)
bboxes = np.vstack(result)
# print(bboxes.shape)
labels = [
    np.full(bbox.shape[0], i, dtype=np.int32)
    for i, bbox in enumerate(result)
]
labels = np.concatenate(labels)
# print(labels)
img = cv2.imread(img_path)
scores = bboxes[:, -1]
inds = scores > threshold
bboxes = bboxes[inds, :]
# print(bboxes.shape)
labels = labels[inds]
# print(labels)
class_names = model.CLASSES
cat_dog_dict = {}
for label in labels:
    cat_dog_dict[class_names[label]] = cat_dog_dict.get(class_names[label], 0) + 1
print(cat_dog_dict)
for k, v in cat_dog_dict.items():
    print('{0} 有 {1} 只 {2}'.format(img_path, v, k))
for bbox in bboxes:
    left_top = (bbox[0], bbox[1])
    right_bottom = (bbox[2], bbox[3])
    cv2.rectangle(img, left_top, right_bottom, color=(0, 255, 0))
cv2.imwrite('/data1/Projects/datasets/cat_dog_single/res_cat.12176.jpg', img)

1.2.4.2 測試多張圖片

from mmdet.apis import init_detector, inference_detector, show_result
import mmcv
import numpy as np
import glob
import os

config_file = 'configs/cat_dog_faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'work_dirs/cat_dog_faster_rcnn_r50_fpn_1x/latest.pth'
score_thr = 0.9

# 通過配置文件(config file)和模型文件(checkpoint file)構(gòu)建檢測模型
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# 測試單張圖片
# img = '/data1/Projects/datasets/cat_dog_single/cat.12176.jpg'
# result = inference_detector(model, img)
# show_result(img, result, model.CLASSES, score_thr=score_thr,
#             out_file='/data1/Projects/datasets/cat_dog_single/res_cat.12176.jpg')

# 測試多張圖片

# imgs = glob.glob('/data1/Projects/datasets/coco/val2017/*.jpg')
imgs = glob.glob('/data1/Projects/datasets/test/*.jpg')
for i, img in enumerate(imgs):
    # 畫 bounding boxes 到圖片上
    # print(i, imgs[i])
    result = inference_detector(model, img)
    file_name = imgs[i].split('/')[-1]
    out_file = os.path.join('/data1/Projects/datasets/test_det', file_name)
    show_result(img, result, model.CLASSES, score_thr=score_thr, out_file=out_file)

    # 輸出圖片的貓和狗的數(shù)量
    img = mmcv.imread(img)
    img = img.copy()
    bbox_result = result
    bboxes = np.vstack(bbox_result)
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)

    # 根據(jù)閾值調(diào)整輸出的 bboxes 和 labels
    scores = bboxes[:, -1]
    inds = scores > score_thr
    bboxes = bboxes[inds, :]
    labels = labels[inds]

    class_names = model.CLASSES
    cat_dog_dict = {}
    for label in labels:
        cat_dog_dict[class_names[label]] = cat_dog_dict.get(class_names[label], 0) + 1
    for k, v in cat_dog_dict.items():
        print('{0} 有 {1} 只 {2}'.format(imgs[i].split('/')[-1], v, k))
    print('--------------------')

測試多張圖片,輸出結(jié)果如下:

mmdetection測試多張圖片輸出結(jié)果.png
bounding box.png

微信公眾號「padluo」,分享數(shù)據(jù)科學(xué)家的自我修養(yǎng),既然遇見,不如一起成長。

數(shù)據(jù)分析二維碼.gif

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,527評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,687評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,640評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,957評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,682評論 6 413
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,011評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,009評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,183評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,714評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,435評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,665評論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,148評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,838評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,251評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,588評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,379評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,627評論 2 380

推薦閱讀更多精彩內(nèi)容