生活随笔
收集整理的這篇文章主要介紹了
使用mmdetection检测并存储结果
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用mmdetection檢測(cè)并存儲(chǔ)結(jié)果
- 使用voc格式樣本訓(xùn)練
- 檢測(cè)并保存結(jié)果(添加nms)
使用voc格式樣本訓(xùn)練
修改mmdet/datasets/voc.py文件中類別,注意格式別出錯(cuò)
CLASSES
= ('bird-nest',)
然后修改配置文件,其中img_scale是你輸入圖片的寬高,num_classes=數(shù)據(jù)集類別數(shù)+1(背景),其他超參按照官網(wǎng)教程修改即可。
如果要做精度評(píng)價(jià)的話,要記得修改mmdet/core/evaluation/class_names.py中的類別跟數(shù)據(jù)集的類別一致。
檢測(cè)并保存結(jié)果(添加nms)
由于我在超算上運(yùn)行,所以沒有做結(jié)果顯示,只是保存為了jpg
在根目錄下新建一個(gè)demo.py文件,并運(yùn)行。
import mmcv
import os
import numpy
as np
from mmcv
.runner
import load_checkpoint
from mmdet
.models
import build_detector
from mmdet
.apis
import init_detector
, inference_detector
, show_resultmodel
= init_detector
('configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc0712.py', 'work_dirs/faster_rcnn_r50_fpn_1x_voc0712/epoch_8.pth', device
='cuda:0')input_dir
= 'data/VOCdevkit/VOC2007/JPEGImages/'
out_dir
= 'results/'if not os
.path
.exists
(out_dir
):os
.mkdir
(out_dir
)def py_cpu_nms(dets
, thresh
):"""Pure Python NMS baseline."""dets
= np
.array
(dets
)x1
= dets
[:, 0]y1
= dets
[:, 1]x2
= dets
[:, 2]y2
= dets
[:, 3]scores
= dets
[:, 4]areas
= (x2
- x1
+ 1) * (y2
- y1
+ 1)order
= scores
.argsort
()[::-1]keep
= []while order
.size
> 0:i
= order
[0]keep
.append
(i
)xx1
= np
.maximum
(x1
[i
], x1
[order
[1:]])yy1
= np
.maximum
(y1
[i
], y1
[order
[1:]])xx2
= np
.minimum
(x2
[i
], x2
[order
[1:]])yy2
= np
.minimum
(y2
[i
], y2
[order
[1:]])w
= np
.maximum
(0.0, xx2
- xx1
+ 1)h
= np
.maximum
(0.0, yy2
- yy1
+ 1)inter
= w
* hovr
= inter
/ (areas
[i
] + areas
[order
[1:]] - inter
+ 0.000000000001)inds
= np
.where
(ovr
<= thresh
)[0]order
= order
[inds
+ 1]return keep
def nms_cpu(total_detections
, classnames
, thresh
=0.5):for i
in range(len(classnames
)):keep
= py_cpu_nms
(total_detections
[i
], thresh
)total_detections
[i
] = total_detections
[i
][keep
]return total_detectionsfiles
= os
.listdir
(input_dir
)
if len(files
) != 0:for file in files
:name
= os
.path
.splitext
(file)[0]print ('detecting: ' + name
)img
= mmcv
.imread
(os
.path
.join
(input_dir
, file))img_resize
= mmcv
.imresize
(img
, (2000, 1500))result
= inference_detector
(model
, img_resize
)result
= nms_cpu
(result
, model
.CLASSES
, 0.1)result_img
= draw_result
(img_resize
, result
, model
.CLASSES
, score_thr
=0.3, out_file
=os
.path
.join
(out_dir
, name
+ '.jpg'))
總結(jié)
以上是生活随笔為你收集整理的使用mmdetection检测并存储结果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。