Skip to content

Commit 47c02b5

Browse files
committed
Add unit tests
1 parent 12619fc commit 47c02b5

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

paddle/fluid/operators/bilinear_interp_op.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,4 @@ REGISTER_OPERATOR(bilinear_interp_grad, ops::BilinearInterpOpGrad);
113113
REGISTER_OP_CPU_KERNEL(bilinear_interp, ops::BilinearInterpKernel<float>,
114114
ops::BilinearInterpKernel<uint8_t>);
115115
REGISTER_OP_CPU_KERNEL(bilinear_interp_grad,
116-
ops::BilinearInterpGradKernel<float>,
117-
ops::BilinearInterpGradKernel<uint8_t>);
116+
ops::BilinearInterpGradKernel<float>);

paddle/fluid/operators/bilinear_interp_op.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ class BilinearInterpKernel : public framework::OpKernel<T> {
7272

7373
for (int c = 0; c < channels; ++c) { // loop for channels
7474
// bilinear interpolation
75-
out_pos[0] =
75+
out_pos[0] = static_cast<T>(
7676
h2lambda * (w2lambda * in_pos[0] + w1lambda * in_pos[wid]) +
7777
h1lambda * (w2lambda * in_pos[hid * in_w] +
78-
w1lambda * in_pos[hid * in_w + wid]);
78+
w1lambda * in_pos[hid * in_w + wid]));
7979
in_pos += in_hw;
8080
out_pos += out_hw;
8181
}
@@ -143,10 +143,12 @@ class BilinearInterpGradKernel : public framework::OpKernel<T> {
143143
const T* out_pos = &d_output[k * out_chw + i * out_w + j];
144144

145145
for (int c = 0; c < channels; ++c) { // loop for channels
146-
in_pos[0] += h2lambda * w2lambda * out_pos[0];
147-
in_pos[wid] += h2lambda * w1lambda * out_pos[0];
148-
in_pos[hid * in_w] += h1lambda * w2lambda * out_pos[0];
149-
in_pos[hid * in_w + wid] += h1lambda * w1lambda * out_pos[0];
146+
in_pos[0] += static_cast<T>(h2lambda * w2lambda * out_pos[0]);
147+
in_pos[wid] += static_cast<T>(h2lambda * w1lambda * out_pos[0]);
148+
in_pos[hid * in_w] +=
149+
static_cast<T>(h1lambda * w2lambda * out_pos[0]);
150+
in_pos[hid * in_w + wid] +=
151+
static_cast<T>(h1lambda * w1lambda * out_pos[0]);
150152
in_pos += in_hw;
151153
out_pos += out_hw;
152154
}

paddle/fluid/pybind/tensor_py.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ struct CastToPyBufferImpl<true, I, ARGS...> {
9797
inline pybind11::buffer_info CastToPyBuffer(const framework::Tensor &tensor) {
9898
auto buffer_info =
9999
details::CastToPyBufferImpl<true, 0, float, int, double, int64_t, bool,
100-
platform::float16>()(tensor);
100+
uint8_t, platform::float16>()(tensor);
101101
return buffer_info;
102102
}
103103

python/paddle/fluid/tests/unittests/test_bilinear_interp_op.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def bilinear_interp_np(input, out_h, out_w, out_size):
4545

4646
out[:, :, i, j] = h2lambda*(w2lambda*input[:, :, h, w] +
4747
w1lambda*input[:, :, h, w+wid]) + \
48-
h1lambda*(w2lambda*input[:, :, h+hid, w] +
49-
w1lambda*input[:, :, h+hid, w+wid])
50-
return out.astype("float32")
48+
h1lambda*(w2lambda*input[:, :, h+hid, w] +
49+
w1lambda*input[:, :, h+hid, w+wid])
50+
return out.astype(input.dtype)
5151

5252

5353
class TestBilinearInterpOp(OpTest):
@@ -122,5 +122,44 @@ def init_test_case(self):
122122
self.out_size = np.array([65, 129]).astype("int32")
123123

124124

125+
class TestBilinearInterpOpUint8(OpTest):
126+
def setUp(self):
127+
self.out_size = None
128+
self.init_test_case()
129+
self.op_type = "bilinear_interp"
130+
input_np = np.random.randint(
131+
low=0, high=256, size=self.input_shape).astype("uint8")
132+
output_np = bilinear_interp_np(input_np, self.out_h, self.out_w,
133+
self.out_size)
134+
self.inputs = {'X': input_np}
135+
if self.out_size is not None:
136+
self.inputs['OutSize'] = self.out_size
137+
self.attrs = {'out_h': self.out_h, 'out_w': self.out_w}
138+
self.outputs = {'Out': output_np}
139+
140+
def test_check_output(self):
141+
self.check_output(atol=1)
142+
143+
def init_test_case(self):
144+
self.input_shape = [1, 3, 9, 6]
145+
self.out_h = 10
146+
self.out_w = 9
147+
148+
149+
class TestCase1Uint8(TestBilinearInterpOpUint8):
150+
def init_test_case(self):
151+
self.input_shape = [2, 3, 128, 64]
152+
self.out_h = 120
153+
self.out_w = 50
154+
155+
156+
class TestCase2Uint8(TestBilinearInterpOpUint8):
157+
def init_test_case(self):
158+
self.input_shape = [4, 1, 7, 8]
159+
self.out_h = 5
160+
self.out_w = 13
161+
self.out_size = np.array([6, 15]).astype("int32")
162+
163+
125164
if __name__ == "__main__":
126165
unittest.main()

0 commit comments

Comments
 (0)