YOLO-World 模型引入了一种先进的、基于 UltralyticsYOLOv8 的实时开放词汇目标检测方法。这项创新使得能够根据描述性文本检测图像中的任何物体。通过显著降低计算需求同时保持有竞争力的性能,YOLO-World 成为众多基于视觉的应用的多功能工具。
YOLO-World 解决了传统开放词汇检测模型面临的挑战,这些模型通常依赖于繁琐的 Transformer 模型,需要大量的计算资源。这些模型对预定义对象类别的依赖也限制了它们在动态场景中的实用性。YOLO-World 通过开放词汇检测功能重振了 YOLOv8 框架,采用视觉-语言建模 并在大型数据集上进行预训练,从而擅长以无与伦比的效率识别零样本场景中的各种对象。
主要功能:
YOLO-World 的架构由三部分组成:
基于 Darknet 主干网络,提取多尺度图像特征。 采用 Path Aggregation Network (PAN) 增强特征融合。
使用 CLIP 文本编码器 将类别名称(如“背包”)转换为语义向量。 文本嵌入可离线缓存,避免实时计算开销。
Re-parameterizable Vision-Language Path Aggregation Network:动态融合图像特征与文本嵌入,通过区域-文本对比损失优化检测。 实现跨模态对齐,提升对未知物体的泛化能力。
Ultralytics 提供了多种安装方法,包括 pip、conda 和 Docker。您可以通过以下方式安装 YOLO: ultralytics pip 软件包(用于最新的稳定版本),或通过克隆 Ultralytics GitHub 仓库 对于最新版本。Docker 也是一种在隔离容器中运行软件包的选项,这样可以避免本地安装。
pip install -U ultralytics也可以安装 ultralytics 直接从 Ultralytics GitHub 仓库。如果想要最新的开发版本,这将非常有用。确保您已安装 Git 命令行工具,然后运行:
# Install the ultralytics package from GitHub
pip install git+https://github.com/ultralytics/ultralytics.git@mainConda 可以用作 pip 的替代包管理器:
# Install the ultralytics package using conda
conda install -c conda-forge ultralytics# Clone the ultralytics repository
git clone https://github.com/ultralytics/ultralytics
# Navigate to the cloned directory
cd ultralytics
# Install the package in editable mode for development
pip install -e .PyTorch pretrained *.pt 模型以及配置 *.yaml 文件可以传递给 YOLOWorld() 类,以便在 python 中创建模型实例:
from ultralytics import YOLOWorld
# Load a pretrained YOLOv8s-worldv2 model
model = YOLOWorld("yolov8s-worldv2.pt")
# Train the model on the COCO8 example dataset for 100 epochs
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)
# Run inference with the YOLO-World model on the 'bus.jpg' image
results = model("path/to/bus.jpg")使用以下方法可以轻松实现对象检测 predict method,如下所示:
from ultralytics import YOLOWorld
# Initialize a YOLO-World model
model = YOLOWorld("yolov8s-world.pt") # or select yolov8m/l-world.pt for different sizes
# Execute inference with the YOLOv8s-world model on the specified image
results = model.predict("path/to/image.jpg")
# Show results
results[0].show()在数据集上进行模型验证的流程如下:
from ultralytics import YOLO
# Create a YOLO-World model
model = YOLO("yolov8s-world.pt") # or select yolov8m/l-world.pt for different sizes
# Conduct model validation on the COCO8 example dataset
metrics = model.val(data="coco8.yaml")使用 YOLO-World 模型在视频/图像上进行对象跟踪的流程如下:
from ultralytics import YOLO
# Create a YOLO-World model
model = YOLO("yolov8s-world.pt") # or select yolov8m/l-world.pt for different sizes
# Track with a YOLO-World model on a video
results = model.track(source="path/to/video.mp4")YOLO-World 框架允许通过自定义提示动态指定类,从而使用户能够根据其特定需求定制模型,而无需重新训练。此功能对于将模型调整到新的领域或最初不属于训练数据的特定任务特别有用。通过设置自定义提示,用户可以从根本上引导模型将重点放在感兴趣的对象上,从而提高检测结果的相关性和准确性。
如果只需要检测 'person' 和 'bus' 对象,可以直接指定这些类别:
from ultralytics import YOLO
# Initialize a YOLO-World model
model = YOLO("yolov8s-world.pt") # or choose yolov8m/l-world.pt
# Define custom classes
model.set_classes(["person", "bus"])
# Execute prediction for specified categories on an image
results = model.predict("path/to/image.jpg")
# Show results
results[0].show()设置自定义类别后,您还可以保存模型。通过此操作,您将创建一个专为特定应用场景YOLO版本。该过程会将自定义类别定义直接嵌入模型文件,使模型无需额外调整即可直接使用指定类别。请按以下步骤保存并YOLO:
首先加载一个 YOLO-World 模型,为其设置自定义类并保存它:
from ultralytics import YOLO
# Initialize a YOLO-World model
model = YOLO("yolov8s-world.pt") # or select yolov8m/l-world.pt
# Define custom classes
model.set_classes(["person", "bus"])
# Save the model with the defined offline vocabulary
model.save("custom_yolov8s.pt")保存后,custom_yolov8s.pt 模型表现得像任何其他预训练的 YOLOv8 模型,但有一个关键区别:它现在已优化为仅 detect 您定义的类别。这种定制可以显著提高您特定应用场景的 detect 性能和效率。
from ultralytics import YOLO
# Load your custom model
model = YOLO("custom_yolov8s.pt")
# Run inference to detect your custom classes
results = model.predict("path/to/image.jpg")
# Show results
results[0].show()使用自定义词汇表进行保存的优势