Replies: 3 comments
-
MMYOLO 源码阅读经验(1)国庆假期间正好有时间,仔细的来阅读一下 MMYOLO 的源码和文档,以此来提升自己的技术功底,主要包含以下几大大部分。
运行
|
Beta Was this translation helpful? Give feedback.
-
MMYOLO 源码阅读之 runner.train()build runner 之后脚本就开始 runner.train()了,本节来仔细看一下这部分的源码。 runner.train()以下代码有触发before_run和after_run的hook, def train(self) -> nn.Module:
"""Launch training.
Returns:
nn.Module: The model after training.
"""
if self._train_loop is None:
raise RuntimeError(
'`self._train_loop` should not be None when calling train '
'method. Please provide `train_dataloader`, `train_cfg`, '
'`optimizer` and `param_scheduler` arguments when '
'initializing runner.')
self._train_loop = self.build_train_loop(
self._train_loop) # type: ignore
# `build_optimizer` should be called before `build_param_scheduler`
# because the latter depends on the former
self.optim_wrapper = self.build_optim_wrapper(self.optim_wrapper)
# Automatically scaling lr by linear scaling rule
self.scale_lr(self.optim_wrapper, self.auto_scale_lr)
if self.param_schedulers is not None:
self.param_schedulers = self.build_param_scheduler( # type: ignore
self.param_schedulers) # type: ignore
if self._val_loop is not None:
self._val_loop = self.build_val_loop(
self._val_loop) # type: ignore
# TODO: add a contextmanager to avoid calling `before_run` many times
self.call_hook('before_run')
# initialize the model weights
self._init_model_weights()
# make sure checkpoint-related hooks are triggered after `before_run`
self.load_or_resume()
# Initiate inner count of `optim_wrapper`.
self.optim_wrapper.initialize_count_status(
self.model,
self._train_loop.iter, # type: ignore
self._train_loop.max_iters) # type: ignore
model = self.train_loop.run() # type: ignore
self.call_hook('after_run')
return model model = self.train_loop.run() # 详细解读通过 Debug 我们发现 def run(self) -> torch.nn.Module:
"""Launch training."""
self.runner.call_hook('before_train') # 触发 before_train 的 hook
while self._epoch < self._max_epochs:
self.run_epoch()
self._decide_current_val_interval()
if (self.runner.val_loop is not None
and self._epoch >= self.val_begin
and self._epoch % self.val_interval == 0):
self.runner.val_loop.run()
self.runner.call_hook('after_train') # 触发 after_train 的 hook
return self.runner.model |
Beta Was this translation helpful? Give feedback.
-
MMYOLO 中使用到的 HOOKdefault_hooks = dict(
timer=dict(type='IterTimerHook'),
logger=dict(type='LoggerHook', interval=1),
param_scheduler=dict(
type='YOLOv5ParamSchedulerHook',
scheduler_type='linear',
lr_factor=0.01,
max_epochs=300),
checkpoint=dict(type='CheckpointHook', interval=10, max_keep_ckpts=3),
sampler_seed=dict(type='DistSamplerSeedHook'),
visualization=dict(type='mmdet.DetVisualizationHook')) |
Beta Was this translation helpful? Give feedback.
-
Share your experience of eead mmyolo source code.
分享您阅读 MMYOLO 源码的经验
Beta Was this translation helpful? Give feedback.
All reactions