57

GitHub - ufoym/imbalanced-dataset-sampler: A (PyTorch) imbalanced dataset sample...

 5 years ago
source link: https://github.com/ufoym/imbalanced-dataset-sampler
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Imbalanced Dataset Sampler

license


Introduction

In many machine learning applications, we often come across datasets where some types of data may be seen more than other types. Take identification of rare diseases for example, there are probably more normal samples than disease ones. In these cases, we need to make sure that the trained model is not biased towards the class that has more data. As an example, consider a dataset where there are 5 disease images and 20 normal images. If the model predicts all images to be normal, its accuracy is 80%, and F1-score of such a model is 0.88. Therefore, the model has high tendency to be biased toward the ‘normal’ class.

To solve this problem, a widely adopted technique is called resampling. It consists of removing samples from the majority class (under-sampling) and / or adding more examples from the minority class (over-sampling). Despite the advantage of balancing classes, these techniques also have their weaknesses (there is no free lunch). The simplest implementation of over-sampling is to duplicate random records from the minority class, which can cause overfitting. In under-sampling, the simplest technique involves removing random records from the majority class, which can cause loss of information.

resampling

In this repo, we implement an easy-to-use PyTorch sampler ImbalancedDatasetSampler that is able to

rebalance the class distributions when sampling from the imbalanced dataset estimate the sampling weights automatically avoid creating a new balanced dataset mitigate overfitting when it is used in conjunction with data augmentation techniques

40677251-b08f504a-63af-11e8-9653-f28e973a5664.png

Usage

Simply pass an ImbalancedDatasetSampler for the parameter sampler when creating a DataLoader. For example:

Then in each epoch, the loader will sample the entire dataset and weigh your samples inversely to your class appearing probability.

Example: Imbalanced MNIST Dataset

Distribution of classes in the imbalanced dataset:

40678060-03c6eb9a-63b2-11e8-9a81-c61c665240e3.png

With Imbalanced Dataset Sampler:

40678100-1e543f12-63b2-11e8-9c05-3b168c3fb39f.png (left: test acc in each epoch; right: confusion matrix)

Without Imbalanced Dataset Sampler:

40678099-1e0a481c-63b2-11e8-9ec8-f2ac4bf7a637.png (left: test acc in each epoch; right: confusion matrix)

Note that there are significant improvements for minor classes such as 2 6 9, while the accuracy of the other classes is preserved.

Contributing

We appreciate all contributions. If you are planning to contribute back bug-fixes, please do so without any further discussion. If you plan to contribute new features, utility functions or extensions, please first open an issue and discuss the feature with us.

Licensing

MIT licensed.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK