{"record":{"id":"2c1f78bb47870f68","repo":"open-mmlab/mmdetection","slug":"batch-size-should-be-a-positive-integer-value-but","errorCode":null,"errorMessage":"batch_size should be a positive integer value, but got batch_size={batch_size}","messagePattern":"batch_size should be a positive integer value, but got batch_size=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mmdet/datasets/samplers/batch_sampler.py","lineNumber":32,"sourceCode":"\n    >= 1) into a same batch.\n\n    Args:\n        sampler (Sampler): Base sampler.\n        batch_size (int): Size of mini-batch.\n        drop_last (bool): If ``True``, the sampler will drop the last batch if\n            its size would be less than ``batch_size``.\n    \"\"\"\n\n    def __init__(self,\n                 sampler: Sampler,\n                 batch_size: int,\n                 drop_last: bool = False) -> None:\n        if not isinstance(sampler, Sampler):\n            raise TypeError('sampler should be an instance of ``Sampler``, '\n                            f'but got {sampler}')\n        if not isinstance(batch_size, int) or batch_size <= 0:\n            raise ValueError('batch_size should be a positive integer value, '\n                             f'but got batch_size={batch_size}')\n        self.sampler = sampler\n        self.batch_size = batch_size\n        self.drop_last = drop_last\n        # two groups for w < h and w >= h\n        self._aspect_ratio_buckets = [[] for _ in range(2)]\n\n    def __iter__(self) -> Sequence[int]:\n        for idx in self.sampler:\n            data_info = self.sampler.dataset.get_data_info(idx)\n            width, height = data_info['width'], data_info['height']\n            bucket_id = 0 if width < height else 1\n            bucket = self._aspect_ratio_buckets[bucket_id]\n            bucket.append(idx)\n            # yield a batch of indices in the same aspect ratio group\n            if len(bucket) == self.batch_size:\n                yield bucket[:]\n                del bucket[:]","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/datasets/samplers/batch_sampler.py#L14-L50","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nbatch_sampler = AspectRatioBatchSampler(sampler=s, batch_size='4')\n# after\nbatch_sampler = AspectRatioBatchSampler(sampler=s, batch_size=4)","handlingStrategy":"type-guard","validationCode":"assert isinstance(batch_size, int) and not isinstance(batch_size, bool) and batch_size > 0, 'batch_size must be a positive int'","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v > 0","tryCatchPattern":null,"preventionTips":["int()-cast batch sizes read from CLI/env/config templating","Guard computed batch sizes (len(dataset)//world_size) with max(1, ...)"],"tags":["mmdet","sampler","batch-size","valueerror","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}