论文地址https://arxiv.org/pdf/2309.11331.pdf
代码地址https://github.com/huawei-noah/Efficient-Computing
目录
01论文介绍
Gold-YOLO是华为2023年9月在NeurIPS顶会上提出的有关yolo系列中普遍存在的FPN中的信息传输问题的解决方案。
01摘要
在过去的几年中,YOLO系列模型已经成为实时目标检测领域的领先方法。许多研究通过修改架构、增加数据和设计新的损失函数,将基线推向了更高的水平。然而以前的模型仍然存在信息融合问题,尽管特征金字塔网络(FPN)和路径聚合网络(PANet)已经在一定程度上缓解了这个问题。因此,本研究提出了一种先进的聚集和分发机制(GD机制),该机制通过卷积和自注意力操作实现。这种新设计的模型被称为Gold-YOLO,它提升了多尺度特征融合能力,在所有模型尺度上实现了延迟和准确性的理想平衡。此外,本文首次在YOLO系列中实现了MAE风格的预训练,使得YOLO系列模型能够从无监督预训练中受益。Gold-YOLO-N在COCO val2017数据集上实现了出色的39.9% AP,并在T4 GPU上实现了1030 FPS,超过了之前的SOTA模型YOLOv6-3.0-N,其FPS相似,但性能提升了2.4%。
02模型训练过程
模型代码如下图,直接在上方代码下就可以,pycharm打开Gold-YOLO路径。
01安装环境
在终端里输入安装依赖项
pip install -r requirements.txt
02修改train.py参数
找到def get_args_parser函数,对其下方参数修改
def get_args_parser(add_help=True):
parser = argparse.ArgumentParser(description='YOLOv6 PyTorch Training', add_help=add_help)
01修改--data-path参数
选择你的数据集格式,我是voc数据集格式,所有我复制data路径下voc.yaml改名pole。
parser.add_argument('--data-path', default='./data/coco.yaml', type=str, help='path of dataset')
上面代码改为
parser.add_argument('--data-path', default='./data/pole.yaml', type=str, help='path of dataset')
进入到data/pole.yaml
# Please insure that your custom_dataset are put in same parent dir with YOLOv6_DIR
train: VOCdevkit/voc_07_12/images/train # train images
val: VOCdevkit/voc_07_12/images/val # val images
test: VOCdevkit/voc_07_12/images/val # test images (optional)
# whether it is coco dataset, only coco dataset should be set to True.
is_coco: False
# Classes
nc: 20 # number of classes
names: ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] # class names
修改你的路径,我用的绝对路径,以及nc类别数量,以及各类名称,下面两种都可以。
# Please insure that your custom_dataset are put in same parent dir with YOLOv6_DIR
train: D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\data\VOCdevkit\images/train # train images
val: D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\data\VOCdevkit\images/train # val images
test: D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\data\VOCdevkit\images/train # test images (optional)
is_coco: False
nc: 8 # number of classes
names: ['powerdirty', 'powerdirtyl', 'powerdirtys', 'lightdirty', 'lightdirtys', 'powerbreakb', 'powerbreak', 'powerbreakt'] # class names
# 0: powerdirty
# 1: powerdirtyl
# 2: powerdirtys
# 3: lightdirty
# 4: lightdirtys
# 5: powerbreakb
# 6: powerbreak
# 7: powerbreakt
数据放置,我是在data/VOCdevkit路径下,创建了VOCdevkit,标签格式是txt。
其中子目录下放置的是,images和labels两个文件夹,分别在这个两个文件夹下放置,train,val和test文件夹。以及分别放置标签。
02修改--conf-file参数
这个是加载训练的文件,在configs路径下,里面有个几个,我选择了gold_yolo-m.py。
parser.add_argument('--conf-file', default='./configs/yolov6n.py', type=str, help='experiments description file')
修改为,同样使用的绝对路径
parser.add_argument('--conf-file', default=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\configs\gold_yolo-m.py', type=str, help='experiments description file')
03加载预训练权重
在yolov6/core/engine.py路径下
def get_model(self, args, cfg, nc, device):
model = build_model(cfg, nc, device, fuse_ab=self.args.fuse_ab, distill_ns=self.distill_ns)
# weights = cfg.model.pretrained
# weights ='D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\configs\Gold_l_pre_dist.pt'
weights =r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\tools\runs\train\12_13\weights\last_ckpt.pt'
04其他参数设置
剩下的各种参数设置比较随意了
--batch-size根据电脑来,我设为了4,设为8,16都行。
parser.add_argument('--batch-size', default=4, type=int, help='total batch size for all GPUs')
--epochs,我没改让他跑400轮看看
parser.add_argument('--epochs', default=400, type=int, help='number of total epochs to run')
--workers这个几线程,一般电脑没有高线程,就改低,我是设为2.
parser.add_argument('--workers', default=2, type=int, help='number of data loading workers (default: 8)')
--device 设备,这个使用的GPU,默认是0,也就是使用第一个GPU.
parser.add_argument('--device', default='0', type=str, help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
--eval-interval,评价相隔轮次,默认20轮评价一次,
parser.add_argument('--eval-interval', default=20, type=int, help='evaluate at every interval epochs')
03训练
路径在tools/train.py,直接在train.py中点击运行,开始训练
04出现问题
如果出现以下报错,这个是多卡运行,需要更改一下参数 。
File "D:\DBSY\Efficient-Computing-masteryolo\toolstrain.py"line 130,in <module>
main(args)
File"D:\DBSY\Efficient-Computtine 120, in maintrainer.train()File "D:\DBSY\Efficient-Computing-masten DetectionlGold-YOLOlyolov6 corelenginepy", line 109, in trainself.train_in_loop(self.epoch)
File "D:IDBSYIEfficient-Comouting-masteriDetectioniold-YOLO volovo core enaine.py". line 127, in train-in_loo
self.print_details()
File "D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO yolov6\coreengine.py"line 339,in print_detailsself.mean_loss = (self.mean_loss * self.step + self.loss_items) (self.step + 1)AttributeError:'Trainer' object has no attribute loss_items'
将你调用的哪个configs/gold_yolo-m.py路径下。其中type取值修改一下
norm_cfg=dict(type='SyncBN', requires_grad=True),
norm_cfg=dict(type='BN', requires_grad=True),
02模型验证过程
路径在tools/eval.py中,
01参数修改
01修改--data,--weights
--data,--weights改为绝对路径。
parser.add_argument('--data', type=str, default=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\data\pole.yaml', help='dataset.yaml path')
parser.add_argument('--weights', type=str, default=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\weights/best_ckpt.pt', help='model.pt path(s)')
02--task任务模式
选择验证还是测试还是速度。
parser.add_argument('--task', default='val', help='val, test, or speed')
03 其他
--batch-size改小点,我设置为4。
--img-size输入图像大小,默认640
--conf-thres置信度阈值,默认
--iou-thres交并比阈值--默认
--device设备默认0
--half半点精度,正常32位精度,fp16位精度可能效果变差
--save_dir保存地址 默认或者设为自己想设的绝对路径
--save_dir文件的保存名称
--test_load_size测试时候加载的影像尺寸
--letterbox_return_int 返回整数的框偏移量
--scale_exact 使用精确的尺度来缩放坐标,默认
02验证
直接运行eval。
03模型推理过程
路径在tools/infer.py下,
01参数修改
01修改--weights,--source,--yaml
分别是模型权重文件,推理影像地址,数据集配置文件地址。都设成绝对路径
parser.add_argument('--weights', type=str,default=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\weights/best_ckpt.pt', help='model path(s) for inference.')
parser.add_argument('--source', type=str, default=r'D:\dataset\河北数据\10.25\10.23\闽江道-侧后\sm_y', help='the source path, e.g. image-file/dir.')
parser.add_argument('--yaml', type=str, default=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\data\pole.yaml', help='data yaml file.')
02--save-txt
--save-txt 生成txt文件,在下行加入default=True。这个txt文件用于与标签评价。
parser.add_argument('--save-txt', default=True, action='store_true', help='save results to *.txt.')
03其他
上文讲过的,不在重复
--max-det 每幅图像的最大推理次数,默认,
--not-save-img 不保存可视化的推理结果,这个是影像的结果。
--view-img 展示预测的影像结果,默认,设为True,自动弹出
--classes 预测的类别 可能有一些负样本不想预测 就设成想预测的类
--hide-conf 隐藏置信度得分,只保留框 没有得分
02解决报错
File "D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\tools\infer.py", line 103, in run
inferer = Inferer(source, weights, device, yaml, img_size, half)
TypeError: __init__() missing 2 required positional arguments: 'img_size' and 'half'
报错是以下这行代码
inferer = Inferer(source, weights, device, yaml, img_size, half)
在yolov6/core/inferer.py下在class Inferer:中,看参数的定义,共8个参数
class Inferer:
def __init__(self, source, webcam, webcam_addr, weights, device, yaml, img_size, half):
参数调用时,少了两个参数。增加这两个参数。
inferer = Inferer(source,webcam, webcam_addr, weights, device, yaml, img_size, half)
并在tools/infer.py路径下,在run中定义中,将这两个参数设置为False。
@torch.no_grad()
def run(weights=osp.join(ROOT, 'yolov6s.pt'),
source=osp.join(ROOT, 'data/images'),
yaml=None,
img_size=640,
conf_thres=0.4,
iou_thres=0.45,
max_det=1000,
device='',
save_txt=False,
not_save_img=False,
save_dir=None,
view_img=True,
classes=None,
agnostic_nms=False,
project=osp.join(ROOT, 'runs/inference'),
name='exp',
hide_labels=False,
hide_conf=False,
half=False,
webcam=False,
webcam_addr=False,
):
点击运行,出现了下一个错误
File "D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\yolov6\core\inferer.py", line 260, in font_check
assert osp.exists(font), f'font path not exists: {font}'
AssertionError: font path not exists: ./yolov6/utils/Arial.ttf
点到报错路径下,还是在yolov6/core/inferer.py。这个无法调用
def font_check(font='./yolov6/utils/Arial.ttf', size=10):
将这个路径写成绝对路径。
def font_check(font=r'D:\DBSY\Efficient-Computing-master\Detection\Gold-YOLO\yolov6\utils/Arial.ttf', size=10):
03 推理
直接运行infer