0
# YOLO Models
1
2
The YOLO class is the primary interface for working with YOLO models, supporting multiple computer vision tasks including object detection, instance segmentation, image classification, pose estimation, and oriented bounding box detection.
3
4
## Capabilities
5
6
### Model Initialization
7
8
Create YOLO model instances with various pretrained models or custom configurations.
9
10
```python { .api }
11
class YOLO:
12
def __init__(self, model="yolo11n.pt", task=None, verbose=False):
13
"""
14
Initialize YOLO model.
15
16
Parameters:
17
- model (str | Path): Model path or name (default: "yolo11n.pt")
18
- task (str, optional): Task type (detect, segment, classify, pose, obb)
19
- verbose (bool): Enable verbose output (default: False)
20
"""
21
```
22
23
**Supported Tasks:**
24
- `detect`: Object detection
25
- `segment`: Instance segmentation
26
- `classify`: Image classification
27
- `pose`: Pose estimation
28
- `obb`: Oriented bounding box detection
29
30
**Usage Examples:**
31
32
```python
33
from ultralytics import YOLO
34
35
# Load different model sizes
36
model_nano = YOLO("yolo11n.pt") # Nano model (fastest)
37
model_small = YOLO("yolo11s.pt") # Small model
38
model_medium = YOLO("yolo11m.pt") # Medium model
39
model_large = YOLO("yolo11l.pt") # Large model
40
model_xlarge = YOLO("yolo11x.pt") # Extra Large model (most accurate)
41
42
# Load task-specific models
43
detect_model = YOLO("yolo11n.pt") # Detection model
44
segment_model = YOLO("yolo11n-seg.pt") # Segmentation model
45
classify_model = YOLO("yolo11n-cls.pt") # Classification model
46
pose_model = YOLO("yolo11n-pose.pt") # Pose estimation model
47
obb_model = YOLO("yolo11n-obb.pt") # Oriented bounding box model
48
49
# Load from custom path
50
custom_model = YOLO("/path/to/custom/model.pt")
51
52
# Initialize with specific task
53
model = YOLO("yolo11n.pt", task="detect")
54
```
55
56
### Prediction
57
58
Perform inference on images, videos, or other sources with comprehensive configuration options.
59
60
```python { .api }
61
def predict(self, source, stream=False, predictor=None, **kwargs) -> List[Results]:
62
"""
63
Perform predictions on the given image source.
64
65
Parameters:
66
- source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | List | Tuple):
67
Source of image(s) to make predictions on
68
- stream (bool): If True, treats input as continuous stream
69
- predictor (BasePredictor, optional): Custom predictor instance
70
- **kwargs: Additional configuration options
71
72
Returns:
73
List[Results]: Prediction results encapsulated in Results objects
74
"""
75
76
def __call__(self, source=None, stream=False, **kwargs) -> List[Results]:
77
"""
78
Alias for predict method, enabling model instance to be callable.
79
"""
80
```
81
82
**Common Prediction Parameters:**
83
- `conf` (float): Confidence threshold (default: 0.25)
84
- `iou` (float): IoU threshold for NMS (default: 0.7)
85
- `imgsz` (int | tuple): Image size for inference (default: 640)
86
- `device` (str): Device to run on ('cpu', '0', '0,1', etc.)
87
- `half` (bool): Use FP16 inference (default: False)
88
- `max_det` (int): Maximum detections per image (default: 300)
89
- `vid_stride` (int): Video frame-rate stride (default: 1)
90
- `save` (bool): Save prediction results (default: False)
91
- `save_txt` (bool): Save results as txt files (default: False)
92
- `save_crop` (bool): Save cropped prediction boxes (default: False)
93
- `show` (bool): Show results (default: False)
94
- `verbose` (bool): Verbose output (default: True)
95
96
**Usage Examples:**
97
98
```python
99
# Single image prediction
100
results = model("image.jpg")
101
102
# Multiple images
103
results = model(["image1.jpg", "image2.jpg", "image3.jpg"])
104
105
# Video prediction
106
results = model("video.mp4")
107
108
# Webcam (device 0)
109
results = model(0)
110
111
# With custom parameters
112
results = model("image.jpg", conf=0.5, iou=0.7, imgsz=1280)
113
114
# Streaming prediction
115
for result in model("video.mp4", stream=True):
116
# Process each frame
117
result.show()
118
```
119
120
### Model Information
121
122
Get detailed information about the model architecture and parameters.
123
124
```python { .api }
125
def info(self, detailed=False, verbose=True):
126
"""
127
Log or return model information.
128
129
Parameters:
130
- detailed (bool): Show detailed model information
131
- verbose (bool): Control verbosity of output
132
"""
133
134
@property
135
def names(self) -> dict:
136
"""Get class names dictionary."""
137
138
@property
139
def device(self):
140
"""Get model device."""
141
142
def fuse(self):
143
"""Fuse Conv2d and BatchNorm2d layers for optimized inference."""
144
```
145
146
### Model Management
147
148
Load, save, and manage model weights and configurations.
149
150
```python { .api }
151
def load(self, weights="yolo11n.pt"):
152
"""
153
Load model weights from specified file.
154
155
Parameters:
156
- weights (str | Path): Path to weights file
157
158
Returns:
159
Model: Model instance with loaded weights
160
"""
161
162
def save(self, filename="saved_model.pt"):
163
"""
164
Save current model state to file.
165
166
Parameters:
167
- filename (str | Path): Filename to save model to
168
"""
169
170
def reset_weights(self):
171
"""
172
Reset model weights to initial state.
173
174
Returns:
175
Model: Model instance with reset weights
176
"""
177
```
178
179
**Usage Examples:**
180
181
```python
182
# Model information
183
model.info() # Basic info
184
model.info(detailed=True) # Detailed info
185
print(model.names) # Class names
186
print(model.device) # Current device
187
188
# Model management
189
model.load("custom_weights.pt") # Load weights
190
model.save("my_model.pt") # Save model
191
model.reset_weights() # Reset to initial state
192
model.fuse() # Optimize for inference
193
```
194
195
## Types
196
197
```python { .api }
198
from typing import List, Union, Optional
199
from pathlib import Path
200
from PIL import Image
201
import numpy as np
202
import torch
203
204
# Input source types
205
SourceType = Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor]
206
207
# Results type
208
from ultralytics.engine.results import Results
209
```