open-mmlab/mmdetection

problem about the test script

kellenf opened this issue · 11 comments

I can not use your test script with my own image.
the problem is:
labels_shape (2, 80) result_shape (2, 80) bboxes.shape[0] 2 labels.shape[0] 160 Traceback (most recent call last): File "tools/test_images.py", line 17, in <module> show_result(img, result,filepath='/mnt/lustre/fangkairen/mmdetection') File "/mnt/lustre/fangkairen/.local/lib/python3.6/site-packages/mmdet-0.5.4+f509f8e-py3.6.egg/mmdet/apis/inference.py", line 69, in show_result out_file=filepath) File "/mnt/lustre/fangkairen/.local/lib/python3.6/site-packages/mmcv/visualization/image.py", line 102, in imshow_det_bboxes assert bboxes.shape[0] == labels.shape[0]
and the scipt I used is:

import mmcv
from mmcv.runner import load_checkpoint
from mmdet.models import build_detector
from mmdet.apis import inference_detector, show_result
import cv2 
import numpy as np
cfg = mmcv.Config.fromfile('configs/mask_rcnn_r50_fpn_1x.py')
cfg.model.pretrained = None

# construct the model and load checkpoint
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
_ = load_checkpoint(model, 'work_dirs/kellen/epoch_12.pth')

# test a single image
img = mmcv.imread('test_images/test.jpg')
result = inference_detector(model, img, cfg)
show_result(img, result,filepath='/mnt/lustre/fangkairen/mmdetection')

and my model is good because i use tool/test.py to test my model in test datasets

You can check whether result is valid, and also see these two lines.

Thanks for your reply,I have check my problem and debug and I know that the test code is useful for faster-rcnn eg to detect the box but not for mask-rcnn.So can you provide a script for test single image with mask?I am working for that ,if I succeed ,I will contribut my code.

the script is usful for test mask-rcnn,the code was changed from another issue which I can not find now.Hope it can help other people...

#encoding:utf-8
import mmcv
from mmcv.runner import load_checkpoint

from mmdet.models import build_detector
from mmdet.apis import inference_detector, show_result
import numpy as np, pycocotools.mask as maskUtils, mmcv
from mmdet.core import tensor2imgs, get_classes
from glob import glob
import os
config_path = 'configs/mask_rcnn_r50_fpn_1x.py'
model_path = 'your .pth file '
img_list = glob('test_images/*.jpg')
img_save= 'result_images'
cfg = mmcv.Config.fromfile(config_path)
cfg.model.pretrained = None

img_norm_cfg = dict(
    mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)

def show_mask_result(img,
                    result,
                    dataset='coco',
                    score_thr=0.7,
                    with_mask=True):
        segm_result=None
        if with_mask:
            bbox_result, segm_result = result
        else:
            bbox_result=result
        if isinstance(dataset, str):#  add own data label to mmdet.core.class_name.py
            class_names = get_classes(dataset)
            # print(class_names)
        elif isinstance(dataset, list):
            class_names = dataset
        else:
            raise TypeError('dataset must be a valid dataset name or a list'
                            ' of class names, not {}'.format(type(dataset)))
        h, w, _ = img.shape
        img_show = img[:h, :w, :]
        labels = [
            np.full(bbox.shape[0], i, dtype=np.int32)
            for i, bbox in enumerate(bbox_result)
        ]
        labels = np.concatenate(labels)
        bboxes = np.vstack(bbox_result)
        if with_mask:
            segms = mmcv.concat_list(segm_result)
            inds = np.where(bboxes[:, -1] > score_thr)[0]
            for i in inds:
                color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)
                mask = maskUtils.decode(segms[i]).astype(np.bool)
                img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5
        result_img=mmcv.imshow_det_bboxes(
            img_show,
            bboxes,
            labels,
            class_names=class_names,
            score_thr=score_thr)
        return  result_img

# construct the model and load checkpoint
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
_ = load_checkpoint(model, model_path)
for img in img_list:
    img_name=os.path.basename(img)
    new_path=os.path.join(img_save,img_name)
    result=inference_detector(model, img, cfg, device='cuda:0')
    answer=show_mask_result(mmcv.imread(img), result,score_thr=0.6,with_mask=True)
    mmcv.imwrite(answer,new_path)

Just to remind that the above script needs some modifications when using the latest version of mmdetection and pytorch.

# -*- coding: utf-8 -*-
import os
import numpy as np
import mmcv
import torch
from mmcv.runner import load_checkpoint
from mmdet.models import build_detector
from mmdet.apis import inference_detector
from mmdet.core import get_classes
import pycocotools.mask as maskutils


def show_mask_result(img, result, save_img, dataset='coco', score_thr=0.7, with_mask=True):
    segm_result = None
    if with_mask:
        bbox_result, segm_result = result
    else:
        bbox_result = result
    if isinstance(dataset, str):  # add own data label to mmdet.core.class_name.py
        class_names = get_classes(dataset)
        # print(class_names)
    elif isinstance(dataset, list):
        class_names = dataset
    else:
        raise TypeError('dataset must be a valid dataset name or a list'
                        ' of class names, not {}'.format(type(dataset)))
    h, w, _ = img.shape
    img_show = img[:h, :w, :]
    labels = [
        np.full(bbox.shape[0], i, dtype=np.int32)
        for i, bbox in enumerate(bbox_result)
    ]
    labels = np.concatenate(labels)
    bboxes = np.vstack(bbox_result)
    if with_mask:
        segms = mmcv.concat_list(segm_result)
        inds = np.where(bboxes[:, -1] > score_thr)[0]
        for i in inds:
            color_mask = np.random.randint(0, 256, (1, 3), dtype=np.uint8)
            mask = maskutils.decode(segms[i]).astype(np.bool)
            img_show[mask] = img_show[mask] * 0.5 + color_mask * 0.5
    result_img = mmcv.imshow_det_bboxes(img_show, bboxes, labels, class_names=class_names,
                                        score_thr=score_thr, show=False, out_file=save_img)
    return result_img


config_path = config file path
model_path = model file path

cfg = mmcv.Config.fromfile(config_path)
cfg.model.pretrained = None
cfg.data.test.test_mode = True

# construct the model and load checkpoint
model = build_detector(cfg.model, test_cfg=cfg.test_cfg)
model.cfg = cfg
device = torch.device("cuda:0")
model = model.to(device)
model.eval()
_ = load_checkpoint(model, model_path)
mask_score_thresh = 0.6

img = []
img_folder = your image folder
for single_img in os.listdir(img_folder):
    img.append(os.path.join(img_folder, single_img))

img_result = inference_detector(model, img)
for single_img in img:
    img = mmcv.imread(single_img)
    result = next(img_result)
    answer = show_mask_result(img, result, os.path.basename(single_img),
                              score_thr=mask_score_thresh, with_mask=True)

The official demo has supported visualizing bbox/mask results for a long time...

@hellock Thanks for your reply. I did not notice the official demo..

Never mind. This repo is developed rapidly.

Yeah, hope to see more networks and papers in this repo.

@hellock how to provide filter class names to get a particular class instance in the output image similar to a mask rcnn?

I also have this question and I used rpn_r50_fpn_1x.py

I am not able to run the videoextractor.py script using newer version of mmcv as it does not contain any module named 'runner', even though if forced install the 1.7.1 version of mmcv, still the script won't run as there is no module available in mmdet in mmdetection with name build_detector. Kindly check and update the code.