Github项目仓库中官方项目提供的预训练模型下载很慢?没有办法下载下载?得一直盯着看进度下载失败了手动点击重新下载?基于python来开发实现自动化预训练模型权重下载存储模块实现预训练模型权重下载

发布时间:2023年12月31日

因为项目开发的原因,在实际的项目中经常会需要基于真实项目中产生的数据来进行模型的开发训练,但是考虑到实际高质量标注数据集的高成本和稀缺性往往不会重头开始开发训练模型,一般的方法都是会基于官方提供的预训练权重模型来进行迁移训练,对于提升模型的效果和收敛速度,降低训练成本都有一定的帮助。

在今年的项目中又以目标检测系列的模型开发居多,从yolov3到yolov8,YOLO系列的版本迭代可以说是非常之快了,以yolov8为例,项目地址在这里,如下所示:

目前已有将近1.8w的star量。

yolov8项目本身同时支持:分类识别、目标检测、图像分割、姿态估计几种不同的任务类型的,对于不同的任务类型官方抖开放了对应的预训练权重可以直接使用,如下:

【分类识别】

Modelsize
(pixels)
acc
top1
acc
top5
Speed
CPU ONNX
(ms)
Speed
A100 TensorRT
(ms)
params
(M)
FLOPs
(B) at 640
YOLOv8n-cls22466.687.012.90.312.74.3
YOLOv8s-cls22472.391.123.40.356.413.5
YOLOv8m-cls22476.493.285.40.6217.042.7
YOLOv8l-cls22478.094.1163.00.8737.599.7
YOLOv8x-cls22478.494.3232.01.0157.4154.8

【目标检测】

Modelsize
(pixels)
mAPval
50-95
Speed
CPU ONNX
(ms)
Speed
A100 TensorRT
(ms)
params
(M)
FLOPs
(B)
YOLOv8n64037.380.40.993.28.7
YOLOv8s64044.9128.41.2011.228.6
YOLOv8m64050.2234.71.8325.978.9
YOLOv8l64052.9375.22.3943.7165.2
YOLOv8x64053.9479.13.5368.2257.8

目标检测还有另外一套是基于Open Image V7数据集训练出来的权重文件,如下所示:

Modelsize
(pixels)
mAPval
50-95
Speed
CPU ONNX
(ms)
Speed
A100 TensorRT
(ms)
params
(M)
FLOPs
(B)
YOLOv8n64018.4142.41.213.510.5
YOLOv8s64027.7183.11.4011.429.7
YOLOv8m64033.6408.52.2626.280.6
YOLOv8l64034.9596.92.4344.1167.4
YOLOv8x64036.3860.63.5668.7260.6

【图像分割】

Modelsize
(pixels)
mAPbox
50-95
mAPmask
50-95
Speed
CPU ONNX
(ms)
Speed
A100 TensorRT
(ms)
params
(M)
FLOPs
(B)
YOLOv8n-seg64036.730.596.11.213.412.6
YOLOv8s-seg64044.636.8155.71.4711.842.6
YOLOv8m-seg64049.940.8317.02.1827.3110.2
YOLOv8l-seg64052.342.6572.42.7946.0220.5
YOLOv8x-seg64053.443.4712.14.0271.8344.1

【姿态估计】

Modelsize
(pixels)
mAPpose
50-95
mAPpose
50
Speed
CPU ONNX
(ms)
Speed
A100 TensorRT
(ms)
params
(M)
FLOPs
(B)
YOLOv8n-pose64050.480.1131.81.183.39.2
YOLOv8s-pose64060.086.2233.21.4211.630.2
YOLOv8m-pose64065.088.8456.32.0026.481.0
YOLOv8l-pose64067.690.0784.52.5944.4168.6
YOLOv8x-pose64069.290.21607.13.7369.4263.2
YOLOv8x-pose-p6128071.691.24088.710.0499.11066.4

在我们想要使用预训练的时候,一般比较建议的方式是预先讲对应的预训练的模型下载到自己的本地项目中去然后将预训练模型的权重路径修改为本地路径这样会省略掉自动从网上下载的步骤,因为很多客观因素,联网下载失败率是很高的。

但是这里的问题是我们想要将预训练的模型预先下载到本地目录的话也是会经常失败的,甚至迟迟无法下载下来的,如下:

以目标检测任务yolov8x.pt权重下载为例:

这让人窒息的下载时长真的是无法接受,最无奈的是,这种下载状况大概率是等不到两小时就会下载失败了,还需要你得一直关注着文件是否下载成功了,如果没有下载成功就需要手动点击操作来重新下载,这是非常不方便的。

为了应对这种问题,这里我们开发实现了可以直接使用的自动预训练权重下载模块,可以帮助我们自动实现所需要预训练权重文件的下载,核心实现如下所示:

#!usr/bin/env python
# encoding:utf-8
from __future__ import division


"""
功能: 自动预训练模型权重下载模块
"""


import os
import time
import logging
import subprocess
import urllib
import requests
import torch
import random
from pathlib import Path



def autoDownload(file_path="all.txt", saveDir="weights/yolov8/"):
    """
    自动下载
    """
    if not os.path.exists(saveDir):
        os.makedirs(saveDir)
    while True:
        with open(file_path) as f:
            alls = [one.strip() for one in f.readlines() if one.strip()]
        count = 0
        for one_url in alls:
            print("downloading url: ", one_url)
            try:
                one_file = saveDir + one_url.split("/")[-1].strip()
                print("one_file: ", one_file)
                if not os.path.exists(one_file):
                    torch.hub.download_url_to_file(one_url, str(one_file))
                else:
                    print("当前预训练模型权重文件已经下载完成!")
                    count += 1
            except Exception as e:
                print("Exception: ", e)
                time.sleep(random.randint(1, 5))
        print("finished num: ", count)
        if count == len(alls):
            break



if __name__ == "__main__":


    autoDownload(file_path="all.txt", saveDir="weights/")

这里最大的好处就是只要你启动之后,他就会无休无止的帮你去完成你所需要的模型权重文件的下载任务,报错信息都可以忽略,如下:

这是我本地下载成功的截图,下载结果如下所示:

这里我们需要人工做的就是将自己需要下载的权重链接存放早alls.txt文件中,这里我是要下载yolov8所有的预训练权重文件,我的alls.txt如下所示:

https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-seg.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-seg.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-seg.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l-seg.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-seg.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-pose.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-pose-p6.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n-cls.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8s-cls.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8m-cls.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8l-cls.pt
https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8x-cls.pt

需要的话可以直接复制拿去使用即可,这里我也是只是以yolov8为例讲解实现了全部预训练权重模型的下载存储实现,其他系列的模型也是一样的流程,感兴趣的话可以拿去试试!

文章来源:https://blog.csdn.net/Together_CZ/article/details/135304390
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。