YOLOv7:可训练的免费书包
YOLOv7 是最先进的实时物体检测器,在 5 FPS 到 160 FPS 的范围内,其速度和准确性都超过了所有已知的物体检测器。在GPU V100 上,YOLOv7 的准确率(56.8% AP)在 30 FPS 或更高的所有已知实时物体检测器中是最高的。此外,YOLOv7 在速度和准确性上都优于 YOLOR、YOLOX、Scaled-YOLOv4、YOLOv5 等其他物体检测器。该模型是在 MS COCO 数据集上从头开始训练的,没有使用任何其他数据集或预先训练的权重。YOLOv7 的源代码可在 GitHub 上获取。
SOTA 物体探测器的比较
从YOLO 对比表中的结果可以看出,我们提出的方法在速度和精确度之间的权衡是最好的。如果将 YOLOv7-tiny-SiLU 与YOLOv5 (r6.1)进行比较,我们的方法在帧速率上比YOLOv5 快 127 fps,在 AP 准确度上比YOLOv5 高 10.7%。此外,YOLOv7 在帧速率为 161 fps 时的 AP 率为 51.4%,而 PPYOLOE-L 在相同 AP 下的帧速率仅为 78 fps。在参数使用方面,YOLOv7 比 PPYOLOE-L 少 41%。
如果将推理速度为 114 fps 的 YOLOv7-X 与推理速度为 99 fps 的YOLOv5(r6.1) 相比,YOLOv7-X 可将 AP 提高 3.9%。如果将 YOLOv7-X 与类似规模的YOLOv5(r6.1) 相比,YOLOv7-X 的推理速度要快 31 fps。此外,在参数和计算量方面,YOLOv7-X 比YOLOv5(r6.1) 减少了 22% 的参数和 8% 的计算量,但 AP 提高了 2.2%(资料来源)。
性能
模型 | 参数 (M) |
FLOPs (G) |
尺寸 (像素) |
FPS | APtest/ val 50-95 |
APtest 50 |
APtest 75 |
APtest S |
APtest M |
APtest L |
---|---|---|---|---|---|---|---|---|---|---|
YOLOX-S | 9.0 | 26.8 | 640 | 102 | 40.5% / 40.5% | - | - | - | - | - |
YOLOX-M | 25.3 | 73.8 | 640 | 81 | 47.2% / 46.9% | - | - | - | - | - |
YOLOX-L | 54.2 | 155.6 | 640 | 69 | 50.1% / 49.7% | - | - | - | - | - |
YOLOX-X | 99.1 | 281.9 | 640 | 58 | 51.5% / 51.1% | - | - | - | - | - |
PPYOLOE-S | 7.9 | 17.4 | 640 | 208 | 43.1% / 42.7% | 60.5% | 46.6% | 23.2% | 46.4% | 56.9% |
PPYOLOE-M | 23.4 | 49.9 | 640 | 123 | 48.9% / 48.6% | 66.5% | 53.0% | 28.6% | 52.9% | 63.8% |
PPYOLOE-L | 52.2 | 110.1 | 640 | 78 | 51.4% / 50.9% | 68.9% | 55.6% | 31.4% | 55.3% | 66.1% |
PPYOLOE-X | 98.4 | 206.6 | 640 | 45 | 52.2% / 51.9% | 69.9% | 56.5% | 33.3% | 56.3% | 66.4% |
YOLOv5-N(r6.1) | 1.9 | 4.5 | 640 | 159 | - / 28.0% | - | - | - | - | - |
YOLOv5-S(r6.1) | 7.2 | 16.5 | 640 | 156 | - / 37.4% | - | - | - | - | - |
YOLOv5-M(r6.1) | 21.2 | 49.0 | 640 | 122 | - / 45.4% | - | - | - | - | - |
YOLOv5-L(r6.1) | 46.5 | 109.1 | 640 | 99 | - / 49.0% | - | - | - | - | - |
YOLOv5-X(r6.1) | 86.7 | 205.7 | 640 | 83 | - /50.7% | - | - | - | - | - |
YOLOR-CSP | 52.9 | 120.4 | 640 | 106 | 51.1% / 50.8% | 69.6% | 55.7% | 31.7% | 55.3% | 64.7% |
YOLOR-CSP-X | 96.9 | 226.8 | 640 | 87 | 53.0% / 52.7% | 71.4% | 57.9% | 33.7% | 57.1% | 66.8% |
YOLOv7-tiny-SiLU | 6.2 | 13.8 | 640 | 286 | 38.7% / 38.7% | 56.7% | 41.7% | 18.8% | 42.4% | 51.9% |
YOLOv7 | 36.9 | 104.7 | 640 | 161 | 51.4% / 51.2% | 69.7% | 55.9% | 31.8% | 55.5% | 65.0% |
YOLOv7-X | 71.3 | 189.9 | 640 | 114 | 53.1% / 52.9% | 71.2% | 57.8% | 33.8% | 57.1% | 67.4% |
YOLOv5-N6(r6.1) | 3.2 | 18.4 | 1280 | 123 | - / 36.0% | - | - | - | - | - |
YOLOv5-S6(R6.1) | 12.6 | 67.2 | 1280 | 122 | - / 44.8% | - | - | - | - | - |
YOLOv5-M6(r6.1) | 35.7 | 200.0 | 1280 | 90 | - / 51.3% | - | - | - | - | - |
YOLOv5-L6(r6.1) | 76.8 | 445.6 | 1280 | 63 | - / 53.7% | - | - | - | - | - |
YOLOv5-X6(r6.1) | 140.7 | 839.2 | 1280 | 38 | - /55.0% | - | - | - | - | - |
YOLOR-P6 | 37.2 | 325.6 | 1280 | 76 | 53.9% / 53.5% | 71.4% | 58.9% | 36.1% | 57.7% | 65.6% |
YOLOR-W6 | 79.8 | 453.2 | 1280 | 66 | 55.2% / 54.8% | 72.7% | 60.5% | 37.7% | 59.1% | 67.1% |
YOLOR-E6 | 115.8 | 683.2 | 1280 | 45 | 55.8% / 55.7% | 73.4% | 61.1% | 38.4% | 59.7% | 67.7% |
YOLOR-D6 | 151.7 | 935.6 | 1280 | 34 | 56.5% / 56.1% | 74.1% | 61.9% | 38.9% | 60.4% | 68.7% |
YOLOv7-W6 | 70.4 | 360.0 | 1280 | 84 | 54.9% / 54.6% | 72.6% | 60.1% | 37.3% | 58.7% | 67.1% |
YOLOv7-E6 | 97.2 | 515.2 | 1280 | 56 | 56.0% / 55.9% | 73.5% | 61.2% | 38.0% | 59.9% | 68.4% |
YOLOv7-D6 | 154.7 | 806.8 | 1280 | 44 | 56.6% / 56.3% | 74.0% | 61.8% | 38.8% | 60.1% | 69.5% |
YOLOv7-E6E | 151.7 | 843.2 | 1280 | 36 | 56.8% / 56.8% | 74.4% | 62.1% | 39.3% | 60.5% | 69.0% |
概述
实时物体检测是多物体跟踪、自动驾驶、机器人和医学图像分析等许多计算机视觉系统的重要组成部分。近年来,实时物体检测的发展主要集中在设计高效架构和提高各种 CPU、GPU 和神经处理单元(NPU)的推理速度上。YOLOv7 支持从边缘到云端的移动GPU 和GPU 设备。
传统的实时物体检测器侧重于结构优化,而 YOLOv7 则不同,它侧重于训练过程的优化。其中包括一些模块和优化方法,目的是在不增加推理成本的情况下提高物体检测的准确性,这一概念被称为 "可训练的无用包"。
主要功能
YOLOv7 引入了几项关键功能:
-
模型重新参数化:YOLOv7 提出了一种有计划的重参数化模型,这是一种适用于不同网络层的策略,具有梯度传播路径的概念。
-
动态标签分配:多输出层模型的训练提出了一个新问题:"如何为不同分支的输出分配动态目标?为了解决这个问题,YOLOv7 引入了一种新的标签分配方法,即从粗到细的引导标签分配法。
-
扩展和复合缩放YOLOv7 为实时对象检测器提出了 "扩展 "和 "复合缩放 "方法,可有效利用参数和计算。
-
效率:YOLOv7 提出的方法能有效减少最先进的实时物体检测器约 40% 的参数和 50% 的计算量,推理速度更快,检测精度更高。
使用示例
截至本文撰写之时,Ultralytics 仅支持 YOLOv7 的ONNX 和TensorRT 推断。
ONNX 出口
在Ultralytics 中使用 YOLOv7ONNX 模型:
- (可选)安装Ultralytics 并导出ONNX 模型,以便自动安装所需的依赖项:
pip install ultralytics
yolo export model=yolo11n.pt format=onnx
- 使用YOLOv7 软件包中的导出器导出所需的YOLOv7 模型:
git clone https://github.com/WongKinYiu/yolov7
cd yolov7
python export.py --weights yolov7-tiny.pt --grid --end2end --simplify --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 --img-size 640 640 --max-wh 640
- 使用以下脚本修改ONNX 模型图,使其与Ultralytics 兼容:
import numpy as np
import onnx
from onnx import helper, numpy_helper
# Load the ONNX model
model_path = "yolov7/yolov7-tiny.onnx" # Replace with your model path
model = onnx.load(model_path)
graph = model.graph
# Fix input shape to batch size 1
input_shape = graph.input[0].type.tensor_type.shape
input_shape.dim[0].dim_value = 1
# Define the output of the original model
original_output_name = graph.output[0].name
# Create slicing nodes
sliced_output_name = f"{original_output_name}_sliced"
# Define initializers for slicing (remove the first value)
start = numpy_helper.from_array(np.array([1], dtype=np.int64), name="slice_start")
end = numpy_helper.from_array(np.array([7], dtype=np.int64), name="slice_end")
axes = numpy_helper.from_array(np.array([1], dtype=np.int64), name="slice_axes")
steps = numpy_helper.from_array(np.array([1], dtype=np.int64), name="slice_steps")
graph.initializer.extend([start, end, axes, steps])
slice_node = helper.make_node(
"Slice",
inputs=[original_output_name, "slice_start", "slice_end", "slice_axes", "slice_steps"],
outputs=[sliced_output_name],
name="SliceNode",
)
graph.node.append(slice_node)
# Define segment slicing
seg1_start = numpy_helper.from_array(np.array([0], dtype=np.int64), name="seg1_start")
seg1_end = numpy_helper.from_array(np.array([4], dtype=np.int64), name="seg1_end")
seg2_start = numpy_helper.from_array(np.array([4], dtype=np.int64), name="seg2_start")
seg2_end = numpy_helper.from_array(np.array([5], dtype=np.int64), name="seg2_end")
seg3_start = numpy_helper.from_array(np.array([5], dtype=np.int64), name="seg3_start")
seg3_end = numpy_helper.from_array(np.array([6], dtype=np.int64), name="seg3_end")
graph.initializer.extend([seg1_start, seg1_end, seg2_start, seg2_end, seg3_start, seg3_end])
# Create intermediate tensors for segments
segment_1_name = f"{sliced_output_name}_segment1"
segment_2_name = f"{sliced_output_name}_segment2"
segment_3_name = f"{sliced_output_name}_segment3"
# Add segment slicing nodes
graph.node.extend(
[
helper.make_node(
"Slice",
inputs=[sliced_output_name, "seg1_start", "seg1_end", "slice_axes", "slice_steps"],
outputs=[segment_1_name],
name="SliceSegment1",
),
helper.make_node(
"Slice",
inputs=[sliced_output_name, "seg2_start", "seg2_end", "slice_axes", "slice_steps"],
outputs=[segment_2_name],
name="SliceSegment2",
),
helper.make_node(
"Slice",
inputs=[sliced_output_name, "seg3_start", "seg3_end", "slice_axes", "slice_steps"],
outputs=[segment_3_name],
name="SliceSegment3",
),
]
)
# Concatenate the segments
concat_output_name = f"{sliced_output_name}_concat"
concat_node = helper.make_node(
"Concat",
inputs=[segment_1_name, segment_3_name, segment_2_name],
outputs=[concat_output_name],
axis=1,
name="ConcatSwapped",
)
graph.node.append(concat_node)
# Reshape to [1, -1, 6]
reshape_shape = numpy_helper.from_array(np.array([1, -1, 6], dtype=np.int64), name="reshape_shape")
graph.initializer.append(reshape_shape)
final_output_name = f"{concat_output_name}_batched"
reshape_node = helper.make_node(
"Reshape",
inputs=[concat_output_name, "reshape_shape"],
outputs=[final_output_name],
name="AddBatchDimension",
)
graph.node.append(reshape_node)
# Get the shape of the reshaped tensor
shape_node_name = f"{final_output_name}_shape"
shape_node = helper.make_node(
"Shape",
inputs=[final_output_name],
outputs=[shape_node_name],
name="GetShapeDim",
)
graph.node.append(shape_node)
# Extract the second dimension
dim_1_index = numpy_helper.from_array(np.array([1], dtype=np.int64), name="dim_1_index")
graph.initializer.append(dim_1_index)
second_dim_name = f"{final_output_name}_dim1"
gather_node = helper.make_node(
"Gather",
inputs=[shape_node_name, "dim_1_index"],
outputs=[second_dim_name],
name="GatherSecondDim",
)
graph.node.append(gather_node)
# Subtract from 100 to determine how many values to pad
target_size = numpy_helper.from_array(np.array([100], dtype=np.int64), name="target_size")
graph.initializer.append(target_size)
pad_size_name = f"{second_dim_name}_padsize"
sub_node = helper.make_node(
"Sub",
inputs=["target_size", second_dim_name],
outputs=[pad_size_name],
name="CalculatePadSize",
)
graph.node.append(sub_node)
# Build the [2, 3] pad array:
# 1st row -> [0, 0, 0] (no padding at the start of any dim)
# 2nd row -> [0, pad_size, 0] (pad only at the end of the second dim)
pad_starts = numpy_helper.from_array(np.array([0, 0, 0], dtype=np.int64), name="pad_starts")
graph.initializer.append(pad_starts)
zero_scalar = numpy_helper.from_array(np.array([0], dtype=np.int64), name="zero_scalar")
graph.initializer.append(zero_scalar)
pad_ends_name = "pad_ends"
concat_pad_ends_node = helper.make_node(
"Concat",
inputs=["zero_scalar", pad_size_name, "zero_scalar"],
outputs=[pad_ends_name],
axis=0,
name="ConcatPadEnds",
)
graph.node.append(concat_pad_ends_node)
pad_values_name = "pad_values"
concat_pad_node = helper.make_node(
"Concat",
inputs=["pad_starts", pad_ends_name],
outputs=[pad_values_name],
axis=0,
name="ConcatPadStartsEnds",
)
graph.node.append(concat_pad_node)
# Create Pad operator to pad with zeros
pad_output_name = f"{final_output_name}_padded"
pad_constant_value = numpy_helper.from_array(
np.array([0.0], dtype=np.float32),
name="pad_constant_value",
)
graph.initializer.append(pad_constant_value)
pad_node = helper.make_node(
"Pad",
inputs=[final_output_name, pad_values_name, "pad_constant_value"],
outputs=[pad_output_name],
mode="constant",
name="PadToFixedSize",
)
graph.node.append(pad_node)
# Update the graph's final output to [1, 100, 6]
new_output_type = onnx.helper.make_tensor_type_proto(
elem_type=graph.output[0].type.tensor_type.elem_type, shape=[1, 100, 6]
)
new_output = onnx.helper.make_value_info(name=pad_output_name, type_proto=new_output_type)
# Replace the old output with the new one
graph.output.pop()
graph.output.extend([new_output])
# Save the modified model
onnx.save(model, "yolov7-ultralytics.onnx")
- 然后,您就可以加载修改后的ONNX 模型,并在Ultralytics 中正常运行推理:
from ultralytics import ASSETS, YOLO
model = YOLO("yolov7-ultralytics.onnx", task="detect")
results = model(ASSETS / "bus.jpg")
TensorRT 导出
-
按照ONNX 导出部分的步骤 1-2 进行操作。
-
安装
TensorRT
Python 软件包:
pip install tensorrt
- 运行以下脚本,将修改后的ONNX 模型转换为TensorRT 引擎:
# Based off of https://github.com/NVIDIA/TensorRT/blob/release/10.7/samples/python/introductory_parser_samples/onnx_resnet50.py
import tensorrt as trt
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
def GiB(val):
return val * 1 << 30
def build_engine_onnx(model_file):
builder = trt.Builder(TRT_LOGGER)
network = builder.create_network(0)
config = builder.create_builder_config()
parser = trt.OnnxParser(network, TRT_LOGGER)
config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, GiB(4))
with open(model_file, "rb") as model:
if not parser.parse(model.read()):
print("ERROR: Failed to parse the ONNX file.")
for error in range(parser.num_errors):
print(parser.get_error(error))
return None
config.set_flag(trt.BuilderFlag.FP16)
engine_bytes = builder.build_serialized_network(network, config)
with open(model_file.replace("onnx", "engine"), "wb") as f:
f.write(engine_bytes)
build_engine_onnx("yolov7-ultralytics.onnx") # path to the modified ONNX file
- 在Ultralytics 中加载并运行模型:
from ultralytics import ASSETS, YOLO
model = YOLO("yolov7-ultralytics.engine", task="detect")
results = model(ASSETS / "bus.jpg")
引用和致谢
我们衷心感谢 YOLOv7 作者在实时物体检测领域做出的重大贡献:
@article{wang2022yolov7,
title={YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors},
author={Wang, Chien-Yao and Bochkovskiy, Alexey and Liao, Hong-Yuan Mark},
journal={arXiv preprint arXiv:2207.02696},
year={2022}
}
YOLOv7 的原始论文可在arXiv 上找到。作者公开了他们的工作,代码库可以在GitHub 上访问。我们感谢他们为推动这一领域的发展和让更多人了解他们的工作所做的努力。
常见问题
什么是 YOLOv7,为什么它被认为是实时物体检测领域的一项突破?
YOLOv7 是一种先进的实时物体检测模型,其速度和准确性无与伦比。它在参数使用和推理速度方面都超过了 YOLOX、YOLOv5 和 PPYOLOE 等其他模型。YOLOv7 的显著特点包括模型重参数化和动态标签分配,在不增加推理成本的情况下优化了性能。有关YOLOv7 架构的更多技术细节,以及与其他先进物体检测器的比较指标,请参阅YOLOv7 论文。
YOLOv7 与之前的YOLO 型号(如 YOLOv4 和YOLOv5 )相比有何改进?
YOLOv7 引入了多项创新,包括模型重参数化和动态标签分配,从而增强了训练过程并提高了推理的准确性。与YOLOv5 相比,YOLOv7 显著提高了速度和准确性。例如,YOLOv7-X 与YOLOv5-X 相比,准确率提高了 2.2%,参数减少了 22%。详细比较见YOLOv7 与 SOTA 物体探测器性能比较表。
我可以在Ultralytics 工具和平台上使用 YOLOv7 吗?
目前,Ultralytics 只支持 YOLOv7ONNX 和TensorRT 推断。要使用Ultralytics 运行 YOLOv7 的ONNX 和TensorRT 导出版本,请查看 "使用示例"部分。
如何使用我的数据集训练自定义 YOLOv7 模型?
要安装和训练自定义 YOLOv7 模型,请按照以下步骤操作:
- 克隆 YOLOv7 版本库:
git clone https://github.com/WongKinYiu/yolov7
- 导航至克隆目录并安装依赖项:
cd yolov7 pip install -r requirements.txt
-
根据资源库中提供的使用说明准备数据集和配置模型参数。 如需进一步指导,请访问 YOLOv7 GitHub 代码库获取最新信息和更新。
-
训练完成后,您可以将模型导出到ONNX 或TensorRT ,以便在Ultralytics 中使用,如 "使用示例"所示。
YOLOv7 中引入了哪些主要功能和优化?
YOLOv7 提供了几项关键功能,彻底改变了实时目标检测:
- 模型重新参数化:通过优化梯度传播路径提高模型性能。
- 动态标签分配:使用由粗到细的引导方法,为不同分支的输出分配动态目标,提高准确性。
- 扩展和复合缩放:有效利用参数和计算,为各种实时应用扩展模型。
- 效率:与其他最先进的模型相比,参数数量减少 40%,计算量减少 50%,同时推理速度更快。
有关这些功能的详细信息,请参阅YOLOv7 概述部分。