Ultralytics YOLO를 사용한 객체 감지
객체 감지는 이미지 또는 비디오 스트림에서 객체의 위치와 클래스를 식별하는 작업입니다.
객체 감지기의 출력은 이미지 속 객체를 둘러싸는 바운딩 박스 집합이며, 각 박스의 클래스 레이블과 신뢰도 점수도 포함됩니다. 장면에서 관심 객체를 식별해야 하지만 정확한 모양까지 알 필요는 없다면 객체 감지가 적합합니다.
시청: 사전 학습된 Ultralytics YOLO 모델을 사용한 객체 감지.
YOLO26 Detect 모델은 기본 YOLO26 모델입니다(예: yolo26n.pt). 이 모델은 COCO로 사전 학습되었습니다.
모델#
COCO 데이터셋으로 사전 학습된 YOLO26 Detect 모델은 아래와 같습니다.
모델은 처음 사용할 때 최신 Ultralytics 릴리스에서 자동으로 다운로드됩니다.
| 모델 | 크기 (픽셀) | mAPval50-95 | mAP검증50-95 (e2e) | 속도 CPU ONNX (ms) | 속도 T4 TensorRT10 (ms) | 파라미터 (M) | FLOPs (B) |
|---|---|---|---|---|---|---|---|
| YOLO26n | 640 | 40.9 | 40.1 | 38.9 ± 0.7 | 1.7 ± 0.0 | 2.4 | 5.5 |
| YOLO26s | 640 | 48.6 | 47.8 | 87.2 ± 0.9 | 2.5 ± 0.0 | 9.5 | 20.9 |
| YOLO26m | 640 | 53.1 | 52.5 | 220.0 ± 1.4 | 4.7 ± 0.1 | 20.4 | 68.4 |
| YOLO26l | 640 | 55.0 | 54.4 | 286.2 ± 2.0 | 6.2 ± 0.2 | 24.8 | 86.8 |
| YOLO26x | 640 | 57.5 | 56.9 | 525.8 ± 4.0 | 11.8 ± 0.2 | 55.7 | 194.4 |
- mAPval 값은 COCO val2017 데이터셋에서 단일 모델, 단일 스케일로 측정한 결과입니다.
yolo detect val data=coco.yaml device=0로 재현할 수 있습니다. e2e 열을 재현하려면nms=False를 추가하세요. - 속도는 CPU의 ONNX와 NVIDIA T4 GPU의 TensorRT10을 사용하여 COCO val 이미지에서 측정한 평균값입니다.
yolo detect val data=coco.yaml batch=1 device=0|cpu nms=False로 재현할 수 있습니다. - Params 및 FLOPs 값은 Conv/BatchNorm 폴딩과 사용하지 않는 감지 브랜치 제거 후의 융합 모델 기준입니다. 사전 학습된 체크포인트에는 전체 학습 아키텍처가 유지되므로 값이 더 높을 수 있습니다.
예비 COCO 감지 결과는 미공개 YOLO27 프리뷰를 참조하세요.
학습#
이미지 크기 640으로 COCO8 데이터셋에서 100 에포크 동안 YOLO26n 모델을 학습합니다.
from ultralytics import YOLO
# 모델 로드
model = YOLO("yolo26n.yaml") # YAML에서 새 모델을 생성합니다.
model = YOLO("yolo26n.pt") # 사전 학습된 모델을 불러옵니다(학습에 권장).
model = YOLO("yolo26n.yaml").load("yolo26n.pt") # YAML에서 모델을 생성하고 가중치를 전이합니다.
# 모델 학습
results = model.train(data="coco8.yaml", epochs=100, imgsz=640)Train 페이지에서 train 모드의 전체 세부 정보를 확인하세요. 감지 모델은 Ultralytics Platform 클라우드 학습으로도 학습할 수 있습니다.
데이터셋 형식#
YOLO 감지 데이터셋 형식에 대한 자세한 내용은 데이터셋 가이드를 참조하세요. 기존 COCO JSON 데이터셋을 YOLO 형식으로 변환하려면 COCO에서 YOLO로 변환하는 가이드에 설명된 내장 convert_coco 함수를 사용하세요. 또한 Ultralytics Platform의 AI 지원 어노테이션 도구를 사용하여 감지 데이터셋에 어노테이션을 추가하고 관리할 수 있습니다.
검증#
학습된 YOLO26n 모델의 정확도를 평균 정밀도(mAP)로 측정해 검증합니다. model은 학습 data와 인수를 모델 속성으로 보존하므로 인수를 지정할 필요가 없습니다. Train 예제의 path/to/best.pt은 COCO8에서 검증합니다. 공식 가중치에는 현재 컴퓨터에 존재하지 않는 학습 데이터셋 경로가 기록되어 있으므로, 경고와 함께 작업 기본값인 coco8.yaml로 대체됩니다. 다른 데이터셋에서 검증하려면 data을 전달하세요.
from ultralytics import YOLO
# 모델 로드
model = YOLO("yolo26n.pt") # 공식 모델을 불러옵니다
model = YOLO("path/to/best.pt") # 사용자 지정 모델 로드
# 모델을 검증합니다
metrics = model.val() # 데이터셋과 설정이 기억되므로 인수가 필요하지 않습니다
metrics.box.map # map50-95
metrics.box.map50 # map50
metrics.box.map75 # map75
metrics.box.maps # 클래스별 mAP50-95; val 세트에 없는 클래스는 전체 mAP 값을 사용합니다
metrics.box.ap_class_index # 실제로 평가된 클래스 인덱스
metrics.box.image_metrics # 정밀도, 재현율, F1, TP, FP, FN이 포함된 이미지별 지표 사전예측#
학습된 YOLO26n 모델을 사용하여 이미지에 대한 예측을 수행합니다.
from ultralytics import YOLO
# 모델 로드
model = YOLO("yolo26n.pt") # 공식 모델을 불러옵니다
model = YOLO("path/to/best.pt") # 사용자 지정 모델 로드
# 모델로 예측을 수행합니다
results = model("https://ultralytics.com/images/bus.jpg") # 이미지에 대해 예측을 수행합니다
# 결과에 액세스합니다
for result in results:
xywh = result.boxes.xywh # 중심 x, 중심 y, 너비, 높이
xywhn = result.boxes.xywhn # normalized
xyxy = result.boxes.xyxy # 왼쪽 위 x, 왼쪽 위 y, 오른쪽 아래 x, 오른쪽 아래 y
xyxyn = result.boxes.xyxyn # normalized
names = [result.names[cls.item()] for cls in result.boxes.cls.int()] # 각 박스의 클래스 이름
confs = result.boxes.conf # 각 박스의 신뢰도 점수Predict 페이지에서 predict 모드의 전체 세부 정보를 확인하세요.
결과 출력#
객체 감지는 이미지당 Results 객체 하나를 반환합니다. 주요 예측 필드는 result.boxes이며, 여기에 감지된 각 객체의 박스 좌표, 클래스 ID, 신뢰도 점수가 포함됩니다.
| 속성 | 유형 | 형태 | 설명 |
|---|---|---|---|
result.boxes | Boxes | (N) | 탐지 박스입니다. |
result.boxes.data | torch.float32 | (N,6/7) | 원시 [x1,y1,x2,y2,conf,cls]이며, 선택적으로 트랙 ID가 포함됩니다. |
result.boxes.xyxy | torch.float32 | (N,4) | xyxy 픽셀 박스입니다. |
result.boxes.conf | torch.float32 | (N,) | 신뢰도 점수입니다. |
result.boxes.cls | torch.float32 | (N,) | 클래스 ID이며, 이름을 가져오려면 int으로 변환합니다. |
모든 작업에서 작업별 Results 필드를 확인하려면 작업별 예측 결과 섹션을 참조하세요.
내보내기#
YOLO26n 모델을 ONNX, CoreML 등의 다른 형식으로 내보냅니다.
from ultralytics import YOLO
# 모델 로드
model = YOLO("yolo26n.pt") # 공식 모델을 불러옵니다
model = YOLO("path/to/best.pt") # 사용자 지정 모델 로드
# 모델을 내보냅니다
model.export(format="onnx")사용 가능한 YOLO26 내보내기 형식은 아래 표에 나와 있습니다. format 인수를 사용하면 원하는 형식으로 내보낼 수 있습니다(예: format='onnx' 또는 format='engine'). 내보낸 모델에서 바로 예측하거나 검증할 수 있습니다(예: yolo predict model=yolo26n.onnx). 내보내기가 완료되면 모델 사용 예제가 표시됩니다.
| 형식 | format 인수 | 모델 | 메타데이터 | 인수 |
|---|---|---|---|---|
| PyTorch | - | yolo26n.pt | ✅ | - |
| TorchScript | torchscript | yolo26n.torchscript | ✅ | imgsz, quantize, dynamic, nms, batch, device |
| ONNX | onnx | yolo26n.onnx | ✅ | imgsz, quantize, dynamic, simplify, opset, nms, batch, data, fraction, device |
| OpenVINO | openvino | yolo26n_openvino_model/ | ✅ | imgsz, quantize, dynamic, nms, batch, data, fraction, device |
| TensorRT | engine | yolo26n.engine | ✅ | imgsz, quantize, dynamic, simplify, opset, workspace, nms, batch, data, fraction, device |
| CoreML | coreml | yolo26n.mlpackage | ✅ | imgsz, dynamic, quantize, nms, batch, device |
| Apple Core AI | coreai | yolo26n.aimodel | ✅ | imgsz, batch, quantize |
| TF SavedModel | saved_model | yolo26n_saved_model/ | ✅ | imgsz, quantize, opset, nms, batch, data, fraction, device |
| TF GraphDef | pb | yolo26n.pb | ❌ | imgsz, opset, batch, device |
| TF Edge TPU | edgetpu | yolo26n_edgetpu.tflite | ✅ | imgsz, quantize, opset, data, fraction, device |
| LiteRT | litert | yolo26n.tflite | ✅ | imgsz, quantize, batch, data, fraction, device |
| PaddlePaddle | paddle | yolo26n_paddle_model/ | ✅ | imgsz, batch, device |
| MNN | mnn | yolo26n.mnn | ✅ | imgsz, batch, dynamic, quantize, simplify, opset, nms, device |
| NCNN | ncnn | yolo26n_ncnn_model/ | ✅ | imgsz, quantize, device |
| IMX500 | imx | yolo11n_imx_model/ | ✅ | imgsz, quantize, data, fraction, nms, device |
| RKNN | rknn | yolo26n_rknn_model/ | ✅ | imgsz, batch, name, quantize, simplify, opset, data, fraction, device |
| ExecuTorch | executorch | yolo26n_executorch_model/ | ✅ | imgsz, quantize, batch, data, fraction, device |
| Axelera | axelera | yolo26n_axelera_model/ | ✅ | imgsz, batch, quantize, data, fraction, device |
| DEEPX | deepx | yolo26n_deepx_model/ | ✅ | imgsz, quantize, simplify, opset, data, optimize, device |
| Qualcomm QNN | qnn | yolo26n_qnn.onnx | ✅ | imgsz, batch, name, quantize, simplify, opset, data, fraction, device |
| Hailo | hailo | yolo26n_hailo_model/ | ✅ | imgsz, name, quantize, data, fraction, simplify, conf, iou, device |
| Huawei Ascend | ascend | yolo26n_ascend_model/ | ✅ | imgsz, batch, name, quantize, opset, simplify, nms, device |
| AMD Xilinx | xilinx | yolo26n_xilinx_model/ | ✅ | imgsz, name, quantize, data, fraction, opset, simplify, device |
| NXP | nxp | yolo26n_nxp.tflite | ✅ | imgsz, quantize, data, fraction, device |
| ST Edge AI | stedgeai | yolo26n_stedgeai.onnx | ✅ | imgsz, quantize, data, fraction, opset, simplify, device |
nms=None은 외부 NMS를 위해 기본적으로 원시 출력을 사용합니다(YOLO26은 Axelera에서 NMS가 필요 없는 헤드를 유지합니다). 사용 가능한 NMS가 필요 없는 헤드를 선택하려면 nms=False를 설정하세요. 지원되지 않는 형식은 해당 형식의 기본 출력 경로로 대체됩니다. 위의 nms 항목은 nms=True로 NMS를 포함할 수 있는 형식을 나타냅니다.
자세한 export 내용은 내보내기 페이지를 참조하세요.
자주 묻는 질문#
예. Ultralytics Platform 빠른 시작에서는 브라우저 기반 워크플로를 통해 데이터셋에 어노테이션을 추가하고, 클라우드 GPU에서 감지 모델을 학습하며, 추론 엔드포인트에 모델을 배포하는 방법을 안내합니다.
사용자 지정 데이터셋으로 YOLO26 모델을 학습하려면 몇 가지 단계를 거쳐야 합니다:
- 데이터셋 준비: 데이터셋이 YOLO 형식인지 확인합니다. 안내는 데이터셋 가이드를 참조하세요.
- 모델 로드: Ultralytics YOLO 라이브러리를 사용하여 사전 학습된 모델을 로드하거나 YAML 파일에서 새 모델을 생성합니다.
- 모델 학습: Python에서
train메서드를 실행하거나 CLI에서yolo detect train명령을 실행합니다.
예제from ultralytics import YOLO # 사전 학습된 모델 불러오기 model = YOLO("yolo26n.pt") # 사용자 지정 데이터셋으로 모델을 학습합니다 model.train(data="my_custom_dataset.yaml", epochs=100, imgsz=640)자세한 구성 옵션은 구성 페이지를 참조하세요.
학습된 YOLO26 모델의 정확도를 검증하려면 Python에서
.val()메서드를 사용하거나 CLI에서yolo detect val명령을 실행하면 됩니다. 이 방법으로 mAP50-95, mAP50 등의 지표를 확인할 수 있습니다.예제from ultralytics import YOLO # 모델을 로드합니다 model = YOLO("path/to/best.pt") # 모델을 검증합니다 metrics = model.val() print(metrics.box.map) # mAP50-95검증에 대한 자세한 내용은 Val 페이지를 참조하세요.
Ultralytics YOLO26은 객체 감지, 인스턴스 분할, 시맨틱 분할, 포즈 추정 및 기타 지원 작업을 위한 빠르고 정확한 모델을 제공합니다. 주요 장점은 다음과 같습니다.
- 사전 학습 모델: COCO 및 ImageNet과 같은 인기 데이터셋으로 사전 학습된 모델을 사용해 개발 속도를 높일 수 있습니다.
- 높은 정확도: COCO에서 높은 mAP를 달성합니다.
- 속도: 지연 시간에 민감한 애플리케이션에서 실시간 추론을 수행하도록 최적화되었습니다.
- 유연성: ONNX 및 TensorRT와 같은 다양한 형식으로 모델을 내보내 여러 플랫폼에 배포할 수 있습니다.
댓글375
질문을 하거나 결과를 공유하거나 다른 사람을 도와주세요.
C:\
Hello,
I'm training YOLO on a custom dataset with two classes that are coupled: class 0 contains class 1. The relationship is like "cat" and "a cat's ear", or "person" and "a person's head" — the category-0 region geometrically encloses the category-1 region.
When I modify YOLO, the accuracy either stays flat, or one class improves while the other drops by roughly the same amount. I've tried many variants and the overall metric refuses to move.
Could you help me understand why? Is this a problem with my dataset/annotation design, or is it something about how YOLO handles nested classes? If the latter, is there a recommended way to formulate such data?
Thank you.
1
Paula DerrengerUltralyticsNested boxes are valid: annotate the whole object and its part as separate instances using the YOLO detection format; standard detection does not explicitly encode their parent–part relationship. Nesting alone does not establish why accuracy plateaus. One possible contributor is competition during target assignment: a prediction candidate selected for multiple ground-truth boxes ultimately receives only one target, but this does not prevent detecting both objects. Before changing the architecture further, establish an unmodified baseline and audit missing part labels and inconsistent box boundaries; compare per-class AP on the same validation split. Could you share your model/package version, training command, before/after per-class AP, and one representative image with its labels? That would help distinguish annotation or small-part localization problems from an assignment-related trade-off.
1C:\
Hi Paula,Thanks a lot for the detailed reply — that was very helpful.
To answer your questions:
- Model: YOLO11n, trained from scratch (pretrained=False, i.e. the .yaml architecture, not the COCO weights).
- Version: ultralytics 8.4.183.
- Dataset: about 540 images, self-collected for damage detection in this domain (masonry/brick walls). Two classes: class 0 = damaged brick (large region), class 1 = crack (thin structure). Box counts are roughly balanced between the two classes, but the scale is not: the median class-0 box covers about 50% of the image width, while the median class-1 box has a short edge of ~40 px, and every class-1 box lies geometrically inside some class-0 box.
- Training: imgsz 960, batch 8, SGD, 150 epochs, single GPU.
The per-class behaviour is exactly what you described: class 0 AP keeps improving when I change the architecture, class 1 stays flat or drops by a similar amount, and the overall metric does not move.
Your point about target assignment competition is the one that matches my numbers best. Since class 1 always sits inside class 0, one candidate location is usually covered by both a class-0 and a class-1 ground-truth box, and TAL will assign it to only one of them. If that is the mechanism, is there a recommended way to handle it — for example tuning the TAL hyperparameters (topk / alpha / beta), decoupling the assignment per class, or treating the nested relationship explicitly rather than as two independent classes?
I can also share the per-class AP before/after numbers and a labelled sample image if that would help.
Paula DerrengerUltralyticsI’d keep the two-class formulation and avoid changing TAL yet: your AP trade-off does not establish assignment competition. Being inside both boxes only makes a location eligible; a conflict occurs when both ground truths select the same candidate after top-k filtering. (docs.ultralytics.com) In 8.4.183, the detection loss initialization uses
topk=10,alpha=0.5, andbeta=6.0; these are internal settings, not standard training arguments. Simply assigning each location independently per class would leave conflicting regression targets because that location predicts one box, not a separate box per class. (docs.ultralytics.com) My first diagnostic would be a crack-only run, removing brick annotations but retaining identical images, splits, initialization, and training settings, then comparing crack AP across multiple seeds. A consistent improvement would indicate an interaction between the tasks, but would not isolate TAL specifically. If you investigate assignment afterward, measure positives retained per crack before/after conflict resolution rather than tuning blindly. Explicit parent–crack association could instead be handled with a custom brick-detection → crack-detection-on-crops pipeline. Please share the before/after per-class AP50 and AP50–95 numbers; those are still missing.1C:\
Thanks a lot for your help! I'm currently running some other models, and given my limited compute I'll work through the diagnostics you suggested over the coming days. I'd like to keep in touch on this — hope that's okay.
C:\
Hi Paula,
Thanks again for the detailed guidance — it was useful, and I followed it.
I trained on a BC-only version of the dataset: all brick annotations removed, with images, splits, initialisation and training settings left unchanged. Crack accuracy did not improve. I'm now confident that there is no parent–child interference between DB and BC in this data, at least none that shows up in accuracy.
What I do see consistently is this: across the variants I have tried, DB improves substantially while BC barely moves. My reading is that BC is hard for two reasons.
The first is simply how few pixels a crack occupies.
The second, and I think the larger one, is annotation noise. I have annotated this dataset three times over and re-checked it, and I still cannot resolve it — because the boundary of a crack has no unique answer. A brick box is fixed by the mortar joint: a clear line. A crack is a grey-to-black gradient. You can draw the box around the darkest core, or around everything faintly visible, and both are defensible. A crack running across two bricks can be one box or two. These are judgement calls rather than facts, and the same careful annotator, working a week later, will land a few pixels away.
On detecting cracks inside brick crops: I take the point, but it gives up the speed advantage that makes a single-stage detector worth using in the first place.
Do you have any further suggestions for this poor crack detection performance?
Paula DerrengerUltralyticsYour BC-only result makes assignment competition a lower-priority hypothesis, although it does not prove there is no interaction. I’d focus next on separating missed cracks from box-localization errors: compare BC AP50 with AP50–95 and inspect the predictions; a large gap would be consistent with localization sensitivity, not proof of annotation noise. This metrics guide explains the distinction. Rather than annotating everything again, write a fixed operational rule—core versus faint extent, when to split, and whether mortar boundaries matter—and apply it to a small audit subset. Independently relabel that subset without seeing the old labels, then compare the boxes’ IoU; this would quantify ambiguity rather than assume it explains the plateau. For your next training run, I’d prioritize COCO-pretrained YOLO11n, using
yolo11n.ptrather than the YAML initialization, while retaining the same split and resolution. That keeps your single-stage inference setup unchanged. If you share a few BC prediction overlays showing both misses and apparently correct detections scored poorly, we can better distinguish visibility, localization, and annotation-policy problems before spending compute on further architecture changes.C:\
Hi Paula,
Thank you very much for the detailed reply — I really appreciate the time you put into it.
Yes, I understand that pretrained weights matter a lot here. My worry is the opposite one: if I load them, the baseline becomes strong enough that my own improvement no longer stands out. Here are my numbers.
Setup. Everything else is identical: same dataset and split, same test set (100 images, 506 instances — DB 227, BC 279), imgsz 960, batch 8, SGD, 150 epochs, 3 seeds per configuration, each run evaluated with its own best checkpoint selected on val. The only difference is the initialization: "scratch" starts from the YAML architecture with random weights; "pretrained" starts from the COCO weights (yolo11n.pt).

