Metadata-Version: 2.1
Name: unified-focal-loss-pytorch
Version: 0.1.2
Summary: An implementation of loss functions from "Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medical image segmentation"
License: MIT
Author: Taylor Denouden
Author-email: taylordenouden@gmail.com
Requires-Python: >=3.9,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Dist: einops (>=0.6.1,<0.7.0)
Requires-Dist: torch (>=2.0.1,<3.0.0)
Description-Content-Type: text/markdown

# Unified Focal Loss PyTorch

An implementation of loss functions
from [“Unified Focal loss: Generalising Dice and cross entropy-based losses to handle class imbalanced medical image segmentation”][1]

Extended for multiclass classification and to allow passing an ignore index.

*Note: This implementation is not tested against the original implementation. It varies
from the original implementation based on my own interpretation of the paper.*

[1]: https://github.com/mlyg/unified-focal-loss

## Installation

```bash
pip install unified-focal-loss-pytorch
```

## Usage

```python
import torch
import torch.nn.functional as F
from unified_focal_loss import AsymmetricUnifiedFocalLoss

loss_fn = AsymmetricUnifiedFocalLoss(
    delta=0.7,
    gamma=0.5,
    ignore_index=2,
)

logits = torch.tensor([
    [[0.1000, 0.4000],
     [0.2000, 0.5000],
     [0.3000, 0.6000]],

    [[0.7000, 0.0000],
     [0.8000, 0.1000],
     [0.9000, 0.2000]]
])

# Shape should be (batch_size, num_classes, ...)
probs = F.softmax(logits, dim=1)
# Shape should be (batch_size, ...). Not one-hot encoded.
targets = torch.tensor([
    [0, 1],
    [2, 0],
])

loss = loss_fn(probs, targets)
print(loss)
# >>> tensor(0.6737)
```

## Detailed API Reference
See [API docs](docs/api.md).

## License
See [LICENSE](LICENSE).

