MMDetection 安装

一、MMDetection

下载:地址

 

环境:

  • anaconda3
  • cuda10.1 (10.0应该也可以)
  • python3.6
  • torch >= 1.3
  • opencv 4.1

 

二、MMCV 安装

1)两种版本:

  • mmcv:精简版,没有CUDA操作,但具有所有其他功能,类似于mmcv <1.0.0。当您不需要那些CUDA操作时,它很有用。
  • mmcv-full:全面,具有完整功能和各种CUDA操作。构建需要更长的时间。

 

2)版本选择:

 

3)文档:地址

 

4)安装(我是torch 1.6、MMDetection 2.7 ):

pip install mmcv-full===1.1.5 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.6.0/index.html

# pip install mmcv-full==={mmcv_version} -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.6.0/index.html

# pip install mmcv-full==latest+torch1.6.0+cu101 -f https://download.openmmlab.com/mmcv/dist/index.html

#其他方式

# pip install mmcv
# pip install mmcv-full

PS:mmcv 与 mmcv-full 不能同时存在

pip uninstall mmcv

 

5)编译方式安装(若上面方式都不行):

git clone https://github.com/open-mmlab/mmcv.git
cd mmcv
MMCV_WITH_OPS=1 pip install -e .  # package mmcv-full will be installed after this step

 

6)针对 win10 用户:

  •  确保 cl 有效

C:\Users\yuki>cl
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.00.24245 版
版权所有(C) Microsoft Corporation。保留所有权利。

用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]
  • 若不能调用 环境变量添加:

添加内容(按实际版本):C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.23.28105\bin\Hostx86\x64

 

 

三、安装 Cpython

pip install cython # conda install cython

 

四、安装 MMDetection

git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection

pip install -r requirements/build.txt
pip install -v -e .  # python setup.py develop


# pip install -v -e . -i https://mirrors.aliyun.com/pypi/simple

ps:按自己需要选择版本

ps:若win10的话,管理员 cmd 打开窗口进行操作

 

五、测试

from argparse import ArgumentParser

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

def show_result_ins(img,
                    result,
                    class_names,
                    score_thr=0.3,
                    sort_by_density=False,
                    out_file=None):

    assert isinstance(class_names, (tuple, list))
    img = mmcv.imread(img)
    img_show = img.copy()
    h, w, _ = img.shape

    cur_result = result[0]
    seg_label = cur_result[0]
    seg_label = seg_label.cpu().numpy().astype(np.uint8)
    cate_label = cur_result[1]
    cate_label = cate_label.cpu().numpy()
    score = cur_result[2].cpu().numpy()

    vis_inds = score > score_thr
    seg_label = seg_label[vis_inds]
    num_mask = seg_label.shape[0]
    cate_label = cate_label[vis_inds]
    cate_score = score[vis_inds]

    if sort_by_density:
        mask_density = []
        for idx in range(num_mask):
            cur_mask = seg_label[idx, :, :]
            cur_mask = mmcv.imresize(cur_mask, (w, h))
            cur_mask = (cur_mask > 0.5).astype(np.int32)
            mask_density.append(cur_mask.sum())
        orders = np.argsort(mask_density)
        seg_label = seg_label[orders]
        cate_label = cate_label[orders]
        cate_score = cate_score[orders]

    np.random.seed(42)
    color_masks = [
        np.random.randint(0, 256, (1, 3), dtype=np.uint8)
        for _ in range(num_mask)
    ]
    for idx in range(num_mask):
        idx = -(idx + 1)
        cur_mask = seg_label[idx, :, :]
        cur_mask = mmcv.imresize(cur_mask, (w, h))
        cur_mask = (cur_mask > 0.5).astype(np.uint8)
        if cur_mask.sum() == 0:
            continue
        color_mask = color_masks[idx]
        cur_mask_bool = cur_mask.astype(np.bool)
        img_show[cur_mask_bool] = img[cur_mask_bool] * 0.5 + color_mask * 0.5

        cur_cate = cate_label[idx]
        cur_score = cate_score[idx]
        label_text = class_names[cur_cate]
        # label_text += '|{:.02f}'.format(cur_score)
        center_y, center_x = ndimage.measurements.center_of_mass(cur_mask)
        vis_pos = (max(int(center_x) - 10, 0), int(center_y))
        cv2.putText(img_show, label_text, vis_pos,
                    cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255))  # green
    if out_file is None:
        return img
    else:
        mmcv.imwrite(img_show, out_file)

def main():
    parser = ArgumentParser()
    parser.add_argument('--img', help='Image file',default='demo.jpg')
    parser.add_argument('--config', help='Config file',default='../configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py')
    parser.add_argument('--checkpoint', help='Checkpoint file',default='../checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--score-thr', type=float, default=0.3, help='bbox score threshold')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)
    # test a single image
    result = inference_detector(model, args.img)
    # show the results
    show_result_pyplot(model, args.img, result, score_thr=args.score_thr)
    # show_result_ins(args.img, result, model.CLASSES, score_thr=0.25, out_file="demo_out.jpg")

if __name__ == '__main__':
    main()

 

六、已测试系统

  • win10
  • ubuntu18.04
  • Jetpack4.4
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页