mmdetection # test and visualize test result

  1. evaluation,这里没有保存结果,但是可以show一下。
tools/dist_test.sh configs/swin/cascade_mask_rcnn_swin_base_patch4_window7_mstrain_480-800_giou_4conv1f_adamw_3x_coco.py latest.pth 1 --eval bbox segm
  1. save result
    refer to demo/image_demo.py

(1) edit mmdet/apis/inference.py 在函数show_result_pyplot中,加上"model.show_result(img, result, out_file=out_file)" 保存测试结果。

def show_result_pyplot(model,
                       img,
                       result,
                       out_file,
                       score_thr=0.3,
                       title='result',
                       wait_time=0):
    #Visualize the detection results on the image.
    if hasattr(model, 'module'):
        model = model.module
    
    model.show_result(
        img,
        result,
        score_thr=score_thr,
        show=True,
        wait_time=wait_time,
        win_name=title,
        bbox_color=(72, 101, 241),
        text_color=(72, 101, 241))

    model.show_result(img, result, out_file=out_file)

(2) test and save test result as image

run image_demo.py:

import os
from argparse import ArgumentParser
from mmdet.apis import inference_detector, init_detector, show_result_pyplot

def main():
    
    checkpointfile = './work_dirs/cascade_mask_rcnn_swin_base_patch4_window7_mstrain_480-800_giou_4conv1f_adamw_3x_coco/latest.pth'  
    configfile = './configs/swin/cascade_mask_rcnn_swin_base_patch4_window7_mstrain_480-800_giou_4conv1f_adamw_3x_coco.py'
    score_thr = 0.3

    # build the model from a config file and a checkpoint file
    model = init_detector(configfile, checkpointfile, device='cuda:0')
    

    imgfolder = './data/coco/test2017/JPEGImages/'
    savefolder = './result/'

    for imgfile in os.listdir(imgfolder):
        img = imgfolder + imgfile
        out_file = savefolder + 're_' + imgfile

        # test a single image
        result = inference_detector(model, img)
        # show the results
        show_result_pyplot(model, img, result, out_file, score_thr = score_thr)
        
if __name__ == '__main__':
    main()

(3) 修改bbox和label格式,mmdet/core/visualization/image.py

你可能感兴趣的:(python,deep,learning,人工智能,深度学习,python)