YOLOX 各功能模块代码解读(持续更新)

一.学习率

1.yolox warmcos 学习率

# 假设10个epoch,每个epoch有10个iters,
# warmup_epochs=2, no_aug_epochs=3,
# warmup_lr_start=0, min_lr_ratio=0.05
 
ys = []
for i in range(10*10):
    v = yolox_warm_cos_lr(
        lr=0.01,
        min_lr_ratio=0.0005,
        total_iters=10*10,
        warmup_total_iters=10*2,
        warmup_lr_start=0,
        no_aug_iter=10*3,
        iters=i,
    )
    ys.append(v)

YOLOX 各功能模块代码解读(持续更新)_第1张图片

结论是:
前(warmup_epochs) 2个epochs, 学习率从0->0.01(warmup_lr_start–>lr)
第3个epochs->第7个epochs, 学习率从0.01->0.0005 (lr—>min_lr_ratio)
最后(no_aug_epochs) 3个epochs, 学习率保持min_lr_ratio =0.0005

另外,
Exp里面,
self.basic_lr_per_img = 0.01 / 64.0,
trainer.py里面:
self.lr_scheduler = self.exp.get_lr_scheduler(
self.exp.basic_lr_per_img * self.args.batch_size, self.max_iter
)
所以训练时的最大学习率是:
lr = self.exp.basic_lr_per_img * self.args.batch_size = 0.01*batch_size / 64.0

import math
from torch.utils.tensorboard import SummaryWriter


def yolox_warm_cos_lr(lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter, iters):
    # 1e-4     1e-6      100               3               1e-5             5       epoch
    if iters <= warmup_total_iters:  # 当前epoch小于等于3
        # lr = (lr - warmup_lr_start) * iters / float(warmup_total_iters) + warmup_lr_start
        lr = (lr - warmup_lr_start) * pow(iters / float(warmup_total_iters), 2) + warmup_lr_start
    elif iters >= total_iters - no_aug_iter:  # 当前epoch大于等于95
        lr = min_lr
    else:  # 当前epoch介于4-94之间的
        lr = min_lr + 0.5 * (lr - min_lr) * (
                1.0 + math.cos(
            math.pi * (iters - warmup_total_iters) / (total_iters - warmup_total_iters - no_aug_iter))
        )
    return lr


lr, min_lr, total_iters, warmup_total_iters, warmup_lr_start, no_aug_iter = 1e-4, 1e-6, 100, 3, 1e-5, 5

writer = SummaryWriter(logdir='logaaa',comment='MyNet')
for iters in range(100):
    y = yolox_warm_cos_lr(1e-4, 1e-6, 100, 3, 1e-5, 5, iters)
    writer.add_scalar("lr_cos", y, iters)
writer.close()

YOLOX 各功能模块代码解读(持续更新)_第2张图片

你可能感兴趣的:(YOLOX,深度学习,机器学习,人工智能)