转载请注明出处:

http://www.cnblogs.com/darkknightzh/p/8525287.html

论文

InsightFace : Additive Angular Margin Loss for Deep Face Recognition

https://arxiv.org/abs/1801.07698

官方mxnet代码:

https://github.com/deepinsight/insightface

说明:没用过mxnet,下面的代码注释只是纯粹从代码的角度来分析并进行注释,如有错误之处,敬请谅解,并欢迎指出。

先查看sphereface,查看$\psi (\theta )$的介绍:http://www.cnblogs.com/darkknightzh/p/8524937.html

论文arcface中,定义$\psi (\theta )$为:

$\psi (\theta )=\cos ({{\theta }_{yi}}+m)$

同时对w及x均进行了归一化,为了使得训练能收敛,增加了一个参数s=64,最终loss如下:

$L=-\frac{1}{m}\sum\limits_{i=1}^{m}{\log \frac{{{e}^{s(\cos ({{\theta }_{yi}}+m))}}}{{{e}^{s(\cos ({{\theta }_{yi}}+m))}}+\sum\nolimits_{j=n,j\ne yi}^{n}{{{e}^{s\cos {{\theta }_{j}}}}}}}$

其中,

${{W}_{j}}=\frac{{{W}_{j}}}{\left\| {{W}_{j}} \right\|}$,${{x}_{i}}=\frac{{{x}_{i}}}{\left\| {{x}_{i}} \right\|}$,$\cos {{\theta }_{j}}=W_{j}^{T}{{x}_{i}}$

程序中先对w及x归一化,然后通过全连接层得到cosθ,再扩大s倍,得到scosθ。

对于yi处,由于

$\cos (\theta +m)=\cos \theta \cos m-\sin \theta \sin m$

以及

$\sin \theta =\sqrt{1-{{\cos }^{2}}\theta }$

得到sinθ。

由于$\cos (\theta +m)$非单调,设置了easy_margin标志,当其为真时,使用0作为阈值,当特征和权重的cos值小于0,直接截断;当其为假时,使用cos(pi-m)=-cos(m)作为阈值。该阈值小于0。

之后判断时,当easy_margin为真时,若s*cos(θ+m)小于0,直接使用s*cos(θ);当easy_margin为假时,若s*cos(θ+m)小于0,使用s*cos(θ)-s*m*sin(m)。

具体的代码如下(完整代码见参考网址):

     s = args.margin_s  # 参数s
m = args.margin_m # 参数m _weight = mx.symbol.Variable("fc7_weight", shape=(args.num_classes, args.emb_size), lr_mult=1.0) # (C,F)
_weight = mx.symbol.L2Normalization(_weight, mode='instance') # 对w进行归一化
nembedding = mx.symbol.L2Normalization(embedding, mode='instance', name='fc1n')*s # 对x进行归一化,并得到s*x,(B,F)
fc7 = mx.sym.FullyConnected(data=nembedding, weight = _weight, no_bias = True, num_hidden=args.num_classes, name='fc7') # Y=XW'+b,(B,F)*(C,F)'=(B,C),'为转置,此处得到scos(theta) zy = mx.sym.pick(fc7, gt_label, axis=1) # 得到fc7中gt_label位置的值。(B,1)或者(B),即当前batch中yi处的scos(theta)
cos_t = zy/s # 由于fc7及zy均为cos的s倍,此处除以s,得到实际的cos值。(B,1)或者(B) cos_m = math.cos(m)
sin_m = math.sin(m)
mm = math.sin(math.pi-m)*m # sin(pi-m)*m = sin(m)*m
threshold = math.cos(math.pi-m) # 阈值,避免theta + m >= pi,实际上threshold < 0
if args.easy_margin:
cond = mx.symbol.Activation(data=cos_t, act_type='relu') #easy_margin=True,直接使用0作为阈值,得到超过阈值的索引
else:
cond_v = cos_t - threshold #easy_margin=False,使用threshold(负数)作为阈值。
cond = mx.symbol.Activation(data=cond_v, act_type='relu') # 得到超过阈值的索引
body = cos_t*cos_t # 通过cos*cos + sin * sin = 1, 来得到sin_theta
body = 1.0-body
sin_t = mx.sym.sqrt(body) # sin_theta
new_zy = cos_t*cos_m # cos(theta+m)=cos(theta)*cos(m)-sin(theta)*sin(m),此处为cos(theta)*cos(m)
b = sin_t*sin_m # 此处为sin(theta)*sin(m)
new_zy = new_zy - b # 此处为cos(theta)*cos(m)-sin(theta)*sin(m)=cos(theta+m)
new_zy = new_zy*s # 此处为s*cos(theta+m),扩充了s倍
if args.easy_margin:
zy_keep = zy # zy_keep为zy,即s*cos(theta)
else:
zy_keep = zy - s*mm # zy_keep为zy-s*sin(m)*m=s*cos(theta)-s*m*sin(m)
new_zy = mx.sym.where(cond, new_zy, zy_keep) # cond中>0的保持new_zy=s*cos(theta+m)不变,<0的裁剪为zy_keep= s*cos(theta) or s*cos(theta)-s*m*sin(m) diff = new_zy - zy #
diff = mx.sym.expand_dims(diff, 1)
gt_one_hot = mx.sym.one_hot(gt_label, depth = args.num_classes, on_value = 1.0, off_value = 0.0)
body = mx.sym.broadcast_mul(gt_one_hot, diff) # 对应yi处为new_zy - zy
fc7 = fc7+body # 对应yi处,fc7=zy + (new_zy - zy) = new_zy,即cond中>0的为s*cos(theta+m),<0的裁剪为s*cos(theta) or s*cos(theta)-s*m*sin(m)

