open-mmlab/mmdetection · error · ValueError
batch_size should be a positive integer value, but got batch
Error message
batch_size should be a positive integer value, but got batch_size={batch_size} What it means
AspectRatioBatchSampler validates that batch_size is a positive Python int (bools excluded implicitly by the isinstance check combined with <=0 guard only for numbers; actually bool passes isinstance(int)). A non-int or non-positive value triggers this ValueError at construction.
Source
Thrown at mmdet/datasets/samplers/batch_sampler.py:32
>= 1) into a same batch.
Args:
sampler (Sampler): Base sampler.
batch_size (int): Size of mini-batch.
drop_last (bool): If ``True``, the sampler will drop the last batch if
its size would be less than ``batch_size``.
"""
def __init__(self,
sampler: Sampler,
batch_size: int,
drop_last: bool = False) -> None:
if not isinstance(sampler, Sampler):
raise TypeError('sampler should be an instance of ``Sampler``, '
f'but got {sampler}')
if not isinstance(batch_size, int) or batch_size <= 0:
raise ValueError('batch_size should be a positive integer value, '
f'but got batch_size={batch_size}')
self.sampler = sampler
self.batch_size = batch_size
self.drop_last = drop_last
# two groups for w < h and w >= h
self._aspect_ratio_buckets = [[] for _ in range(2)]
def __iter__(self) -> Sequence[int]:
for idx in self.sampler:
data_info = self.sampler.dataset.get_data_info(idx)
width, height = data_info['width'], data_info['height']
bucket_id = 0 if width < height else 1
bucket = self._aspect_ratio_buckets[bucket_id]
bucket.append(idx)
# yield a batch of indices in the same aspect ratio group
if len(bucket) == self.batch_size:
yield bucket[:]
del bucket[:]View on GitHub (pinned to cfd5d3a985)
Solutions
- Convert to a plain positive int before passing: batch_size=int(batch_size)
- Ensure the computed value is >= 1 (guard divisions like len(dataset)//world_size so they never yield 0)
- Fix the config so batch_size is an integer literal, not a string or float
Example fix
# before batch_sampler = AspectRatioBatchSampler(sampler=s, batch_size='4') # after batch_sampler = AspectRatioBatchSampler(sampler=s, batch_size=4)
Defensive patterns
Strategy: type-guard
Validate before calling
assert isinstance(batch_size, int) and not isinstance(batch_size, bool) and batch_size > 0, 'batch_size must be a positive int'
Type guard
def is_positive_int(v) -> bool:
return isinstance(v, int) and not isinstance(v, bool) and v > 0 Prevention
- int()-cast batch sizes read from CLI/env/config templating
- Guard computed batch sizes (len(dataset)//world_size) with max(1, ...)
When it happens
Trigger: Passing batch_size as a float (2.0), a string ('2'), 0, a negative number, or None to AspectRatioBatchSampler; commonly happens when batch_size comes from a config/env variable parsed as string or float.
Common situations: Reading batch size from CLI args or environment without int() conversion; computing batch_size dynamically (e.g. len(dataset)//num_gpus yielding 0 for tiny datasets); config templating that leaves it as a string.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- sampler should be an instance of ``Sampler``, but got {sampl
- dataset metainfo must contain `classes`
- img_border_value must be float or tuple with 3 elements.
- metric item "{metric_item}" is not supported
- metric must be a list or a str.
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/2c1f78bb47870f68.
Report an issue: GitHub.