ImbalancedDatasetSampler reweights a PyTorch DataLoader by inverse class frequency and leaves your dataset untouched
A (PyTorch) imbalanced dataset sampler for oversampling low frequent classes and undersampling high frequent ones.
At a glance
- What is it?
- A one-class PyTorch package that plugs into DataLoader's sampler argument and estimates weights on its own. The README documents a single usage pattern and an MNIST demo, and says little about label handling or other task types.
- Who is it for?
- Reach for ImbalancedDatasetSampler if you train a PyTorch classifier on a skewed dataset and want rebalancing from one argument to DataLoader, without writing a weight table or copying your data. Look elsewhere if you need documented behaviour for custom label formats, regression or multi-label targets, because the README covers none of those.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 33 days ago.
- What is it written in?
- Mainly Python, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 6, 2026, and from our analysis. They are not legal advice.
Star history
- ufoym/imbalanced-dataset-sampler2,325 ★ · -2
| Repository | 2026-10-14 | 2026-11-06 |
|---|---|---|
| ufoym/imbalanced-dataset-sampler | 2.3k | 2.3k |
-2 Stars in the last 23 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Accuracy of 80% on 5 disease images and 20 normal ones is the problem it targets
The README opens with an example of why class imbalance misleads. With a dataset of 5 disease images and 20 normal images, a model that predicts every image as normal scores 80% accuracy, and the README gives its F1-score as 0.88. The model has learned nothing about disease, yet both numbers look respectable.
The repository then names resampling as the widely adopted fix. Under-sampling removes samples from the majority class. Over-sampling adds more examples from the minority class. Both come with a stated cost. The simplest over-sampling duplicates random records from the minority class, which can cause overfitting. The simplest under-sampling removes random records from the majority class, which can cause loss of information.
Those two costs frame the rest of the project. The package does not claim to remove them. It claims to make resampling easy to apply and to pair with data augmentation, which the README says mitigates overfitting.
Four behaviours the package promises from a single class
The class is called ImbalancedDatasetSampler, and the README lists four things it does. It rebalances the class distributions when sampling from the imbalanced dataset. It estimates the sampling weights automatically, so there is no weight table to compute. It avoids creating a new balanced dataset, so nothing is copied to disk or memory as a second dataset. And it mitigates overfitting when it is used in conjunction with data augmentation techniques.
The third point decides how it fits a pipeline. Duplicating minority records into a rebuilt dataset would double storage for the rare class. A sampler works at draw time: the dataset stays as it is and the loader decides which indices come out.
The description line adds that the sampler handles oversampling of low frequency classes and undersampling of high frequency ones. The README does not separate the two modes into options.
One pip install and one keyword argument to DataLoader
The package installs under the name torchsampler:
pip install torchsamplerUsage is a single change to the loader. You pass an ImbalancedDatasetSampler for the sampler parameter when creating a DataLoader:
from torchsampler import ImbalancedDatasetSampler
train_loader = torch.utils.data.DataLoader(
train_dataset,
sampler=ImbalancedDatasetSampler(train_dataset),
batch_size=args.batch_size,
**kwargs
)The README adds that in each epoch the loader samples the entire dataset and weighs your samples inversely to your class appearing probability. Rare classes are drawn more often, common classes less.
Because DataLoader takes a sampler argument, this replaces shuffle=True. The package is meant for the training loader. A validation or test loader should keep the natural class distribution if you want accuracy figures that reflect your real data.
The MNIST demo shows gains for classes 2, 6 and 9 with the others preserved
The README's example builds an imbalanced MNIST dataset and shows the class distribution, then trains with and without the sampler. Each run is reported as two plots: test accuracy at every epoch, and a confusion matrix.
The written conclusion is narrow. There are significant improvements for minor classes such as 2, 6 and 9, while the accuracy of the other classes is preserved. No numbers appear in the text, and the plots are images, so the size of the gain has to be read from the figures or reproduced.
The repository includes the code to reproduce it: examples/mnist.ipynb, plus examples/imagefolder.ipynb for a dataset laid out as an image folder. If you want to know whether the sampler helps on your own data, the imagefolder notebook is the closer template, since it uses a layout that real image projects share.
Nothing in the README says how labels are read from your dataset
To weigh samples by class frequency, the sampler has to know each sample's label. The README says it estimates the weights automatically and gives no description of how. It does not say which dataset attributes are read, whether a custom Dataset needs a particular method, or what happens when labels are not integers.
That is the first limitation to test. A standard torchvision dataset may work as the MNIST and image folder notebooks suggest, but a hand-written Dataset that returns tuples of tensors may not. The only way to know from this repository is to open the code under torchsampler/ or run the notebooks against your own class.
The pandas entry in requirements.txt hints that some part of the label handling goes through a DataFrame, but the README never states it, so nothing here depends on that guess. Expect to read the source before using a non-standard dataset.
Classification only: the README never mentions regression, multi-label or detection
Every sentence in the README talks about classes. The introduction is about classes, the example is MNIST, the weighing is inverse to class appearing probability. Regression targets, multi-label outputs and detection or segmentation datasets are not covered or ruled out.
The practical consequence is that a project with a continuous target has no documented route here, and a multi-label dataset has no documented definition of what a class is. Both may fail or silently produce a distribution you did not intend.
The same applies to distributed training. DataLoader accepts one sampler, and the README does not say whether the sampler works alongside a DistributedSampler. If you train across several processes, test that on a small run first, because the weights and the sharding both decide which indices each process sees.
Torch 1.3, Python 3.6 and a deprecated __about__.py show the package's age
setup.py names the package torchsampler, sets python_requires to at least 3.6 and marks the Development Status as 4 - Beta. The version comes from use_scm_version, with setuptools_scm as a setup requirement, so it is read from git tags rather than a literal in the file.
requirements.txt asks for torch>=1.3 and torchvision>=0.5, which are floors rather than pins, plus pandas. An importlib_metadata line for Python below 3.8 carries a comment: it is only needed by __about__.py, which is deprecated. The URL field in setup.py points to imbalanced-dataset-sample, without the final r, so the packaged metadata link does not match the repository name.
None of this stops a modern install, since the floors are low. But it means the project does not state which recent PyTorch release it has been run against, and the README makes no claim about it.
Two releases in May 2022 and a push in October 2026 leave the maintenance picture thin
The recent releases are v0.1.0 on 18 May 2022 and v0.1.2 on 23 May 2022. The repository was last pushed to on 5 October 2026, it is not archived, and the licence is MIT with the default branch master.
So the latest tag is more than four years old, while the branch has seen later activity. The README does not say what the later changes were. If you install from PyPI with pip install torchsampler, you get whatever version is published there. Whether that matches the master branch is not stated, so pin the version you tested.
Contribution rules are short. Bug fixes can be sent without further discussion, and new features, utility functions or extensions should start with an issue so the maintainers can discuss them first. The authors' setup.py credits Ming, et al.
Editorial conclusion
Reach for ImbalancedDatasetSampler if you train a PyTorch classifier on a skewed dataset and want rebalancing from one argument to DataLoader, without writing a weight table or copying your data. Look elsewhere if you need documented behaviour for custom label formats, regression or multi-label targets, because the README covers none of those. Before relying on it, run the imagefolder or mnist notebook on your own labels and count how often each class actually appears in a batch.
Frequently asked questions
What does it mean when a dataset is imbalanced?
Some types of data appear more often than others. The README gives 5 disease images against 20 normal images as an example, where a model that predicts everything as normal scores 80% accuracy.
How can I fix an imbalanced dataset?
The README names resampling: remove samples from the majority class, add examples from the minority class, or both. This repository implements it as a PyTorch sampler, ImbalancedDatasetSampler, so no new dataset is created.
Which method is commonly used to handle imbalanced datasets?
The README calls resampling the widely adopted technique. Over-sampling the minority class can cause overfitting, and under-sampling the majority class can cause loss of information.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/ufoym-imbalanced-dataset-sampler)Star history chart for your README
[](https://hysenlabs.com/projects/ufoym-imbalanced-dataset-sampler)Was this useful?