CosFace中的cos loss(Large Margin Cosine Loss)实现(tensorflow)

本文介绍了一种用于深度人脸识别的大边际余弦损失(MLCL)方法——CosFace,并提供了其在TensorFlow中的实现代码。CosFace通过引入一个预定义的边际来增加不同类别之间的间隔,从而提高人脸识别的准确性。

你要的答案或许都在这里小鹏的博客目录

MachineLP的Github(欢迎follow):https://github.com/MachineLP

paper:

CosFace: Large Margin Cosine Loss(MLCL) for Deep Face Recognition

下载地址: https://arxiv.org/pdf/1801.09414.pdf

论文中的cos loss:




cos loss 的 TF 实现:

# coding=utf-8

import tensorflow as tf
import numpy as np

def py_func(func, inp, Tout, stateful = True, name=None, grad_func=None):  
    rand_name = 'PyFuncGrad' + str(np.random.randint(0,1E+8))  
    tf.RegisterGradient(rand_name)(grad_func)  
    g = tf.get_default_graph()  
    with g.gradient_override_map({'PyFunc':rand_name}):  
        return tf.py_func(func,inp,Tout,stateful=stateful, name=name)  
  
def coco_forward(xw, y, m, name=None):  
    #pdb.set_trace()  
    xw_copy = xw.copy()  
    num = len(y)  
    orig_ind = range(num)  
    xw_copy[orig_ind,y] -= m  
    return xw_copy  
  
def coco_help(grad,y):  
    grad_copy = grad.copy()  
    return grad_copy  
  
def coco_backward(op, grad):  
      
    y = op.inputs[1]  
    m = op.inputs[2]  
    grad_copy = tf.py_func(coco_help,[grad,y],tf.float32)  
    return grad_copy,y,m  
  
def coco_func(xw,y,m, name=None):  
    with tf.op_scope([xw,y,m],name,"Coco_func") as name:  
        coco_out = py_func(coco_forward,[xw,y,m],tf.float32,name=name,grad_func=coco_backward)  
        return coco_out  
  
def cos_loss(x, y,  num_cls, reuse=False, alpha=0.25, scale=64,name = 'cos_loss'):  
    ''''' 
    x: B x D - features 
    y: B x 1 - labels 
    num_cls: 1 - total class number 
    alpah: 1 - margin 
    scale: 1 - scaling paramter 
    '''  
    # define the classifier weights  
    xs = x.get_shape()  
    y = tf.reshape(tf.cast(y, dtype = tf.int32),[-1])
    with tf.variable_scope('centers_var',reuse=reuse) as center_scope:  
        w = tf.get_variable("centers", [xs[1], num_cls], dtype=tf.float32,   
            initializer=tf.contrib.layers.xavier_initializer(),trainable=True)  
     
    #normalize the feature and weight  
    #(N,D)  
    x_feat_norm = tf.nn.l2_normalize(x,1,1e-10)  
    #(D,C)  
    w_feat_norm = tf.nn.l2_normalize(w,0,1e-10)  
      
    # get the scores after normalization   
    #(N,C)  
    xw_norm = tf.matmul(x_feat_norm, w_feat_norm)    
    #value = tf.identity(xw)  
    #substract the marigin and scale it  
    value = coco_func(xw_norm,y,alpha) * scale  
      
    # compute the loss as softmax loss  
    cos_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=value))  
  
    return cos_loss



评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MachineLP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值