@fanisfun
2017-06-01T12:12:57.000000Z
字数 7143
阅读 2426
DeepLearning
Face
Theory details
Caffe Inplementations on github
python
exprimental
152
c++
cosine matric
19
python
17
sampledata.py
, config.py
and train.py
to fit your dataset and working environment.Most References
- losstype
- FaceNet: A Unified Embedding for Face Recognition and Clustering
- Learning Descriptors for Object Recognition and 3D Pose Estimation
- Learning Descriptors for Object Recognition and 3D Pose Estimation
- partial deriviation (losstype 2)
where is the input of the loss layer for sample and is the margin for triplet.
Denote that and , so the partial
differential equations for the input of triplet loss layer are:
Layer Setup in caffe
Forward
Backward
Each negative:
Each triplet:
Layer setup in caffe
Dtype(n,1,dim,1) diff_pos; // cache for backward
Dtype(n,m,dim,1) diff_neg; // cache for backward
Dtype(n,m,1,1) loss_ij; // cache for backward
Forward in caffe
Diff:
for (int i = 0; i < n; ++i) {
Dtype(n,1,dim,1) diff_pos[i] = fa - fp; // cached for backward
for (int j = 0; j < m; ++j)
Dtype(n,m,dim,1) diff_neg[i][j] = fa - fn; // cached for backward
}
Dist:
for (int i = 0; i < n; ++i) {
local Dtype(0.0) dist_sq_pos = diff_pos^T * diff_pos;
for (int j = 0; j < m; ++j)
local Dtype(0.0) dist_sq_neg = diff_neg^T * diff_neg;
}
Loss:
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
Dtype(n,m,1,1) loss_ij = max(0, dist_sq_pos - dist_sq_neg + margin); // cached for backward
loss += loss_ij;
}
// out of loops
loss /= 2n;
Backward in caffe
local const Dtype(0.0) alpha = top[0].cpu_diff[0] / n; // top_diff
Dtype* bout = bottom[0]->mutable_cpu_diff(); // bottom_diff
caffe_set(bottom[0]->count(), Dtype(0), bout);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (loss_ij[i][j] > Dtype(0.0)] {
// axpby: y = alpha x + beta y
// anchor f_n - x_p = diff_pos - diff_neg
axpby(dim, alpha, diff_pos[i], Dtype(1.0), bout[i*(m+2)+0]);
axpby(dim, -alpha, diff_neg[i][j], Dtype(1.0), bout[i*(m+2)+0]);
// positive
axpby(dim, -alpha, diff_pos[i], Dtype(1.0), bout[i*(m+2)+1]);
// negative
axpby(dim, alpha, diff_neg[i][j], Dtype(0.0), bout[i*(m+2)+(j+2)]);
}
}
}
Read Google Papar
typeloss
Semi-hard Selection
python code
OpenFace(torch): 0.1.0
->0.2.0
Ref-details: FaceNet Paper
details and tricks of training later
triplet-net
triplet-net
PPT
Ways to own layers:
1. Python Layer
forward and backward, work
2. C++ CPU
forward: check variables
3. Cuda GPU
forward: deploy
4. C++ CPU
backward: stable and convergent
5. Cuda GPU
backward: Quicker Training
6. Test
code
6. Format Style and Comments
7. PR
to Offcial Caffe