FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CaffeOnACL/src/caffe/layers/eltwise_layer.cpp at master · 2php/CaffeOnACL · GitHub
2php
CaffeOnACL
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
CaffeOnACL
/
src
/
caffe
/
layers
/
eltwise_layer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
160 lines (151 loc) · 5.37 KB
Breadcrumbs
CaffeOnACL
/
src
/
caffe
/
layers
/
eltwise_layer.cpp
Copy path
File metadata and controls
160 lines (151 loc) · 5.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
158
159
160
#
include
<
cfloat
>
#
include
<
vector
>
#
include
"
caffe/layers/eltwise_layer.hpp
"
#
include
"
caffe/util/math_functions.hpp
"
namespace
caffe
{
template
<
typename
Dtype>
void
EltwiseLayer<Dtype>::LayerSetUp(
const
vector<Blob<Dtype>*>& bottom,
const
vector<Blob<Dtype>*>& top) {
CHECK
(
this
->
layer_param
().
eltwise_param
().
coeff_size
() ==
0
||
this
->
layer_param
().
eltwise_param
().
coeff_size
() == bottom.
size
()) <<
"
Eltwise Layer takes one coefficient per bottom blob.
"
;
CHECK
(!(
this
->
layer_param
().
eltwise_param
().
operation
()
== EltwiseParameter_EltwiseOp_PROD
&&
this
->
layer_param
().
eltwise_param
().
coeff_size
())) <<
"
Eltwise layer only takes coefficients for summation.
"
;
op_ =
this
->
layer_param_
.
eltwise_param
().
operation
();
//
Blob-wise coefficients for the elementwise operation.
coeffs_ = vector<Dtype>(bottom.
size
(),
1
);
if
(
this
->
layer_param
().
eltwise_param
().
coeff_size
()) {
for
(
int
i =
0
; i < bottom.
size
(); ++i) {
coeffs_[i] =
this
->
layer_param
().
eltwise_param
().
coeff
(i);
}
}
stable_prod_grad_ =
this
->
layer_param_
.
eltwise_param
().
stable_prod_grad
();
}
template
<
typename
Dtype>
void
EltwiseLayer<Dtype>::Reshape(
const
vector<Blob<Dtype>*>& bottom,
const
vector<Blob<Dtype>*>& top) {
for
(
int
i =
1
; i < bottom.
size
(); ++i) {
CHECK
(bottom[i]->
shape
() == bottom[
0
]->
shape
());
}
top[
0
]->
ReshapeLike
(*bottom[
0
]);
//
If max operation, we will initialize the vector index part.
if
(
this
->
layer_param_
.
eltwise_param
().
operation
() ==
EltwiseParameter_EltwiseOp_MAX && top.
size
() ==
1
) {
max_idx_.
Reshape
(bottom[
0
]->
shape
());
}
}
template
<
typename
Dtype>
void
EltwiseLayer<Dtype>::Forward_cpu(
const
vector<Blob<Dtype>*>& bottom,
const
vector<Blob<Dtype>*>& top) {
int
* mask =
NULL
;
const
Dtype* bottom_data_a =
NULL
;
const
Dtype* bottom_data_b =
NULL
;
const
int
count = top[
0
]->
count
();
Dtype* top_data = top[
0
]->
mutable_cpu_data
();
switch
(op_) {
case
EltwiseParameter_EltwiseOp_PROD:
caffe_mul
(count, bottom[
0
]->
cpu_data
(), bottom[
1
]->
cpu_data
(), top_data);
for
(
int
i =
2
; i < bottom.
size
(); ++i) {
caffe_mul
(count, top_data, bottom[i]->
cpu_data
(), top_data);
}
break
;
case
EltwiseParameter_EltwiseOp_SUM:
caffe_set
(count,
Dtype
(
0
), top_data);
//
TODO(shelhamer) does BLAS optimize to sum for coeff = 1?
for
(
int
i =
0
; i < bottom.
size
(); ++i) {
caffe_axpy
(count, coeffs_[i], bottom[i]->
cpu_data
(), top_data);
}
break
;
case
EltwiseParameter_EltwiseOp_MAX:
//
Initialize
mask = max_idx_.
mutable_cpu_data
();
caffe_set
(count, -
1
, mask);
caffe_set
(count,
Dtype
(-
FLT_MAX
), top_data);
//
bottom 0 & 1
bottom_data_a = bottom[
0
]->
cpu_data
();
bottom_data_b = bottom[
1
]->
cpu_data
();
for
(
int
idx =
0
; idx < count; ++idx) {
if
(bottom_data_a[idx] > bottom_data_b[idx]) {
top_data[idx] = bottom_data_a[idx];
//
maxval
mask[idx] =
0
;
//
maxid
}
else
{
top_data[idx] = bottom_data_b[idx];
//
maxval
mask[idx] =
1
;
//
maxid
}
}
//
bottom 2++
for
(
int
blob_idx =
2
; blob_idx < bottom.
size
(); ++blob_idx) {
bottom_data_b = bottom[blob_idx]->
cpu_data
();
for
(
int
idx =
0
; idx < count; ++idx) {
if
(bottom_data_b[idx] > top_data[idx]) {
top_data[idx] = bottom_data_b[idx];
//
maxval
mask[idx] = blob_idx;
//
maxid
}
}
}
break
;
default
:
LOG
(
FATAL
) <<
"
Unknown elementwise operation.
"
;
}
}
template
<
typename
Dtype>
void
EltwiseLayer<Dtype>::Backward_cpu(
const
vector<Blob<Dtype>*>& top,
const
vector<
bool
>& propagate_down,
const
vector<Blob<Dtype>*>& bottom) {
const
int
* mask =
NULL
;
const
int
count = top[
0
]->
count
();
const
Dtype* top_data = top[
0
]->
cpu_data
();
const
Dtype* top_diff = top[
0
]->
cpu_diff
();
for
(
int
i =
0
; i < bottom.
size
(); ++i) {
if
(propagate_down[i]) {
const
Dtype* bottom_data = bottom[i]->
cpu_data
();
Dtype* bottom_diff = bottom[i]->
mutable_cpu_diff
();
switch
(op_) {
case
EltwiseParameter_EltwiseOp_PROD:
if
(stable_prod_grad_) {
bool
initialized =
false
;
for
(
int
j =
0
; j < bottom.
size
(); ++j) {
if
(i == j) {
continue
; }
if
(!initialized) {
caffe_copy
(count, bottom[j]->
cpu_data
(), bottom_diff);
initialized =
true
;
}
else
{
caffe_mul
(count, bottom[j]->
cpu_data
(), bottom_diff,
bottom_diff);
}
}
}
else
{
caffe_div
(count, top_data, bottom_data, bottom_diff);
}
caffe_mul
(count, bottom_diff, top_diff, bottom_diff);
break
;
case
EltwiseParameter_EltwiseOp_SUM:
if
(coeffs_[i] ==
Dtype
(
1
)) {
caffe_copy
(count, top_diff, bottom_diff);
}
else
{
caffe_cpu_scale
(count, coeffs_[i], top_diff, bottom_diff);
}
break
;
case
EltwiseParameter_EltwiseOp_MAX:
mask = max_idx_.
cpu_data
();
for
(
int
index =
0
; index < count; ++index) {
Dtype gradient =
0
;
if
(mask[index] == i) {
gradient += top_diff[index];
}
bottom_diff[index] = gradient;
}
break
;
default
:
LOG
(
FATAL
) <<
"
Unknown elementwise operation.
"
;
}
}
}
}
#
ifdef
CPU_ONLY
STUB_GPU
(EltwiseLayer);
#
endif
INSTANTIATE_CLASS
(EltwiseLayer);
REGISTER_LAYER_CLASS
(Eltwise);
}
//
namespace caffe
Back
|
FazBrowse Home
|
New Git URL