{"id":17983147,"url":"https://github.com/kaiyangzhou/pytorch-center-loss","last_synced_at":"2025-05-16T16:09:18.495Z","repository":{"id":39579035,"uuid":"126206423","full_name":"KaiyangZhou/pytorch-center-loss","owner":"KaiyangZhou","description":"Pytorch implementation of Center Loss","archived":false,"fork":false,"pushed_at":"2023-02-19T11:38:39.000Z","size":5805,"stargazers_count":988,"open_issues_count":22,"forks_count":219,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-08T16:46:37.952Z","etag":null,"topics":["center-loss","computer-vision","deep-learning","feature-learning","loss-functions","python","pytorch"],"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/KaiyangZhou.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}},"created_at":"2018-03-21T16:12:01.000Z","updated_at":"2025-05-05T03:03:33.000Z","dependencies_parsed_at":"2022-07-14T22:30:42.933Z","dependency_job_id":"fc65dbbc-c357-4565-82dc-87c20a4abe27","html_url":"https://github.com/KaiyangZhou/pytorch-center-loss","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KaiyangZhou%2Fpytorch-center-loss","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KaiyangZhou%2Fpytorch-center-loss/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KaiyangZhou%2Fpytorch-center-loss/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KaiyangZhou%2Fpytorch-center-loss/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KaiyangZhou","download_url":"https://codeload.github.com/KaiyangZhou/pytorch-center-loss/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254564127,"owners_count":22092122,"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":["center-loss","computer-vision","deep-learning","feature-learning","loss-functions","python","pytorch"],"created_at":"2024-10-29T18:16:16.310Z","updated_at":"2025-05-16T16:09:18.470Z","avatar_url":"https://github.com/KaiyangZhou.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pytorch-center-loss\nPytorch implementation of center loss: [Wen et al. A Discriminative Feature Learning Approach for Deep Face Recognition. ECCV 2016.](https://ydwen.github.io/papers/WenECCV16.pdf)\n\nThis loss function is also used by [deep-person-reid](https://github.com/KaiyangZhou/deep-person-reid).\n\n## Get started\nClone this repo and run the code\n```bash\n$ git clone https://github.com/KaiyangZhou/pytorch-center-loss\n$ cd pytorch-center-loss\n$ python main.py --eval-freq 1 --gpu 0 --save-dir log/ --plot\n```\nYou will see the following info in your terminal\n```bash\nCurrently using GPU: 0\nCreating dataset: mnist\nCreating model: cnn\n==\u003e Epoch 1/100\nBatch 50/469     Loss 2.332793 (2.557837) XentLoss 2.332744 (2.388296) CenterLoss 0.000048 (0.169540)\nBatch 100/469    Loss 2.354638 (2.463851) XentLoss 2.354637 (2.379078) CenterLoss 0.000001 (0.084773)\nBatch 150/469    Loss 2.361732 (2.434477) XentLoss 2.361732 (2.377962) CenterLoss 0.000000 (0.056515)\nBatch 200/469    Loss 2.336701 (2.417842) XentLoss 2.336700 (2.375455) CenterLoss 0.000001 (0.042386)\nBatch 250/469    Loss 2.404814 (2.407015) XentLoss 2.404813 (2.373106) CenterLoss 0.000001 (0.033909)\nBatch 300/469    Loss 2.338753 (2.398546) XentLoss 2.338752 (2.370288) CenterLoss 0.000001 (0.028258)\nBatch 350/469    Loss 2.367068 (2.390672) XentLoss 2.367059 (2.366450) CenterLoss 0.000009 (0.024221)\nBatch 400/469    Loss 2.344178 (2.384820) XentLoss 2.344142 (2.363620) CenterLoss 0.000036 (0.021199)\nBatch 450/469    Loss 2.329708 (2.379460) XentLoss 2.329661 (2.360611) CenterLoss 0.000047 (0.018848)\n==\u003e Test\nAccuracy (%): 10.32  Error rate (%): 89.68\n... ...\n==\u003e Epoch 30/100\nBatch 50/469     Loss 0.141117 (0.155986) XentLoss 0.084169 (0.091617) CenterLoss 0.056949 (0.064369)\nBatch 100/469    Loss 0.138201 (0.151291) XentLoss 0.089146 (0.092839) CenterLoss 0.049055 (0.058452)\nBatch 150/469    Loss 0.151055 (0.151985) XentLoss 0.090816 (0.092405) CenterLoss 0.060239 (0.059580)\nBatch 200/469    Loss 0.150803 (0.153333) XentLoss 0.092857 (0.092156) CenterLoss 0.057946 (0.061176)\nBatch 250/469    Loss 0.162954 (0.154971) XentLoss 0.094889 (0.092099) CenterLoss 0.068065 (0.062872)\nBatch 300/469    Loss 0.162895 (0.156038) XentLoss 0.093100 (0.092034) CenterLoss 0.069795 (0.064004)\nBatch 350/469    Loss 0.146187 (0.156491) XentLoss 0.082508 (0.091787) CenterLoss 0.063679 (0.064704)\nBatch 400/469    Loss 0.171533 (0.157390) XentLoss 0.092526 (0.091674) CenterLoss 0.079007 (0.065716)\nBatch 450/469    Loss 0.209196 (0.158371) XentLoss 0.098388 (0.091560) CenterLoss 0.110808 (0.066811)\n==\u003e Test\nAccuracy (%): 98.51  Error rate (%): 1.49\n... ...\n```\n\nPlease run `python main.py -h` for more details regarding input arguments.\n\n## Results\nWe visualize the feature learning process below.\n\nSoftmax only. Left: training set. Right: test set.\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"gifs/softmax_train.gif\" alt=\"train\" width=\"30%\"\u003e\n  \u003cimg src=\"gifs/softmax_test.gif\" alt=\"train\" width=\"30%\"\u003e\n\u003c/div\u003e\n\nSoftmax + center loss. Left: training set. Right: test set.\n\u003cdiv align=\"center\"\u003e\n  \u003cimg src=\"gifs/center_train.gif\" alt=\"train\" width=\"30%\"\u003e\n  \u003cimg src=\"gifs/center_test.gif\" alt=\"train\" width=\"30%\"\u003e\n\u003c/div\u003e\n\n## How to use center loss in your own project\n1. All you need is the `center_loss.py` file\n```python\nfrom center_loss import CenterLoss\n```\n2. Initialize center loss in the main function\n```python\ncenter_loss = CenterLoss(num_classes=10, feat_dim=2, use_gpu=True)\n```\n3. Construct an optimizer for center loss\n```python\noptimizer_centloss = torch.optim.SGD(center_loss.parameters(), lr=0.5)\n```\nAlternatively, you can merge optimizers of model and center loss, like\n```\nparams = list(model.parameters()) + list(center_loss.parameters())\noptimizer = torch.optim.SGD(params, lr=0.1) # here lr is the overall learning rate\n```\n\n4. Update class centers just like how you update a pytorch model\n```python\n# features (torch tensor): a 2D torch float tensor with shape (batch_size, feat_dim)\n# labels (torch long tensor): 1D torch long tensor with shape (batch_size)\n# alpha (float): weight for center loss\nloss = center_loss(features, labels) * alpha + other_loss\noptimizer_centloss.zero_grad()\nloss.backward()\n# multiple (1./alpha) in order to remove the effect of alpha on updating centers\nfor param in center_loss.parameters():\n    param.grad.data *= (1./alpha)\noptimizer_centloss.step()\n```\nIf you adopt the second way (i.e. use one optimizer for both model and center loss), the update code would look like\n```python\nloss = center_loss(features, labels) * alpha + other_loss\noptimizer.zero_grad()\nloss.backward()\nfor param in center_loss.parameters():\n    # lr_cent is learning rate for center loss, e.g. lr_cent = 0.5\n    param.grad.data *= (lr_cent / (alpha * lr))\noptimizer.step()\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiyangzhou%2Fpytorch-center-loss","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaiyangzhou%2Fpytorch-center-loss","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiyangzhou%2Fpytorch-center-loss/lists"}