# YOLOv8 检测

使用 Ultralytics YOLOv8 和 YOLOv11 运行实时目标检测。

{% hint style="success" %}
所有示例都可以在通过以下方式租用的 GPU 服务器上运行 [CLORE.AI 市场](https://clore.ai/marketplace).
{% endhint %}

{% hint style="info" %}
**更新：YOLOv11（2025）— 提速 22%**

YOLOv11 现已通过相同的 `ultralytics` 软件包提供。它带来 **推理速度提高 22%** 并在相同简易 API 下相比 YOLOv8 提升了 mAP，新功能包括定向边界框（OBB）检测。通过运行以下命令升级 `pip install -U ultralytics`.
{% endhint %}

## 在 CLORE.AI 上租用

1. 访问 [CLORE.AI 市场](https://clore.ai/marketplace)
2. 按 GPU 类型、显存和价格筛选
3. 选择 **按需（固定费率）** （固定费率）或 **竞价（Spot）** （竞价价格）
4. 配置您的订单：
   * 选择 Docker 镜像
   * 设置端口（SSH 使用 TCP，网页界面使用 HTTP）
   * 如有需要添加环境变量
   * 输入启动命令
5. 选择支付方式： **CLORE**, **BTC**，或 **USDT/USDC**
6. 创建订单并等待部署

### 访问您的服务器

* 在以下位置查找连接详情 **我的订单**
* Web 界面：使用 HTTP 端口的 URL
* SSH： `ssh -p <port> root@<proxy-address>`

## 什么是 YOLOv8？

YOLOv8 是一款高性能的 YOLO 模型，提供：

* 目标检测
* 实例分割
* 姿态估计
* 图像分类
* 目标跟踪

## 什么是 YOLOv11？

YOLOv11（2025）是最新一代，新增：

* **推理速度提高 22%** 与 YOLOv8 相比
* 在所有模型规模上更高的 mAP
* **定向边界框（OBB）** 检测 — 新任务
* 改进的架构（C3k2 模块、SPPF、C2PSA）
* 相同的 `ultralytics` 软件包，可直接替换

### 支持的任务（YOLOv11）

| 任务         | 后缀      | 说明                |
| ---------- | ------- | ----------------- |
| `detect`   | *（无）*   | 使用边界框的目标检测        |
| `segment`  | `-seg`  | 带掩码的实例分割          |
| `classify` | `-cls`  | 图像分类              |
| `pose`     | `-pose` | 人体姿态估计            |
| `obb`      | `-obb`  | **新** 定向边界框（旋转检测） |

## 模型规模

### YOLOv8 模型

| 模型      | 规模    | mAP  | 速度（RTX 3090） |
| ------- | ----- | ---- | ------------ |
| YOLOv8n | 3.2M  | 37.3 | \~1ms        |
| YOLOv8s | 11.2M | 44.9 | \~2ms        |
| YOLOv8m | 25.9M | 50.2 | \~4ms        |
| YOLOv8l | 43.7M | 52.9 | \~6ms        |
| YOLOv8x | 68.2M | 53.9 | \~8ms        |

### YOLOv11 模型

| 模型      | 规模    | mAP  | 速度（RTX 3090） |
| ------- | ----- | ---- | ------------ |
| yolo11n | 2.6M  | 39.5 | \~0.8ms      |
| yolo11s | 9.4M  | 47.0 | \~1.5ms      |
| yolo11m | 20.1M | 51.5 | \~3.2ms      |
| yolo11l | 25.3M | 53.4 | \~4.7ms      |
| yolo11x | 56.9M | 54.7 | \~6.5ms      |

### YOLOv8 与 YOLOv11 对比

| 指标                  | YOLOv8x | yolo11x | 改进            |
| ------------------- | ------- | ------- | ------------- |
| 参数量                 | 68.2M   | 56.9M   | **小 17%**     |
| mAP50-95（COCO）      | 53.9    | 54.7    | **+0.8 mAP**  |
| 推理（RTX 3090）        | \~8ms   | \~6.5ms | **+22% 更快**   |
| FPS（RTX 3090，640px） | \~150   | \~183   | **+22% 更快**   |
| OBB 任务              | ❌       | ✅       | **v11 中的新功能** |

## 快速部署

**Docker 镜像：**

```
pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime
```

**端口：**

```
22/tcp
7860/http
```

**命令（YOLOv11）：**

```bash
pip install ultralytics gradio && \
python -c "
import gradio as gr
from ultralytics import YOLO
from PIL import Image

model = YOLO('yolo11m.pt')

def detect(image):
    results = model(image)
    return Image.fromarray(results[0].plot())

demo = gr.Interface(fn=detect, inputs=gr.Image(type='pil'), outputs=gr.Image(), title='YOLOv11 Detection')
demo.launch(server_name='0.0.0.0', server_port=7860)
"
```

## 访问您的服务

部署后，查找您的 `http_pub` URL 在 **我的订单**:

1. 转到 **我的订单** 页面
2. 点击您的订单
3. 查找 `http_pub` URL（例如， `abc123.clorecloud.net`)

使用 `https://YOUR_HTTP_PUB_URL` 代替 `localhost` 在下面的示例中。

## 安装

```bash
pip install ultralytics
```

相同的软件包同时支持 YOLOv8 和 YOLOv11。升级以获取 YOLOv11：

```bash
pip install -U ultralytics
```

## YOLOv11 目标检测

### 使用 yolo11m 的基础检测

```python
from ultralytics import YOLO
from PIL import Image

# 加载 YOLOv11 中等模型
model = YOLO('yolo11m.pt')

# 运行推理
results = model('image.jpg')

# 显示结果
results[0].show()

# 保存结果
results[0].save('output.jpg')
```

### 获取检测结果

```python
results = model('image.jpg')

for result in results:
    boxes = result.boxes
    for box in boxes:
        # 坐标
        x1, y1, x2, y2 = box.xyxy[0].tolist()

        # 置信度
        conf = box.conf[0].item()

        # 类别
        cls = int(box.cls[0].item())
        name = model.names[cls]

        print(f"{name}: {conf:.2f} at ({x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f})")
```

### 批量处理

```python
from ultralytics import YOLO
import os

model = YOLO('yolo11m.pt')

input_dir = './images'
output_dir = './detected'
os.makedirs(output_dir, exist_ok=True)

# 处理所有图像
results = model(input_dir, save=True, project=output_dir)
```

## YOLOv11 任务

### 实例分割

```python
from ultralytics import YOLO

# 加载 YOLOv11 分割模型
model = YOLO('yolo11m-seg.pt')

results = model('image.jpg')

for result in results:
    masks = result.masks  # 分割掩码
    if masks is not None:
        for mask in masks.data:
            # mask 是二值张量
            pass
```

### 姿态估计

```python
from ultralytics import YOLO

# 加载 YOLOv11 姿态模型
model = YOLO('yolo11m-pose.pt')

results = model('image.jpg')

for result in results:
    keypoints = result.keypoints
    if keypoints is not None:
        for kp in keypoints.data:
            # 17 个关键点：鼻子、眼睛、耳朵、肩膀、肘部、手腕、臀部、膝盖、脚踝
            pass
```

### 分类

```python
from ultralytics import YOLO

# 加载 YOLOv11 分类模型
model = YOLO('yolo11m-cls.pt')

results = model('image.jpg')
for result in results:
    # Top-1 类别和置信度
    top1 = result.probs.top1
    top1conf = result.probs.top1conf.item()
    print(f"Class: {result.names[top1]} ({top1conf:.2f})")
```

### 定向边界框（OBB）— YOLOv11 的新功能

OBB 可检测任意旋转角度的对象 — 非常适合航拍/卫星影像、文档扫描和文本检测。

```python
from ultralytics import YOLO

# 加载 YOLOv11 OBB 模型
model = YOLO('yolo11m-obb.pt')

results = model('aerial_image.jpg')

for result in results:
    obb = result.obb
    if obb is not None:
        for box in obb:
            # 旋转框：x_center, y_center, 宽度, 高度, 角度
            xywhr = box.xywhr[0].tolist()
            conf = box.conf[0].item()
            cls = int(box.cls[0].item())
            print(f"{result.names[cls]}: {conf:.2f} rotated box: {xywhr}")
```

## 视频处理

### 处理视频

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 处理视频
results = model('video.mp4', save=True)
```

### 实时摄像头

```python
from ultralytics import YOLO
import cv2

model = YOLO('yolo11n.pt')  # 实时使用 nano 模型

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        break

    results = model(frame)
    annotated = results[0].plot()

    cv2.imshow('YOLOv11', annotated)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
```

### 保存处理后的视频

```python
from ultralytics import YOLO
import cv2

model = YOLO('yolo11m.pt')

cap = cv2.VideoCapture('input.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

out = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    results = model(frame)
    annotated = results[0].plot()
    out.write(annotated)

cap.release()
out.release()
```

## 目标跟踪

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 跟踪视频中的目标
results = model.track('video.mp4', save=True, tracker='bytetrack.yaml')

# 获取跟踪 ID
for result in results:
    boxes = result.boxes
    if boxes.id is not None:
        track_ids = boxes.id.tolist()
```

## 自定义训练

### 准备数据集

```yaml

# dataset.yaml
path: /workspace/dataset
train: images/train
val: images/val

names:
  0: cat
  1: dog
  2: bird
```

### 训练 YOLOv11

```python
from ultralytics import YOLO

# 加载预训练 YOLOv11 模型
model = YOLO('yolo11n.pt')

# 训练
results = model.train(
    data='dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    device=0
)
```

### 训练参数

```python
model.train(
    data='dataset.yaml',
    epochs=100,
    imgsz=640,
    batch=16,
    device=0,
    workers=8,
    patience=50,         # 提前停止
    save=True,
    save_period=10,      # 每 N 个 epoch 保存一次
    cache=True,          # 缓存图像
    amp=True,            # 混合精度
    lr0=0.01,
    lrf=0.01,
    momentum=0.937,
    weight_decay=0.0005,
    warmup_epochs=3.0,
    box=7.5,
    cls=0.5,
    dfl=1.5,
    augment=True,
    hsv_h=0.015,
    hsv_s=0.7,
    hsv_v=0.4,
    flipud=0.0,
    fliplr=0.5,
    mosaic=1.0,
    mixup=0.0,
)
```

## 导出模型

```python
from ultralytics import YOLO

model = YOLO('yolo11m.pt')

# 导出为不同格式
model.export(format='onnx')           # ONNX
model.export(format='tensorrt')       # TensorRT
model.export(format='openvino')       # OpenVINO
model.export(format='coreml')         # CoreML
model.export(format='tflite')         # TensorFlow Lite
```

## API 服务器

```python
from fastapi import FastAPI, UploadFile
from fastapi.responses import JSONResponse
from ultralytics import YOLO
from PIL import Image
import io

app = FastAPI()
model = YOLO('yolo11m.pt')

@app.post("/detect")
async def detect(file: UploadFile):
    contents = await file.read()
    image = Image.open(io.BytesIO(contents))

    results = model(image)

    detections = []
    for box in results[0].boxes:
        detections.append({
            "class": model.names[int(box.cls[0])],
            "confidence": float(box.conf[0]),
            "bbox": box.xyxy[0].tolist()
        })

    return JSONResponse(content={"detections": detections})

@app.post("/segment")
async def segment(file: UploadFile):
    contents = await file.read()
    image = Image.open(io.BytesIO(contents))

    model_seg = YOLO('yolo11m-seg.pt')
    results = model_seg(image)

    # 返回带标注的图像
    annotated = results[0].plot()
    # ... 转换并返回

# 运行：uvicorn server:app --host 0.0.0.0 --port 8000
```

## 性能优化

### TensorRT 导出

```python
model = YOLO('yolo11m.pt')
model.export(format='tensorrt', half=True)

# 使用导出的模型
model_trt = YOLO('yolo11m.engine')
results = model_trt('image.jpg')
```

### 批量推理

```python

# 同时处理多张图像
images = ['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']
results = model(images, batch=4)
```

## 性能基准

### YOLOv11 FPS（640px 输入）

| 模型      | GPU      | FPS    |
| ------- | -------- | ------ |
| yolo11n | RTX 3090 | \~1100 |
| yolo11s | RTX 3090 | \~730  |
| yolo11m | RTX 3090 | \~370  |
| yolo11x | RTX 3090 | \~183  |
| yolo11x | RTX 4090 | \~305  |

### YOLOv8 FPS（640px 输入）— 之前一代

| 模型      | GPU      | FPS   |
| ------- | -------- | ----- |
| YOLOv8n | RTX 3090 | \~900 |
| YOLOv8s | RTX 3090 | \~600 |
| YOLOv8m | RTX 3090 | \~300 |
| YOLOv8x | RTX 3090 | \~150 |
| YOLOv8x | RTX 4090 | \~250 |

## 故障排除

### 内存不足

```python

# 使用更小的模型
model = YOLO('yolo11n.pt')

# 或者降低图像尺寸
results = model('image.jpg', imgsz=320)
```

### 处理缓慢

* 使用 TensorRT 导出
* 使用更小的模型（yolo11n 或 yolo11s）
* 降低图像大小

### 准确率低

* 使用更大的模型（使用 yolo11x 替代 yolo11n）
* 在自定义数据上训练
* 增加图像尺寸

## 费用估算

典型 CLORE.AI 市场费率（截至 2025）：

| GPU       | 小时费率    | 日费率     | 4 小时会话  |
| --------- | ------- | ------- | ------- |
| RTX 3060  | \~$0.03 | \~$0.70 | \~$0.12 |
| RTX 3090  | \~$0.06 | \~$1.50 | \~$0.25 |
| RTX 4090  | \~$0.10 | \~$2.30 | \~$0.40 |
| A100 40GB | \~$0.17 | \~$4.00 | \~$0.70 |
| A100 80GB | \~$0.25 | \~$6.00 | \~$1.00 |

*价格因提供商和需求而异。请查看* [*CLORE.AI 市场*](https://clore.ai/marketplace) *以获取当前费率。*

**省钱建议：**

* 使用 **竞价（Spot）** 针对弹性工作负载使用市场（通常便宜 30-50%）
* 使用以下方式支付 **CLORE** 代币
* 比较不同提供商的价格

## 下一步

* [分割一切（Segment Anything）](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-chu-li/segment-anything) - 高级分割
* Detectron2 - 更多检测选项
* [Real-ESRGAN](https://docs.clore.ai/guides/guides_v2-zh/tu-xiang-chu-li/real-esrgan-upscaling) - 提升检测到的对象


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/ji-suan-ji-shi-jue/yolov8-detection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
