-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdeep_hand_model_solve_global_hand_scale_layer.cpp
More file actions
113 lines (85 loc) · 4.01 KB
/
deep_hand_model_solve_global_hand_scale_layer.cpp
File metadata and controls
113 lines (85 loc) · 4.01 KB
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
#include <algorithm>
#include "caffe/layer.hpp"
#include "caffe/deep_hand_model_layers.hpp"
namespace caffe {
template <typename Dtype>
void DeepHandModelSolveGlobalHandScaleLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
}
template <typename Dtype>
void DeepHandModelSolveGlobalHandScaleLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const int axis = bottom[0]->CanonicalAxisIndex(
this->layer_param_.inner_product_param().axis());
vector<int> top_shape = bottom[0]->shape();
top_shape.resize(axis + 1);
top_shape[axis] = 1;
top[0]->Reshape(top_shape);
}
template <typename Dtype>
void DeepHandModelSolveGlobalHandScaleLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* scale_norm_global_location_data = bottom[0]->cpu_data();
const Dtype* stat_avg_bone_len_data = bottom[1]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
const int batSize = (bottom[0]->shape())[0];
for (int t = 0; t < batSize; t++)
{
int Bid = t * JointNum_RHD * 3;
int Sid = t * BoneNum_RHD;
//solving scale
double sum_of_square_bone_len = 0.0;
for (int j = 0; j < BoneNum_RHD; j++)
{
int u = bones_RHD[j][0], v = bones_RHD[j][1];
double scale_norm_xku = scale_norm_global_location_data[Bid + u * 3];
double scale_norm_yku = scale_norm_global_location_data[Bid + u * 3 + 1];
double scale_norm_zku = scale_norm_global_location_data[Bid + u * 3 + 2];
double scale_norm_xkv = scale_norm_global_location_data[Bid + v * 3];
double scale_norm_ykv = scale_norm_global_location_data[Bid + v * 3 + 1];
double scale_norm_zkv = scale_norm_global_location_data[Bid + v * 3 + 2];
sum_of_square_bone_len += pow(scale_norm_xku - scale_norm_xkv, 2) + pow(scale_norm_yku - scale_norm_ykv, 2) + pow(scale_norm_zku - scale_norm_zkv, 2);
}
double solve_scale_argmin_equation_A = sum_of_square_bone_len;
double solve_scale_argmin_equation_B = 0.0;
for (int j = 0; j < BoneNum_RHD; j++)
{
int u = bones_RHD[j][0], v = bones_RHD[j][1];
double scale_norm_xku = scale_norm_global_location_data[Bid + u * 3];
double scale_norm_yku = scale_norm_global_location_data[Bid + u * 3 + 1];
double scale_norm_zku = scale_norm_global_location_data[Bid + u * 3 + 2];
double scale_norm_xkv = scale_norm_global_location_data[Bid + v * 3];
double scale_norm_ykv = scale_norm_global_location_data[Bid + v * 3 + 1];
double scale_norm_zkv = scale_norm_global_location_data[Bid + v * 3 + 2];
solve_scale_argmin_equation_B += sqrt(pow(scale_norm_xku - scale_norm_xkv, 2) + pow(scale_norm_yku - scale_norm_ykv, 2) + pow(scale_norm_zku - scale_norm_zkv, 2)) * stat_avg_bone_len_data[Sid + j];
}
solve_scale_argmin_equation_B *= -2.0;
double solve_scale_argmin_equation_C = 0.0;
for (int j = 0; j < BoneNum_RHD; j++)
{
solve_scale_argmin_equation_C += pow(stat_avg_bone_len_data[Sid + j], 2);
}
//get min nadir to solve quadratic equation
//-b+-sqrt(b*b-4*a*c) /2a
//min: might not equal to zero
//so is b/-2a
double global_hand_scale = solve_scale_argmin_equation_B / (-2 * solve_scale_argmin_equation_A);
int Tid = t;
top_data[Tid] = global_hand_scale;
}
}
template <typename Dtype>
void DeepHandModelSolveGlobalHandScaleLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* top_diff = top[0]->cpu_diff();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
const int batSize = (bottom[0]->shape())[0];
}
#ifdef CPU_ONLY
STUB_GPU(DeepHandModelSolveGlobalHandScaleLayer);
#endif
INSTANTIATE_CLASS(DeepHandModelSolveGlobalHandScaleLayer);
REGISTER_LAYER_CLASS(DeepHandModelSolveGlobalHandScale);
} // namespace caffe