近期学习YOLOv5的朋友比较多,为便于大家更好理解源码,对YOLOv5进行中文注释。需要的朋友可以关注我。
以下为部分代码示例。
def main(opt):
# 1、logging和wandb初始化
# 日志初始化
set_logging(RANK)
if RANK in [-1, 0]:
# 可以输出所有训练opt参数
print(colorstr('train: ') + ', '.join(f'{k}={v}' for k, v in vars(opt).items()))
# 这句代码用来检查代码版本是否是最新的
check_git_status()
# 用来检查requirements.txt所需包是否都满足
check_requirements(exclude=['thop'])
# wandb logging初始化
wandb_run = check_wandb_resume(opt)
# 2、判断是否使用断点续训resume, 加载参数
if opt.resume and not wandb_run:
# 使用断点续训 就从last.pt中读取相关参数
# 如果resume是str,则将表示传入的是模型的路径地址
# 如果resume是True,则通过get_lastest_run()函数找到runs为文件夹中最近的权重文件last.pt
ckpt = opt.resume if isinstance(opt.resume, str) else get_latest_run()
assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist' # check
# 相关的opt参数也要替换成last.pt中的opt参数
with open(Path(ckpt).parent.parent / 'opt.yaml') as f:
opt = argparse.Namespace(**yaml.safe_load(f)) # replace
opt.cfg, opt.weights, opt.resume = '', ckpt, True # reinstate
logger.info('Resuming training from %s' % ckpt) # print
else:
# 不使用断点续训 就可以文件中读取相关参数
# opt.hyp = opt.hyp or ('hyp.finetune.yaml' if opt.weights else 'hyp.scratch.yaml')
opt.data, opt.cfg, opt.hyp = check_file(opt.data), check_file(opt.cfg), check_file(opt.hyp) # check files
如果觉得对大家有帮助,欢迎点赞收藏关注,我会继续给大家做实验提供参考。有问题也欢迎私信我。