转载请注明出处:

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. Windows server 2012 R2 与 Windows 2016 的双系统重启选项

    一台主机上,同时安装了Windows 2012R2还有Windows 2016, 但是如何能在任意一个系统重启到另一个呢? 下图中,在Win2012R2中,无法选择重启到2016中. 解决方案 === ...

  2. 简单使用Google Analytics监控网站浏览行为

    之前对网页做用户转化率调查这块,找到了谷歌GA事件,现在有时间对使用方法和遇到问题做个简单记录.官方文档其实也介绍的比较清楚,可以查看官方文档. 首先,在官网申请UA-id,然后在主页加入如下代码: ...

  3. jQuery UI全教程之一(dialog的使用教程)

    jQuery UI目前的版本已经更新到了1.8.7.个人感觉和easyui相比起来,jQuery UI在界面的美观程度和可定制型更强一些.所以再次将一些jQuery UI组件的用法说明一下,方便日后查 ...

  4. SAP ABAP编程 取得用户中文名称

    有时候我们知道SAP当前用户登录的ID,也就是SY-UNAME.能够取得用户中文名称.例如以下: ***取得用户中文名称 DATA: g_sheet_jsr TYPE string.  "用 ...

  5. Java-JUC(八):使用wait,notify|notifyAll完成生产者消费者通信,虚假唤醒(Spurious Wakeups)问题出现场景,及问题解决方案。

    模拟通过线程实现消费者和订阅者模式: 首先,定义一个店员:店员包含进货.卖货方法:其次,定义一个生产者,生产者负责给店员生产产品:再者,定义一个消费者,消费者负责从店员那里消费产品. 店员: /** ...

  6. Linux echo 显示内容颜色

    Linux echo 显示内容颜色 https://www.cnblogs.com/kimbo/p/6816566.html #字体颜色:30m-37m 黑.红.绿.黄.蓝.紫.青.白 str=&qu ...

  7. 利用OSG实现模拟飞机尾迹-粒子系统

    利用OSG实现模拟飞机尾迹-粒子系统 粒子系统简介:         粒子系统是用于不规则模糊物体建模及图像生成的一种方法.         粒子系统是一种过程模型,即利用各种计算过程生成模型各个体素 ...

  8. Python编译exe

    有几种办法,选择py2exe,从pip安装,还不行,下载看起来都比较老,还是在csdn上下载了一个64位版本for2.7的 http://download.csdn.net/download/henu ...

  9. Angular入门笔记

    AngularJS(下面简称其为ng)是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足,其通过使用指令(directives)结构来扩展HTML词汇,使开发 ...

  10. 准备Mahout所用的向量ApplesToVectors

    <strong><span style="font-size:18px;">/*** * @author YangXin * @info 准备Mahout所 ...