小喵的唠叨话:前一篇博客,我们做完了L-Softmax的准备工作。而这一章,我们开始进行前馈的研究。

小喵博客: http://miaoerduo.com

博客原文:  http://www.miaoerduo.com/deep-learning/基于caffe的large-margin-softmax-loss的实现(中).html

四、前馈

还记得上一篇博客,小喵给出的三个公式吗?不记得也没关系。

这次,我们要一点一点的通过代码来实现这些公式。小喵主要是GPU上实现前后馈的代码,因为这个层只是用来训练,GPU速度应该会快一点。

我们首先要进行一般的FC层的前馈,因为LM_FC的前馈只是修改了一般的FC中的若干个值,而大部分的值都是没有修改过的。

 const Dtype* bottom_data = bottom[]->gpu_data();
const Dtype* label_data = bottom[]->gpu_data();
Dtype* top_data = top[]->mutable_gpu_data();
const Dtype* weight = this->blobs_[]->gpu_data();
// 普通fc层的计算
if (M_ == ) {
caffe_gpu_gemv<Dtype>(CblasNoTrans, N_, K_, (Dtype).,
weight, bottom_data, (Dtype)., top_data);
} else {
caffe_gpu_gemm<Dtype>(CblasNoTrans,
transpose_ ? CblasNoTrans : CblasTrans,
M_, N_, K_, (Dtype).,
bottom_data, weight, (Dtype)., top_data);
}

这样就计算完了一个普通的FC的前馈。

之后是一些具体的实现。

1,$\cos(\theta_j)=\frac{W_j^Tx_i}{\|W_j\|\|x_i\|}$

这是要求出label为$j$的weight的权值和feature之间的余弦值。公式大家在高中应该就学过了。这样需要出三部分:$W_j^Tx_i$,$\|W_j\|$和$\|x_i\|$。这里$i$表示feature的序号,因为一个mini batch中有很多张图片。$j$表示正确的label值。

$W_j^Tx_i$的计算非常简单,因为FC层的前馈计算出来的就是这个值。因此我们可以直接从FC的前馈结果中直接复制对应位置的结果。

$\|W_j\|$和$\|x_i\|$是比较简单的模值的计算,使用caffe_cpu_dot很容易就可以求得(为什么不使用caffe_gpu_dot呢?因为小喵在使用caffe_gpu_dot的时候,caffe会报一个奇怪的错误,不知道是不是因为GPU的显存不能随意访问的)。

最后的余弦值带入到上面的式子,就一下子搞定~

这里用到了几个变量:

M_: batch size

N_: class num

K_: feature length

 // w * x
// 直接从前馈的结果中复制
Dtype *wx_data = this->wx_.mutable_gpu_data();
copy_label_score<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(M_, N_, label_data, top_data, wx_data); // w * w
Dtype *abs_w_data = this->abs_w_.mutable_cpu_data();
for (int m = ; m < M_; ++ m) {
abs_w_data[m] = caffe_cpu_dot<Dtype>(
K_,
this->blobs_[]->cpu_data() + static_cast<int>(label_cpu_data[m]) * K_,
this->blobs_[]->cpu_data() + static_cast<int>(label_cpu_data[m]) * K_
);
} // x * x
Dtype *abs_x_data = this->abs_x_.mutable_cpu_data();
for (int m = ; m < M_; ++ m) {
abs_x_data[m] = caffe_cpu_dot<Dtype>(
K_,
bottom[]->cpu_data() + m * K_,
bottom[]->cpu_data() + m * K_
);
}
// abs_w, abs_x
caffe_gpu_powx<Dtype>(M_, this->abs_w_.mutable_gpu_data(), 0.5, this->abs_w_.mutable_gpu_data());
caffe_gpu_powx<Dtype>(M_, this->abs_x_.mutable_gpu_data(), 0.5, this->abs_x_.mutable_gpu_data()); // cos_t = wx / (|x| * |w|)
Dtype *cos_t_data = this->cos_t_.mutable_gpu_data();
caffe_gpu_div<Dtype>(M_, wx_data, this->abs_x_.gpu_data(), cos_t_data);
caffe_gpu_div<Dtype>(M_, cos_t_data, this->abs_w_.gpu_data(), cos_t_data);

