I trained a YOLOv8 detection model with 3 classes, but the raw forward pass still shows a final detect output of (1, 7, 8400) instead of (1, 8, 8400).
What I’ve Done: Checked my data.yaml:
train: path/to/train/images
val: path/to/val/images
nc: 3
names: ['glioma', 'meningioma', 'pituitary']
Confirmed nc: 3 is correct. Trained from scratch with the command:
yolo detect train \
data=path/to/data.yaml \
model=yolov8x \
epochs=1000 \
imgsz=640 \
device=1 \
patience=100
The training runs without error and completes successfully. Installed the latest Ultralytics version (v8.3.72) to ensure no version issues:
pip uninstall ultralytics
pip install ultralytics
Loaded the new best.pt directly:
from ultralytics import YOLO
import torch
model = YOLO(r"best.pt").model
model.eval()
dummy_input = torch.randn(1, 3, 640, 640)
with torch.no_grad():
outputs = model(dummy_input)
for out in outputs:
# Some outputs are lists; checking each element carefully
if isinstance(out, torch.Tensor):
print(out.shape)
else:
print("List output:", [o.shape for o in out if hasattr(o, 'shape')])
The console shows (1, 7, 8400) for the detection output. Verified model metadata says nc=3 and model.names has 3 classes. However, the raw detect layer output is still 7 channels.
Observations: If a YOLO detect layer is genuinely for 3 classes, it should output (5 + 3)=8 channels per anchor, not 7. The mismatch (1, 7, 8400) typically indicates it’s still set for 2 classes despite nc=3.
Question / Request for Help: Why is the raw detect head still (1, 7, 8400) even though I trained from scratch for 3 classes? How can I ensure the detect layer is fully re-initialized to (5 + 3)=8 for 3-class detection? I’ve tried deleting old .pt files, re-checking my data.yaml, reinstalling ultralytics, and confirming model.model.nc == 3. But the final detect layer continues to yield 7 channels instead of 8.
Any ideas on what might cause this persistent mismatch?