新版 MMDetection V2.3.0训练测试笔记

目录

Installation

验证

使用自己的数据集训练faster-rcnn

训练命令

Use pre-trained model

测试命令(测试整个测试集)

Image demo(测试单张图像)

测试图像的High-level APIs

Useful tools

分析日志

引用


MMDetection已经更新到了v2.3.0版本,很多博客是基于1.0版本的操作,这里记录一下自己使用新版的一些操作

主分支使用 PyTorch 1.3 to 1.5,强烈推荐使用2.0版本,以实现更快的速度、更高的性能、更好的设计和更友好的使用。


Installation

  • Python 3.6+
  • PyTorch 1.3+

step1:

conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab

step2: 

conda install pytorch cudatoolkit=10.1 torchvision -c pytorch

或者使用pip安装

pip install  torch==1.5.0 torchvision==0.6.0 mmcv==0.5.5

Pytorch 使用不同版本的cuda, 以及为什么还要安装cudatoolkit  : 点此链接

step3:

pip install mmcv-full

step4:

# install 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

验证

python tools/train.py -h


使用自己的数据集训练faster-rcnn

(1)修改faster_rcnn_r50_fpn_1x_coco.py  

_base_ = [
    '../_base_/models/faster_rcnn_r50_fpn.py',
    '../_base_/datasets/coco_detection.py',
    '../_base_/schedules/schedule_1x.py', '../_base_/default_runtime.py'
]

 

(2)修改faster_rcnn_r50_fpn.py

anchor_generator=dict(
            type='AnchorGenerator',
            scales=[8],
            ratios=[0.5, 1.0, 2.0],
            strides=[4, 8, 16, 32, 64])
 num_classes=80

类别不需加1 

(3)数据集的位置以及samples_per_gpu修改

 samples_per_gpu=2,
 workers_per_gpu=2,
evaluation = dict(interval=1, metric='mAP')

(4)学习率和epoch的修改位置

optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)

重要:配置文件中的默认学习率(lr=0.02)是8个GPU和samples_per_gpu=2(批大小= 8 * 2 = 16)。根据线性缩放规则,如果您使用不同的GPU或每个GPU的有多少张图像,则需要按批大小设置学习率,例如,对于4GPU* 2 img / gpu=8,lr =8/16 * 0.02 = 0.01 ;对于16GPU* 4 img / gpu=64,lr =64/16 *0.02 = 0.08 。

计算公式:批大小(gup-num * samples_per_gpu) / 16 * 0.02

(5)修改测试的标签类别文件

(6)修改voc.py文件

重要: 修改完 class_names.py 和 voc.py 之后一定要重新编译代码,否则验证输出仍然为VOC的类别,且训练过程中指标异常

 loss_rpn_cls: 0.00, loss_rpn_bbox: 0.0000, loss_cls: 0.000, acc: 100, loss_bbox: 0.0000, loss: 0.000


训练命令

1. 使用单个GPU进行训练

python tools/train.py ${CONFIG_FILE} [optional arguments]

python tools/train.py  configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py

2.使用多个GPU进行训练

./tools/dist_train.sh ${CONFIG_FILE} ${GPU_NUM} [optional arguments]

./tools/dist_train.sh  configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py

[optional arguments]  可选参数

--no-validate  : 不建议使用,代码中每隔K(默认为1)执行评估,可以在configs/_base_/datasets/voc0712.py 修改evaluation = dict(interval=1, metric='mAP')

--work-dir ${WORK_DIR}   覆盖配置文件中指定的工作目录

--resume-from ${CHECKPOINT_FILE}   程序中断后继续训练,从先前的检查点文件恢复

--options 'Key=value'   :  在使用的配置中覆盖一些设置。

 

Use pre-trained model

要使用预训练的模型,新的配置在load_from中添加预训练模型的链接。用户可能需要在训练前下载模型权重,以避免训练期间的下载时间。

checkpoint_config = dict(interval=1)
# yapf:disable
log_config = dict(
    interval=50,
    hooks=[
        dict(type='TextLoggerHook'),
        # dict(type='TensorboardLoggerHook')
    ])
# yapf:enable
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/mask_rcnn_r50_fpn_2x_20181010-41d35c05.pth'
resume_from = None
workflow = [('train', 1)]

 


测试命令(测试整个测试集)

 

# single-gpu testing
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show]
  • RESULT_FILE:输出结果的文件名,采用pickle格式。如果未指定,结果将不会保存到文件中。
  • EVAL_METRICS:要根据结果评估的项目。允许的值是:COCO:proposal_fastproposalbboxsegm                            PASCAL VOC: mAPrecall
  • --show:如果指定,检测结果将绘制在图像上并显示在新窗口中。仅适用于单GPU测试,用于调试和可视化。请确保您的环境中可以使用GUI,否则您可能会遇到类似的错误。cannot connect to server
  • --show-dir:   如果指定,检测结果将绘制在图像上并保存到指定的目录中。仅适用于单GPU测试,用于调试和可视化。使用此选项时,您的环境中不需要可用的GUI。
  • --show-score-thr:  如果指定,则将删除分数低于此阈值的检测。

具体示例:

1. Test Faster R-CNN and visualize the results. Press any key for the next image.

python tools/test.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
    checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth \
    --show

2. Test Faster R-CNN and save the painted images for latter visualization.

python tools/test.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x.py \
    checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth \
    --show-dir faster_rcnn_r50_fpn_1x_results

3. Test Faster R-CNN on PASCAL VOC (without saving the test results) and evaluate the mAP.

