ultralytics icon indicating copy to clipboard operation
ultralytics copied to clipboard

Validation error with custom classes: ValueError: 'list' argument must have no negative elements

Open mousedown opened this issue 2 months ago β€’ 6 comments

When training YOLOv8 or YOLO11 on custom building detection classes (not present in COCO pretrained weights), validation fails after epoch 1 with the error:

ValueError: 'list' argument must have no negative elements

Environment:

  • Ultralytics version: 8.3.203
  • PyTorch version: 2.8.0
  • Python version: 3.13.7
  • Platform: macOS (Darwin 24.6.0)
  • Device: MPS (Apple Silicon)

Dataset:

  • Type: Custom building detection
  • Classes: 5 custom classes (1, 2, 4, 5, building) - not present in COCO
  • Source: Roboflow Building Dataset
  • Size: 1,740 training images, 166 validation images
  • Format: YOLOv8 format with properly configured data.yaml

Reproduction Steps:

  1. Load pretrained YOLOv8 or YOLO11 model
  2. Train on custom dataset with classes not in COCO pretrained weights
  3. Training completes epoch 1 successfully
  4. Validation phase triggers the error

Expected Behavior:

Validation should complete successfully after each epoch, regardless of whether custom classes match pretrained weights.

Actual Behavior:

Validation crashes with ValueError: 'list' argument must have no negative elements after epoch 1.

Workaround:

Downgrading to stable versions resolves the issue:

pip install ultralytics==8.0.196
pip install 'torch<2.6' torchvision --upgrade --force-reinstall

With these versions, training completes successfully with:

  • βœ… 3 epochs without validation errors
  • βœ… 100% detection rate on validation set
  • βœ… Successful CoreML export

Root Cause Analysis:

Two potential issues:

  1. Ultralytics 8.3.203: Validation bug when training on classes not in COCO pretrained weights
  2. PyTorch 2.8.0: Changed default weights_only=True causing weight loading failures

Additional Context:

  • The issue blocks training pipeline completely
  • Affects both detection and segmentation models
  • Only occurs with custom classes; COCO classes work fine with newer versions

mousedown avatar Sep 29 '25 18:09 mousedown

πŸ‘‹ Hello @mousedown, thanks for reporting this and for the detailed context πŸš€ This is an automated triage to help you get faster assistanceβ€”an Ultralytics engineer will follow up soon.

We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it. Since your report involves validation failing after epoch 1 with custom classes, please include: -1-Exact training/validation command(s) you ran (CLI or Python) -2-Full console logs with the traceback from start to crash -3-Your data.yaml and 2–3 sample label files from the val set -4-The exact model checkpoint used (e.g., YOLO11n/yolo11n.pt or YOLOv8n/yolov8n.pt) -5-Output of pip show ultralytics torch torchvision and python -V -6-Confirmation if the same issue occurs when running the provided Notebook environments below

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the Ultralytics community where it suits you best. For real-time chat, head to Discord 🎧. Prefer in-depth discussions? Check out Discourse. Or dive into threads on our Subreddit to share knowledge with the community.

Upgrade

Upgrade to the latest ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8 to verify your issue is not already resolved in the latest version:

pip install -U ultralytics

Environments

YOLO may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

To help isolate environment-specific issues, please try to reproduce the problem in one of the Notebook links above and share the exact steps if it persists.

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLO Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Thank you again for the detailed report. Once you share the MRE and requested details, we’ll help investigate further πŸ™

UltralyticsAssistant avatar Sep 29 '25 18:09 UltralyticsAssistant

Minimum Reproducible Example

1. Training Command

Python Script: test_stable_ultralytics.py

model = YOLO('yolov8n.pt')  # Using YOLOv8n pretrained model

results = model.train(
    data='roboflow_building_model/data.yaml',
    epochs=3,
    imgsz=640,
    batch=16,
    device='mps',
    project='runs/detect',
    name='stable_test',
    pretrained=True,
    optimizer='AdamW',
    lr0=0.001,
    workers=0,
    verbose=True,
    val=True  # Validation enabled - this triggers the error
)

2. Full Error Traceback

Epoch 1/100 completes successfully...
Validation starts...

/venv_arm/lib/python3.9/site-packages/ultralytics/utils/metrics.py:854: RuntimeWarning: invalid value encountered in cast
  return tp, fp, p, r, f1, ap, unique_classes.astype(int), p_curve, r_curve, f1_curve, x, prec_values
/venv_arm/lib/python3.9/site-packages/ultralytics/utils/metrics.py:1115: RuntimeWarning: invalid value encountered in cast
  self.nt_per_class = np.bincount(stats["target_cls"].astype(int), minlength=len(self.names))

