-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcenterloss.cu
170 lines (144 loc) · 5.84 KB
/
centerloss.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*!
* Copyright (c) 2017 by Contributors
* \file centerloss.cu
* \brief centerloss
* \author deepearthgo
*/
#include "./centerloss-inl.h"
namespace mshadow {
namespace cuda {
define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
MSHADOW_XINLINE int LSPowOfMO(const int k) {
return 1 - ((k&0x01) << 1);
}
template<typename DType>
__global__ void centerlossForwardKernel(const Tensor<gpu, 2, DType> x,
const Tensor<gpu, 2, DType> w,
const Tensor<gpu, 1, DType> label,
Tensor<gpu, 2, DType> out,
const Tensor<cpu, 2, DType> &diff,
const Tensor<cpu, 2, DType> ¢er,
const int batch_size) {
const int n = x.size(0);
const int feature_dim = x.size(1);
const int m = w.size(0);
CUDA_KERNEL_LOOP(i, n) {
const int yi = static_cast<int>(label[i]);
diff[i] = x[i] - center[yi]
for (int j = 0; j < n; ++j){
sum += pow(diff[i][j],2);
}
out += sqrt(sum)/(0.5*batch_size)
}
}
template<typename DType>
inline void centerlossForward(const Tensor<gpu, 2, DType> &x,
const Tensor<gpu, 2, DType> &w,
const Tensor<gpu, 1, DType> &label,
const Tensor<gpu, 2, DType> &out,
const Tensor<cpu, 2, DType> &diff,
const Tensor<cpu, 2, DType> ¢er,
const int batch_size) {
const int n = x.size(0);
const int m = w.size(0);
dim3 dimBlock(kBaseThreadNum);
dim3 dimGrid((n + kBaseThreadNum - 1) / kBaseThreadNum);
dimGrid.x = ((n + kBaseThreadNum - 1) / kBaseThreadNum);
LSoftmaxForwardKernel<<<dimGrid, dimBlock>>>(x, w, label, out, diff, center);
}
template<typename DType>
__global__ void centerlossBackwardCenterlabel(const Tensor<gpu, 2, DType> diff,
const Tensor<gpu, 2, DType> center,
const int class_num,
const float alpha) {
const int n = diff.size(0);
const int feature_dim = diff.size(1);
CUDA_KERNEL_LOOP(i,class_num){
const float sum_[feature_dim] ={0};
const int ind1 = 0;
// update sum_
for(int k=0;k<n;++k){
if (<int>(center[k])==i)
{
sum_ = sum_ + diff[k];
ind1 = ind1 + 1;
}
// update center
delta_c = sum_/(1+ind_1);
}
center[i] += alpha * delta_c;
}
}
template<typename DType>
__global__ void centerlossBackwardGradKernel(const Tensor<gpu, 2, DType> diff,
const Tensor<gpu, 2, DType> center,
const Tensor<gpu, 1, DType> label,
const Tensor<gpu, 2, DType> o_grad,
Tensor<gpu, 2, DType> x_grad,
const float scale,
const int batch_size) {
const int n = diff.size(0);
const float feature_dim = diff.size(1);
CUDA_KERNEL_LOOP(i,n) {
x_grad[i]= static_cast<float>(scale/batch_size) * diff[i];
}
}
template<typename DType>
inline void centerlossBackward(const Tensor<cpu, 2, DType> &diff,
const Tensor<cpu, 2, DType> ¢er,
const Tensor<gpu, 1, DType> &label,
const Tensor<gpu, 2, DType> &o_grad,
const Tensor<gpu, 2, DType> &x_grad,
const float scale,
const int batch_size,
const int class_num,
const float alpha) {
const int n = diff.size(0);
const int feature_dim = diff.size(1);
//const int m = w.size(0);
dim3 dimBlock(kBaseThreadNum);
dim3 dimGrid((n + kBaseThreadNum - 1) / kBaseThreadNum);
dimGrid.x = ((n * feature_dim + kBaseThreadNum - 1) / kBaseThreadNum);
centerlossBackwardCenterlabel<<<dimGrid, dimBlock>>>(diff, center, class_num, alpha);
dimGrid.x = ((n * feature_dim + kBaseThreadNum - 1) / kBaseThreadNum);
centerlossBackwardGradKernel<<<dimGrid, dimBlock>>>(diff, center, label, o_grad, x_grad, scale,batch_size);
}
} // namespace cuda
template<typename DType>
inline void centerlossForward(const Tensor<gpu, 2, DType> &x,
const Tensor<gpu, 2, DType> &w,
const Tensor<gpu, 1, DType> &label,
const Tensor<gpu, 2, DType> &out,
const Tensor<cpu, 2, DType> &diff,
const Tensor<cpu, 2, DType> ¢er,
const int batch_size) {
cuda::centerlossForward(x, w, label, out, diff,center,batch_size);
}
template<typename DType>
inline void centerlossBackward(const Tensor<cpu, 2, DType> &diff,
const Tensor<cpu, 2, DType> ¢er,
const Tensor<gpu, 1, DType> &label,
const Tensor<gpu, 2, DType> &o_grad,
const Tensor<gpu, 2, DType> &x_grad,
const float scale,
const int batch_size,
const int class_num,
const float alpha) {
cuda::LSoftmaxBackward(diff,center,label,o_grad,x_grad,scale,batch_size,class_num,alpha);
}
} // namespace mshadow
namespace mxnet {
namespace op {
template<>
Operator *CreateOp<gpu>(centerlossParam param, int dtype) {
Operator *op = NULL;
MSHADOW_REAL_TYPE_SWITCH(dtype, DType, {
op = new LSoftmaxOp<gpu, DType>(param);
})
return op;
}
} // namespace op
} // namespace mxnet