{"id":13738167,"url":"https://github.com/AdeelH/pytorch-multi-class-focal-loss","last_synced_at":"2025-05-08T16:32:31.138Z","repository":{"id":37037198,"uuid":"292520399","full_name":"AdeelH/pytorch-multi-class-focal-loss","owner":"AdeelH","description":"An (unofficial) implementation of Focal Loss, as described in the RetinaNet paper, generalized to the multi-class case.","archived":false,"fork":false,"pushed_at":"2024-01-22T19:03:41.000Z","size":28,"stargazers_count":235,"open_issues_count":0,"forks_count":26,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T03:58:16.879Z","etag":null,"topics":["classification","deep-learning","imbalanced-classes","implementation-of-research-paper","loss-functions","machine-learning","multiclass-classification","neural-network","pytorch","pytorch-implementation","retinanet"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AdeelH.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,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-09-03T09:08:36.000Z","updated_at":"2025-04-09T02:38:15.000Z","dependencies_parsed_at":"2024-01-26T11:04:07.474Z","dependency_job_id":"e514b014-54fb-4390-8831-a71877ec6102","html_url":"https://github.com/AdeelH/pytorch-multi-class-focal-loss","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdeelH%2Fpytorch-multi-class-focal-loss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdeelH%2Fpytorch-multi-class-focal-loss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdeelH%2Fpytorch-multi-class-focal-loss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AdeelH%2Fpytorch-multi-class-focal-loss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AdeelH","download_url":"https://codeload.github.com/AdeelH/pytorch-multi-class-focal-loss/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253105328,"owners_count":21855005,"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","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":["classification","deep-learning","imbalanced-classes","implementation-of-research-paper","loss-functions","machine-learning","multiclass-classification","neural-network","pytorch","pytorch-implementation","retinanet"],"created_at":"2024-08-03T03:02:13.029Z","updated_at":"2025-05-08T16:32:30.848Z","avatar_url":"https://github.com/AdeelH.png","language":"Python","funding_links":[],"categories":["Python","Classification"],"sub_categories":[],"readme":"[![DOI](https://zenodo.org/badge/292520399.svg)](https://zenodo.org/badge/latestdoi/292520399)\n\n# Multi-class Focal Loss\n\nAn (unofficial) implementation of Focal Loss, as described in the RetinaNet paper, https://arxiv.org/abs/1708.02002, generalized to the multi-class case.\n\nIt is essentially an enhancement to cross-entropy loss and is useful for classification tasks when there is a large class imbalance. It has the effect of underweighting easy examples.\n\n# Usage\n- `FocalLoss` is an `nn.Module` and behaves very much like `nn.CrossEntropyLoss()` i.e.\n    - supports the `reduction` and `ignore_index` params, and\n    - is able to work with 2D inputs of shape `(N, C)` as well as K-dimensional inputs of shape `(N, C, d1, d2, ..., dK)`.\n\n- Example usage\n    ```python3\n    focal_loss = FocalLoss(alpha, gamma)\n\t...\n\tinp, targets = batch\n    out = model(inp)\n\tloss = focal_loss(out, targets)\n    ```\n\n# Loading through torch.hub\nThis repo supports importing modules through `torch.hub`. `FocalLoss` can be easily imported into your code via, for example:\n```python3\nfocal_loss = torch.hub.load(\n\t'adeelh/pytorch-multi-class-focal-loss',\n\tmodel='FocalLoss',\n\talpha=torch.tensor([.75, .25]),\n\tgamma=2,\n\treduction='mean',\n\tforce_reload=False\n)\nx, y = torch.randn(10, 2), (torch.rand(10) \u003e .5).long()\nloss = focal_loss(x, y)\n```\nOr:\n```python3\nfocal_loss = torch.hub.load(\n\t'adeelh/pytorch-multi-class-focal-loss',\n\tmodel='focal_loss',\n\talpha=[.75, .25],\n\tgamma=2,\n\treduction='mean',\n\tdevice='cpu',\n\tdtype=torch.float32,\n\tforce_reload=False\n)\nx, y = torch.randn(10, 2), (torch.rand(10) \u003e .5).long()\nloss = focal_loss(x, y)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAdeelH%2Fpytorch-multi-class-focal-loss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAdeelH%2Fpytorch-multi-class-focal-loss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAdeelH%2Fpytorch-multi-class-focal-loss/lists"}