-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnormalize_layer.cpp
157 lines (144 loc) · 5.91 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
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
#include <algorithm>
#include <vector>
#include <cmath>
#include "caffe/layer.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/layers/normalize_layer.hpp"
namespace caffe {
#define sign(x) (Dtype(0) < (x)) - ((x) < Dtype(0))
template <typename Dtype>
void NormalizeLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
normalize_type_ =
this->layer_param_.normalize_param().normalize_type();
fix_gradient_ =
this->layer_param_.normalize_param().fix_gradient();
bp_norm_ = this->layer_param_.normalize_param().bp_norm() && top.size() == 2;
}
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());
if (top.size() == 2) {
top[1]->Reshape(bottom[0]->num(), 1,
bottom[0]->height(), bottom[0]->width());
}
norm_.Reshape(bottom[0]->num(), 1,
bottom[0]->height(), bottom[0]->width());
}
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* square_data = squared_.mutable_cpu_data();
Dtype* norm_data = (top.size() == 2) ? top[1]->mutable_cpu_data() : norm_.mutable_cpu_data();
int num = bottom[0]->num();
int channels = bottom[0]->channels();
int spatial_dim = bottom[0]->height() * bottom[0]->width();
if (normalize_type_ == "L2") {
caffe_sqr<Dtype>(num*channels*spatial_dim, bottom_data, square_data);
for (int n = 0; n < num; n++) {
for (int s = 0; s < spatial_dim; s++) {
norm_data[n*spatial_dim + s] = Dtype(0);
for (int c = 0; c < channels; c++) {
norm_data[n*spatial_dim + s] += square_data[(n * channels + c) * spatial_dim + s];
}
norm_data[n*spatial_dim + s] += 1e-6;
norm_data[n*spatial_dim + s] = sqrt(norm_data[n*spatial_dim + s]);
for (int c = 0; c < channels; c++) {
top_data[(n * channels + c) * spatial_dim + s] = bottom_data[(n * channels + c) * spatial_dim + s] / norm_data[n*spatial_dim + s];
}
}
}
}
else if (normalize_type_ == "L1") {
caffe_abs<Dtype>(num*channels*spatial_dim, bottom_data, square_data);
for (int n = 0; n < num; n++) {
for (int s = 0; s < spatial_dim; s++) {
norm_data[n*spatial_dim +s] = Dtype(0);
for (int c = 0; c < channels; c++) {
norm_data[n*spatial_dim + s] += square_data[(n * channels + c) * spatial_dim + s];
}
norm_data[n*spatial_dim + s] += 1e-6;
norm_data[n*spatial_dim + s] = norm_data[n*spatial_dim + s];
for (int c = 0; c < channels; c++) {
top_data[(n * channels + c) * spatial_dim + s] = bottom_data[(n * channels + c) * spatial_dim + s] / norm_data[n*spatial_dim + s];
}
}
}
}
else {
NOT_IMPLEMENTED;
}
}
template <typename Dtype>
void NormalizeLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* top_data = top[0]->cpu_data();
const Dtype* bottom_data = bottom[0]->cpu_data();
const Dtype* square_data = squared_.cpu_data();
const Dtype* norm_data = (top.size() == 2) ? top[1]->cpu_data() : norm_.cpu_data();
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
int num = bottom[0]->num();
int channels = bottom[0]->channels();
int spatial_dim = bottom[0]->height() * bottom[0]->width();
if (propagate_down[0]) {
if (normalize_type_ == "L2") {
for (int n = 0; n < num; ++n) {
for (int s = 0; s < spatial_dim; s++) {
Dtype a = caffe_cpu_strided_dot(channels, top_data + n*channels*spatial_dim + s, spatial_dim, top_diff + n*channels*spatial_dim + s, spatial_dim);
for (int c = 0; c < channels; c++) {
bottom_diff[(n * channels + c) * spatial_dim + s] =
(top_diff[(n * channels + c) * spatial_dim + s] - top_data[(n * channels + c) * spatial_dim + s] * a) / norm_data[n*spatial_dim + s];
}
}
}
}
else if (normalize_type_ == "L1") {
for (int n = 0; n < num; ++n) {
for (int s = 0; s < spatial_dim; s++) {
Dtype a = caffe_cpu_strided_dot(channels, top_data + n*channels*spatial_dim + s, spatial_dim, top_diff + n*channels*spatial_dim + s, spatial_dim);
for (int c = 0; c < channels; c++) {
bottom_diff[(n * channels + c) * spatial_dim + s] =
(top_diff[(n * channels + c) * spatial_dim + s] - sign(bottom_data[(n * channels + c) * spatial_dim + s]) * a) / norm_data[n*spatial_dim + s];
}
}
}
}
else {
NOT_IMPLEMENTED;
}
}
if (bp_norm_) {
const Dtype* norm_diff =top[1]->cpu_diff();
if (normalize_type_ == "L2") {
for (int n = 0; n < num; ++n) {
for (int s = 0; s < spatial_dim; s++) {
for (int c = 0; c < channels; c++) {
bottom_diff[(n * channels + c) * spatial_dim + s] += norm_diff[n*spatial_dim + s] * top_data[(n * channels + c) * spatial_dim + s];
}
}
}
}
else if (normalize_type_ == "L1") {
for (int n = 0; n < num; ++n) {
for (int s = 0; s < spatial_dim; s++) {
for (int c = 0; c < channels; c++) {
bottom_diff[(n * channels + c) * spatial_dim + s] += norm_diff[n*spatial_dim + s] * sign(bottom_data[(n * channels + c) * spatial_dim + s]);
}
}
}
}
}
}
#ifdef CPU_ONLY
STUB_GPU(NormalizeLayer);
#endif
INSTANTIATE_CLASS(NormalizeLayer);
REGISTER_LAYER_CLASS(Normalize);
} // namespace caffe