-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroi_feature_boost_op.cc
105 lines (77 loc) · 2.57 KB
/
roi_feature_boost_op.cc
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
#include <functional>
#include "roi_feature_boost_op.h"
namespace caffe2 {
template <>
bool RoIFeatureBoostOp<float, CPUContext>::RunOnDevice() {
const auto& X = Input(0);
const auto& S = Input(1);
CAFFE_ENFORCE_EQ(S.dim32(0), S.numel());
CAFFE_ENFORCE_EQ(X.dim32(0), S.dim32(0));
const int batch_size = X.dim32(0);
const int feature_size = X.size_from_dim(1);
const float* Xdata = X.data<float>();
const float* Sdata = S.data<float>();
auto* Y = Output(0);
Y->ResizeLike(X);
float* Ydata = Y->mutable_data<float>();
for (int b = 0; b < batch_size; ++b) {
int index_S = b;
for (int f = 0; f < feature_size; ++f) {
int index_XY = b * feature_size + f;
Ydata[index_XY] = Xdata[index_XY] * Sdata[index_S];
}
}
return true;
}
template <>
bool RoIFeatureBoostGradientOp<float, CPUContext>::RunOnDevice() {
const auto& dY = Input(0);
const auto& S = Input(1);
CAFFE_ENFORCE_EQ(S.dim32(0), S.numel());
CAFFE_ENFORCE_EQ(dY.dim32(0), S.dim32(0));
const int batch_size = dY.dim32(0);
const int feature_size = dY.size_from_dim(1);
const float* dYdata = dY.data<float>();
const float* Sdata = S.data<float>();
auto* dX = Output(0);
dX->ResizeLike(dY);
float* dXdata = dX->mutable_data<float>();
for (int b = 0; b < batch_size; ++b) {
int index_S = b;
for (int f = 0; f < feature_size; ++f) {
int index_dXY = b * feature_size + f;
dXdata[index_dXY] = dYdata[index_dXY] * Sdata[index_S];
}
}
return true;
}
REGISTER_CPU_OPERATOR(RoIFeatureBoost, RoIFeatureBoostOp<float, CPUContext>);
REGISTER_CPU_OPERATOR(RoIFeatureBoostGradient,
RoIFeatureBoostGradientOp<float, CPUContext>);
namespace {} // namespace
using namespace std::placeholders;
OPERATOR_SCHEMA(RoIFeatureBoost)
.NumInputs(2)
.NumOutputs(1)
.AllowInplace({{0, 0}})
.SetDoc(R"DOC(
)DOC")
.Input(0, "X", "The input tensor of size (NxCxHxW).")
.Input(1, "S", "The roi tensor of size (N).")
.Output(0, "Y", "output tensor of size (NxCxHxW).");
OPERATOR_SCHEMA(RoIFeatureBoostGradient)
.NumInputs(2)
.NumOutputs(1)
.AllowInplace({{0, 0}});
namespace {
class GetRoIFeatureBoostGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
std::vector<OperatorDef> GetGradientDefs() override {
return SingleGradientDef(def_.type() + "Gradient", "",
vector<string>{GO(0), I(1)},
vector<string>{GI(0)});
}
};
REGISTER_GRADIENT(RoIFeatureBoost, GetRoIFeatureBoostGradient);
} // namespace
} // namespace caffe2