ERROR - ❌ Training failed: 'list' argument must have no negative elements
Traceback (most recent call last):
  File "train_overnight_multiclass.py", line 268, in train_with_monitoring
    results = self.model.train(
  File "venv_arm/lib/python3.9/site-packages/ultralytics/engine/model.py", line 800, in train
    self.trainer.train()
  File "venv_arm/lib/python3.9/site-packages/ultralytics/engine/trainer.py", line 235, in train
    self._do_train()
  File "venv_arm/lib/python3.9/site-packages/ultralytics/engine/trainer.py", line 477, in _do_train
    self.metrics, self.fitness = self.validate()
  File "venv_arm/lib/python3.9/site-packages/ultralytics/engine/trainer.py", line 691, in validate
    metrics = self.validator(self)
  File "torch/utils/_contextlib.py", line 120, in decorate_context
    return func(*args, **kwargs)
  File "ultralytics/engine/validator.py", line 231, in __call__
    stats = self.get_stats()
  File "ultralytics/models/yolo/detect/val.py", line 236, in get_stats
    self.metrics.process(save_dir=self.save_dir, plot=self.args.plots, on_plot=self.on_plot)
  File "ultralytics/utils/metrics.py", line 1115, in process
    self.nt_per_class = np.bincount(stats["target_cls"].astype(int), minlength=len(self.names))
ValueError: 'list' argument must have no negative elements

3. Dataset Configuration (data.yaml)

names:
- '1'
- '2'
- '4'
- '5'
- building
nc: 5
test: /Users/chris/Local/git/proptic/roboflow_building_model/test/images
train: /Users/chris/Local/git/proptic/roboflow_building_model/train/images
val: /Users/chris/Local/git/proptic/roboflow_building_model/valid/images

Sample Label File (train/labels/10000_jpg.rf.b4deccddc478094255a676e48b740b3b.txt):

0 0.1955923927932585 0.3295181932573746 0.28356128333979885 0.2500978768511919
0 0.16964327158628034 0.15010835987698315 0.2633923045516262 0.2381644558233278
0 0.2808535147491427 0.8101526758596428 0.3560259568274393 0.3542131750125967
3 0.3316727693153164 0.07601560106047195 0.10658445303441755 0.1520312021209439

Dataset Details:

  • 1,740 training images, 166 validation images
  • 5 custom classes not in COCO (1, 2, 4, 5, building)
  • Roboflow Building Dataset
  • Labels are valid YOLO format

4. Model Checkpoint

yolov8n.pt (pretrained COCO weights)

Also tested with yolo11n.pt - same error

5. Environment Details

Name: ultralytics
Version: 8.3.203  # <-- Version with the bug

Name: torch
Version: 2.8.0

Name: torchvision
Version: 0.20.1

Python: 3.9.6
Platform: macOS (Darwin 24.6.0)
Device: mps (Apple Silicon M1/M2/M3)

6. Notebook Environment Testing

Unable to test in provided notebooks as the issue specifically occurs with:

  • Custom classes not in COCO pretrained weights
  • Apple Silicon (MPS) device
  • Dataset requires ~2GB download

Workaround That Works βœ…

Downgrading to stable versions completely resolves the issue:

pip install ultralytics==8.0.196
pip install 'torch<2.6' torchvision --upgrade --force-reinstall

With these versions:

  • βœ… Training completes all epochs
  • βœ… Validation passes without errors
  • βœ… 100% detection rate achieved
  • βœ… Successful CoreML export

Key Observations

  1. Error occurs specifically during validation after epoch 1 completes
  2. RuntimeWarning precedes the error: "invalid value encountered in cast" in metrics.py line 854 and 1115
  3. The error is in np.bincount(): Suggests stats["target_cls"].astype(int) contains negative values
  4. Only affects custom classes: When class IDs don't match COCO pretrained weights
  5. Reproducible 100%: Happens every time with 8.3.203 on custom classes

The bug appears to be in the validation metrics calculation when handling custom class mappings that differ from the pretrained model's class structure.

mousedown avatar Sep 29 '25 18:09 mousedown

It should be fixed next release

Y-T-G avatar Sep 29 '25 19:09 Y-T-G

@mousedown I'm getting the following error for your posted work around

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. opencv-python 4.12.0.88 requires numpy<2.3.0,>=2; python_version >= "3.9", but you have numpy 2.3.3 which is incompatible.

Were you able to overcome this?

Kent-McHenry avatar Oct 09 '25 15:10 Kent-McHenry

This error shouldn't occur in the latest Ultralytics. You don't need to downgrade to older Ultralytics.

Y-T-G avatar Oct 09 '25 16:10 Y-T-G

πŸ‘‹ Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

  • Docs: https://docs.ultralytics.com
  • HUB: https://hub.ultralytics.com
  • Community: https://community.ultralytics.com

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO πŸš€ and Vision AI ⭐

github-actions[bot] avatar Nov 09 '25 00:11 github-actions[bot]