This repository contains a PyTorch implementation of the Hungarian loss function, utilizing the SciPy library for solving the assignment problem. The Hungarian method, also known as the Kuhn-Munkres algorithm, is an efficient way to solve assignment problems and is particularly useful in object detection tasks where predictions need to be matched with ground truth data optimally.
- Simple Bounding Box Predictor: A neural network module that predicts bounding boxes given input features.
- Hungarian Loss Function: A permutation invariant loss function that computes the match between predicted bounding boxes and target bounding boxes using the Hungarian method, minimizing the L1 loss between matched pairs.
- Efficient Matching: Utilizes SciPy's
linear_sum_assignment
function for optimal matching between predicted and target bounding boxes. - Dataset and DataLoader: A simple dataset setup using PyTorch's
TensorDataset
andDataLoader
for easy batching and shuffling.
- Python 3.6+
- PyTorch 1.7+
- SciPy 1.5+
To use this implementation, ensure you have Python installed on your system. Then, install the required libraries using pip:
pip install torch scipy
The main components of this implementation include the SimpleBoundingBoxPredictor
class, the hungarian_loss
function, and a training loop example. To use these components:
-
Define your dataset: Replace the synthetic dataset (
x_data
andy_data
) with your dataset of features and bounding boxes. -
Initialize the model and optimizer:
model = SimpleBoundingBoxPredictor()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
- Train your model: Run the provided training loop, which automatically computes the Hungarian loss and updates the model parameters.
During training, the script prints the epoch number and the current training loss, calculated using Welford's algorithm for a running average.
epoch=0, train_loss=2.345
epoch=1, train_loss=1.987
...
This project is made available under the "MIT No Attribution" (MIT-0) License. You are free to use, modify, and distribute this software without attribution.
This implementation is provided as-is, without warranty. The author(s) are not responsible for any damage, data loss, or unexpected behavior resulting from the use of this software.
Feel free to contribute to the project or report any issues you encounter. Your feedback is highly appreciated!