MMDetection系列 | 5. MMDetection运行配置介绍

简介: MMDetection系列 | 5. MMDetection运行配置介绍

1. 优化器配置


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


  • 使用梯度剪辑来稳定训练
optimizer_config = dict(
    _delete_=True, grad_clip=dict(max_norm=35, norm_type=2))


其中,_delete_=True将用新键替换backbone字段中的所有旧键


2. 学习率配置


lr_config = dict(
    policy='step',
    warmup='linear',
    warmup_iters=500,
    warmup_ratio=0.001,
    step=[8, 11])       # 表示初始学习率在第8和11个epoch衰减10倍


还有其他的配置方案:


  • Poly schedule
lr_config = dict(policy='poly', power=0.9, min_lr=1e-4, by_epoch=False)


  • ConsineAnnealing schedule
lr_config = dict(
    policy='CosineAnnealing',
    warmup='linear',
    warmup_iters=1000,
    warmup_ratio=1.0 / 10,
    min_lr_ratio=1e-5)


  • 使用动量调度加速模型收敛

支持动量调度器根据学习率修改模型的动量,这可以使模型以更快的方式收敛。Momentum 调度器通常与 LR 调度器一起使用


lr_config = dict(
    policy='cyclic',
    target_ratio=(10, 1e-4),
    cyclic_times=1,
    step_ratio_up=0.4,
)
momentum_config = dict(
    policy='cyclic',
    target_ratio=(0.85 / 0.95, 1),
    cyclic_times=1,
    step_ratio_up=0.4,
)


3. 工作流程配置


工作流是 (phase, epochs) 的列表,用于指定运行顺序和时期。默认情况下,它设置为:


workflow = [('train', 1)]


这意味着运行 1 个 epoch 进行训练。有时用户可能想要检查验证集上模型的一些指标(例如损失、准确性)。在这种情况下,我们可以将工作流设置为


[('train', 1), ('val', 1)]


这样 1 个 epoch 的训练和 1 个 epoch 的验证将被迭代运行。而验证集的损失同样会被计算出来。如果想先进行验证,再进行训练,还可以设置如下:


[('val', 1), ('train', n)]


这样设置表示先对验证集进行验证与损失计算,再进行n个epoch的计算。


4. 检查点配置


checkpoint_config = dict(interval=20)          # 20个epoch保存一次权重


参数说明见:https://mmcv.readthedocs.io/en/latest/api.html#mmcv.runner.CheckpointHook


CLASSmmcv.runner.CheckpointHook(interval: int = - 1, by_epoch: bool = True, save_optimizer: bool = True, out_dir: Optional[str] = None, max_keep_ckpts: int = - 1, save_last: bool = True, sync_buffer: bool = False, file_client_args: Optional[dict] = None, **kwargs)


  • interval (int) – The saving period. If by_epoch=True, interval indicates epochs, otherwise it indicates iterations. Default: -1, which means “never”.
  • by_epoch (bool) – Saving checkpoints by epoch or by iteration. Default: True.
  • save_optimizer (bool) – Whether to save optimizer state_dict in the checkpoint. It is usually used for resuming experiments. Default: True.
  • out_dir (str, optional) – The root directory to save checkpoints. If not specified, runner.work_dir will be used by default. If specified, the out_dir will be the concatenation of out_dir and the last level directory of runner.work_dir. Changed in version 1.3.16.
  • max_keep_ckpts (int, optional) – The maximum checkpoints to keep. In some cases we want only the latest few checkpoints and would like to delete old ones to save the disk space. Default: -1, which means unlimited.
  • save_last (bool, optional) – Whether to force the last checkpoint to be saved regardless of interval. Default: True.
  • sync_buffer (bool, optional) – Whether to synchronize buffers in different gpus. Default: False.
  • file_client_args (dict, optional) – Arguments to instantiate a FileClient. See mmcv.fileio.FileClient for details. Default: None. New in version 1.3.16.


5. 日志配置


包装多个记录器log_config挂钩并允许设置间隔。现在 MMCV 支持WandbLoggerHook、MlflowLoggerHook和TensorboardLoggerHook.


log_config = dict(
    interval=50,    # 每500个迭代就打印一次训练信息
    hooks=[
        dict(type='TextLoggerHook'),
        # dict(type='TensorboardLoggerHook')
    ])


参数说明见:https://mmcv.readthedocs.io/en/latest/api.html#mmcv.runner.EvalHook


CLASSmmcv.runner.LoggerHook(interval: int = 10, ignore_last: bool = True, reset_flag: bool = False, by_epoch: bool = True)[SOURCE]


  • interval (int) – Logging interval (every k iterations). Default 10.
  • ignore_last (bool) – Ignore the log of last iterations in each epoch if less than interval. Default True.
  • reset_flag (bool) – Whether to clear the output buffer after logging. Default False.
  • by_epoch (bool) – Whether EpochBasedRunner is used. Default True.


6. 评估配置


