跳至内容

快速入门

安装Ultralytics

Ultralytics provides various installation methods including pip, conda, and Docker. Install YOLO via the ultralytics pip 软件包的最新稳定版本,或通过克隆 Ultralytics GitHub 存储库 获取最新版本。可以使用 Docker 在隔离的容器中执行软件包,避免本地安装。



观看: Ultralytics YOLO 快速入门指南

安装

PyPI -Python 版本

安装 ultralytics 软件包,或通过运行 pip install -U ultralytics.请访问Python Package Index (PyPI),了解更多有关 ultralytics 包装 https://pypi.org/project/ultralytics/.

PyPI - 版本 下载

# Install the ultralytics package from PyPI
pip install ultralytics

您还可以安装 ultralytics 直接从 GitHub 知识库.如果您需要最新的开发版本,这可能会很有用。确保在系统中安装了 Git 命令行工具。Git 命令行工具 @main 命令安装 main 分支,并可修改为另一个分支,即 @my-branch或完全删除,默认为 main 分支。

# Install the ultralytics package from GitHub
pip install git+https://github.com/ultralytics/ultralytics.git@main

Conda 是 pip 的替代软件包管理器,也可用于安装。更多详情请访问 Anaconda:https://anaconda.org/conda-forge/ultralytics 。Ultralytics 用于更新 conda 软件包的 feedstock 资源库位于https://github.com/conda-forge/ultralytics-feedstock/。

康达版本 康达下载 康达食谱 Conda 平台

# Install the ultralytics package using conda
conda install -c conda-forge ultralytics

备注

如果在CUDA 环境中安装,最佳做法是安装 ultralytics, pytorchpytorch-cuda 在同一命令中允许 conda 软件包管理器解决任何冲突,或者安装 pytorch-cuda 最后,允许它覆盖CPU-specific pytorch 如有必要,请将该程序包添加到"... "中。

# Install all packages together using conda
conda install -c pytorch -c nvidia -c conda-forge pytorch torchvision pytorch-cuda=11.8 ultralytics

Conda Docker 映像

Ultralytics Conda Docker 映像还可从 DockerHub.这些图像基于 迷你世界3 是开始使用 ultralytics 在 Conda 环境中。

# Set image name as a variable
t=ultralytics/ultralytics:latest-conda

# Pull the latest ultralytics image from Docker Hub
sudo docker pull $t

# Run the ultralytics image in a container with GPU support
sudo docker run -it --ipc=host --gpus all $t  # all GPUs
sudo docker run -it --ipc=host --gpus '"device=2,3"' $t  # specify GPUs

克隆 ultralytics 如果你有兴趣参与开发,或希望使用最新的源代码进行实验,请访问软件仓库。克隆后,导航到目录,以可编辑模式安装软件包 -e 使用 pip。

GitHub 最后提交 GitHub 提交活动

# 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 .

利用 Docker 轻松执行 ultralytics 软件包在一个隔离的容器中,确保在各种环境中都能实现一致、流畅的性能。通过选择官方 ultralytics 图片来自 Docker HubUltralytics 提供 5 种主要支持的 Docker 镜像,每种镜像都为不同平台和用例提供高兼容性和高效性:

Docker 映像版本 Docker 拉动

  • Dockerfile:推荐用于培训的GPU 镜像。
  • Dockerfile-arm64:针对 ARM64 架构进行了优化,可在 Raspberry Pi 等设备和其他基于 ARM64 的平台上部署。
  • Dockerfile-cpu :基于 Ubuntu 的CPU- 仅适用于推理和无 GPU 环境的版本。
  • Dockerfile-jetson:专为NVIDIA Jetson 设备定制,集成了针对这些平台优化的GPU 支持。
  • Dockerfile-python :仅包含Python 和必要依赖项的最小镜像,是轻量级应用和开发的理想选择。
  • Dockerfile-conda:基于 Miniconda3,使用 conda 安装ultralytics 软件包。

下面是获取并执行最新映像的命令:

# Set image name as a variable
t=ultralytics/ultralytics:latest

# Pull the latest ultralytics image from Docker Hub
sudo docker pull $t

# Run the ultralytics image in a container with GPU support
sudo docker run -it --ipc=host --gpus all $t  # all GPUs
sudo docker run -it --ipc=host --gpus '"device=2,3"' $t  # specify GPUs

上述命令使用最新的 ultralytics 图像图像 -it 标记会分配一个伪 TTY 并保持 stdin 开启,以便与容器进行交互。标记 --ipc=host 标记将 IPC(进程间通信)命名空间设置为主机命名空间,这对进程间共享内存至关重要。进程间通信 --gpus all flag 可以访问容器内所有可用的 GPU,这对于需要GPU 计算的任务至关重要。

