首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CosFace中的cos loss(Large Margin Cosine Loss)实现(tensorflow)

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

作者头像
MachineLP
发布2022-05-09 15:02:22
6550
发布2022-05-09 15:02:22
举报
文章被收录于专栏:小鹏的专栏小鹏的专栏

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

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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档