其中copy_label_score是我们自己编写的用来复制结果的核函数(如何编写Cuda程序就是另一门学科了):

 template <typename Dtype>
__global__ void copy_label_score(const int M, const int N, const Dtype *label_data, const Dtype *top_data, Dtype *wx_data) {
CUDA_KERNEL_LOOP(index, M) {
wx_data[index] = top_data[index * N + static_cast<int>(label_data[index])];
}
}

相信机智如你的喵粉,看到这几行代码,一定可以轻松理解。

这里,小喵想多介绍一点东西。
我们知道Caffe里面的数据都是通过Blob结构来存储的,比如这里的bottom_data,其实就是一个blob,默认形状是(n, c, h, w),n表示的就是batch size,c是channel数,h,w分贝表示高和宽。而且blob中的内存的存储顺序,也和一般的C语言中的数组一样。因此我们这里计算feature的模的时候,是直接每K_个数值计算一次点乘。
同理,weight是存储在this->blobs[0]中的,那么weight的形状又是什么样子的呢?这里非常碰巧的是,如果我们在prototxt中设置的transpose为false的话,weight的形状是N_*K_,也就是说,我们可以将weight看成一个矩阵,它的每一行都与feature直接点乘,得到输出,也就是说weight的每一行都是我们需要计算模值的$W_j$,所以我们计算weight的模的时候,用的计算方法和计算feature模时很相似。我们这里强制设置transpose为false,因为这样计算会比较简单。如果你设成了true,那就必须自己写个求模的函数了。

2,$\cos(m\theta_i)=\sum_n(-1)^n{C_m^{2n}\cos^{m-2n}(\theta_i)\cdot(1-\cos(\theta_i)^2)^n}, (2n\leq m)$

我们在(1)中求出了$\cos(\theta)$,对于给定的margin,只需要代入公式就可以求出$\cos(m\theta)$的值了。

 template <typename Dtype>
__global__ void cal_cos_mt(const int count, const unsigned int margin, const int *C_M_N, const Dtype *cos_t_data, Dtype *cos_mt_data) {
CUDA_KERNEL_LOOP(index, count) {
Dtype cos_t = cos_t_data[index];
Dtype sin_t_2 = - cos_t * cos_t;
Dtype cos_mt = .;
int flag = -;
for (int n = ; n <= (margin / ); ++ n) {
flag *= -;
cos_mt += flag * C_M_N[ * n] * powf(cos_t, (margin - * n)) * powf(sin_t_2, n);
}
cos_mt_data[index] = cos_mt;
}
}

上面是用来计算$\cos(m\theta)$的cuda函数,调用也十分的简单:

 // cos(mt)
cal_cos_mt<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(
M_, this->margin, this->C_M_N_.gpu_data(), this->cos_t_.mutable_gpu_data(), this->cos_mt_->mutable_gpu_data());

3,$f_{y_{i}}=(-1)^k\cdot\|W_{y_{i}}\|\|x_{i}\|\cos(m\theta_i)-2k\cdot\|W_{y_i}\|\|x_i\|$

严格上来说,我们需要求的并不是这个式子,而是:

\[f_{y_i}=\frac{\lambda\|W_{y_i}\|\|x_i\|\cos(\theta_{y_i})+\|W_{y_i}\|\|x_i\|\varphi(\theta_{y_i})}{1+\lambda}\]

\[\varphi(\theta)=(-1)^k\cos(m\theta)-2k, \theta\in[\frac{k\pi}{m}, \frac{(k+1)\pi}{m}]\]

可以看出,当$\lambda$为0的时候,这两个式子就退化成前面的一个式子了。

k的求法十分简单,只需要将$\cos(\theta)$与各个区间进行比较就可以得到。

 // k
