Skip to content

COCOLoader ignores max_individuals from the model config #3432

Closed

Description

@juan-cobos

Is there an existing issue for this?

  • I have searched the existing issues

Operating System

Manjaro Linux

DeepLabCut version

3.0.1

What engine are you using?

pytorch

DeepLabCut mode

multi animal

Device type

NVIDIA RTX 4000 Ada Generation

Bug description 🐛

COCOLoader.get_dataset_parameters derives individuals from train.json and ignores model_cfg["metadata"]["individuals"]. DLCLoader does the opposite and treats the config as authoritative. As a result, max_individuals has no effect on COCO projects, and adaptation_train crashes partway through training if any evaluation image holds more individuals than the busiest training image.

Steps To Reproduce

  1. Build a COCO project where the busiest train image has 3 instances and the busiest test image has 4.
  2. PoseConfig.build_for_superanimal_inference(..., max_individuals=4) and write it to pytorch_config.yaml.
  3. Call adaptation_train(project_root=..., train_file="train.json", test_file="test.json", model_config_path=, eval_interval=5, ...).

Relevant log output

Traceback (most recent call last):
  File "train_dlc.py", line 149, in <module>
    train_dlc(project_root)
  File "train_dlc.py", line 109, in train_dlc
    adaptation_train(
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/modelzoo/train_from_coco.py", line 79, in adaptation_train
    train(
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/apis/training.py", line 194, in train
    runner.fit(
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/runners/train.py", line 230, in fit
    valid_loss = self._epoch(valid_loader, mode="eval", display_iters=display_iters)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/runners/train.py", line 275, in _epoch
    for i, batch in enumerate(loader):
                    ^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/torch/utils/data/dataloader.py", line 725, in __next__
    data = self._next_data()
           ^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/torch/utils/data/dataloader.py", line 785, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/torch/utils/data/_utils/fetch.py", line 54, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
            ~~~~~~~~~~~~^^^^^
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/data/dataset.py", line 276, in __getitem__
    return self._prepare_final_data_dict(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/data/dataset.py", line 313, in _prepare_final_data_dict
    "annotations": self._prepare_final_annotation_dict(keypoints, keypoints_unique, bboxes, annotations_merged),
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/data/dataset.py", line 347, in _prepare_final_annotation_dict
    "keypoints": pad_to_length(keypoints[..., :3], num_animals, 0).astype(np.single),
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/deeplabcut/pose_estimation_pytorch/data/utils.py", line 528, in pad_to_length
    raise ValueError(f"Cannot pad! data.shape={data.shape} > length={length}")
ValueError: Cannot pad! data.shape=(4, 27, 3) > length=3

Anything else?

I would suggest to validate individuals before any training run to avoid hanging out at validation after X epochs.

Code of Conduct

Activity

  1. deruyter92 commented on Aug 5, 2026

    @deruyter92
    Collaborator

    @juan-cobos thanks a lot for raising this issue and for the clear analysis! I've opened #3435 to address the issue.

    Let me know if it works for you or in case you have any additionall suggestions for the PR.

  2. added theissue type on Aug 5, 2026
  3. added
    bug
    on Aug 5, 2026
  4. juan-cobos commented on Aug 5, 2026

    @juan-cobos
    ContributorAuthor

    @deruyter92 I tested your PR in my previous train/test split and it fixes it. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions