balanced-loss


Namebalanced-loss JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/fcakyon/balanced-loss
SummaryEasy to use class-balanced cross-entropy and focal loss implementation for Pytorch.
upload_time2024-12-16 18:59:18
maintainerNone
docs_urlNone
authorfcakyon
requires_python>=3.7
licenseMIT
keywords machine-learning deep-learning ml pytorch vision loss image-classification video-classification
VCS
bugtrack_url
requirements torch numpy
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
<img src="https://user-images.githubusercontent.com/34196005/180311379-1003da44-cdf9-46e8-af83-e65fbc3710cd.png" width="350">
</p>

<div align="center">
    <a href="https://badge.fury.io/py/balanced-loss"><img src="https://badge.fury.io/py/balanced-loss.svg" alt="pypi version"></a>
    <a href="https://pepy.tech/project/balanced-loss"><img src="https://pepy.tech/badge/balanced-loss" alt="total downloads"></a>
    <a href="https://twitter.com/fcakyon"><img src="https://img.shields.io/badge/twitter-fcakyon_-blue?logo=twitter&style=flat" alt="fcakyon twitter"></a>
</div>

<p align="center">
    Easy-to-use, class-balanced, cross-entropy and focal loss implementation for Pytorch.
</p>

## Theory

When training dataset labels are imbalanced, one thing to do is to balance the loss across sample classes.

- First, the effective number of samples are calculated for all classes as:

![alt-text](https://user-images.githubusercontent.com/34196005/180266195-aa2e8696-cdeb-48ed-a85f-7ffb353942a4.png)

- Then the class balanced loss function is defined as:

![alt-text](https://user-images.githubusercontent.com/34196005/180266198-e27d8cba-f5e1-49ca-9f82-d8656333e3c4.png)

## Installation

```bash
pip install balanced-loss
```

## Usage

- Standard losses:

```python
import torch
from balanced_loss import Loss

# outputs and labels
logits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class
labels = torch.tensor([0]) # 1 batch

# focal loss
focal_loss = Loss(loss_type="focal_loss")
loss = focal_loss(logits, labels)
```

```python
# cross-entropy loss
ce_loss = Loss(loss_type="cross_entropy")
loss = ce_loss(logits, labels)
```

```python
# binary cross-entropy loss
bce_loss = Loss(loss_type="binary_cross_entropy")
loss = bce_loss(logits, labels)
```

- Class-balanced losses:

```python
import torch
from balanced_loss import Loss

# outputs and labels
logits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class
labels = torch.tensor([0]) # 1 batch

# number of samples per class in the training dataset
samples_per_class = [30, 100, 25] # 30, 100, 25 samples for labels 0, 1 and 2, respectively

# class-balanced focal loss
focal_loss = Loss(
    loss_type="focal_loss",
    samples_per_class=samples_per_class,
    class_balanced=True
)
loss = focal_loss(logits, labels)
```

```python
# class-balanced cross-entropy loss
ce_loss = Loss(
    loss_type="cross_entropy",
    samples_per_class=samples_per_class,
    class_balanced=True
)
loss = ce_loss(logits, labels)
```

```python
# class-balanced binary cross-entropy loss
bce_loss = Loss(
    loss_type="binary_cross_entropy",
    samples_per_class=samples_per_class,
    class_balanced=True
)
loss = bce_loss(logits, labels)
```

- Customize parameters:

```python
import torch
from balanced_loss import Loss

# outputs and labels
logits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class
labels = torch.tensor([0])

# number of samples per class in the training dataset
samples_per_class = [30, 100, 25] # 30, 100, 25 samples for labels 0, 1 and 2, respectively

# class-balanced focal loss
focal_loss = Loss(
    loss_type="focal_loss",
    beta=0.999, # class-balanced loss beta
    fl_gamma=2, # focal loss gamma
    samples_per_class=samples_per_class,
    class_balanced=True
)
loss = focal_loss(logits, labels)
```

## Improvements

What is the difference between this repo and vandit15's?

- This repo is a pypi installable package
- This repo implements loss functions as `torch.nn.Module`
- In addition to class balanced losses, this repo also supports the standard versions of the cross entropy/focal loss etc. over the same API
- All typos and errors in vandit15's source are fixed
- Continiously tested on PyTorch 1.13.1 and 2.5.1

## References

https://arxiv.org/abs/1901.05555

https://github.com/richardaecn/class-balanced-loss

https://github.com/vandit15/Class-balanced-loss-pytorch

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/fcakyon/balanced-loss",
    "name": "balanced-loss",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "machine-learning, deep-learning, ml, pytorch, vision, loss, image-classification, video-classification",
    "author": "fcakyon",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/87/9e/c8d9e2a1df92968f7f7c2f440431363240df9004795f8e407c8efab7076d/balanced_loss-0.1.1.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n<img src=\"https://user-images.githubusercontent.com/34196005/180311379-1003da44-cdf9-46e8-af83-e65fbc3710cd.png\" width=\"350\">\n</p>\n\n<div align=\"center\">\n    <a href=\"https://badge.fury.io/py/balanced-loss\"><img src=\"https://badge.fury.io/py/balanced-loss.svg\" alt=\"pypi version\"></a>\n    <a href=\"https://pepy.tech/project/balanced-loss\"><img src=\"https://pepy.tech/badge/balanced-loss\" alt=\"total downloads\"></a>\n    <a href=\"https://twitter.com/fcakyon\"><img src=\"https://img.shields.io/badge/twitter-fcakyon_-blue?logo=twitter&style=flat\" alt=\"fcakyon twitter\"></a>\n</div>\n\n<p align=\"center\">\n    Easy-to-use, class-balanced, cross-entropy and focal loss implementation for Pytorch.\n</p>\n\n## Theory\n\nWhen training dataset labels are imbalanced, one thing to do is to balance the loss across sample classes.\n\n- First, the effective number of samples are calculated for all classes as:\n\n![alt-text](https://user-images.githubusercontent.com/34196005/180266195-aa2e8696-cdeb-48ed-a85f-7ffb353942a4.png)\n\n- Then the class balanced loss function is defined as:\n\n![alt-text](https://user-images.githubusercontent.com/34196005/180266198-e27d8cba-f5e1-49ca-9f82-d8656333e3c4.png)\n\n## Installation\n\n```bash\npip install balanced-loss\n```\n\n## Usage\n\n- Standard losses:\n\n```python\nimport torch\nfrom balanced_loss import Loss\n\n# outputs and labels\nlogits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class\nlabels = torch.tensor([0]) # 1 batch\n\n# focal loss\nfocal_loss = Loss(loss_type=\"focal_loss\")\nloss = focal_loss(logits, labels)\n```\n\n```python\n# cross-entropy loss\nce_loss = Loss(loss_type=\"cross_entropy\")\nloss = ce_loss(logits, labels)\n```\n\n```python\n# binary cross-entropy loss\nbce_loss = Loss(loss_type=\"binary_cross_entropy\")\nloss = bce_loss(logits, labels)\n```\n\n- Class-balanced losses:\n\n```python\nimport torch\nfrom balanced_loss import Loss\n\n# outputs and labels\nlogits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class\nlabels = torch.tensor([0]) # 1 batch\n\n# number of samples per class in the training dataset\nsamples_per_class = [30, 100, 25] # 30, 100, 25 samples for labels 0, 1 and 2, respectively\n\n# class-balanced focal loss\nfocal_loss = Loss(\n    loss_type=\"focal_loss\",\n    samples_per_class=samples_per_class,\n    class_balanced=True\n)\nloss = focal_loss(logits, labels)\n```\n\n```python\n# class-balanced cross-entropy loss\nce_loss = Loss(\n    loss_type=\"cross_entropy\",\n    samples_per_class=samples_per_class,\n    class_balanced=True\n)\nloss = ce_loss(logits, labels)\n```\n\n```python\n# class-balanced binary cross-entropy loss\nbce_loss = Loss(\n    loss_type=\"binary_cross_entropy\",\n    samples_per_class=samples_per_class,\n    class_balanced=True\n)\nloss = bce_loss(logits, labels)\n```\n\n- Customize parameters:\n\n```python\nimport torch\nfrom balanced_loss import Loss\n\n# outputs and labels\nlogits = torch.tensor([[0.78, 0.1, 0.05]]) # 1 batch, 3 class\nlabels = torch.tensor([0])\n\n# number of samples per class in the training dataset\nsamples_per_class = [30, 100, 25] # 30, 100, 25 samples for labels 0, 1 and 2, respectively\n\n# class-balanced focal loss\nfocal_loss = Loss(\n    loss_type=\"focal_loss\",\n    beta=0.999, # class-balanced loss beta\n    fl_gamma=2, # focal loss gamma\n    samples_per_class=samples_per_class,\n    class_balanced=True\n)\nloss = focal_loss(logits, labels)\n```\n\n## Improvements\n\nWhat is the difference between this repo and vandit15's?\n\n- This repo is a pypi installable package\n- This repo implements loss functions as `torch.nn.Module`\n- In addition to class balanced losses, this repo also supports the standard versions of the cross entropy/focal loss etc. over the same API\n- All typos and errors in vandit15's source are fixed\n- Continiously tested on PyTorch 1.13.1 and 2.5.1\n\n## References\n\nhttps://arxiv.org/abs/1901.05555\n\nhttps://github.com/richardaecn/class-balanced-loss\n\nhttps://github.com/vandit15/Class-balanced-loss-pytorch\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Easy to use class-balanced cross-entropy and focal loss implementation for Pytorch.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/fcakyon/balanced-loss"
    },
    "split_keywords": [
        "machine-learning",
        " deep-learning",
        " ml",
        " pytorch",
        " vision",
        " loss",
        " image-classification",
        " video-classification"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fe4ba6d4f86228c88c71637da4729723844df4c40185598e84d6125f386766ab",
                "md5": "b970a5a5ae5f55ca4531adffd6ce9e94",
                "sha256": "1e7f993c6751a52d9c2aa250f091ba37acc558cc576d9756e5072922e6258a13"
            },
            "downloads": -1,
            "filename": "balanced_loss-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b970a5a5ae5f55ca4531adffd6ce9e94",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 5511,
            "upload_time": "2024-12-16T18:59:16",
            "upload_time_iso_8601": "2024-12-16T18:59:16.888356Z",
            "url": "https://files.pythonhosted.org/packages/fe/4b/a6d4f86228c88c71637da4729723844df4c40185598e84d6125f386766ab/balanced_loss-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "879ec8d9e2a1df92968f7f7c2f440431363240df9004795f8e407c8efab7076d",
                "md5": "56a037b32aef34660ceea8e4f0c5e02f",
                "sha256": "f282420d5e743f530c818d89a3b114746bad2d01a929155d6cfb1667f1cd60fb"
            },
            "downloads": -1,
            "filename": "balanced_loss-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "56a037b32aef34660ceea8e4f0c5e02f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 6187,
            "upload_time": "2024-12-16T18:59:18",
            "upload_time_iso_8601": "2024-12-16T18:59:18.050555Z",
            "url": "https://files.pythonhosted.org/packages/87/9e/c8d9e2a1df92968f7f7c2f440431363240df9004795f8e407c8efab7076d/balanced_loss-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-16 18:59:18",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "fcakyon",
    "github_project": "balanced-loss",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "torch",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        }
    ],
    "lcname": "balanced-loss"
}
        
Elapsed time: 0.45251s