int *k_cpu_data = this->k_.mutable_cpu_data();
const Dtype *cos_t_cpu_data = this->cos_t_.cpu_data();
for (int m = ; m < M_; ++ m) {
for (int _k = ; _k < this->cos_theta_bound_.count(); ++ _k) {
if (this->cos_theta_bound_.cpu_data()[_k] < cos_t_cpu_data[m]) {
k_cpu_data[m] = _k - ;
break;
}
}
}

最后一步就是计算出真正的前馈值了!按照公式容易编写程序:

 template <typename Dtype>
__global__ void LMForward(
const int M, const int N, const float lambda,
const Dtype *label_data, const Dtype *cos_mt_data, const int *k_data,
const Dtype *abs_w_data, const Dtype *abs_x_data, Dtype *top_data) { CUDA_KERNEL_LOOP(index, M) {
Dtype cos_mt = cos_mt_data[index];
int k = k_data[index];
int label = static_cast<int>(label_data[index]);
Dtype abs_w = abs_w_data[index];
Dtype abs_x = abs_x_data[index];
top_data[N * index + label] = (lambda * top_data[N * index + label] + abs_w * abs_x * ( powf(-, k) * cos_mt - * k )) / ( + lambda);
}
}

调用也十分简单:

 // y
LMForward<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(
M_, N_, this->lambda,
label_data, this->cos_mt_->gpu_data(), this->k_.gpu_data(),
this->abs_w_.gpu_data(), this->abs_x_.gpu_data(), top[]->mutable_gpu_data());

最后附上,完整的前馈代码(省略头文件和caffe的名字空间):

 template <typename Dtype>