python tools/test.py configs/pascal_voc/faster_rcnn_r50_fpn_1x_voc.py \
    checkpoints/SOME_CHECKPOINT.pth \
    --eval mAP

4. Test Mask R-CNN with 8 GPUs, and evaluate the bbox and mask AP.

./tools/dist_test.sh configs/mask_rcnn_r50_fpn_1x_coco.py \
    checkpoints/mask_rcnn_r50_fpn_1x_20181010-069fa190.pth \
    8 --out results.pkl --eval bbox segm

 

Image demo(测试单张图像)

python demo/image_demo.py ${IMAGE_FILE} ${CONFIG_FILE} ${CHECKPOINT_FILE} [--device ${GPU_ID}] [--score-thr ${SCORE_THR}]

Examples:

python demo/webcam_demo.py configs/faster_rcnn_r50_fpn_1x_coco.py \
    checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth

 

测试图像的High-level APIs

下面是一个构建模型和测试给定图像的示例。

from mmdet.apis import init_detector, inference_detector
import mmcv

config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.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
model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')

# test a video and show the results
video = mmcv.VideoReader('video.mp4')
for frame in video:
    result = inference_detector(model, frame)
    model.show_result(frame, result, wait_time=1)

Useful tools

分析日志

pip install seaborn

python tools/analyze_logs.py plot_curve [--keys ${KEYS}] [--title ${TITLE}] [--legend ${LEGEND}] [--backend ${BACKEND}] [--style ${STYLE}] [--out ${OUT_FILE}]
  • 绘制分类损失
python tools/analyze_logs.py plot_curve log.json --keys loss_cls --legend loss_cls
  • 绘制训练时的分类和回归损失,并将该图保存为pdf。
python tools/analyze_logs.py plot_curve log.json --keys loss_cls loss_bbox --out losses.pdf
  • 在同一个图中绘制两次训练的bbox mAP
python tools/analyze_logs.py plot_curve log1.json log2.json --keys bbox_mAP --legend run1 run2

 

引用

https://blog.csdn.net/weixin_41010198/article/details/106258366

https://zhuanlan.zhihu.com/p/101225733

 

### 回答1: mmdetection test.py 是一个命令行工具,用于在mmdetection框架中进行模型测试。通过该工具,可以加载训练好的模型,对测试集进行预测,并输出预测结果。在使用该工具时,需要指定测试集的路径、模型的路径、输出结果的路径等参数。具体使用方法可以参考mmdetection官方文档。 ### 回答2: MMDetection是一个基于PyTorch的开源目标检测框架,可用于各种物体检测任务。在MMDetection中,test.py是用于测试训练好的模型的主要脚本之一。 使用test.py脚本,需要先准备测试集数据和训练好的模型。测试集数据通常包括一组或多组待检测图像,这些图像需要与训练集数据的格式相同。训练好的模型可以是从已有的预训练模型微调而来,也可以是从头开始训练的。 在准备好数据和模型后,可以使用test.py脚本进行测试。具体步骤如下: 1. 修改配置文件 首先需要编辑配置文件,将项目的路径和测试文件路径等参数设置正确。配置文件通常保存在config文件夹中,其中包括运行模型的参数、模型架构、训练方案等。可用记事本或其他编辑器打开该文件进行修改。 2. 运行测试 在命令行中输入以下代码即可运行测试 python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show] [--show-dir ${SHOW_DIR}] 其中,${CONFIG_FILE}是指配置文件的路径;${CHECKPOINT_FILE}是指训练好的模型的路径;${RESULT_FILE}是指测试结果保存的路径;${EVAL_METRICS}是指测试评价指标,如AP、AR等;--show和--show-dir参数是可选的,前者用于显示测试结果的图像,后者用于指定显示结果的文件夹路径。 3. 测试结果 测试完成后,可以查看测试结果。如果指定了--out参数,则测试结果会保存在该路径下,可以使用文本编辑器或其他工具打开查看。如果使用了--show参数,则测试结果的图像会显示在屏幕上,可以逐一查看每张图片的检测结果。 总之,MMDetection的test.py脚本是一个十分重要的工具,它可以方便快捷地进行目标检测模型的测试。需要注意的是,在实际应用中,还需根据具体任务和数据情况进行适当的参数调整和模型优化,以获得更佳的检测结果。 ### 回答3: mmdetection是一个基于PyTorch框架的目标检测工具,提供了训练测试目标检测模型的工具链。其中test.py是用来测试训练好的目标检测模型的脚本。 在使用mmdetection之前,需要先安装相关的依赖包和mmdetection。可以使用以下命令安装: ``` pip install -r requirements/build.txt pip install -v -e . ``` 安装完成后,需要下载数据集和预训练模型,可以通过以下命令进行下载: ``` python tools/get_data.py ``` 然后,需要准备测试集的数据和相应的配置文件。测试集的数据应该放在一个文件夹中,并且每个图片需要有一个对应的xml文件。配置文件可以在configs文件夹中找到,需要选择与训练时使用的配置文件相同的配置文件。 接下来就可以运行test.py脚本进行测试,例如: ``` python tools/test.py configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py work_dirs/faster_rcnn_r50_fpn_1x_coco/epoch_12.pth --eval bbox ``` 其中,第一个参数是配置文件的路径,第二个参数是模型权重文件的路径,第三个参数是评估的指标,这里选择bbox指标,即bounding box的准确率和AP值。运行完后,会在结果文件夹中生成测试结果。 mmdetection的test.py脚本还提供了很多其他的参数,可以通过以下命令查看: ``` python tools/test.py --help ``` 总之,mmdetection test.py可以方便地测试训练好的目标检测模型,并输出评估结果,可用于模型优化和验证。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值