WZMIAOMIAO/deep-learning-for-image-processing · error · ValueError
sampler should be an instance of torch.utils.data.Sampler, b
Error message
sampler should be an instance of torch.utils.data.Sampler, but got sampler={} What it means
GroupedBatchSampler.__init__ requires `sampler` to be an instance of torch.utils.data.Sampler; anything else raises ValueError. group_ids must also be a continuous integer range [0, num_groups).
Source
Thrown at pytorch_object_detection/faster_rcnn/train_utils/group_by_aspect_ratio.py:39
class GroupedBatchSampler(BatchSampler):
"""
Wraps another sampler to yield a mini-batch of indices.
It enforces that the batch only contain elements from the same group.
It also tries to provide mini-batches which follows an ordering which is
as close as possible to the ordering from the original sampler.
Arguments:
sampler (Sampler): Base sampler.
group_ids (list[int]): If the sampler produces indices in range [0, N),
`group_ids` must be a list of `N` ints which contains the group id of each sample.
The group ids must be a continuous set of integers starting from
0, i.e. they must be in the range [0, num_groups).
batch_size (int): Size of mini-batch.
"""
def __init__(self, sampler, group_ids, batch_size):
if not isinstance(sampler, Sampler):
raise ValueError(
"sampler should be an instance of "
"torch.utils.data.Sampler, but got sampler={}".format(sampler)
)
self.sampler = sampler
self.group_ids = group_ids
self.batch_size = batch_size
def __iter__(self):
buffer_per_group = defaultdict(list)
samples_per_group = defaultdict(list)
num_batches = 0
for idx in self.sampler:
group_id = self.group_ids[idx]
buffer_per_group[group_id].append(idx)
samples_per_group[group_id].append(idx)
if len(buffer_per_group[group_id]) == self.batch_size:
yield buffer_per_group[group_id]View on GitHub (pinned to 1ec3fe6f37)
Solutions
- Wrap the indices in torch.utils.data.sampler.SequentialSampler or RandomSampler before passing
- If using a custom sampler, make it inherit from torch.utils.data.Sampler and implement __iter__/__len__
- Check you did not accidentally pass the dataset or batch_size object as the first argument
Example fix
// before sampler = list(range(len(dataset))) gbs = GroupedBatchSampler(sampler, group_ids, batch_size=8) // after from torch.utils.data import SequentialSampler sampler = SequentialSampler(dataset) gbs = GroupedBatchSampler(sampler, group_ids, batch_size=8)
Defensive patterns
Strategy: type-guard
Validate before calling
from torch.utils.data import Sampler assert isinstance(sampler, Sampler), "sampler must be a torch.utils.data.Sampler" gbs = GroupedBatchSampler(sampler, group_ids, batch_size=8)
Type guard
from torch.utils.data import Sampler
def is_sampler(s) -> bool:
return isinstance(s, Sampler) Try / catch
try:
gbs = GroupedBatchSampler(sampler, group_ids, batch_size)
except ValueError as e:
if "instance of" in str(e):
from torch.utils.data import SequentialSampler
gbs = GroupedBatchSampler(SequentialSampler(dataset), group_ids, batch_size) Prevention
- Wrap raw index lists in SequentialSampler/RandomSampler
- Ensure custom samplers subclass torch.utils.data.Sampler
- Verify group_ids is a continuous range starting at 0
When it happens
Trigger: Passing a plain list, range, generator, or a function as `sampler` instead of a Sampler instance, e.g. GroupedBatchSampler(list(range(100)), group_ids, batch_size).
Common situations: Confusing sampler with the underlying indices; passing a dataset or DataLoader instead of a sampler; custom samplers not inheriting from torch.utils.data.Sampler after a refactor.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- sampler should be an instance of torch.utils.data.Sampler, b
- expected stages_repeats as list of 3 positive ints
- expected stages_out_channels as list of 5 positive ints
- image: {} isn't RGB mode.
- dataset have {} classes, but input {}
AI-assisted analysis of WZMIAOMIAO/deep-learning-for-image-processing@1ec3fe6f37 (2026-08-30).
Data as JSON: /api/errors/acf2ac8b5bd5ee81.
Report an issue: GitHub.