InsightFace及其mxnet、tensorflow代码实现
生活随笔
收集整理的這篇文章主要介紹了
InsightFace及其mxnet、tensorflow代码实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
arcface論文:https://arxiv.org/pdf/1801.07698.pdf
說明文檔:https://www.cnblogs.com/darkknightzh/p/8525287.html
tensorflow代碼實現(xiàn)
參考流程圖:
import tensorflow as tf import math#未考慮margin_b的情況,基本與下一個函數(shù)類似,可優(yōu)先采用combine_loss_val,合理設置margin_a, #margin_m, margin_b, s四個參數(shù)即可def arcface_loss(embedding, labels, w_init, out_num, s=64., m=0.5):''':param embedding: the input embedding vectors:param labels: the input labels, the shape should be eg: (batch_size, 1):param s: scalar value default is 64:param out_num: output class num:param m: the margin value, default is 0.5:return: the final cacualted output, this output is send into the tf.nn.softmax directly'''cos_m = math.cos(m)sin_m = math.sin(m)with tf.variable_scope('arcface_loss'):# inputs and weights normembedding_norm = tf.norm(embedding, axis=1, keep_dims=True)embedding = tf.div(embedding, embedding_norm, name='norm_embedding')weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),initializer=w_init, dtype=tf.float32)weights_norm = tf.norm(weights, axis=0, keep_dims=True)weights_unit = tf.div(weights, weights_norm, name='norm_weights')# cos(theta+m)cos_t = tf.matmul(embedding, weights_unit, name='cos_t')cos_t2 = tf.square(cos_t, name='cos_2')sin_t2 = tf.subtract(1., cos_t2, name='sin_2')sin_t = tf.sqrt(sin_t2, name='sin_t')cos_mt = s * tf.subtract(tf.multiply(cos_t, cos_m), tf.multiply(sin_t, sin_m), name='cos_mt')mask = tf.one_hot(labels, depth=out_num, name='one_hot_mask')# mask = tf.squeeze(mask, 1)inv_mask = tf.subtract(1., mask, name='inverse_mask')s_cos_t = tf.multiply(s, cos_t, name='scalar_cos_t')updated_logits = tf.add(tf.multiply(s_cos_t, inv_mask), tf.multiply(cos_mt, mask), name='arcface_loss_output')loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=updated_logits))return loss, weightsdef combine_loss_val(embedding, labels, w_init, out_num, margin_a, margin_m, margin_b, s):'''This code is contributed by RogerLo. Thanks for you contribution.:param embedding: the input embedding vectors:param labels: the input labels, the shape should be eg: (batch_size, 1):param s: scalar value default is 64:param out_num: output class num:param m: the margin value, default is 0.5:return: the final cacualted output, this output is send into the tf.nn.softmax directly'''weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),initializer=w_init, dtype=tf.float32)weights_unit = tf.nn.l2_normalize(weights, axis=0)embedding_unit = tf.nn.l2_normalize(embedding, axis=1) * scos_t = tf.matmul(embedding_unit, weights_unit)ordinal = tf.constant(list(range(0, embedding.get_shape().as_list()[0])), tf.int64)ordinal_y = tf.stack([ordinal, labels], axis=1)sel_cos_t = tf.gather_nd(cos_t, ordinal_y)if margin_a != 1.0 or margin_m != 0.0 or margin_b != 0.0:if margin_a == 1.0 and margin_m == 0.0:s_m = s * margin_bnew_zy = sel_cos_t - s_melse:cos_value = sel_cos_t / st = tf.acos(cos_value)if margin_a != 1.0:t = t * margin_aif margin_m > 0.0:t = t + margin_mbody = tf.cos(t)if margin_b > 0.0:body = body - margin_bnew_zy = body * supdated_logits = tf.add(cos_t, tf.scatter_nd(ordinal_y, tf.subtract(new_zy, sel_cos_t), cos_t.get_shape()))loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=updated_logits))return loss, weights?
總結
以上是生活随笔為你收集整理的InsightFace及其mxnet、tensorflow代码实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux静态库的打包及链接使用
- 下一篇: Caffe源码解析—核函数