Imageomics / Imageomics/saev

Bug: BatchLimiter incorrectly counts samples

Open
#17 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
66
Forks
12
PR merge metrics
No merged PRs in 30d

Description

Description

The BatchLimiter class in src/saev/utils/scheduling.py incorrectly counts the number of samples seen during iteration, leading to premature termination or overcounting when the actual batch size is smaller than the expected batch size.

Location

src/saev/utils/scheduling.py:114

Root Cause

In the __iter__ method, the code always increments self.n_seen by self.batch_size:

self.n_seen += self.batch_size
if self.n_seen > self.n_samples:
    return

However, the actual batch yielded might have fewer samples than self.batch_size, particularly:

  1. For the last batch when drop_last=False
  2. For dataloaders with uneven dataset sizes

This causes the limiter to overcount samples, terminating the iterator at the wrong time.

Expected Behavior

The BatchLimiter should count the actual number of samples in each batch, not assume all batches have size self.batch_size.

Actual Behavior

The limiter terminates based on incorrect counts, yielding either too many or too few samples.

Example

If we have:

  • A dataloader with 105 samples
  • batch_size = 32
  • drop_last = False
  • n_samples = 100 (what we want from BatchLimiter)

The batches would be: [32, 32, 32, 9]

But the counter would be: [32, 64, 96, 128]

When the counter hits 128 > 100, it returns after yielding all 105 samples (not 100).

Reproduction

See the unit tests in tests/test_batch_limiter.py which demonstrate this bug:

uv run --no-dev python -m pytest tests/test_batch_limiter.py -v

Test results:

  • test_batch_limiter_with_uneven_batches: Expected ≤100 samples, got 105
  • test_batch_limiter_early_termination: Expected 100 samples, got 160

Proposed Fix

Change line 114 to count the actual batch size instead of always using self.batch_size.

See PR for the implementation.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/saev/utils/scheduling.py at the BatchLimiter.iter method and run tests/test_batch_limiter.py using the provided pytest command. Verify the uneven-batch and early-termination cases, then confirm the limiter yields no more than the requested number of samples for both scenarios.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.