转载请注明出处:

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. springboot的Web开发-Web相关配置

    一:Spring Boot提供自动配置 通过查看WebMvcAutoConfiguration及WebMvcProperties的源码,可以发现Spring Boot为我们提供了如下的自动配置. 1, ...

  2. Topic Model的分类和设计原则

    Topic Model的分类和设计原则 http://blog.csdn.net/xianlingmao/article/details/7065318 topic model的介绍性文章已经很多,在 ...

  3. Cognos11中Dashboard和HTML页面的简单集成

    一.需求 之前很多第三方的程序都是通脱URL的形式可以和cognos Report进行集成,在我前几天的博文<Cognos11中通过URL访问report的设置>一篇中也提到了普通repo ...

  4. Jquery的分页插件

    Jquery的分页插件, 用起来还不错. 来自: http://flaviusmatis.github.io/simplePagination.js/   下载地址: https://github.c ...

  5. Redis实战总结-Redis的高可用性

    在之前的博客<Redis实战总结-配置.持久化.复制>给出了一种Redis主从复制机制,简单地实现了Redis高可用.然后,如果Master服务器宕机,会导致整个Redis瘫痪,这种方式的 ...

  6. installers PHPManager

    === Verbose logging started: // :: Build type: SHIP UNICODE 5.00.10011.00 Calling process: C:\Progra ...

  7. [Docker] Hooking a Volume to Node.js Source Code

    Normally when you create a Volume, it will store in Docket Host, you can also tell the folder which ...

  8. 面试总结——Java高级工程师(三)

    https://blog.csdn.net/moneyshi/article/details/53086927

  9. iOS 在不添加库的情况下 通过抽象类来获取自己想要的方法

    #define SYSTEM_VERSION_MORE_THAN_BFDATA(v) ([[[UIDevice currentDevice] systemVersion] compare:v opti ...

  10. [UIView setShowsFPS:]: unrecognized selector sent to instance XXX

    今天在做sprite Kit game时遇到一个问题. 新建一个项目运行时发现就加了这几句代码无法运行.后来一查原来是storyboard uiview要改一下.改成SKview In your st ...