YOLOV8替换Lion优化器
创始人
2024-11-12 06:11:19
0

YOLOV8替换Lion优化器

1 优化器介绍博客

参考bilibili讲解视频

论文地址:https://arxiv.org/abs/2302.06675

代码地址:https://github.com/google/automl/blob/master/lion/lion_pytorch.py

"""PyTorch implementation of the Lion optimizer.""" import torch from torch.optim.optimizer import Optimizer     class Lion(Optimizer):   r"""Implements Lion algorithm."""     def __init__(self, params, lr=1e-4, betas=(0.9, 0.99), weight_decay=0.0):     """Initialize the hyperparameters.     Args:       params (iterable): iterable of parameters to optimize or dicts defining         parameter groups       lr (float, optional): learning rate (default: 1e-4)       betas (Tuple[float, float], optional): coefficients used for computing         running averages of gradient and its square (default: (0.9, 0.99))       weight_decay (float, optional): weight decay coefficient (default: 0)     """       if not 0.0 <= lr:       raise ValueError('Invalid learning rate: {}'.format(lr))     if not 0.0 <= betas[0] < 1.0:       raise ValueError('Invalid beta parameter at index 0: {}'.format(betas[0]))     if not 0.0 <= betas[1] < 1.0:       raise ValueError('Invalid beta parameter at index 1: {}'.format(betas[1]))     defaults = dict(lr=lr, betas=betas, weight_decay=weight_decay)     super().__init__(params, defaults)     @torch.no_grad()   def step(self, closure=None):     """Performs a single optimization step.     Args:       closure (callable, optional): A closure that reevaluates the model         and returns the loss.     Returns:       the loss.     """     loss = None     if closure is not None:       with torch.enable_grad():         loss = closure()       for group in self.param_groups:       for p in group['params']:         if p.grad is None:           continue           # Perform stepweight decay         p.data.mul_(1 - group['lr'] * group['weight_decay'])           grad = p.grad         state = self.state[p]         # State initialization         if len(state) == 0:           # Exponential moving average of gradient values           state['exp_avg'] = torch.zeros_like(p)           exp_avg = state['exp_avg']         beta1, beta2 = group['betas']           # Weight update         update = exp_avg * beta1 + grad * (1 - beta1)         p.add_(torch.sign(update), alpha=-group['lr'])         # Decay the momentum running average coefficient         exp_avg.mul_(beta2).add_(grad, alpha=1 - beta2)       return loss 

2 在相应的文件夹内新建lion_pytorch.py文件

在这里插入图片描述

3 在trianer.py中添加Lion优化器

在这里插入图片描述

from ultralytics.engine.lion_pytorch import Lion    #Lion optimizer 

然后在末尾build_optimizer函数中添加判断是否使用Lion优化器:
在这里插入图片描述

def build_optimizer(self, model, name="auto", lr=0.001, momentum=0.9, decay=1e-5, iterations=1e5): ·······       elif name == "Lion":         optimizer = Lion(g[2], lr=lr, betas=(momentum, 0.99), weight_decay=0.0) ······· 

4 设置Lion优化器并训练查看

方法1:defalut.yaml中修改默认设置:
在这里插入图片描述
方法2:训练文件中自定义设置:
在这里插入图片描述Lion优化器默认的学习率改为为1e-4,不然就是yolov8中默认的0.01。

运行训练文件后可以看到如下提示则修改成功:
在这里插入图片描述

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...