InsightFace及其mxnet、tensorflow代码实现
arcface论文:https://arxiv.org/pdf/1801.07698.pdf
说明文档:https://www.cnblogs.com/darkknightzh/p/8525287.html
tensorflow代码实现
参考流程图:
import tensorflow as tf
import math#未考虑margin_b的情况,基本与下一个函数类似,可优先采用combine_loss_val,合理设置margin_a, #margin_m, margin_b, s四个参数即可def arcface_loss(embedding, labels, w_init, out_num, s=64., m=0.5):''':param embedding: the input embedding vectors:param labels: the input labels, the shape should be eg: (batch_size, 1):param s: scalar value default is 64:param out_num: output class num:param m: the margin value, default is 0.5:return: the final cacualted output, this output is send into the tf.nn.softmax directly'''cos_m = math.cos(m)sin_m = math.sin(m)with tf.variable_scope('arcface_loss'):# inputs and weights normembedding_norm = tf.norm(embedding, axis=1, keep_dims=True)embedding = tf.div(embedding, embedding_norm, name='norm_embedding')weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),initializer=w_init, dtype=tf.float32)weights_norm = tf.norm(weights, axis=0, keep_dims=True)weights_unit = tf.div(weights, weights_norm, name='norm_weights')# cos(theta+m)cos_t = tf.matmul(embedding, weights_unit, name='cos_t')cos_t2 = tf.square(cos_t, name='cos_2')sin_t2 = tf.subtract(1., cos_t2, name='sin_2')sin_t = tf.sqrt(sin_t2, name='sin_t')cos_mt = s * tf.subtract(tf.multiply(cos_t, cos_m), tf.multiply(sin_t, sin_m), name='cos_mt')mask = tf.one_hot(labels, depth=out_num, name='one_hot_mask')# mask = tf.squeeze(mask, 1)inv_mask = tf.subtract(1., mask, name='inverse_mask')s_cos_t = tf.multiply(s, cos_t, name='scalar_cos_t')updated_logits = tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(cos_mt, mask), name='arcface_loss_output')loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=updated_logits))return loss, weightsdef combine_loss_val(embedding, labels, w_init, out_num, margin_a, margin_m, margin_b, s):'''This code is contributed by RogerLo. Thanks for you contribution.:param embedding: the input embedding vectors:param labels: the input labels, the shape should be eg: (batch_size, 1):param s: scalar value default is 64:param out_num: output class num:param m: the margin value, default is 0.5:return: the final cacualted output, this output is send into the tf.nn.softmax directly'''weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),initializer=w_init, dtype=tf.float32)weights_unit = tf.nn.l2_normalize(weights, axis=0)embedding_unit = tf.nn.l2_normalize(embedding, axis=1) * scos_t = tf.matmul(embedding_unit, weights_unit)ordinal = tf.constant(list(range(0, embedding.get_shape().as_list()[0])), tf.int64)ordinal_y = tf.stack([ordinal, labels], axis=1)sel_cos_t = tf.gather_nd(cos_t, ordinal_y)if margin_a != 1.0 or margin_m != 0.0 or margin_b != 0.0:if margin_a == 1.0 and margin_m == 0.0:s_m = s * margin_bnew_zy = sel_cos_t - s_melse:cos_value = sel_cos_t / st = tf.acos(cos_value)if margin_a != 1.0:t = t * margin_aif margin_m > 0.0:t = t + margin_mbody = tf.cos(t)if margin_b > 0.0:body = body - margin_bnew_zy = body * supdated_logits = tf.add(cos_t, tf.scatter_nd(ordinal_y, tf.subtract(new_zy, sel_cos_t), cos_t.get_shape()))loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=updated_logits))return loss, weights
InsightFace及其mxnet、tensorflow代码实现相关推荐
- 如何高效的学习TensorFlow代码?
如何高效的学习TensorFlow代码? 如题,或者如何掌握TensorFlow,应用到任何领域? 添加评论分享 10 个回答 爱琳李,老李,明天就辍学了 8 人赞同 本来都忘了这个问题了,不过看到很 ...
- GitHub上共享的简单易用 TensorFlow 代码集
最近来自韩国的AI研究科学家Junho Kim做了一份易于使用的 TensorFlow 代码集,目前该项目包含一般深度学习架构所需要的代码,例如初始化和正则化.各种卷积运算.基本网络架构与模块.损失函 ...
- tensorflow 代码调试工具tfdbg的用法
tensorflow 代码调试工具tfdbg的用法 在windows10下安装 使用tfdbg前确定安装好了tensorflow和pyrendline 终端输入 pip install pyreadl ...
- tensorflow 代码阅读
具体实现: https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/framework 『深度长文』Tensorflo ...
- 从Tensorflow代码中理解LSTM网络
目录 RNN LSTM 参考文档与引子 缩略词 RNN (Recurrent neural network) 循环神经网络 LSTM (Long short-term memory) 长短期记忆人 ...
- 生成对抗网络简介(包含TensorFlow代码示例)【翻译】
判别模型 vs. 生成模型 示例:近似一维高斯分布 提高样本多样性 最后的思考 关于GAN的一些讨论 最近,大家对生成模型的兴趣又开始出现(OpenAI关于生成模型的案例).生成模型可以学习如何生成数 ...
- 深度学习框架Caffe, MXNet, TensorFlow, Torch, CNTK性能测试报告
香港浸会大学对于深度学习框架Caffe, MXNet, TensorFlow, Torch, CNTK性能测试报告 http://dlbench.comp.hkbu.edu.hk/
- Resnet论文解读与TensorFlow代码分析
残差网络Resnet论文解读 1.论文解读 博客地址:https://blog.csdn.net/loveliuzz/article/details/79117397 2.理解ResNet结构与Ten ...
- DL | TensorFlow代码调试
简介 在机器之心公众号上看到一篇博文(原文地址),针对TensorFlow代码调试问题,是翻译的英文,虽然语句非常的长,但是感觉还不错.这里留作备份,吸取经验. 原文 转自:机器之心 到底是选 Ten ...
最新文章
- iOS 自定义双向滑块Slider
- matlab vector用法,C++ vector 用法汇总
- QString::QString 中文乱码
- 程序 峰谷值 提取_医学影像组学特征值(Radiomics Features)提取之Pyradiomics(一)理论篇...
- 提升PHP性能的21种方法
- [置顶] NoSQl mongodb数据库 配置篇
- ubuntu19.10安装deepin的微信和qq(转载+自己在新系统上验证)
- [css] 移动端微信页面有哪些兼容性问题及解决方案是什么?
- Kubernetes-保障集群内节点和网络安全
- sort numbers with three stacks
- 导出csv文件,导出axlsx文件。gem 'Axlsx-Rails' (470);导入csv文件。
- Spss的基本方法使用步骤
- 佳能相机G7 Mark Ⅱ (测光与对焦)
- MongoTemplate根据时间查询的大坑
- python中使用splash如何挂代理?
- sqlserver分组统计最新一条数据
- 15分钟正则表达式快速上手(js)
- 统计字符串英文字母个数
- 【Mybatis】一二级缓存的源码研究
- python实现文档图像倾斜矫正,实现类似扫描仪功能
热门文章
- openssh升级sftp_CentOS6.5升级OpenSSH 8.3版本
- luogu P2512 [HAOI2008]糖果传递
- 【Spring 基础篇三】属性注入与属性编辑器
- 行为驱动开发BDD概要
- 宣布 Azure Backup 支持备份 Windows Server 2008
- UML轻松入门--类和对象
- Git入门之上传本地项目至Github(一)
- HarmonyOS之深入解析图像的编码和解码
- HarmonyOS之基础环境和应用开发流程
- POJ 2965.The Pilots Brothers‘ refrigerator