{"id":13560793,"url":"https://github.com/HiKapok/tf.extra_losses","last_synced_at":"2025-04-03T16:31:07.266Z","repository":{"id":151549014,"uuid":"130790519","full_name":"HiKapok/tf.extra_losses","owner":"HiKapok","description":"Large-Margin Softmax Loss, Angular Softmax Loss, Additive Margin Softmax, ArcFaceLoss And FocalLoss In Tensorflow","archived":false,"fork":false,"pushed_at":"2019-03-25T02:21:15.000Z","size":23,"stargazers_count":106,"open_issues_count":3,"forks_count":25,"subscribers_count":11,"default_branch":"master","last_synced_at":"2024-11-04T12:40:00.163Z","etag":null,"topics":["angular-softmax","arcface","focalloss","large-margin-softmax","sphereface","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"C++","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/HiKapok.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":"2018-04-24T03:23:51.000Z","updated_at":"2024-11-03T17:39:56.000Z","dependencies_parsed_at":null,"dependency_job_id":"7a5d2eb5-b817-474f-958a-cb967fe3b78c","html_url":"https://github.com/HiKapok/tf.extra_losses","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/HiKapok%2Ftf.extra_losses","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HiKapok%2Ftf.extra_losses/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HiKapok%2Ftf.extra_losses/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HiKapok%2Ftf.extra_losses/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HiKapok","download_url":"https://codeload.github.com/HiKapok/tf.extra_losses/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247036956,"owners_count":20873059,"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":["angular-softmax","arcface","focalloss","large-margin-softmax","sphereface","tensorflow"],"created_at":"2024-08-01T13:00:49.559Z","updated_at":"2025-04-03T16:31:06.963Z","avatar_url":"https://github.com/HiKapok.png","language":"C++","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# Large-Margin Softmax Loss, Angular Softmax Loss, Additive Margin Softmax, ArcFaceLoss And FocalLoss In Tensorflow\n\nThis repository contains core codes of the reimplementation of the following papers in TensorFlow:\n\n- [Large-Margin Softmax Loss for Convolutional Neural Networks](https://arxiv.org/abs/1612.02295)\n- [SphereFace: Deep Hypersphere Embedding for Face Recognition](https://arxiv.org/abs/1704.08063)\n- [Additive Margin Softmax for Face Verification](https://arxiv.org/abs/1801.05599) or [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/abs/1801.09414)\n- [ArcFace: Additive Angular Margin Loss for Deep Face Recognition](https://arxiv.org/abs/1801.07698)\n- [Focal Loss for Dense Object Detection](https://arxiv.org/abs/1708.02002)\n\nIf your goal is to reproduce the results in the original paper, please use the official codes:\n\n- [Large Margin Softmax Loss in ICML 2016](https://github.com/wy1iu/LargeMargin_Softmax_Loss)\n- [Angular Softmax Loss in CVPR 2017](https://github.com/wy1iu/sphereface)\n- [Additive Margin Softmax](https://github.com/happynear/AMSoftmax)\n- [ArcFace: Additive Angular Margin Loss](https://github.com/deepinsight/insightface)\n- [Focal Loss in ICCV 2017](https://github.com/facebookresearch/Detectron)\n\n## ##\n\nFor using these Ops on your own machine:\n\n- copy the header file \"cuda\\_config.h\" from \"your\\_python\\_path/site-packages/external/local\\_config\\_cuda/cuda/cuda/cuda\\_config.h\" to \"your\\_python\\_path/site-packages/tensorflow/include/tensorflow/stream\\_executor/cuda/cuda\\_config.h\".\n\n- run the following script:\n\n```sh\nmkdir build\ncd build \u0026\u0026 cmake ..\nmake\n```\n\n- run \"test\\_op.py\" and check the numeric errors to test your install\n- follow the below codes snippet to integrate this Op into your own code:\n\t- For Large Margin Softmax Loss:\n\n\t```python\n\top_module = tf.load_op_library(so_lib_path)\n\tlarge_margin_softmax = op_module.large_margin_softmax\n\n\t@ops.RegisterGradient(\"LargeMarginSoftmax\")\n\tdef _large_margin_softmax_grad(op, grad, _):\n\t  '''The gradients for `LargeMarginSoftmax`.\n\t  '''\n\t  inputs_features = op.inputs[0]\n\t  inputs_weights = op.inputs[1]\n\t  inputs_labels = op.inputs[2]\n\t  cur_lambda = op.outputs[1]\n\t  margin_order = op.get_attr('margin_order')\n\n\t  grads = op_module.large_margin_softmax_grad(inputs_features, inputs_weights, inputs_labels, grad, cur_lambda[0], margin_order)\n\t  return [grads[0], grads[1], None, None]\n\n\tvar_weights = tf.Variable(initial_value, trainable=True, name='lsoftmax_weights')\n\tresult = large_margin_softmax(features, var_weights, labels, global_step, 4, 1000., 0.000025, 35., 0.)\n\tloss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=result[0]))\n\t```\n\n\t- For Angular Softmax Loss:\n\n\t```python\n\top_module = tf.load_op_library(so_lib_path)\n\tangular_softmax = op_module.angular_softmax\n\n\t@ops.RegisterGradient(\"AngularSoftmax\")\n\tdef _angular_softmax_grad(op, grad, _):\n\t  '''The gradients for `AngularSoftmax`.\n\t  '''\n\t  inputs_features = op.inputs[0]\n\t  inputs_weights = op.inputs[1]\n\t  inputs_labels = op.inputs[2]\n\t  cur_lambda = op.outputs[1]\n\t  margin_order = op.get_attr('margin_order')\n\n\t  grads = op_module.angular_softmax_grad(inputs_features, inputs_weights, inputs_labels, grad, cur_lambda[0], margin_order)\n\t  return [grads[0], grads[1], None, None]\n\n\tvar_weights = tf.Variable(initial_value, trainable=True, name='asoftmax_weights')\n\tnormed_var_weights = tf.nn.l2_normalize(var_weights, 1, 1e-10, name='weights_normed')\n\tresult = angular_softmax(features, normed_var_weights, labels, global_step, 4, 1000., 0.000025, 35., 0.)\n\tloss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=result[0]))\n\t```\n\t- For others just refer to this [script](https://github.com/HiKapok/tf.extra_losses/blob/master/py_loss.py).\n\nAll the codes was tested under TensorFlow 1.6, Python 3.5, Ubuntu 16.04 with CUDA 8.0. The outputs of these Ops in C++ had been compared with the original caffe codes' outputs, and the bias could be ignored. The gradients of this Op had been checked using [tf.test.compute\\_gradient\\_error](https://www.tensorflow.org/api_docs/python/tf/test/compute_gradient_error) and [tf.test.compute\\_gradient](https://www.tensorflow.org/api_docs/python/tf/test/compute_gradient). While the others are implemented following the official implementation in Python Ops.\n\nIf you encountered some linkage problem when generating or loading *.so, you are highly recommended to read this section in the [official tourial](https://www.tensorflow.org/extend/adding_an_op#compile_the_op_using_your_system_compiler_tensorflow_binary_installation) to make sure you were using the same C++ ABI version.\n\nAny contributions to this repo is welcomed.\n\n## ##\nMIT License","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHiKapok%2Ftf.extra_losses","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHiKapok%2Ftf.extra_losses","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHiKapok%2Ftf.extra_losses/lists"}