配置的evaluation将用于初始化EvalHook. 除了 key interval,其他参数如metric将传递给dataset.evaluate()

evaluation = dict(interval=1, metric=‘bbox’)


参数说明:https://mmcv.readthedocs.io/en/latest/api.html?highlight=EpochBasedRunner#mmcv.runner.EpochBasedRunner


mmcv.runner.EvalHook(dataloader: torch.utils.data.dataloader.DataLoader, start: Optional[int] = None, interval: int = 1, by_epoch: bool = True, save_best: Optional[str] = None, rule: Optional[str] = None, test_fn: Optional[Callable] = None, greater_keys: Optional[List[str]] = None, less_keys: Optional[List[str]] = None, out_dir: Optional[str] = None, file_client_args: Optional[dict] = None, **eval_kwargs)


  • dataloader (DataLoader) – A PyTorch dataloader, whose dataset has implemented evaluate function.
  • start (int | None, optional) – Evaluation starting epoch. It enables evaluation before the training starts if start <= the resuming epoch. If None, whether to evaluate is merely decided by interval. Default: None.
  • interval (int) – Evaluation interval. Default: 1.
  • by_epoch (bool) – Determine perform evaluation by epoch or by iteration. If set to True, it will perform by epoch. Otherwise, by iteration. Default: True.
  • save_best (str, optional) – If a metric is specified, it would measure the best checkpoint during evaluation. The information about best checkpoint would be saved in runner.meta[‘hook_msgs’] to keep best score value and best checkpoint path, which will be also loaded when resume checkpoint. Options are the evaluation metrics on 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 of the returned OrderedDict result will be used. Default: None.
  • rule (str | None, optional) – Comparison rule for best score. If set to None, it will infer a reasonable rule. Keys such as ‘acc’, ‘top’ .etc will be inferred by ‘greater’ rule. Keys contain ‘loss’ will be inferred by ‘less’ rule. Options are ‘greater’, ‘less’, None. Default: None.
  • test_fn (callable, optional) – test a model with samples from a dataloader, and return the test results. If None, the default test function mmcv.engine.single_gpu_test will be used. (default: None)
  • greater_keys (List[str] | None, optional) – Metric keys that will be inferred by ‘greater’ comparison rule. If None, _default_greater_keys will be used. (default: None)
  • less_keys (List[str] | None, optional) – Metric keys that will be inferred by ‘less’ comparison rule. If None, _default_less_keys will be used. (default: None)
  • out_dir (str, optional) – The root directory to save checkpoints. If not specified, runner.work_dir will be used by default. If specified, the out_dir will be the concatenation of out_dir and the last level directory of runner.work_dir. New in version 1.3.16.
  • file_client_args (dict) – Arguments to instantiate a FileClient. See mmcv.fileio.FileClient for details. Default: None. New in version 1.3.16.
  • **eval_kwargs – Evaluation arguments fed into the evaluate function of the dataset.


7. 训练设置


runner = dict(type='EpochBasedRunner', max_epochs=150)   # 设置模型训练多少次


参数说明:https://mmcv.readthedocs.io/en/latest/api.html#mmcv.runner.EpochBasedRunner


mmcv.runner.EpochBasedRunner(model: torch.nn.modules.module.Module, batch_processor: Optional[Callable] = None, optimizer: Optional[Union[Dict, torch.optim.optimizer.Optimizer]] = None, work_dir: Optional[str] = None, logger: Optional[logging.Logger] = None, meta: Optional[Dict] = None, max_iters: Optional[int] = None, max_epochs: Optional[int] = None)


总结:


一般来说,我们写配置文件都会继承default_runtime.py这个文件


_base_ = [
    '../_base_/default_runtime.py'
]


这个文件的内容如下所示:


checkpoint_config = dict(interval=5)    # 每5个epoch保存一次权重
# yapf:disable
log_config = dict(
    interval=50,    # 每500个迭代就打印一次训练信息
    hooks=[
        dict(type='TextLoggerHook'),
        # dict(type='TensorboardLoggerHook')
    ])
# yapf:enable
custom_hooks = [dict(type='NumClassCheckHook')]
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = None        # 加载权重文件
resume_from = None
workflow = [('train', 1)]
# disable opencv multithreading to avoid system being overloaded
opencv_num_threads = 0
# set multi-process start method as `fork` to speed up the training
mp_start_method = 'fork'
# Default setting for scaling LR automatically
#   - `enable` means enable scaling LR automatically
#       or not by default.
#   - `base_batch_size` = (8 GPUs) x (2 samples per GPU).
auto_scale_lr = dict(enable=False, base_batch_size=16)


一般不需要更改太多的内容,可以时代的更改log_config进行合理的打印训练信息,还有设置checkpoint_config进行合理的保存权重文件,其他的设置按默认即可。


下面展示我继承了default_runtime.py后更改的内容,其实就是更改了以上我所介绍的七点内容:


