(原)CosFace/AM-Softmax及其mxnet代碼


轉載請注明出處:

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

論文:

CosFace: Large Margin Cosine Loss for Deep Face Recognition

https://arxiv.org/abs/1801.09414

Additive Margin Softmax for Face Verification

https://arxiv.org/abs/1801.05599

第一篇論文目前無代碼

第二篇論文官方代碼:

https://github.com/happynear/AMSoftmax

這兩篇論文第三方mxnet代碼:

https://github.com/deepinsight/insightface

 

說明:沒用過mxnet,下面的代碼注釋只是純粹從代碼的角度來分析並進行注釋,如有錯誤之處,敬請諒解,並歡迎指出。

 

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

論文AM中定義$\psi (\theta )$為:

$\psi (\theta )=\cos (\theta )-m$

sphereface中只對w進行歸一化,AM中對w及x均進行了歸一化,不過為了使得訓練能收斂,增加了一個參數s=30,最終AM如下:

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

程序中計算時,$s\centerdot (\cos {{\theta }_{yi}}-m)=s\centerdot \cos {{\theta }_{yi}}-sm$,分別計算$s\centerdot \cos {{\theta }_{yi}}$,sm。而后將yi處的減去sm,之后通過log softmax,得到概率,在計算損失。

具體的代碼如下(完整代碼請見參考網址中mxnet的代碼):

 1     s = args.margin_s  # 參數s
 2     m = args.margin_m  # 參數m
 3     _weight = mx.symbol.Variable("fc7_weight", shape=(args.num_classes, args.emb_size), lr_mult=1.0) # (C,F)
 4     _weight = mx.symbol.L2Normalization(_weight, mode='instance')  # 對w進行歸一化
 5     
 6     nembedding = mx.symbol.L2Normalization(embedding, mode='instance', name='fc1n')*s # 對x進行歸一化,並得到s*x,(B,F)
 7     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), '為轉置
 8        
 9     s_m = s*m  # 計算s*m
10     gt_one_hot = mx.sym.one_hot(gt_label, depth = args.num_classes, on_value = s_m, off_value = 0.0) # 得到one-hot矩陣,每行對應i處值為s_m
11     fc7 = fc7-gt_one_hot  # 將對應i處的減去s_m

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM