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”
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.
Installation
pip install unified-focal-loss-pytorch
Usage
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.
License
See LICENSE.
Metadata
Release files for unified-focal-loss-pytorch 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| unified_focal_loss_pytorch-0.1.2.tar.gz | 6.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| unified_focal_loss_pytorch-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.4 kB
Release files / unified_focal_loss_pytorch-0.1.2.tar.gz
| Download URL | unified_focal_loss_pytorch-0.1.2.tar.gz |
|---|---|
| Size | 6.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d43be8e91943bd951ee7353e6a64b07a296373827de2374cbb0126ed8d30ab01
|
|
BLAKE2b-256 checksum How to use checksums |
6adc581e25141b9068933bf5edad4c790ebe34868fd82fb19ff2a81173b6a5a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
|
Release files / unified_focal_loss_pytorch-0.1.2-py3-none-any.whl
| Download URL | unified_focal_loss_pytorch-0.1.2-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0631b618d10c9537a0385f618c78ad6f1ecec8d68489f9812d2f76a54c5b73b6
|
|
BLAKE2b-256 checksum How to use checksums |
fe15756875ed41bd147d7e7a9eee5bb76429b83ce3867c0cfff4d1b0cda04766
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.5.1 CPython/3.10.12 Linux/5.15.0-1041-azure
|