(原)InsightFace及其mxnet代码的更多相关文章

  1. (原)CosFace/AM-Softmax及其mxnet代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/8525241.html 论文: CosFace: Large Margin Cosine Loss fo ...

  2. 【原】聊聊js代码异常监控

    在平时的工作,js报错是比较常见的一个情景,尤其是有一些错误可能我们在本地测试的时候测试不出来,当发布到线上之后才可以发现,如果抢救及时,那还好,假如很晚才发 现,那就可能造成很大的损失了.如果我们前 ...

  3. 《笨办法学python第三版》习题26,原错误代码及正确代码

    #import ex25 1 def break_words(stuff): """This function will break up words for us.&q ...

  4. (原)使用TortoiseGit提交代码push的时候报错:HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large

    今天我想rk的sdk包里面的一些东西提交到我的git服务器上,结果,总是报错,折腾了一下午,结果才解决. 首先看看我提交代码的时候,报错的信息: git.exe push --progress &qu ...

  5. [原][JSBSim]基于qt代码实现:TCP|UDP与飞行模拟软件JSBSim的通信,现实模型飞行!

    废话没有,上关键代码 头文件 #include <QUdpSocket> #include <qtcpsocket.h> #ifndef vrUDP #define vrUDP ...

  6. 转换流的原理和OutputStreamWriter介绍&代码实现

    转换流的原理 OutputStreamWriter介绍&代码实现 package com.yang.Test.ReverseStream; import java.io.FileNotFoun ...

  7. 树的深度优先遍历和广度优先遍历的原理和java实现代码

    import java.util.ArrayDeque; public class BinaryTree { static class TreeNode{ int value; TreeNode le ...

  8. 【PyTorch】深度学习与PyTorch资料链接整理

    欢迎来到我的博客! 以下链接均是日常学习,偶然得之,并加以收集整理,感兴趣的朋友可以多多访问和学习.如果以下内容对你有所帮助,不妨转载和分享.(Update on 5,November,2019) 1 ...

  9. ubuntu+anaconda+mxnet环境配置

    为了insightface和mxnet较劲的一天 mxnet环境: 官网下载pyhton2.7版本的anaconda,随便找个安装教程 sh Anacondaxxxx.sh #一路默认即可,第二个回车 ...

随机推荐

  1. 全文检索(SOLR)前端应用浅析 (转)

    最近在一个关于知识管理系统中检索的一个功能方案,找到了一个很好的参考应用方案嘎要的分析一下,希望有类似应用的可以交流交流. 提起全文检索,Lucene的大名估计地球人都知道,通过这么多年的发展,外围的 ...

  2. Python3 比较两个图片是否类似或相同

    Python代码 #coding:utf8 import os from PIL import Image,ImageDraw,ImageFile import numpy import pytess ...

  3. memcache的资料集

    1. 安装与查看过程(完整) https://www.cyberciti.biz/faq/rhel-fedora-linux-install-memcached-caching-system-rpm/ ...

  4. 调试工具DebugView

      可以用来跟踪如下函数的输出: Win32 OutputDebugString Kernel-mode DbgPrint All kernel-mode variants of DbgPrint i ...

  5. Cocos2d-X研究之v3.x 事件分发机制具体解释

    事件分发机制 " src="http://www.cgzhw.com/wp-content/uploads/2014/07/inherent3.png" style=&q ...

  6. Disqus评论框改造工程-Jekyll等静态博客实现Disqus代理访问

    文章最初发表于szhshp的第三边境研究所转载请注明 关于博客评论 六月多说挂了,地球人都知道. 倡言.云跟帖.来必力都很烂,地球人都知道. 转Disqus的都是人才. Disqus使用中遇到的问题 ...

  7. JAVA Eclipse打开报错failed to load the jni shared library怎么办

    JRE是64位的,但是Eclipse是32位的   一般都用绿色版的了,可以直接解压运行  

  8. SuperMap开发入门2——环境部署

    由于超图的相关资源比较少,可参考官方提供的<SuperMap iDesktop 9D安装指南>和<SuperMap iObjects .NET 9D安装指南>完成应用软件和开发 ...

  9. 10分钟精通require.js

    require.js的诞生,就是为了解决这两个问题:(1)实现js文件的异步加载,避免网页失去响应:(2)管理模块之间的依赖性,便于代码的编写和维护. 实例下载:require.js应用实例 一.re ...

  10. jQuery函数的等价原生函数代码示例

    选择器 jQuery的核心之一就是能非常方便的取到DOM元素.我们只需输入CSS选择字符串,便可以得到匹配的元素.但在大多数情况下,我们可以用简单的原生代码达到同样的效果. .代码如下: //---- ...