注意:要在容器中处理本地机器上的文件,请使用 Docker 卷将本地目录挂载到容器中:

# Mount local directory to a directory inside the container
sudo docker run -it --ipc=host --gpus all -v /path/on/host:/path/in/container $t

改变 /path/on/host 的目录路径,以及 /path/in/container 与 Docker 容器内的所需路径一致,以便访问。

有关 Docker 的高级用法,请访问Ultralytics Docker 指南

参见 ultralytics pyproject.toml 文件中的依赖项列表。请注意,上述所有示例都安装了所有必需的依赖项。

提示

PyTorch requirements vary by operating system and CUDA requirements, so it's recommended to install PyTorch first following instructions at https://pytorch.org/get-started/locally.

PyTorch 安装说明

将Ultralytics 与CLI

Ultralytics 命令行界面 (CLI) 允许使用简单的单行命令,而无需Python 环境。CLI 不需要定制或Python 代码。您只需使用 yolo 指挥部。查看 CLI 指南 to learn more about using YOLO from the command line.

示例

Ultralytics yolo 命令使用以下语法:

yolo TASK MODE ARGS

查看全部 ARGS 在充分 配置指南 或使用 yolo cfg CLI 指挥。

Train a detection model for 10 epochs with an initial learning_rate of 0.01

yolo train data=coco8.yaml model=yolo11n.pt epochs=10 lr0=0.01

在图像大小为 320 时,使用预训练的分割模型预测 YouTube 视频:

yolo predict model=yolo11n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320

在批量大小为 1、图像大小为 640 时,验证预先训练的检测模型:

yolo val model=yolo11n.pt data=coco8.yaml batch=1 imgsz=640

Export a yolo11n classification model to ONNX format at image size 224 by 128 (no TASK required)

yolo export model=yolo11n-cls.pt format=onnx imgsz=224,128

运行特殊命令可查看版本、查看设置、运行检查等:

yolo help
yolo checks
yolo version
yolo settings
yolo copy-cfg
yolo cfg

警告

参数必须以 arg=val 对,用等号分割 = 符号,每对之间用空格分隔。请勿使用 -- 参数 , 参数之间。

  • yolo predict model=yolo11n.pt imgsz=640 conf=0.25
  • yolo predict model yolo11n.pt imgsz 640 conf 0.25 ❌(缺失 =)
  • yolo predict model=yolo11n.pt, imgsz=640, conf=0.25 ❌(不要使用 ,)
  • yolo predict --model yolo11n.pt --imgsz 640 --conf 0.25 ❌(不要使用 --)

CLI 指南

将Ultralytics 与Python

YOLO's Python interface allows for seamless integration into your Python projects, making it easy to load, run, and process the model's output. Designed with simplicity and ease of use in mind, the Python interface enables users to quickly implement object detection, segmentation, and classification in their projects. This makes YOLO's Python interface an invaluable tool for anyone looking to incorporate these functionalities into their Python projects.

For example, users can load a model, train it, evaluate its performance on a validation set, and even export it to ONNX format with just a few lines of code. Check out the Python Guide to learn more about using YOLO within your Python projects.

示例

from ultralytics import YOLO

# Create a new YOLO model from scratch
model = YOLO("yolo11n.yaml")

# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo11n.pt")

# Train the model using the 'coco8.yaml' dataset for 3 epochs
results = model.train(data="coco8.yaml", epochs=3)

# Evaluate the model's performance on the validation set
results = model.val()

# Perform object detection on an image using the model
results = model("https://ultralytics.com/images/bus.jpg")

# Export the model to ONNX format
success = model.export(format="onnx")

Python 指南

Ultralytics 设置

Ultralytics 库提供了一个功能强大的设置管理系统,可对实验进行精细控制。通过使用 SettingsManager 设在 ultralytics.utils module, users can readily access and alter their settings. These are stored in a JSON file in the environment user configuration directory, and can be viewed or modified directly within the Python environment or via the Command-Line Interface (CLI).

检查设置

要深入了解设置的当前配置,可以直接查看:

查看设置

您可以使用Python 查看您的设置。首先导入 settings 对象的 ultralytics 模块。使用以下命令打印并返回设置:

from ultralytics import settings

# View all settings
print(settings)

# Return a specific setting
value = settings["runs_dir"]

此外,命令行界面也允许您使用简单的命令检查设置:

yolo settings

修改设置