__global__ void copy_label_score(const int M, const int N, const Dtype *label_data, const Dtype *top_data, Dtype *wx_data) {
CUDA_KERNEL_LOOP(index, M) {
wx_data[index] = top_data[index * N + static_cast<int>(label_data[index])];
}
} template <typename Dtype>
__global__ void cal_cos_mt(const int count, const unsigned int margin, const int *C_M_N, const Dtype *cos_t_data, Dtype *cos_mt_data) {
CUDA_KERNEL_LOOP(index, count) {
Dtype cos_t = cos_t_data[index];
Dtype sin_t_2 = - cos_t * cos_t;
Dtype cos_mt = .;
int flag = -;
for (int n = ; n <= (margin / ); ++ n) {
flag *= -;
cos_mt += flag * C_M_N[ * n] * powf(cos_t, (margin - * n)) * powf(sin_t_2, n);
}
cos_mt_data[index] = cos_mt;
}
} template <typename Dtype>
__global__ void LMForward(
const int M, const int N, const float lambda,
const Dtype *label_data, const Dtype *cos_mt_data, const int *k_data,
const Dtype *abs_w_data, const Dtype *abs_x_data, Dtype *top_data) { CUDA_KERNEL_LOOP(index, M) {
Dtype cos_mt = cos_mt_data[index];
int k = k_data[index];
int label = static_cast<int>(label_data[index]);
Dtype abs_w = abs_w_data[index];
Dtype abs_x = abs_x_data[index];
top_data[N * index + label] = (lambda * top_data[N * index + label] + abs_w * abs_x * ( powf(-, k) * cos_mt - * k )) / ( + lambda);
}
} template <typename Dtype>
void LargeMarginInnerProductLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->gpu_data();
const Dtype* label_data = bottom[]->gpu_data();
Dtype* top_data = top[]->mutable_gpu_data();
const Dtype* weight = this->blobs_[]->gpu_data(); // 普通fc层的计算
if (M_ == ) {
caffe_gpu_gemv<Dtype>(CblasNoTrans, N_, K_, (Dtype).,
weight, bottom_data, (Dtype)., top_data);
} else {
caffe_gpu_gemm<Dtype>(CblasNoTrans,
transpose_ ? CblasNoTrans : CblasTrans,
M_, N_, K_, (Dtype).,
bottom_data, weight, (Dtype)., top_data);
} const Dtype* label_cpu_data = bottom[]->cpu_data(); // w * x
// 直接从前馈的结果中复制
Dtype *wx_data = this->wx_.mutable_gpu_data();
copy_label_score<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(M_, N_, label_data, top_data, wx_data); // w * w
Dtype *abs_w_data = this->abs_w_.mutable_cpu_data();
for (int m = ; m < M_; ++ m) {
abs_w_data[m] = caffe_cpu_dot<Dtype>(
K_,
this->blobs_[]->cpu_data() + static_cast<int>(label_cpu_data[m]) * K_,
this->blobs_[]->cpu_data() + static_cast<int>(label_cpu_data[m]) * K_
);
} // x * x
Dtype *abs_x_data = this->abs_x_.mutable_cpu_data();
for (int m = ; m < M_; ++ m) {
abs_x_data[m] = caffe_cpu_dot<Dtype>(
K_,
bottom[]->cpu_data() + m * K_,
bottom[]->cpu_data() + m * K_
);
} // abs_w, abs_x
caffe_gpu_powx<Dtype>(M_, this->abs_w_.mutable_gpu_data(), 0.5, this->abs_w_.mutable_gpu_data());
caffe_gpu_powx<Dtype>(M_, this->abs_x_.mutable_gpu_data(), 0.5, this->abs_x_.mutable_gpu_data()); // cos_t = wx / (|x| * |w|)
Dtype *cos_t_data = this->cos_t_.mutable_gpu_data();
caffe_gpu_div<Dtype>(M_, wx_data, this->abs_x_.gpu_data(), cos_t_data);
caffe_gpu_div<Dtype>(M_, cos_t_data, this->abs_w_.gpu_data(), cos_t_data); // cos(mt)
cal_cos_mt<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(
M_, this->margin,
this->C_M_N_.gpu_data(),
this->cos_t_.gpu_data(),
this->cos_mt_.mutable_gpu_data()
); // k
int *k_cpu_data = this->k_.mutable_cpu_data();
const Dtype *cos_t_cpu_data = this->cos_t_.cpu_data();
for (int m = ; m < M_; ++ m) {
for (int _k = ; _k < this->cos_theta_bound_.count(); ++ _k) {
if (this->cos_theta_bound_.cpu_data()[_k] < cos_t_cpu_data[m]) {
k_cpu_data[m] = _k - ;
break;
}
}
} // y
LMForward<Dtype><<<CAFFE_GET_BLOCKS(M_), CAFFE_CUDA_NUM_THREADS>>>(
M_, N_, this->lambda,
label_data, this->cos_mt_.gpu_data(), this->k_.gpu_data(),
this->abs_w_.gpu_data(), this->abs_x_.gpu_data(), top[]->mutable_gpu_data());
}

那么,这样关于large margin softmax loss的前馈我们就轻松的实现了。下一篇,我们要讲最复杂的后馈的实现了。

如果您觉得本文对您有帮助,那请小喵喝杯茶吧~~O(∩_∩)O~~ 再次感慨 $\LaTeX$ 大法好。

转载请注明出处~