Per-seed overall mAP50 — scratch: 85.43 / 84.15 / 83.61; COCO-init: 83.77 / 84.82 / 84.78.
So on overall AP50 the two are indistinguishable, and on AP50-95 essentially the whole gain lands on the crack class. That is consistent with your reading that BC is limited by box quality rather than visibility — but it also means pretraining did not give me a stronger baseline on the metric I care about, and it did not move BC AP50 at all.
C:\
The overlays below are generated at conf=0.01, iou=0.7 (the default 0.25 would have removed every low-scoring detections, which is exactly what I want you to see).





Paula DerrengerUltralyticsYour three-seed results support no clear overall AP50 benefit from pretraining here; the BC AP50–95 gain suggests better localization, but does not establish annotation noise as the limiting cause. I also wouldn’t weaken the baseline to make an architectural gain stand out—report whether that gain survives both initializations. I couldn’t retrieve the overlay images, so I can’t reliably diagnose their individual detections. The next low-compute step is to inspect BC’s precision–recall curve using validation with
conf=0.001, plots=True(validation settings): lowering the display threshold exposes candidates, but does not show whether true cracks rank above false positives. Choose any deployment threshold on validation data, leaving the test set for final comparison. Could you share the BC PR curve and an accessible side-by-side ground-truth/prediction example? That would distinguish a usable low-confidence operating point from poor ranking or localization without another training run.
MrBug1024
I have been using YOLO for many years, but I have always been looking forward to the official release of a face recognition solution. Because the current open-source solutions in the face recognition field, whether in terms of performance or subsequent enhanced training, are not as simple as training a custom object detection model with YOLO. This is what shocks me. Therefore, I still hope that the official team can provide a convenient solution like object detection in the fields of face recognition and OCR!
1sabrinabounehar
Hello, I'm currently working on a project for detecting downy mildew on grapevines using the YOLOv8 model. I've successfully retrained it with my own dataset, and it performs well during testing on images. However, when transitioning to real-time detection, the model produces false detections due to color issues. The disease appears as yellow, gray, and brown spots on the vines, but in real-time, the model detects anything of these colors as the disease. I'm seeking assistance to address this specific problem. thanks
1
Paula DerrengerUltralyticsHello! It's great to hear about your progress with detecting downy mildew on grapevines using YOLOv8. 🌿 The issue of color-based false detections in a real-time setting can indeed pose a challenge. To refine your model's ability to distinguish between actual disease symptoms and similar-colored objects, you might consider a few strategies:
-
Data Augmentation: Enhance your dataset with more varied lighting and background conditions. Incorporating images that have similar colors but are not the disease can help the model learn to discriminate better.
-
Fine-tuning with Hard Examples: Identify cases where the model currently fails and add more such examples to your training set. This could include images with objects that are similar in color to the disease but aren't the disease itself.
-
Adjust Confidence Threshold: Experiment with adjusting the confidence threshold of your detection. A higher threshold may reduce false positives but be careful to not miss genuine cases of mildew.
results = model.predict('image.jpg', conf=0.5) # Adjusting confidence threshold- Post-Processing: Implement a custom post-processing step that considers additional context or rules specific to your problem domain, for instance, the typical size or shape of diseased spots, or their common locations on a vine.
It's a process of trial and error, so don't hesitate to iterate on these strategies. If you continue to experience issues, please share some examples or further details so we can offer more targeted advice. 🍇
-
arifan-instrumentasi-UB
Hi, can yolov8 detect color sequence?. i create color sequence of rainbow (red, yellow, green) as a real rainbow. if i change the yellow to orange, can yolov8 detect it as a wrong rainbow?
1
Glenn JocherUltralytics@arifan-instrumentasi-UB hi there! 🌈
YOLOv8 is primarily designed for detecting objects and their categories, rather than the sequence or color composition of objects. It can recognize objects (like a rainbow) based on the training data it has been exposed to, but it doesn't inherently understand the "correct" color sequence of a natural rainbow.
However, you could approach the problem creatively by training your model on labeled data specifying "correct" and "incorrect" rainbows. Here’s a simplified example of how your training annotations might look:
- Image 1 (correct rainbow): Class label “rainbow_correct”
- Image 2 (rainbow with yellow replaced by orange): Class label “rainbow_incorrect”
This way, YOLOv8 could potentially learn to distinguish between the different types of rainbows based on your specific definitions of "correct" and "incorrect."
Remember, accurate detection relies heavily on the diversity and quality of the training dataset. 📊
Hope that helps!
lixjohn
I am working on an object detection project. During the annotation phase, the challenge is that there are a large number of objects in a single image, making the labeling effort extremely high. So my question is, for an object detection task, do we have to label all objects in the image? Or can we just label a few of them and leave the rest unlabeled during training? If not, are there any best practices for this kind of challenge? I appreciate any suggestions..
Glenn JocherUltralyticsFor YOLO detection training, you should label all visible instances of the classes you want the model to learn in each training image; leaving same-class objects unlabeled usually turns them into false background and hurts training. If images are very dense, the usual workaround is to tile/crop them into smaller patches and use model-assisted pre-annotation plus active learning or the Ultralytics Platform annotation tools to reduce manual effort, and you can find more dataset guidance in the data collection and annotation guide.
lixjohn
Thank you for the helpful response. I will proceed with training a model on the initial annotations and integrate it into the active learning cycle. I have one follow-up question: if I train the model on small 640×640 patches cropped from a typical 5k×3k image, can I safely perform inference on the original 5k×3k images?
Glenn JocherUltralyticsYes—you can train on
640x640crops and run inference on the original5k×3kimages, but if you infer at a smallimgszthe objects may shrink too much after resize, so for dense/small targets the usual best practice is a largerimgszor tiled inference with SAHI, as noted in the image tiling guidance.lixjohn
Got point. Appricate your help on that, I do believe I will have more questions in project. :-)
Glenn JocherUltralyticsHappy to help—feel free to post any follow-up questions here so the team and community can weigh in, and if you end up working with tiny or dense targets the small-object mAP guide has a few practical tuning ideas.
lixjohn
Thanks for further information, I see the sentence in guild "there are P2 model variants of the Ultralytics YOLOv8 model that are optimized for preserving fine spatial detail.", does it mean p2 yolo structure such as yolo26-p2.yaml? I plan use this to train my model. And one more question, if I use orignal image size 5k*3k to train model, and use SAHI to do inference, is it right approch for small object?
lixjohn
The project I am working on is a little bit tricky. It includes both dense and sparse objects with vastly different sizes. Some images just have a couple of big objects, but some images include over one hundred small objects. My plan is to label all of them first and train a YOLO model with the original image size of 5k × 3k using an imgsz of 1024 or higher. For the inference phase, I can use SAHI, but the question is, it seems like I cannot apply SAHI to all images because most of the images are sparse. So, how do I handle this situation?
Glenn JocherUltralyticsYes, the
P2note refers to the small-object architecture variants such asyolo26-p2.yaml; for training you’d normally use a scaled config likemodel=yolo26n-p2.yaml, and the YOLO26 model docs note these are YAML-only architectures. For your mixed dense/sparse case, using5k×3ksources is fine but the model still trains at your chosenimgsz, so a practical setup is one model trained with all labels at the largestimgszyou can afford, optionally adding tiled crops for the densest images, then using normal inference by default and routing only the high-resolution or small-object cases to SAHI when you need extra recall.lixjohn
Thanks, I will use yolo train model=yolo26x-p2.yaml pretrained=yolo26x.pt as training command. In terms of router, the key is I have to indentify the image is dense/sparse automaticlly, however, seems like no way to know if the image is dense/sparse before inference?
Glenn JocherUltralyticsCorrect—you usually cannot know dense vs. sparse reliably before inference, so a practical setup is a cheap first pass with normal YOLO at your target
imgsz, then re-run only the uncertain high-resolution cases with tiled inference when you see no detections, very low confidence, or mostly tiny boxes; sparse images can stay on the standard object detection pipeline.lixjohn
I see. I've completed an end-to-end cycle from labeling to prediction. Thanks for your help!
During the process, one issue I encountered is that some objects cluster together as a group, so I labeled them with one box and assigned the same label as a single object. The reason for this is that we still need to recognize the object group as well, not just individual objects. Is this the right approach? Or do we have to assign a new, distinct label for the grouped objects?
Another issue is that the predictor assigns multiple/duplicate labels to a single object, the boxes are most overlap. I tried using agnostic_nms and tweaking the iou parameter to reduce these multiple/duplicate labels, but it didn't work well. I am wondering if the final solution to eliminate multiple/duplicate labels is a custom post-prediction step? For example, calculating box overlaps manually and keeping only the one with the highest confidence score?
Glenn JocherUltralyticsIf a cluster is a different target from a single instance, give it a separate class such as
object_groupand apply that rule consistently; mixing single objects and merged clusters under the same class usually creates ambiguous supervision and can cause overlapping duplicates. I’d avoid a custom post-filter first—if you can share 2–3 labeled images and the matching predictions here, we can check whether this is a labeling consistency or small-object resolution issue, since with Ultralytics YOLO26 end-to-end inference and standard NMS behavior, persistent same-class duplicates are usually caused upstream rather than by a missing manual overlap step.lixjohn(수정됨)
Understood. The situation is that there is no exact number of objects in a group—it could range from one to a hundred. This makes it difficult to decide which labels to create, so currently, I put them under the same label regardless of how many objects are in the group. What is the right approach here? Could this ambiguity be the root cause of the duplicate label issue?
I have attached 3 pictures (sorry I am not allowed to share data so I am using masks and fake labels):
Objects 1a and 1b represent an object group containing 3 individual objects. The inference outputs show two different-sized rectangles with the same label. (Confidence = 0.25, IoU = 0.70; changing IoU to 0.25 did not help, agnostic_nms=False).
Object 2 is a single object, but it produces two almost identical, overlapping rectangles with the same label. At a low confidence threshold of 0.10, both rectangles appear; changing it to 0.25 leaves just one.
Is this expected behavior given the IoU settings, or is it a result of the labeling issue?
Thanks again.
Glenn JocherUltralyticsThe better rule here is to define labels by visual granularity rather than count: annotate an individual object when it can be boxed cleanly, annotate a separate
object_groupclass only when the cluster itself is the target or the instances cannot be separated, and avoid mixing large cluster boxes and tight instance boxes under the same class over the same region, since that teaches the model two valid box scales for one label and is a likely cause of yourobject1duplicates; forobject2, seeing two boxes atconf=0.10but one at0.25is generally expected low-threshold behavior, and IoU mainly governs overlap matching rather than fixing class-definition ambiguity, so I’d standardize the labels before adding any custom post-processing.lixjohn
Thank you very much, let me summarize to ensure I understand the label approach:
- Define two labels: one for a single object (object) and another for an object cluster (object_group).
- Annotate an individual object when it can be boxed cleanly — use the object label. 3.Use the object_group label when the cluster itself is the target or the instances cannot be separated. That means if there are multiple objects that cannot be separated and the cluster is the target, use the object_group label.
- Avoid mixing large cluster boxes and tight instance boxes under the same class over the same region — I'm a little confused here: in one image, there must be both single objects and clusters, so how do I avoid mixing large cluster boxes (object_group) and tight instance boxes (object)? I guess the same region means object box and cluster object box overlap?
Glenn JocherUltralyticsYes—“same region” means overlapping annotations for the same underlying objects; in one image it’s fine to have both
objectandobject_group, but they should be mutually exclusive by area: useobjectwhere instances can be boxed cleanly, and use oneobject_groupbox only for the dense patch where they cannot, without also boxing those same members individually. If you train both levels on the same pixels, duplicate predictions are very likely, so the key is a consistent one-representation-per-region rule as described in the annotation best-practices guide.lixjohn
Thanks, understand the approach. Really appricate your help! Will let you know any further question. :)
Glenn JocherUltralyticsGlad that clarified it; if any new edge cases come up, please share a couple of representative labels and predictions here before retraining and we can help sanity-check the annotation rule.
lixjohn
Thanks again! Yes for sure, we'll follow the annotation rule and how things go, keep you post if anything new.
Glenn JocherUltralyticsSounds good—after updating the annotations, please delete any
*.cachefiles in yourlabelsfolder before retraining, and feel free to post back here if any edge cases still produce duplicate boxes.lixjohn
Got it. Thanks for reminding. :-)
Glenn JocherUltralyticsYou’re welcome—if anything unexpected shows up after retraining, share a sample label/prediction pair and the
yolo checksoutput here and the Ultralytics team can help verify it.
mslabdelhak29
Hello i want to use yollo in Detection and Tracking for Surveillance Cameras wich is the best config
Glenn JocherUltralytics(수정됨)For surveillance, a strong default is Ultralytics YOLO
yolo26s.ptfor balanced speed/accuracy oryolo26m.ptfor denser scenes, paired withmodel.track(..., tracker="bytetrack.yaml"); the detection guide and tracking docs are good references, and if you share your camera resolution, FPS, and hardware here in Discussions the Ultralytics team can suggest a tighter config.
Hassaan149
Multi-GPU TensorRT Inference Issue (Docker + RTSP Stream)
I am running object detection on an RTSP input inside the
predictfunction.I want to dynamically assign specific GPUs based on VRAM usage (which I monitor using py-nvml).
However, even when I pass:
model.predict(source=stream_url, stream=True, device=str(device))(where
deviceis0or1), the engine file always loads on GPU 0.
Docker Compose Configuration
My
docker-compose.ymlincludes all GPUs:deploy: resources: reservations: devices: - driver: nvidia count: all capabilities: [gpu]
Expected Behavior
- My code automatically selects the GPU ID (
0or1) based on VRAM usage. - I have exported
.enginefiles per GPU. - If
device = 0→ load engine exported for GPU 0. - If
device = 1→ load engine exported for GPU 1.
Current Problem
Regardless of the
devicevalue passed tomodel.predict(), the engine always loads on GPU 0 inside Docker.
Goal
Ensure that:
- The correct GPU is selected at runtime.
- The corresponding
.enginefile (exported for that GPU) is loaded. - Inference runs on the intended GPU device instead of defaulting to GPU 0.
Glenn JocherUltralyticsFor TensorRT
*.enginemodels the GPU is effectively chosen when the engine is deserialized/loaded, anddevice=inmodel.predict()won’t “move” an already-loaded engine off CUDA:0; to avoid PyTorch’s default CUDA init grabbing GPU 0, set the device before creatingYOLO(...)(or pin the process withCUDA_VISIBLE_DEVICES), as noted in our common multi-GPU troubleshooting guidance and predict docs.import os os.environ["CUDA_VISIBLE_DEVICES"] = "1" # pin to physical GPU 1 (it becomes cuda:0 in-process) from ultralytics import YOLO model = YOLO("yolo26n.engine") # load AFTER pinning results = model.predict(source=stream_url, stream=True, device=0)If you need to switch GPUs dynamically per stream/job, you’ll generally need separate processes (or separate containers) per GPU; also please paste
!yolo checksplusnvidia-smi -Lfrom inside the container and the full text logs showing the engine load so we can confirm whether the issue is GPU index remapping vs. early CUDA initialization.Hassaan149
I am using Python multiprocessing to offload inference input streams onto separate processes. In each process, I instantiate a YOLO model (TensorRT
.enginefile) for GPU inference.I attempt to assign a specific GPU per process using:
os.environ["CUDA_VISIBLE_DEVICES"] = str(device)However, even when
device=1, the process still runs on GPU 0.
Environment
Inside the container:
nvidia-smi -LOutput:
GPU 0: NVIDIA GeForce RTX 2080 SUPER (UUID: GPU-xxxx...) GPU 1: NVIDIA GeForce RTX 2080 SUPER (UUID: GPU-yyyy...)On container startup, I see this warning:
/opt/venv/lib/python3.10/site-packages/torch/cuda/_init_.py:65: FutureWarning: The pynvml package is deprecated. Please install nvidia-ml-py instead. If you did not install pynvml directly, please report this to the maintainers of the package that installed pynvml for you. import pynvml INFO: Started server process [1] INFO: Waiting for application startup
Worker Function (Per Stream Process)
def inference_worker( stream_url: str, frame_queue, idx: int, width: int = 640, height: int = 480, conf: float = 0.7, polygon: list = None, thread_name: str = None, camera_name: str = None, device: int = 0, ): """Runs YOLO inference in a separate process""" alert_thread = {} KNOWN_HEIGHT_CM = 155 FOCAL_LENGTH = 1400 # Attempt to set device per process os.environ["CUDA_VISIBLE_DEVICES"] = str(device) try: from ultralytics import YOLO except Exception as e: frame_queue.put(("_ERROR_", f"Import error: {e}")) return try: model = YOLO(model _path+ f"_{device}.engine") except Exception: model = export(intruder_path + f"_{device}.pt", device=device) return
Issue
Even when:
device = 1 os.environ["CUDA_VISIBLE_DEVICES"] = "1"The inference still runs on GPU 0 instead of GPU 1.
Glenn JocherUltralyticsThis is almost always caused by Linux
multiprocessingdefaulting tofork: if your main server process (e.g. Uvicorn/FastAPI) importstorch/ultralytics(or anything that initializes CUDA/TensorRT) before the fork, the child workers inherit a CUDA context already bound to physical GPU 0, and changingCUDA_VISIBLE_DEVICESinsideinference_worker()won’t take effect; the fix is to usespawn(or run 1 worker process per GPU as a separate OS process/container) and ensure CUDA-related imports only happen after the env var is set, per our multi-GPU troubleshooting guide.# main.py (make sure ultralytics/torch are NOT imported at module top-level) import multiprocessing as mp def inference_worker(stream_url: str, device: int): import os os.environ["CUDA_VISIBLE_DEVICES"] = str(device) # set BEFORE any torch/ultralytics import import torch from ultralytics import YOLO print("CUDA_VISIBLE_DEVICES:", os.environ["CUDA_VISIBLE_DEVICES"]) print("torch sees", torch.cuda.device_count(), "GPU(s); current:", torch.cuda.get_device_name(0)) model = YOLO(f"{model_path}_{device}.engine") for r in model.predict(source=stream_url, stream=True, device=0): pass if __name__ == "__main__": mp.set_start_method("spawn", force=True) # start processes hereIf this still pins to physical GPU 0, please paste the
!yolo checksoutput from inside the container and confirm whether any part of your server importstorchbefore the worker processes are started.Hassaan149
Thanks alot Glen, this helped, I had an ocr code which was subtly initializing cuda contexts at container startup, thus preventing GPU selections using os.environ["CUDA_VISIBLE_DEVICES"] = str(device)
Glenn JocherUltralyticsGlad that surfaced it — anything that touches CUDA (OCR libs,
torch, TensorRT, etc.) must be imported/initialized only afterCUDA_VISIBLE_DEVICESis set in each spawned worker (see PyTorch multiprocessing notes), and you can also swappynvml→nvidia-ml-pyto clear the deprecation warning you’re seeing.
- My code automatically selects the GPU ID (
VeryOnline
你好,yolo的开发者,我使用yolo的CLI转换ONNX模型后,得到的onnx模型输出是一个float32[1,300,6]的输出,这里的输出是如何定义的?我没有找到相关的定义,能帮我解释一下这个输出么?
Glenn JocherUltralytics(수정됨)你好,你看到的
float32[1,300,6]是 YOLO26 的 NMS-free(端到端)检测头的输出,导出时使用nms=False会得到这种格式:300表示最多保留的检测数(max_det),每个检测的 6 个值对应x1, y1, x2, y2, conf, cls(cls以 float 形式存储)。如果你想拿到未做 NMS 的原始输出用于自定义后处理,请使用默认设置导出(不设置nms),此时输出为(B, 4+num_classes, num_predictions)结构,参考 导出文档里的输出张量说明,以及 端到端检测指南。yolo export model=yolo26n.pt format=onnx nms=False # (1, 300, 6) yolo export model=yolo26n.pt format=onnx # (1, 4+nc, N) 原始输出如果方便的话,把你实际的导出命令(包含
imgsz/nms等参数)和 ONNX 的 input/output 名称贴在这个讨论里,我们可以更精确地确认每一维的含义和坐标尺度。
ghost
I want to detect only 'Person' class. Do I need to modify some yaml file and retrain the model for that - I want to reduce the parameters.
rugvedjalit
Not specifically you can use the pretrained model of yolo you just ne to call on person id and reflect it on your processed frames. Class_id == 0 : person
Glenn JocherUltralytics(수정됨)@ShahnawazHusian to add new classes you should fine‑tune from your existing checkpoint on a dataset that contains both the old and new classes; YOLO doesn’t support “locking” all old weights and only appending new ones. By default all layers are trainable, but you can freeze early layers to preserve most features, for example:
from ultralytics import YOLO model = YOLO("path/to/old/best.pt") # model already trained on old classes results = model.train( data="data_with_old_and_new_classes.yaml", epochs=100, freeze=10, # freeze first 10 layers (backbone-heavy); head stays trainable lr0=0.001 # often use a smaller LR when extending a trained model )If you want to “re‑train” an existing class, just keep that class in your labels and oversample or rebalance your dataset; freezing the head/neck/backbone entirely would stop any learning. The
freezeargument and related options are summarized in the training configuration docs.
@heymuun as @rugvedjalit mentioned, you don’t need to change the YAML or retrain if you only want the
personclass: you can filter at inference time withclasses=0(COCO’s person index), for example:from ultralytics import YOLO model = YOLO("yolo26n.pt") # COCO‑pretrained results = model.predict(source="video.mp4", classes=0)This keeps the model size the same but returns only person detections; if you truly need fewer parameters, use a smaller model variant (e.g.,
yolo26n.pt) or retrain anc: 1model, though the parameter reduction from changingncalone is quite small.
legend-zhen
Hello, I would like to use the weight file obtained after modifying the YOLO11 model to provide a checkbox for the top-down pose estimation algorithm. Why can it only run normally under the official weight file(yolo11.pt)? When using the weight of the YOLO11 structure improved by myself, the following error message appears. Please ask what should be done to use the weight file trained by myself. D:\Anaconda\envs\openmmlab\python.exe D:\openmmlab\mmpose-11\topdown_demo_with_YOLO1111.py D:\Anaconda\envs\openmmlab\lib\site-packages\mmengine\utils\package_utils.py:48: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81. from pkg_resources import DistributionNotFound, get_distribution WARNING ⚠️ D:\YOLO\YOLO11\ultralytics-main\runs\detect\train8\weights\best.pt appears to require 'ultralytics.nn.add_modules', which is not in Ultralytics requirements. AutoInstall will run now for 'ultralytics.nn.add_modules' but this feature will be removed in the future. Recommend fixes are to train a new model using the latest 'ultralytics' package or to run a command with an official Ultralytics model, i.e. 'yolo predict model=yolov8n.pt' Traceback (most recent call last): File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\nn\tasks.py", line 837, in torch_safe_load ckpt = torch.load(file, map_location="cpu") File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\utils\patches.py", line 86, in torch_load return _torch_load(*args, **kwargs) File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 712, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 1049, in _load result = unpickler.load() File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 1042, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named 'ultralytics.nn.add_modules'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "D:\openmmlab\mmpose-11\topdown_demo_with_YOLO1111.py", line 316, in main() File "D:\openmmlab\mmpose-11\topdown_demo_with_YOLO1111.py", line 196, in main model_yolo11 = YOLO(args.det_checkpoint) File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\models\yolo\model.py", line 23, in init super().init(model=model, task=task, verbose=verbose) File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\engine\model.py", line 145, in init self._load(model, task=task) File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\engine\model.py", line 285, in _load self.model, self.ckpt = attempt_load_one_weight(weights) File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\nn\tasks.py", line 910, in attempt_load_one_weight ckpt, weight = torch_safe_load(weight) # load ckpt File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\nn\tasks.py", line 857, in torch_safe_load ckpt = torch.load(file, map_location="cpu") File "D:\Anaconda\envs\openmmlab\lib\site-packages\ultralytics\utils\patches.py", line 86, in torch_load return _torch_load(*args, **kwargs) File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 712, in load return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args) File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 1049, in _load result = unpickler.load() File "D:\Anaconda\envs\openmmlab\lib\site-packages\torch\serialization.py", line 1042, in find_class return super().find_class(mod_name, name) ModuleNotFoundError: No module named 'ultralytics.nn.add_modules'
Glenn JocherUltralytics(수정됨)@legend-zhen Thanks for the traceback. Your checkpoint was saved with custom code (
ultralytics.nn.add_modules) from a modified Ultralytics repo, but you’re loading it with the pip-installed package where that module doesn’t exist. Fix by either installing your modified repo into the same environment, or exporting to a format that doesn’t require the original Python classes:# Option A: load with the same modified code pip uninstall -y ultralytics pip install -e path\to\your_modified_ultralytics # Option B: export once from the environment where it loads, then use the artifact yolo export model="D:\YOLO\YOLO11\ultralytics-main\runs\detect\train8\weights\best.pt" format=onnxPlease also run and paste the text output of:
yolo checksto confirm you’re not mixing a local repo and a pip install; compare the Ultralytics install path it prints with the paths in your traceback. Export formats and usage are summarized in the Export guide and general troubleshooting tips are in Troubleshooting Common YOLO Issues.
