{"id":13577661,"url":"https://github.com/dlmacedo/distinction-maximization-loss","last_synced_at":"2025-10-10T15:18:08.333Z","repository":{"id":43510114,"uuid":"490491564","full_name":"dlmacedo/distinction-maximization-loss","owner":"dlmacedo","description":"A project to improve out-of-distribution detection (open set recognition) and uncertainty estimation by changing a few lines of code in your project! Perform efficient inferences (i.e., do not increase inference time) without repetitive model training, hyperparameter tuning, or collecting additional data.","archived":false,"fork":false,"pushed_at":"2022-09-22T13:04:58.000Z","size":2570,"stargazers_count":45,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T12:33:17.340Z","etag":null,"topics":["ai-safety","anomaly-detection","classification","deep-learning","machine-learning","novelty-detection","ood","ood-detection","open-set","open-set-recognition","osr","out-of-distribution","out-of-distribution-detection","pytorch","robust-machine-learning","trustworthy-ai","trustworthy-machine-learning","uncertainty-estimation"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dlmacedo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-10T00:47:48.000Z","updated_at":"2024-02-05T13:45:04.000Z","dependencies_parsed_at":"2023-01-18T14:45:11.527Z","dependency_job_id":null,"html_url":"https://github.com/dlmacedo/distinction-maximization-loss","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dlmacedo/distinction-maximization-loss","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlmacedo%2Fdistinction-maximization-loss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlmacedo%2Fdistinction-maximization-loss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlmacedo%2Fdistinction-maximization-loss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlmacedo%2Fdistinction-maximization-loss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dlmacedo","download_url":"https://codeload.github.com/dlmacedo/distinction-maximization-loss/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dlmacedo%2Fdistinction-maximization-loss/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279004581,"owners_count":26083735,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai-safety","anomaly-detection","classification","deep-learning","machine-learning","novelty-detection","ood","ood-detection","open-set","open-set-recognition","osr","out-of-distribution","out-of-distribution-detection","pytorch","robust-machine-learning","trustworthy-ai","trustworthy-machine-learning","uncertainty-estimation"],"created_at":"2024-08-01T15:01:23.364Z","updated_at":"2025-10-10T15:18:08.314Z","avatar_url":"https://github.com/dlmacedo.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# **Visit \"The Robust Deep Learning Library\" (our newest work) to quickly use this loss and much more:**\n\n# **[The Robust Deep Learning Library](https://github.com/dlmacedo/robust-deep-learning)**\n\n---\n\n\u003cimg align=\"center\" src=\"assets/dismax.png\" width=\"750\"\u003e\n\n# Distinction Maximization Loss (DisMax)\n\n## Efficiently Improving Out-of-Distribution Detection and Uncertainty Estimation by Replacing the Loss and Calibrating\n\n\u003e\u003e**We keep single network inference efficiency. No hyperparameter tuning. We need to train only once. SOTA.**\n\n\u003e\u003e**Read the full paper: [Distinction Maximization Loss: Efficiently Improving Out-of-Distribution Detection and Uncertainty Estimation by Replacing the Loss and Calibrating](https://arxiv.org/abs/2205.05874).**\n\n\u003e\u003e ## **Train on CIFAR10, CIFAR100, and ImageNet.**\n\n## Results\n\n### Dataset=ImageNet, Model=ResNet18, Near OOD=ImageNet-O \n\n| Loss [Score] | Class (ACC) | Near OOD (AUROC) |\n|:---|:---:|:---:|\n| Cross-Entropy [MPS] | 69.9 | 52.4 |\n| DisMax [MMLES] | 69.6 | 75.8 |\n\n### Dataset=CIFAR\n\n\u003cimg align=\"center\" src=\"assets/table.png\" width=\"750\"\u003e\n\n# Use DisMax in your project!!!\n\n## Replace the SoftMax loss with the DisMax loss changing few lines of code!\n\n### Replace the model classifier last layer with the DisMax loss first part:\n\n```python\nclass Model(nn.Module):\n    def __init__(self):\n    (...)\n    #self.classifier = nn.Linear(num_features, num_classes)\n    self.classifier = losses.DisMaxLossFirstPart(num_features, num_classes)\n```\n\n### Replace the criterion by the DisMax loss second part:\n\n```python\nmodel = Model()\n#criterion = nn.CrossEntropyLoss()\ncriterion = losses.DisMaxLossSecondPart(model.classifier)\n```\n\n### Preprocess before forwarding in the training loop:\n\n```python\n# In the training loop, add the line of code below for preprocessing before forwarding.\ninputs, targets = criterion.preprocess(inputs, targets) \n(...)\n# The code below is preexistent. Just keep the following lines unchanged!\noutputs = model(inputs)\nloss = criterion(outputs, targets)\n```\n\n## Detect during inference:\n\n```python\n# Return the score values during inference.\nscores = model.classifier.scores(outputs) \n```\n\n## Run the example:\n\n```\npython example.py\n```\n\n# Code\n\n## Software requirements\n\nMuch code reused from [deep_Mahalanobis_detector](https://github.com/pokaxpoka/deep_Mahalanobis_detector), [odin-pytorch](https://github.com/facebookresearch/odin), and [entropic-out-of-distribution-detection](https://github.com/dlmacedo/entropic-out-of-distribution-detection).\n\n### Please, install all package requirments runing the command bellow:\n\n```bash\npip install -r requirements.txt\n```\n\n## Preparing the data\n\n### Please, move to the `data` directory and run all the prepare data bash scripts:\n\n```bash\n# Download and prepare out-of-distrbution data for CIFAR10 and CIFAR100 datasets.\n./prepare_cifar.sh\n# Download and prepare out-of-distrbution data for ImageNet.\n./prepare_imagenet.sh\n```\n\n## Reproducing the experiments\n\n### Train and evaluate the classification, uncertainty estimation, and out-of-distribution detection performances:\n\n```bash\n./run_cifar100_densenetbc100.sh*\n./run_cifar100_resnet34.sh*\n./run_cifar100_wideresnet2810.sh*\n./run_cifar10_densenetbc100.sh*\n./run_cifar10_resnet34.sh*\n./run_cifar10_wideresnet2810.sh*\n./run_imagenet1k_resnet18.sh*\n```\n\n## Analyzing the results\n\n### Print the experiment results:\n\n```bash\n./analyze.sh\n```\n\n# Citation\n\nPlease, cite our papers if you use our loss in your works:\n\n```bibtex\n@article{DBLP:journals/corr/abs-2205-05874,\n  author    = {David Mac{\\^{e}}do and\n               Cleber Zanchettin and\n               Teresa Bernarda Ludermir},\n  title     = {Distinction Maximization Loss:\n  Efficiently Improving Out-of-Distribution Detection and Uncertainty Estimation\n  Simply Replacing the Loss and Calibrating},\n  journal   = {CoRR},\n  volume    = {abs/2205.05874},\n  year      = {2022},\n  url       = {https://doi.org/10.48550/arXiv.2205.05874},\n  doi       = {10.48550/arXiv.2205.05874},\n  eprinttype = {arXiv},\n  eprint    = {2205.05874},\n  timestamp = {Tue, 17 May 2022 17:31:03 +0200},\n  biburl    = {https://dblp.org/rec/journals/corr/abs-2205-05874.bib},\n  bibsource = {dblp computer science bibliography, https://dblp.org}\n}\n```\n\n```bibtex\n@article{DBLP:journals/corr/abs-2208-03566,\n  author    = {David Mac{\\^{e}}do},\n  title     = {Towards Robust Deep Learning using Entropic Losses},\n  journal   = {CoRR},\n  volume    = {abs/2208.03566},\n  year      = {2022},\n  url       = {https://doi.org/10.48550/arXiv.2208.03566},\n  doi       = {10.48550/arXiv.2208.03566},\n  eprinttype = {arXiv},\n  eprint    = {2208.03566},\n  timestamp = {Wed, 10 Aug 2022 14:49:54 +0200},\n  biburl    = {https://dblp.org/rec/journals/corr/abs-2208-03566.bib},\n  bibsource = {dblp computer science bibliography, https://dblp.org}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlmacedo%2Fdistinction-maximization-loss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdlmacedo%2Fdistinction-maximization-loss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdlmacedo%2Fdistinction-maximization-loss/lists"}