Link to this section安装 Ultralytics#
Ultralytics 提供了多种安装方式,包括 pip、conda 和 Docker。你可以通过 ultralytics pip 包安装 YOLO 以获取最新的稳定版本,或者通过克隆 Ultralytics GitHub 仓库 获取最新版本。你也可以选择使用 Docker 在隔离容器中运行该包,从而避免本地安装。
Watch: Ultralytics YOLO Quick Start Guide
Install or update the ultralytics package using pip by running pip install -U ultralytics. For more details on the ultralytics package, visit the Python Package Index (PyPI).
# Install or upgrade the ultralytics package from PyPI
pip install -U ultralyticsYou can also install ultralytics directly from the Ultralytics GitHub repository. This can be useful if you want the latest development version. Ensure you have the Git command-line tool installed, and then run:
# Install the ultralytics package from GitHub
pip install git+https://github.com/ultralytics/ultralytics.git@main查看 ultralytics pyproject.toml 文件以获取依赖列表。请注意,上面的所有示例都会安装所有必需的依赖项。
Link to this section无头服务器安装#
对于没有显示器的服务器环境(例如云虚拟机、Docker 容器、CI/CD 流水线),请使用 ultralytics-opencv-headless 包。这与标准 ultralytics 包相同,但它依赖 opencv-python-headless 而不是 opencv-python,从而避免了不必要的 GUI 依赖和潜在的 libGL 错误。
pip install ultralytics-opencv-headless两个包提供相同的功能和 API。无头变体只是排除了需要显示库的 OpenCV GUI 组件。
Link to this section高级安装#
虽然标准安装方法涵盖了大多数用例,但你可能需要为开发或自定义配置进行更量身定制的设置。
如果你需要持久的自定义修改,你可以 fork Ultralytics 仓库,对 pyproject.toml 或其他代码进行更改,然后从你的分支安装。
- Fork Ultralytics GitHub 仓库 到你自己的 GitHub 账号。
- 克隆 你的分支到本地:
git clone https://github.com/YOUR_USERNAME/ultralytics.git cd ultralytics - 创建新分支 以进行你的更改:
git checkout -b my-custom-branch - 进行修改 到
pyproject.toml或其他文件,根据需要。 - 提交并推送 你的更改:
git add . git commit -m "My custom changes" git push origin my-custom-branch - 安装 使用 pip 并配合
git+https语法,指向你的分支:pip install git+https://github.com/YOUR_USERNAME/ultralytics.git@my-custom-branch
Link to this section通过 CLI 使用 Ultralytics#
Ultralytics 命令行界面 (CLI) 允许使用简单的单行命令,而无需 Python 环境。CLI 不需要自定义或 Python 代码;使用 yolo 命令从终端运行所有任务。有关从命令行使用 YOLO 的更多信息,请参阅 CLI 指南。
参数必须作为 arg=value 对传递,以等号 = 分隔并用空格分隔。不要使用 -- 参数前缀或在参数之间使用逗号 ,。
yolo predict model=yolo26n.pt imgsz=640 conf=0.25✅yolo predict model yolo26n.pt imgsz 640 conf 0.25❌(缺少=)yolo predict model=yolo26n.pt, imgsz=640, conf=0.25❌(不要使用,)yolo predict --model yolo26n.pt --imgsz 640 --conf 0.25❌(不要使用--)yolo solution model=yolo26n.pt imgsz=640 conf=0.25❌(使用solutions,而不是solution)
Link to this section通过 Python 使用 Ultralytics#
Ultralytics YOLO Python 接口提供与 Python 项目的无缝集成,从而轻松加载、运行和处理模型输出。Python 接口设计简洁,允许用户快速实现 对象检测、实例分割、语义分割 和 分类。这使得 YOLO Python 接口成为将这些功能合并到 Python 项目中的宝贵工具。
例如,用户只需几行代码即可加载模型、进行训练、评估性能并将其导出为 ONNX 格式。探索 Python 指南 以了解更多关于在 Python 项目中使用 YOLO 的信息。
from ultralytics import YOLO
# Create a new YOLO model from scratch
model = YOLO("yolo26n.yaml")
# Load a pretrained YOLO model (recommended for training)
model = YOLO("yolo26n.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")Link to this sectionUltralytics 设置#
Ultralytics 库包含一个用于精细控制实验的 SettingsManager,允许用户轻松访问和修改设置。这些设置存储在环境用户配置目录下的 JSON 文件中,可以在 Python 环境或通过命令行界面 (CLI) 查看或修改。
Link to this section检查设置#
若要查看你当前的配置设置:
Use Python to view your settings by importing the settings object from the ultralytics module. Print and return settings with these commands:
from ultralytics import settings
# View all settings
print(settings)
# Return a specific setting
value = settings["runs_dir"]Link to this section修改设置#
Ultralytics 使你可以通过以下方式轻松修改设置:
In Python, use the update method on the settings object:
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()Link to this section理解设置#
下表概述了 Ultralytics 中的可调设置,包括示例值、数据类型和说明。
| 名称 | 示例值 | 数据类型 | 描述 |
|---|---|---|---|
settings_version | '0.0.6' | str | Ultralytics settings version (distinct from the Ultralytics pip version) |
datasets_dir | '/path/to/datasets' | str | 存储数据集的目录 |
weights_dir | '/path/to/weights' | str | 存储模型权重的目录 |
runs_dir | '/path/to/runs' | str | 存储实验运行结果的目录 |
uuid | 'a1b2c3d4' | str | 当前设置的唯一标识符 |
sync | True | bool | Option to sync analytics and crashes to Ultralytics Platform |
api_key | '' | str | Ultralytics Platform API Key |
clearml | True | bool | Option to use ClearML logging |
comet | True | bool | Option to use Comet ML for experiment tracking and visualization |
dvc | True | bool | Option to use DVC for experiment tracking and version control |
hub | True | bool | Option to use Ultralytics Platform integration |
mlflow | True | bool | Option to use MLFlow for experiment tracking |
neptune | True | bool | Option to use Neptune for experiment tracking |
raytune | True | bool | Option to use Ray Tune for hyperparameter tuning |
tensorboard | False | bool | Option to use TensorBoard for visualization |
wandb | False | bool | Option to use Weights & Biases logging |
vscode_msg | True | bool | When a VS Code terminal is detected, enables a prompt to download the Ultralytics-Snippets extension. |
在进行项目或实验的过程中,请回顾这些设置以确保最佳配置。
Link to this section常见问题解答#
Link to this section我该如何使用 pip 安装 Ultralytics?#
使用 pip 安装 Ultralytics:
pip install -U ultralyticsThis installs the latest stable release of the ultralytics package from PyPI. To install the development version directly from GitHub:
pip install git+https://github.com/ultralytics/ultralytics.git请确保你的系统中安装了 Git 命令行工具。
Link to this section我可以使用 conda 安装 Ultralytics YOLO 吗?#
可以,使用 conda 安装 Ultralytics YOLO:
conda install -c conda-forge ultralytics这种方法是 pip 的绝佳替代方案,可确保与其他软件包的兼容性。对于 CUDA 环境,请同时安装 ultralytics、pytorch 和 pytorch-cuda 以解决冲突:
conda install -c pytorch -c nvidia -c conda-forge pytorch torchvision pytorch-cuda=11.8 ultralytics有关更多说明,请参阅 Conda quickstart guide。
Link to this section使用 Docker 运行 Ultralytics YOLO 有什么优势?#
Docker 为 Ultralytics YOLO 提供了一个隔离且一致的环境,确保跨系统的流畅性能,并避免本地安装的复杂性。官方 Docker 镜像可在 Docker Hub 上获取,并提供针对 GPU、CPU、ARM64、NVIDIA Jetson 和 Conda 的变体。若要拉取并运行最新镜像:
# 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 --runtime=nvidia --gpus all ultralytics/ultralytics:latest有关 Docker 的详细说明,请参阅 Docker quickstart guide。
Link to this section我该如何克隆 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 repository。
Link to this section为什么我应该使用 Ultralytics YOLO CLI?#
Ultralytics YOLO CLI 简化了在没有 Python 代码的情况下运行目标检测任务的过程,只需在终端输入一行命令即可进行训练、验证和预测。基本语法如下:
yolo TASK MODE ARGS例如,训练一个检测模型:
yolo train data=coco8.yaml model=yolo26n.pt epochs=10 lr0=0.01在完整的 CLI Guide 中探索更多命令和使用示例。