-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroi_score_pool_op.cc
141 lines (121 loc) · 3.67 KB
/
roi_score_pool_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
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
#include <functional>
#include "roi_score_pool_op.h"
namespace caffe2 {
template <>
bool RoIScorePoolOp<float, CPUContext>::RunOnDevice() {
const auto& X = Input(0);
const int batch_size = X.dim32(0);
auto* Y = Output(0);
Y->Resize(batch_size, num_classes_);
float* Ydata = Y->mutable_data<float>();
math::Set<float, CPUContext>(Y->numel(), 0.f, Ydata, &context_);
for (int i = 0; i < InputSize(); ++i) {
const auto& X = Input(i);
const float* Xdata = X.data<float>();
const int channels = X.dim32(1);
// const int height = X.dim32(2);
// const int width = X.dim32(3);
int height, width;
if (X.dim() == 2) {
height = 1;
width = 1;
} else if (X.dim() == 3) {
height = X.dim32(2);
width = 1;
} else if (X.dim() == 4) {
height = X.dim32(2);
width = X.dim32(3);
}
for (int b = 0; b < batch_size; ++b) {
for (int c = 0; c < channels; ++c) {
int c_Y = c % num_classes_;
int index_Y = b * num_classes_ + c_Y;
for (int w = 0; w < width; ++w) {
for (int h = 0; h < height; ++h) {
int index_X = ((b * channels + c) * width + w) * height + h;
Ydata[index_Y] += Xdata[index_X];
}
}
}
}
}
return true;
}
template <>
bool RoIScorePoolGradientOp<float, CPUContext>::RunOnDevice() {
const auto& dY = Input(0);
const float* dYdata = dY.data<float>();
for (int i = 1; i < InputSize(); ++i) {
const auto& X = Input(i);
// TODO: Handle the storage_order properly to get the NCWH.
int batch_size = X.dim32(0);
int channels = X.dim32(1);
// int height = X.dim32(2);
// int width = X.dim32(3);
int height, width;
if (X.dim() == 2) {
height = 1;
width = 1;
} else if (X.dim() == 3) {
height = X.dim32(2);
width = 1;
} else if (X.dim() == 4) {
height = X.dim32(2);
width = X.dim32(3);
}
auto* dX = Output(i - 1);
dX->ResizeLike(X);
float* dXdata = dX->mutable_data<float>();
math::Set<float, CPUContext>(dX->numel(), 0.f, dXdata, &context_);
for (int b = 0; b < batch_size; ++b) {
for (int c = 0; c < channels; ++c) {
int c_dY = c % num_classes_;
int index_dY = b * num_classes_ + c_dY;
for (int w = 0; w < width; ++w) {
for (int h = 0; h < height; ++h) {
int index_dX = ((b * channels + c) * width + w) * height + h;
dXdata[index_dX] = dYdata[index_dY];
}
}
}
}
}
return true;
}
REGISTER_CPU_OPERATOR(RoIScorePool, RoIScorePoolOp<float, CPUContext>);
REGISTER_CPU_OPERATOR(RoIScorePoolGradient,
RoIScorePoolGradientOp<float, CPUContext>);
namespace {} // namespace
using namespace std::placeholders;
OPERATOR_SCHEMA(RoIScorePool)
.NumInputs(1, INT_MAX)
.NumOutputs(1)
.SetDoc(R"DOC(
)DOC")
.Arg("num_classes_", "(int32_t) default to -1")
.Output(0, "Y", "output tensor of size (NxC)");
OPERATOR_SCHEMA(RoIScorePoolGradient)
.NumInputs(1, INT_MAX)
.NumOutputs(1, INT_MAX);
namespace {
class GetRoIScorePoolGradient : public GradientMakerBase {
using GradientMakerBase::GradientMakerBase;
std::vector<OperatorDef> GetGradientDefs() override {
if (GradOut(0).IsEmpty()) {
return {};
}
vector<string> ins;
ins.push_back(GO(0));
for (int i = 0; i < def_.input_size(); ++i) {
ins.push_back(I(i));
}
vector<string> outs;
for (int i = 0; i < def_.input_size(); ++i) {
outs.push_back(GI(i));
}
return SingleGradientDef("RoIScorePoolGradient", "", ins, outs);
}
};
REGISTER_GRADIENT(RoIScorePool, GetRoIScorePoolGradient);
} // namespace
} // namespace caffe2