Ultralytics 允许用户轻松修改设置。可以通过以下方式进行更改:

更新设置

在Python 环境中,调用 update 上的 settings 对象来更改设置:

from ultralytics import settings

# Update a setting
settings.update({"runs_dir": "/path/to/runs"})

# Update multiple settings
settings.update({"runs_dir": "/path/to/runs", "tensorboard": False})

# Reset settings to default values
settings.reset()

如果您喜欢使用命令行界面,可以使用以下命令修改设置:

# Update a setting
yolo settings runs_dir='/path/to/runs'

# Update multiple settings
yolo settings runs_dir='/path/to/runs' tensorboard=False

# Reset settings to default values
yolo settings reset

了解设置

下表概述了Ultralytics 中可供调整的设置。每项设置都附有示例值、数据类型和简要说明。

名称示例值数据类型说明
settings_version'0.0.4'strUltralytics 设置版本(与Ultralytics pip版本不同)
datasets_dir'/path/to/datasets'str存储数据集的目录
weights_dir'/path/to/weights'str存储模型权重的目录
runs_dir'/path/to/runs'str存储实验运行的目录
uuid'a1b2c3d4'str当前设置的唯一标识符
syncTruebool是否将分析和崩溃同步到 HUB
api_key''strUltralytics HUBAPI 密钥
clearmlTrueboolWhether to use ClearML logging
cometTruebool是否使用Comet ML 进行实验跟踪和可视化
dvcTruebool是否使用DVC 进行实验跟踪和版本控制
hubTruebool是否使用Ultralytics HUB 集成
mlflowTrueboolWhether to use MLFlow for experiment tracking
neptuneTrueboolWhether to use Neptune for experiment tracking
raytuneTrueboolWhether to use Ray Tune for hyperparameter tuning
tensorboardTrueboolWhether to use TensorBoard for visualization
wandbTrueboolWhether to use Weights & Biases logging
vscode_msgTrueboolWhen VS Code terminal detected, enables prompt to download Ultralytics-Snippets extension.

当您浏览项目或实验时,请务必重新查看这些设置,以确保它们的最佳配置符合您的需求。

常见问题

How do I install Ultralytics using pip?

To install Ultralytics with pip, execute the following command:

pip install ultralytics

对于最新的稳定版,这将安装 ultralytics 软件包直接从Python Package Index (PyPI) 获取。有关详细信息,请访问 ultralytics 软件包.

或者,您也可以直接从 GitHub 安装最新的开发版本:

pip install git+https://github.com/ultralytics/ultralytics.git

确保在系统中安装了 Git 命令行工具。

Can I install Ultralytics YOLO using conda?

Yes, you can install Ultralytics YOLO using conda by running:

conda install -c conda-forge ultralytics

这种方法是 pip 的最佳替代方案,可确保与环境中的其他软件包兼容。对于CUDA 环境,最好安装 ultralytics, pytorchpytorch-cuda 同时解决任何冲突:

conda install -c pytorch -c nvidia -c conda-forge pytorch torchvision pytorch-cuda=11.8 ultralytics

更多说明,请访问Conda 快速入门指南

What are the advantages of using Docker to run Ultralytics YOLO?

Using Docker to run Ultralytics YOLO provides an isolated and consistent environment, ensuring smooth performance across different systems. It also eliminates the complexity of local installation. Official Docker images from Ultralytics are available on Docker Hub, with different variants tailored for GPU, CPU, ARM64, NVIDIA Jetson, and Conda environments. Below are the commands to pull and run the latest image:

# Pull the latest ultralytics image from Docker Hub
sudo docker pull ultralytics/ultralytics:latest

# Run the ultralytics image in a container with GPU support
sudo docker run -it --ipc=host --gpus all ultralytics/ultralytics:latest

有关更详细的 Docker 说明,请查看Docker 快速入门指南

如何克隆Ultralytics 仓库进行开发?

要克隆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 .

通过这种方式,您可以为项目做出贡献,或使用最新的源代码进行实验。更多详情,请访问Ultralytics GitHub 代码库

Why should I use Ultralytics YOLO CLI?

The Ultralytics YOLO command line interface (CLI) simplifies running object detection tasks without requiring Python code. You can execute single-line commands for tasks like training, validation, and prediction straight from your terminal. The basic syntax for yolo 命令是

yolo TASK MODE ARGS

例如,使用指定参数训练检测模型:

yolo train data=coco8.yaml model=yolo11n.pt epochs=10 lr0=0.01

查看CLI 指南全文,了解更多命令和使用示例。

📅 Created 11 months ago ✏️ Updated 21 days ago

评论