关于YOLOv5的训练,GPU单卡、多卡设置,加速训练

yolov5毫无疑问是目前目标检测框架中非常准确快速的检测框架之一,在工业界和学术界应用广泛,其优势不言而喻。

在模型训练或推理时,我们都想快速完成,特别是数据量很大的时候,效率就是非常迫切需要提升的。这里简单介绍一下yolov5的多种训练方法,便于理解深度学习的模型训练方法,同时基于自身的硬件条件选择高效的训练方法。

yolov5训练方法的官方网站:https://github.com/ultralytics/yolov5/issues/475

如果条件允许,首推的是多卡DDP训练模式
在这里插入图片描述

1、Single GPU 单卡训练

python train.py  --batch 64 --data coco.yaml --weights yolov5s.pt --device 0

2、Multi-GPU DataParallel Mode (⚠️ not recommended)

多卡DP训练 不推荐

官方也不推荐该方法,该方法训练的时候速度快不了多少,而且该方法训练时把数据放到多张卡上,但是计算结果在主卡上进行,会导致主卡和其他卡的内存使用不平衡,不推荐。

python train.py  --batch 64 --data coco.yaml --weights yolov5s.pt --device 0,1

官方说该方法慢,训练时比起单卡加速很小,This method is slow and barely speeds up training compared to using just 1 GPU.

3、Multi-GPU DistributedDataParallel Mode (✅ recommended)

强推多卡DDP方法
需要通过设置 python -m torch.distributed.run --nproc_per_node 运行命令如下

python -m torch.distributed.run --nproc_per_node 2 train.py --batch 64 --data coco.yaml --weights yolov5s.pt --device 0,1

--nproc_per_node 表示使用的GPU数量,specifies how many GPUs you would like to use. In the example above, it is 2.
--batch 表示批量处理的图片数量,会被均分到每张卡上,the total batch-size. It will be divided evenly to each GPU. In the example above, it is 64/2=32 per GPU.

示例:

python -m torch.distributed.run --nproc_per_node 2 train.py --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights '' --device 2,3

关于DDP方法使用 SyncBatchNorm,使用SyncBatchNorm可以提升精度,但是会降低训练速度,而且只适用于DDP,平分到每张卡上的 batch-size <= 8 时效果更好, 通过命令行增加参数标志--sync-bn 执行

SyncBatchNorm could increase accuracy for multiple gpu training, however, it will slow down training by a significant factor. It is only available for Multiple GPU DistributedDataParallel training.

It is best used when the batch-size on each GPU is small (<= 8).

To use SyncBatchNorm, simple pass --sync-bn to the command like below,

python -m torch.distributed.run --nproc_per_node 2 train.py --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights '' --sync-bn

多台机器训练,多台机器训练需要保持机器之间的通信,其效率会受一定影响,官方的多机器训练设置命令:

# On machine R
python -m torch.distributed.run --nproc_per_node G --nnodes N --node_rank R --master_addr "192.168.1.1" --master_port 1234 train.py --batch 64 --data coco.yaml --cfg yolov5s.yaml --weights ''

where G is number of GPU per machine, N is the number of machines, and R is the machine number from 0…(N-1).
Let’s say I have two machines with two GPUs each, it would be G = 2 , N = 2, and R = 1 for the above.
其中G是每台机器的GPU数量,N是机器数量,R是机器序号,表示汇总到哪台机器(master machine)
Training will not start until all N machines are connected. Output will only be shown on master machine!

4、在train.py的相关代码

DP方法的代码:

# DP mode
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
    LOGGER.warning(
        'WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.\n'
        'See Multi-GPU Tutorial at https://docs.ultralytics.com/yolov5/tutorials/multi_gpu_training to get started.'
    )
    model = torch.nn.DataParallel(model)

是否使用SyncBatchNorm

# SyncBatchNorm
if opt.sync_bn and cuda and RANK != -1:
    model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
    LOGGER.info('Using SyncBatchNorm()')

使用DDP方法:

# DDP mode
if cuda and RANK != -1:
    model = smart_DDP(model)

其中涉及smart_DDP的代码:

from torch.nn.parallel import DistributedDataParallel as DDP
def smart_DDP(model):
    # Model DDP creation with checks
    assert not check_version(torch.__version__, '1.12.0', pinned=True), \
        'torch==1.12.0 torchvision==0.13.0 DDP training is not supported due to a known issue. ' \
        'Please upgrade or downgrade torch to use DDP. See https://github.com/ultralytics/yolov5/issues/8395'
    if check_version(torch.__version__, '1.11.0'):
        return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK, static_graph=True)
    else:
        return DDP(model, device_ids=[LOCAL_RANK], output_device=LOCAL_RANK)

从上可以看到DDP训练不支持 torch==1.12.0 torchvision==0.13.0 版本的库,torch1.11.0需要单独设置。

  • 11
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
yolov8多卡训练暂停是指在yolov8模型多卡训练过程中,由于一些原因需要中断训练,并在之后的时间继续训练。在这种情况下,有一些常见的误区和优化建议可以提供给你。 首先,常见的误区包括训练时间过长、设备性能不足、训练效果不佳和不想从头开始训练。这些问题可能导致训练暂停的需求。 为了解决这个问题,你可以参考一些优化建议。一个常见的优化建议是在train.py文件中使用resume代码来复制当前的训练配置,以便在完成训练后可以继续训练。例如,在train.py文件中增加以下代码:opt_resume = deepcopy(opt)。 另外,如果你在继续训练时遇到内存报错和电脑卡顿等问题,可以尝试调整一些训练参数,如图片大小、batch-size和epoch等。但需要注意的是,调整参数并不总是有效的,因此可能需要进一步的优化措施。 综上所述,yolov8多卡训练暂停是一种中断训练并在之后继续训练的情况。为了解决这个问题,可以避免常见的误区并采取优化建议,如使用resume代码和调整参数等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [纠正yoloV7 断点后训练的错误说明与优化](https://blog.csdn.net/u014613075/article/details/128883660)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [使用YOLOv5训练模型,遇到报错OS error:[winerror 1455]的解决方法](https://blog.csdn.net/m0_47593541/article/details/126229082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

清梦枕星河~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值