Skip to main content

Package for computing the mean squared error between `y_true` and `y_pred` objects with prior assignment using the Hungarian algorithm.

Project description

Hungarian Loss

hungarian-loss Logo

Continuous Integration Status Code Coverage Percentage Codacy Badge Code style: black Python Badge Tensorflow Badge Project License

When you train a computer vision model detecting multiple objects within an image, you need to define a strategy for computing the loss between ground y_true truth and predicted y_pred sets of bounding boxes. This strategy needs to provide consistent matching between these two sets. The function implemented in this project uses a Hungarian algorithm to determine the optimal assignments between these two sets of bounding boxes and uses it for computing the loss.

Installing

Install and update using pip:

~$ pip install hungarian-loss

Note, this package does not have extra dependencies except Tensorflow :tada:.

How to use it

The following example shows how to compute loss for the model head predicting bounding boxes.

from hungarian_loss import hungarian_loss

model = ...

losses = {"...": ..., "bbox": hungarian_loss}
lossWeights = {"...": ..., "bbox": 1}

model.compile(optimizer='adam', loss=losses, loss_weights=lossWeights)

Where to use it

Let's assume you are working on a deep learning model detecting multiple objects on an image. For simplicity of this example, let's consider, that our model intends to detect just two objects of kittens (see example below).

Use-case Example

Our model predicts 2 bounding boxes where it "thinks" kittens are located. We need to compute the difference between true and predicted bounding boxes to update model weights via back-propagation. But how to know which predicted boxes belong to which true boxes? Without the optimal assignment algorithm which consistently assigns the predicted boxes to the true boxes, we will not be able to successfully train our model.

The loss function implemented in this project can help you. Intuitively you can see that predicted BBox 1 is close to the true BBox 1 and likewise predicted BBox 2 is close to the true BBox 2. the cost of assigning these pairs would be minimal compared to any other combinations. As you can see, this is a classical assignment problem. You can solve this problem using the Hungarian Algorithm. Its Python implementation can be found here. It is also used by DERT Facebook End-to-End Object Detection with Transformers model. However, if you wish to use pure Tensor-based implementation this library is for you.

How it works

To give you more insights into this implementation we will review a hypothetical example. Let define true-bounding boxes for objects T1 and T2:

Object Bounding boxes
T1 1., 2., 3., 4.
T2 5., 6., 7., 8.

Do the same for the predicted boxes P1 and P2:

Object Bounding boxes
P1 1., 1., 1., 1.
P2 2., 2., 2., 2.

et's compute the Euclidean distances between all combinations of True and Predicted bounding boxes:

P1 P2
T1 3.7416575 2.449489
T2 11.224972 9.273619

This algorithm will compute the assignment mask first:

P1 P2
T1 1 0
T2 0 1

And then compute the final error:

loss = (3.7416575 + 9.273619) / 2 = 6.50763825

In contrast, if we would use the different assignment:

loss = (2.449489 + 11.224972) / 2 = 6.8372305

As you can see the error for the optimal assignment is smaller compared to the other solution(s).

Contributing

For information on how to set up a development environment and how to make a contribution to Hungarian Loss, see the contributing guidelines.

Links

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

hungarian-loss-1.0.1.tar.gz (105.9 kB view details)

Uploaded Source

Built Distribution

hungarian_loss-1.0.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file hungarian-loss-1.0.1.tar.gz.

File metadata

  • Download URL: hungarian-loss-1.0.1.tar.gz
  • Upload date:
  • Size: 105.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for hungarian-loss-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bac2881d1dd49877a989606cbf191deb864551c5d3997faa1de77b08a327c834
MD5 13259916101a78cbfa8c9865f7c55c37
BLAKE2b-256 180855d64a37d6a33be1c679be99da2db27e89c069e29a062e73fefa21e7fc6d

See more details on using hashes here.

File details

Details for the file hungarian_loss-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for hungarian_loss-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b52c45d2a08dfb5ee91be8aa9ca942f943b1ad111e17868f8c64ba5a51cb5858
MD5 5f389c158875a9c8aae988787b59f76c
BLAKE2b-256 698fc66746856441fd0d5dc8faf84c10675658eae2b186ee09a92b3f02b767dc

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