基于Caffe的Large Margin Softmax Loss的实现(中)的更多相关文章

  1. 基于Caffe的Large Margin Softmax Loss的实现(上)

    小喵的唠叨话:在写完上一次的博客之后,已经过去了2个月的时间,小喵在此期间,做了大量的实验工作,最终在使用的DeepID2的方法之后,取得了很不错的结果.这次呢,主要讲述一个比较新的论文中的方法,L- ...

  2. Large Margin Softmax Loss for Speaker Verification

    [INTERSPEECH 2019接收] 链接:https://arxiv.org/pdf/1904.03479.pdf 这篇文章在会议的speaker session中.本文主要讨论了说话人验证中的 ...

  3. cosface: large margin cosine loss for deep face recognition

    目录 概 主要内容 Wang H, Wang Y, Zhou Z, et al. CosFace: Large Margin Cosine Loss for Deep Face Recognition ...

  4. Large-Margin Softmax Loss for Convolutional Neural Networks

    paper url: https://arxiv.org/pdf/1612.02295 year:2017 Introduction 交叉熵损失与softmax一起使用可以说是CNN中最常用的监督组件 ...

  5. caffe中softmax loss源码阅读

    (1) softmax loss <1> softmax loss的函数形式为:     (1) zi为softmax的输入,f(zi)为softmax的输出. <2> sof ...

  6. 基于Caffe的DeepID2实现(下)

    小喵的唠叨话:这次的博客,真心累伤了小喵的心.但考虑到知识需要巩固和分享,小喵决定这次把剩下的内容都写完. 小喵的博客:http://www.miaoerduo.com 博客原文: http://ww ...

  7. 基于Caffe的DeepID2实现(中)

    小喵的唠叨话:我们在上一篇博客里面,介绍了Caffe的Data层的编写.有了Data层,下一步则是如何去使用生成好的训练数据.也就是这一篇的内容. 小喵的博客:http://www.miaoerduo ...

  8. 基于Caffe的DeepID2实现(上)

    小喵的唠叨话:小喵最近在做人脸识别的工作,打算将汤晓鸥前辈的DeepID,DeepID2等算法进行实验和复现.DeepID的方法最简单,而DeepID2的实现却略微复杂,并且互联网上也没有比较好的资源 ...

  9. 卷积神经网络系列之softmax,softmax loss和cross entropy的讲解

    我们知道卷积神经网络(CNN)在图像领域的应用已经非常广泛了,一般一个CNN网络主要包含卷积层,池化层(pooling),全连接层,损失层等.虽然现在已经开源了很多深度学习框架(比如MxNet,Caf ...

随机推荐

  1. webp图片实践之路

    最近,我们在项目中实践了webp图片,并且抽离出了工具模块,整合到了项目的基础模板中.传闻IOS10也将要支持webp,那么使用webp带来的性能提升将更加明显.估计在不久的将来,webp会成为标配. ...

  2. 使用TSQL查询和更新 JSON 数据

    JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...

  3. 学习ASP.NET Core,怎能不了解请求处理管道[1]: 中间件究竟是个什么东西?

    ASP.NET Core管道虽然在结构组成上显得非常简单,但是在具体实现上却涉及到太多的对象,所以我们在 "通过重建Hosting系统理解HTTP请求在ASP.NET Core管道中的处理流 ...

  4. [.NET] C# 知识回顾 - Event 事件

    C# 知识回顾 - Event 事件 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6060297.html 序 昨天,通过<C# 知识回顾 - ...

  5. Android 几种消息推送方案总结

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6241354.html 首先看一张国内Top500 Android应用中它们用到的第三方推送以及所占数量: 现 ...

  6. C#——传值参数(2)

    //我的C#是跟着猛哥(刘铁猛)(算是我的正式老师)<C#语言入门详解>学习的,微信上猛哥也给我讲解了一些不懂得地方,对于我来说简直是一笔巨额财富,难得良师! 这次与大家共同学习C#中的 ...

  7. HTTP API接口安全设计

    HTTP API接口安全设计 API接口调用方式 HTTP + 请求签名机制   HTTP + 参数签名机制 HTTPS + 访问令牌机制 有没有更好的方案? OAuth授权机制 OAuth2.0服务 ...

  8. vue.js几行实现的简单的todo list

    序:目前前端框架如:vue.react.angular,构建工具fis3.gulp.webpack等等...... 可谓是五花八门,层出不穷,眼花缭乱...其实吧只要你想玩还是可以玩玩的..下面是看了 ...

  9. 【干货分享】流程DEMO-合同会审表

    流程名: 合同会审表  业务描述: 合同的审批及签订  流程相关文件: 流程包.xml 事务呈批表业务服务.xml 事务呈批表主数据.xml  流程说明: 1.此流程必须先进行事务呈批表流程的配置才可 ...

  10. 【SAP业务模式】之ICS(四):组织单元的配置

    SAP的ICS业务后台配置主要有以下几个配置点: 1.组织单元的配置(公司代码.销售组织.工厂.采购组织等): 2.主数据的部分: 3.订单和开票的定价过程: 4.开票输出类型: 5.公司间发票的配置 ...