pytorch 如何在mm检测中运行train_detector后保存模型权重?

taor4pac  于 2022-11-29  发布在  其他
关注(0)|答案(2)|浏览(178)
cfg.optimizer.lr = 0.02 / 8
cfg.lr_config.warmup = None
cfg.log_config.interval = 600
# Change the evaluation metric since we use customized dataset.
cfg.evaluation.metric = 'bbox'
# We can set the evaluation interval to reduce the evaluation times
cfg.evaluation.interval = 3
# We can set the checkpoint saving interval to reduce the storage cost
cfg.checkpoint_config.interval = 3
# Set seed thus the results are more reproducible
cfg.seed = 0
set_random_seed(0, deterministic=False)
cfg.gpu_ids = range(1)

cfg.load_from = 'gdrive/My Drive/mmdetection/checkpoints/vfnet_r50_fpn_mdconv_c3- 
c5_mstrain_2x_coco_20201027pth-6879c318.pth'
cfg.work_dir = "../vinbig"
cfg.runner.max_epochs = 6
cfg.total_epochs = 6model = build_detector(cfg.model)
datasets = [build_dataset(cfg.data.train)]
train_detector(model, datasets[0], cfg, distributed=False, validate=True)

现在,我的问题是,一旦我对自定义数据集的模型进行了微调,我如何使用它进行测试?微调后的模型存储在哪里?在大多数地方,模型会立即用于测试,但我如何保存微调后的模型以供稍后测试。

img = mmcv.imread('kitti_tiny/training/image_2/000068.jpeg')

model.cfg = cfg
result = inference_detector(model, img)
show_result_pyplot(model, img, result)

以上是在训练阶段之后发生的事情。但那是因为模型已经在运行时了。我如何创建我自己的mmdetection模型检查点呢?我一直在Google colab上工作。

qxsslcnc

qxsslcnc1#

好吧,我不知道如何手动操作,但是您的检查点会自动保存在cfg.work_dir = "../vinbig"中。在那里您可以找到'latest.pth'文件作为您的最终检查点。

von4xj4u

von4xj4u2#

我花了一段时间才找到,因为mmdet.core.evaluation.eval_hooks中的文档不是很清楚,但是their readthedocs的旧版本描述了EvalHooksave_best属性

save_best (str, optional): If a metric is specified, it would measure
        the best checkpoint during evaluation. The information about best
        checkpoint would be save in best.json.
        Options are the evaluation metrics to the test dataset. e.g.,
        ``bbox_mAP``, ``segm_mAP`` for bbox detection and instance
        segmentation. ``AR@100`` for proposal recall. If ``save_best`` is
        ``auto``, the first key will be used. The interval of
        ``CheckpointHook`` should device EvalHook. Default: None.

要启用它,只需将参数添加到config:

cfg.evaluation = dict(interval= 2, metric='mAP', save_best='mAP')

这将每隔2个时期在验证集上测试模型,并保存获得最佳 mAP 指标的检查点(在您的情况下,可能需要改为bbox),以及checkpoint_config.interval属性指示的每个检查点。
:)
编辑:mmdet的EvalHook继承自mmcv的EvalHook,其中文档是完整的。

相关问题