-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelu_layer.cpp
140 lines (124 loc) · 4.64 KB
/
prelu_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
#include <algorithm>
#include <vector>
#include "caffe/filler.hpp"
#include "caffe/layer.hpp"
#include "caffe/vision_layers.hpp"
namespace caffe {
template <typename Dtype>
void PReLULayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
CHECK_GE(bottom[0]->num_axes(), 2)
<< "Number of axes of bottom blob must be >=2.";
PReLUParameter prelu_param = this->layer_param().prelu_param();
int channels = bottom[0]->channels();
channel_shared_ = prelu_param.channel_shared();
if (this->blobs_.size() > 0) {
LOG(INFO) << "Skipping parameter initialization";
} else {
this->blobs_.resize(1);
if (channel_shared_) {
this->blobs_[0].reset(new Blob<Dtype>(vector<int>(0)));
} else {
this->blobs_[0].reset(new Blob<Dtype>(vector<int>(1, channels)));
}
shared_ptr<Filler<Dtype> > filler;
if (prelu_param.has_filler()) {
filler.reset(GetFiller<Dtype>(prelu_param.filler()));
} else {
FillerParameter filler_param;
filler_param.set_type("constant");
filler_param.set_value(0.25);
filler.reset(GetFiller<Dtype>(filler_param));
}
filler->Fill(this->blobs_[0].get());
}
if (channel_shared_) {
CHECK_EQ(this->blobs_[0]->count(), 1)
<< "Negative slope size is inconsistent with prototxt config";
} else {
CHECK_EQ(this->blobs_[0]->count(), channels)
<< "Negative slope size is inconsistent with prototxt config";
}
// Propagate gradients to the parameters (as directed by backward pass).
this->param_propagate_down_.resize(this->blobs_.size(), true);
multiplier_.Reshape(vector<int>(1, bottom[0]->count(1)));
backward_buff_.Reshape(vector<int>(1, bottom[0]->count(1)));
caffe_set(multiplier_.count(), Dtype(1), multiplier_.mutable_cpu_data());
}
template <typename Dtype>
void PReLULayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
CHECK_GE(bottom[0]->num_axes(), 2)
<< "Number of axes of bottom blob must be >=2.";
top[0]->ReshapeLike(*bottom[0]);
if (bottom[0] == top[0]) {
// For in-place computation
bottom_memory_.ReshapeLike(*bottom[0]);
}
}
template <typename Dtype>
void PReLULayer<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();
const int count = bottom[0]->count();
const int dim = bottom[0]->count(2);
const int channels = bottom[0]->channels();
const Dtype* slope_data = this->blobs_[0]->cpu_data();
// For in-place computation
if (bottom[0] == top[0]) {
caffe_copy(count, bottom_data, bottom_memory_.mutable_cpu_data());
}
// if channel_shared, channel index in the following computation becomes
// always zero.
const int div_factor = channel_shared_ ? channels : 1;
for (int i = 0; i < count; ++i) {
int c = (i / dim) % channels / div_factor;
top_data[i] = std::max(bottom_data[i], Dtype(0))
+ slope_data[c] * std::min(bottom_data[i], Dtype(0));
}
}
template <typename Dtype>
void PReLULayer<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* slope_data = this->blobs_[0]->cpu_data();
const Dtype* top_diff = top[0]->cpu_diff();
const int count = bottom[0]->count();
const int dim = bottom[0]->count(2);
const int channels = bottom[0]->channels();
// For in-place computation
if (top[0] == bottom[0]) {
bottom_data = bottom_memory_.cpu_data();
}
// if channel_shared, channel index in the following computation becomes
// always zero.
const int div_factor = channel_shared_ ? channels : 1;
// Propagte to param
// Since to write bottom diff will affect top diff if top and bottom blobs
// are identical (in-place computaion), we first compute param backward to
// keep top_diff unchanged.
if (this->param_propagate_down_[0]) {
Dtype* slope_diff = this->blobs_[0]->mutable_cpu_diff();
for (int i = 0; i < count; ++i) {
int c = (i / dim) % channels / div_factor;
slope_diff[c] += top_diff[i] * bottom_data[i] * (bottom_data[i] <= 0);
}
}
// Propagate to bottom
if (propagate_down[0]) {
Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
for (int i = 0; i < count; ++i) {
int c = (i / dim) % channels / div_factor;
bottom_diff[i] = top_diff[i] * ((bottom_data[i] > 0)
+ slope_data[c] * (bottom_data[i] <= 0));
}
}
}
#ifdef CPU_ONLY
STUB_GPU(PReLULayer);
#endif
INSTANTIATE_CLASS(PReLULayer);
REGISTER_LAYER_CLASS(PReLU);
} // namespace caffe