Skip to main content

No project description provided

Project description

PyPI - Latest release GitHub - Repository license

A PyTorch implementation of the Exclusive Cross Entropy Loss.

Features

  • Perform sparse-shot learning from non-exhaustively annotated datasets

  • Plug-n-play components of Binary Exclusive Cross-Entropy and Exclusive Cross-entropy as substitutes for the original Cross-entropy pytorch functions

  • 1-2 changes in lines of code for Exclusive cross-entropy loss compared to native pytorch cross-entropy

  • Simple and modular loss class for problem personalisation if needed

  • Example training code provided for a simple segmentation case

Examples

See here for the simplest example in a converged training state

import ExclusiveCrossEntropyLoss

loss = ExclusiveCrossEntropyLoss()
input = torch.randn(2, 3, requires_grad=True)
target = torch.empty(2, dtype=torch.long).random_(3)
output = loss(input, target)

For setting the epoch state during training the set_epoch must be used for the annealing functions as

import ExclusiveCrossEntropyLoss

# Epoch `loss.epoch` is initialised at zero. Can be changed by `ExclusiveCrossEntropyLoss(epoch=epoch)`
loss = ExclusiveCrossEntropyLoss()

# Updates the current epoch. The loss value for the unallabelled samples depends heavily on the current state
#     because the background sampling and threshold annealing function decide how much of the background class
#     to incorporate into the loss and how strict the exclusivity condition should be.
loss.set_epoch(100)

input = torch.randn(2, 3, requires_grad=True)
target = torch.empty(2, dtype=torch.long).random_(3)
output = loss(input, target)
# Alternatively, epoch can be given as an optional argument as `loss(input, target, epoch=epoch)`

Be default, sigmoid annealing functions are used for both the (negative) background sampling and the exclusivity condition. If a different annealing is required then this can be changed by

import ExclusiveCrossEntropyLoss
annealing_funct = lambda epoch, threshold: threshold  # For no annealing
loss = ExclusiveCrossEntropyLoss(exclusivity_threshold_annealing=annealing_funct, background_sampling_annealing=annealing_funct)

For switching off the exclusivity condition, or adjusting the other parameters of the exclusive loss this can be done by

import ExclusiveCrossEntropyLoss
loss = ExclusiveCrossEntropyLoss(exclusivity_threshold=1,
                                 background_sampling_threshold=1,
                                 exclusivity_threshold_annealing=lambda epoch, threshold: threshold,
                                 background_sampling_annealing=lambda epoch, threshold: threshold,
                                 focal_loss_parameters=(1, 0),
                                )

Lastly and very importantly, the samples that are considered as unlabelled must be defined. By default the multiclass exlusive cross entropy (ExclusiveCrossEntropyLoss) defines unlabelled samples as the ones with targets equal to zero; as defined by the zeroth_label_as_unannotated function.

For the binary loss (BCELoss, BCEWithLogitsLoss), this is switched off and assumes all input as unlabelled, as defined by the _identity_mapping_single_input_torch_ones inner function.

If custom unlabelled sample mapping is required this can be adjusted by setting the unannotated_mapping variable as

import ExclusiveCrossEntropyLoss
loss = ExclusiveCrossEntropyLoss()

loss.set_unannotated_mapping(lambda targets: targets == 1)  # For the background class being assigned integer 1

A proof of concept is provided for the TNBC dataset in the examples directory with the necessary code to use the exclusive cross-entropy loss in a segmentation task.

Install

pip install -e .

Use the loss

import ExclusiveCrossEntropyLoss
loss = ExclusiveCrossEntropyLoss()
output = loss(input, target)  # just as in the ordinary CrossEntropyLoss

For more specific usages the exclusive configuration can be adjusted by:

loss = ExclusiveCrossEntropyLoss(exclusivity_threshold= 0.5,
                                 background_sampling_threshold = 0.5,
                                 exclusivity_threshold_annealing = annealing_function,
                                 background_sampling_annealing = annealing_function,
                                 focal_loss_parameters = (0.2, 0.1)
                                )  # indicating the default values and a general annealing_function

Run PyTorch Experiments

After installing ECE run:

python train_tnbc [--seed] [--lr] [--loss] [--train_path] [--train_path] [--eval_path] [--test_path] [--epochs] [--batch_size] [--device]
  • Available values for --loss are ece and ce for exclusive cross-entropy and cross-entropy respectively.

  • Use the --device flag to set device either cuda to train on the GPU or cpu to train on the CPU.

The simple segmentation task on TNBC on the lisa surf sara cluster, using a GTX1080-ti GPU the results are:

DICE

Cross-entropy

Exclusive Cross-Entropy

TNBC @30%

0.78

0.78

TNBC @30%

0.08

0.41

Citation

Panteli, A., Teuwen, J., Horlings, H. and Gavves, E.; Sparse-shot Learning with Exclusive Cross-Entropy for ExtremelyMany Localisations; Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV), 2021, pp. 2813-2823

If you use our code, please cite:

@InProceedings{Panteli_2021_ICCV,
    author    = {Panteli, Andreas and Teuwen, Jonas and Horlings, Hugo and Gavves, Efstratios},
    title     = {Sparse-Shot Learning With Exclusive Cross-Entropy for Extremely Many Localisations},
    booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV)},
    month     = {October},
    year      = {2021},
    pages     = {2813-2823}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ece_loss-1.0.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

ece_loss-1.0.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file ece_loss-1.0.0.tar.gz.

File metadata

  • Download URL: ece_loss-1.0.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for ece_loss-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b0aa8885e61f081f15f9e7b55d8c132d7fb425b4d00f31866a4e9a9d42348247
MD5 241425963597f29ef3351ddf70018ab1
BLAKE2b-256 3fd31fe777a1fab36e10858709061089851ec3cdc5d920552d46dd210a882313

See more details on using hashes here.

File details

Details for the file ece_loss-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: ece_loss-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for ece_loss-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ff3270324490f2e4fa790e4df5aa473ba5d4b396944f2344e229d2a1d032936
MD5 f1401f1ba5615f5dc76773c9f6837ab7
BLAKE2b-256 a5b4231251a7ee9647d0d9b6b49687babff6399459ee5e3d5f8996f9446915cd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page