跑通mmdetection及測試的py文件

一、本人安裝環境

操作系統:Ubuntu16.04

Python版本:3.6.9

Cuda版本:10

pytorch=1.2

NCCL=2.4.

可參考博客https://blog.csdn.net/hanchan94/article/details/80265922

GCC=5.4.0

sudo apt-get update
sudo apt-get install gcc

二、如何安裝環境

1、創建Python虛擬環境(若有虛擬環境,可忽略)

conda create -n mmdetection python=3.6 -y

source activate mmdetection

2、安裝pytorch

conda install pytorch torchvision -c pytorch

3、安裝Cython

conda install cython

4、安裝mmcv

pip install mmcv

或者使用(注意-e後有一點)

git clone https://github.com/open-mmlab/mmcv.git
cd mmcv
pip install -e .

5、克隆mmdetection(可克隆在自己路徑下)

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
python setup.py install

三、測試

由於我在測試部分加入了輸出爲excel,因此需要安裝pip install xlwt

在mmdetection中創建一個my_code文件夾,在文件夾中添加測試代碼:

# coding=utf-8

from mmdet.apis import init_detector
from mmdet.apis import inference_detector
from mmdet.apis import show_result

# 模型配置文件
config_file = '../configs/pascal_voc/ga_faster_x101_32x4d_fpn_1x.py'

# 預訓練模型文件
checkpoint_file = '../work_dirs/ga_faster_x101_32x4d_fpn_1x/latest.pth'

# 通過模型配置文件與預訓練文件構建模型
model = init_detector(config_file, checkpoint_file, device='cuda:0')

# 測試單張圖片並進行展示
img = 'test1.jpg'
result = inference_detector(model, img)
show_result(img, result, model.CLASSES)

# 測試一個圖像列表並保存結果圖像
imgs = ['1.jpg', '2.jpg', '3.jpg','4.jpg']
for i, result in enumerate(inference_detector(model, imgs)):
    show_result(imgs[i], result, model.CLASSES, wait_time=30, out_file='result_{}.jpg'.format(i))

其中,checkpoint_file文件中存放了我重新訓練好的.pth文件,config_file裏存放的是配置文件,這裏我使用的是GA-RPN,大家可根據自己的情況做相關調整。

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