_base_ = [
    '../_base_/default_runtime.py'
]
......
# optimizer
optimizer = dict(   # 设置使用AdamW优化器(默认使用的是SGD)
    type='AdamW',
    lr=0.0001,
    weight_decay=0.0001,
    paramwise_cfg=dict(custom_keys={'backbone': dict(lr_mult=0.1, decay_mult=1.0)}))
evaluation = dict(interval=5, metric='bbox')   # 5个epoch验证一次
optimizer_config = dict(grad_clip=dict(max_norm=0.1, norm_type=2))  # 设置梯度裁剪(default_runtime.py中默认为None)
checkpoint_config = dict(interval=20)          # 20个epoch保存一次权重
log_config = dict(interval=50,     # 每50次迭代训练就打印一次信息(注意是迭代而不是epoch)
                  hooks=[dict(type='TextLoggerHook')])
# learning policy
lr_config = dict(policy='step', step=[100])              # 学习率在100个epoch进行衰减
runner = dict(type='EpochBasedRunner', max_epochs=150)   # 训练150个epoch



参考资料:


1. Customize Runtime Settings


2. mmcv官方文档


相关实践学习
在云上部署ChatGLM2-6B大模型(GPU版)
ChatGLM2-6B是由智谱AI及清华KEG实验室于2023年6月发布的中英双语对话开源大模型。通过本实验,可以学习如何配置AIGC开发环境,如何部署ChatGLM2-6B大模型。
目录
相关文章
|
Linux Python
bypy:使用Linux命令行上传及下载百度云盘文件(远程服务器大文件传输必备)
bypy:使用Linux命令行上传及下载百度云盘文件(远程服务器大文件传输必备)
bypy:使用Linux命令行上传及下载百度云盘文件(远程服务器大文件传输必备)
|
SQL 监控 关系型数据库
【MYSQL高级】Mysql找出执行慢的SQL【慢查询日志使用与分析】
【MYSQL高级】Mysql找出执行慢的SQL【慢查询日志使用与分析】
7064 0
|
9月前
|
存储 人工智能 API
5分钟上手OpenClaw(小龙虾):阿里云/本地部署+多Agent协作+免费大模型对接指南
2026年,OpenClaw(昵称“小龙虾”)已从单纯的AI助手升级为一套完整的“Agent Runtime”运行时网关系统——它的核心优势并非简单的对话与工具调用,而是背后贯穿始终的工程化设计:从消息接收、协议适配到路由分发、上下文组装,再到技能注入、流式执行与持久化存储,形成了一套闭环的任务处理链路。
2979 0
|
Docker 容器
error during connect: this error may indicate that the docker daemon is not running
error during connect: this error may indicate that the docker daemon is not running
1270 0
|
安全 网络安全 数据安全/隐私保护
SSL/TLS证书**是一种用于加密网络通信的数字证书
SSL/TLS证书**是一种用于加密网络通信的数字证书
934 6
|
容器
创建frps容器遇到的下载速度很慢的问题
该脚本更新系统后安装wget,并从GitHub下载指定版本的frp服务端,解压至指定目录。由于GitHub下载速度较慢,可能需要多次尝试或使用加速工具。
|
算法 计算机视觉
Opencv学习笔记(六):cv2.resize函数的介绍
这篇文章介绍了OpenCV库中cv2.resize函数的使用方法,包括其参数、插值方式选择以及实际代码示例。
4275 1
Opencv学习笔记(六):cv2.resize函数的介绍
|
机器学习/深度学习 数据可视化 PyTorch
深度学习之如何使用Grad-CAM绘制自己的特征提取图-(Pytorch代码,详细注释)神经网络可视化-绘制自己的热力图
深度学习之如何使用Grad-CAM绘制自己的特征提取图-(Pytorch代码,详细注释)神经网络可视化-绘制自己的热力图
深度学习之如何使用Grad-CAM绘制自己的特征提取图-(Pytorch代码,详细注释)神经网络可视化-绘制自己的热力图
|
Shell 索引
shell脚本变量详解(自定义变量、环境变量、变量赋值、变量运算、变量内容替换)
shell变量 shell变量是指用一个特定的字符串去表示不固定的内容 1.变量的类型 1.1自定义变量 一般情况下不怎么使用环境变量,如果需要在其他文件中引入某个文件的变量则在脚本最开始的位置使用source 或者. 执行下该脚本即可
4583 0
shell脚本变量详解(自定义变量、环境变量、变量赋值、变量运算、变量内容替换)
|
Kubernetes Linux 虚拟化
入门级容器技术解析:Docker和K8s的区别与关系
本文介绍了容器技术的发展历程及其重要组成部分Docker和Kubernetes。从传统物理机到虚拟机,再到容器化,每一步都旨在更高效地利用服务器资源并简化应用部署。容器技术通过隔离环境、减少依赖冲突和提高可移植性,解决了传统部署方式中的诸多问题。Docker作为容器化平台,专注于创建和管理容器;而Kubernetes则是一个强大的容器编排系统,用于自动化部署、扩展和管理容器化应用。两者相辅相成,共同推动了现代云原生应用的快速发展。
5122 11