mmDetection源码分析(一):inference阶段代码调用

mmdetection介绍

mmDetection(mmdetection)应该是目前最流行的检测网络框架了,由香港中文大学与商汤合作维护的一个检测工具箱,目前仍然在不断的更新中。

本系列打算研究学习下如何使用mmDetection去复现其他论文的检测网络模型或者构建自己的网络模型,以及如何使用mmDetection训练自己的数据。网上已有不少mmDetection源码分析博客,大家也都是按照各自学习的节奏来排版的,这里记录下学习的过程。

照例将学习的链接贴出,尊重原创~

DateLoader学习链接:

AI之路-PyTorch源码解读之torch.utils.data.DataLoader
一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系
pytorch 函数DataLoade

推理阶段—demo.py(通过读取一张图像,显示效果)

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

config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth'

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

# test a single image and show the results
img = 'test.jpg'  # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
show_result(img, result, model.CLASSES)
# or save the visualization results to image files
show_result(img, result, model.CLASSES, out_file='result.jpg')

init_detector

init_detector三个输入参数:

  • config:检测模型的配置文件,一般位于 mmdetection/config/ 中
  • checkpoint:即训练好的权重 在mmdetection的model zoo中可以下载
  • device:分配到的设备对象

返回:model(检测模型)

inference_detector

inference_detector 输入两个参数:

  • model: init_detector返回的model
  • imgs: 图像的路径

返回:测试图像的结果

_ inference_single

将输入的图像进过resize后,输入到模型中。

  • _prepare_data

    用于将图像转换成config中test.img_scale

_ inference_generator

show_result

show_result参数:

  • img :图像的路径(字符串)
  • result:inference_detector的返回值
  • class_name:训练图像的类别名称
    阈值

推理阶段—./tools/test.py(对数据集进行测试)

通过

# single-gpu testing
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show]

# multi-gpu testing
./tools/dist_test.sh ${CONFIG_FILE} ${CHECKPOINT_FILE} ${GPU_NUM} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}]

build_dataset

build_dataset一个输入参数:

  • cfg.data.test: 是mmcv.utils.config.ConfigDict类型。cfg则是通过mmcv.Config.fromfile(mmdetection/config/xx.py的路径)转换成mmcv.utils.config.Config得到。cfg.data.test:包含了测试集的路径,标注文件路径,测试图像大小等信息。

返回:mmdet定义的Dataset类(是torch.utils.data.Dataset的子类)

build_dataloader

build_dataloader:将数据转换成pytorch可读的类型

DataLoader的学习链接见上~

build_detector

build_detector三个参数:

  • cfg: 仍然是目标检测框架配置文件(如:faster_rcnn_r50_fpn_1x.py)中model部分。需要包括目标检测框架backbone、neck、head等部分信息
  • train_cfg: 推理阶段,该参数为None
  • test_cfg: cfg中test_cfg字段的内容,需要包含阈值等信息

init_detector中包含函数,init_detector=build_detector + load_checkpoint

load_checkpoint

load_checkpoint两个个主要输入参数:

  • model:build_detecor返回的model
  • checkpoint:训练权重(.pth,如 /models/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth)

gpt_test

single_gpu_test

single_gpu_test三个输入参数:

  • model:build_detector返回的model,并通过 load_checkpoint初始化了权重。
  • data_loader:build_dataloader返回的DataLoader
  • show:默认为False

multi_gpu_test

与single_gpu_test相似,用多块GPU进行测试。

eval

调用mmdet.corecoco_eval,根据eval_type对COCO数据集进行评估。

eval_type有以下几种:
'proposal', 'proposal_fast', 'bbox', 'segm', 'keypoints'.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值