-
Notifications
You must be signed in to change notification settings - Fork 47
/
normalize_layer.cpp
72 lines (62 loc) · 2.64 KB
/
normalize_layer.cpp
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
#include <algorithm>
#include <vector>
#include <cmath>
#include "caffe/util/math_functions.hpp"
#include "caffe/layers/normalize_layer.hpp"
namespace caffe {
template <typename Dtype>
void NormalizeLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
top[0]->Reshape(bottom[0]->num(), bottom[0]->channels(),
bottom[0]->height(), bottom[0]->width());
squared_.Reshape(bottom[0]->num(), bottom[0]->channels(),
bottom[0]->height(), bottom[0]->width());
}
template <typename Dtype>
void NormalizeLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
NormalizeParameter normalize_param = this->layer_param_.normalize_param();
rescale_coeff_ = normalize_param.coeff();
LOG(INFO) << "Rescale coeff: " << rescale_coeff_;
}
template <typename Dtype>
void NormalizeLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* top_data = top[0]->mutable_cpu_data();
Dtype* squared_data = squared_.mutable_cpu_data();
int n = bottom[0]->num();
int d = bottom[0]->count() / n;
caffe_sqr<Dtype>(n*d, bottom_data, squared_data);
for (int i=0; i<n; ++i) {
Dtype normsqr = caffe_cpu_asum<Dtype>(d, squared_data+i*d) + static_cast<Dtype>(1e-12);
caffe_cpu_scale<Dtype>(d, pow(normsqr, -0.5), bottom_data+i*d, top_data+i*d);
caffe_cpu_scale<Dtype>(d, rescale_coeff_, top_data+i*d, top_data+i*d);
}
}
template <typename Dtype>
void NormalizeLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
Dtype* top_diff = top[0]->mutable_cpu_diff();
Dtype* top_data = top[0]->mutable_cpu_data();
const Dtype* bottom_data = bottom[0]->cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
int n = top[0]->num();
int d = top[0]->count() / n;
for (int i=0; i<n; ++i) {
caffe_cpu_scale(d, rescale_coeff_, top_diff+i*d, top_diff+i*d);
caffe_cpu_scale(d, Dtype(1.0)/rescale_coeff_, top_data+i*d, top_data+i*d);
Dtype a = caffe_cpu_dot(d, top_data+i*d, top_diff+i*d);
caffe_cpu_scale(d, a, top_data+i*d, bottom_diff+i*d);
caffe_sub(d, top_diff+i*d, bottom_diff+i*d, bottom_diff+i*d);
a = caffe_cpu_dot(d, bottom_data+i*d, bottom_data+i*d) + Dtype(1e-12);
caffe_cpu_scale(d, Dtype(pow(a, -0.5)), bottom_diff+i*d, bottom_diff+i*d);
//caffe_cpu_scale(d, rescale_coeff_, bottom_diff+i*d, bottom_diff+i*d);
}
}
#ifdef CPU_ONLY
STUB_GPU(NormalizeLayer);
#endif
INSTANTIATE_CLASS(NormalizeLayer);
REGISTER_LAYER_CLASS(Normalize);
} // namespace caffe