未验证 提交 63194d6e 编写于 作者: Q qingqing01 提交者: GitHub

Enhance InferShape in deformable_conv and prior_box op (#20372)

上级 0b321c8a
...@@ -175,30 +175,41 @@ class DeformableConvOp : public framework::OperatorWithKernel { ...@@ -175,30 +175,41 @@ class DeformableConvOp : public framework::OperatorWithKernel {
std::vector<int64_t> output_shape({in_dims[0], filter_dims[0]}); std::vector<int64_t> output_shape({in_dims[0], filter_dims[0]});
for (size_t i = 0; i < strides.size(); ++i) { for (size_t i = 0; i < strides.size(); ++i) {
output_shape.push_back(ConvOutputSize(in_dims[i + 2], filter_dims[i + 2], if ((!ctx->IsRuntime()) &&
dilations[i], paddings[i], (in_dims[i + 2] <= 0 || filter_dims[i + 2] <= 0)) {
strides[i])); output_shape.push_back(-1);
} else {
output_shape.push_back(ConvOutputSize(in_dims[i + 2],
filter_dims[i + 2], dilations[i],
paddings[i], strides[i]));
}
} }
PADDLE_ENFORCE_EQ(output_shape[1] % deformable_groups, 0U, PADDLE_ENFORCE_EQ(output_shape[1] % deformable_groups, 0U,
"output num_filter must divide deformable group size."); "output num_filter must divide deformable group size.");
PADDLE_ENFORCE_EQ(output_shape[2], offset_dims[2],
"output height must equal to offset map height."); if (ctx->IsRuntime()) {
PADDLE_ENFORCE_EQ(output_shape[3], offset_dims[3], PADDLE_ENFORCE_EQ(output_shape[2], offset_dims[2],
"output width must equal to offset map width."); "output height must equal to offset map height.");
PADDLE_ENFORCE_EQ(offset_dims[1] % (filter_dims[2] * filter_dims[3]), 0U, PADDLE_ENFORCE_EQ(output_shape[3], offset_dims[3],
"offset filter must divide deformable group size."); "output width must equal to offset map width.");
PADDLE_ENFORCE_EQ(offset_dims[1] / (2 * filter_dims[2] * filter_dims[3]), PADDLE_ENFORCE_EQ(offset_dims[1] % (filter_dims[2] * filter_dims[3]), 0U,
deformable_groups, "offset filter must divide deformable group size.");
"offset filter must divide deformable group size."); PADDLE_ENFORCE_EQ(offset_dims[1] / (2 * filter_dims[2] * filter_dims[3]),
PADDLE_ENFORCE_EQ(output_shape[2], mask_dims[2], deformable_groups,
"output height must equal to mask map height."); "offset filter must divide deformable group size.");
PADDLE_ENFORCE_EQ(output_shape[3], mask_dims[3], PADDLE_ENFORCE_EQ(output_shape[2], mask_dims[2],
"output width must equal to mask map width."); "output height must equal to mask map height.");
PADDLE_ENFORCE_EQ(mask_dims[1] % (filter_dims[2] * filter_dims[3]), 0U, PADDLE_ENFORCE_EQ(output_shape[3], mask_dims[3],
"mask filter must divide deformable group size."); "output width must equal to mask map width.");
PADDLE_ENFORCE_EQ(mask_dims[1] / (filter_dims[2] * filter_dims[3]),
deformable_groups, PADDLE_ENFORCE_EQ(mask_dims[1] % (filter_dims[2] * filter_dims[3]), 0U,
"mask filter must divide deformable group size."); "mask filter must divide deformable group size.");
PADDLE_ENFORCE_EQ(mask_dims[1] / (filter_dims[2] * filter_dims[3]),
deformable_groups,
"mask filter must divide deformable group size.");
}
ctx->SetOutputDim("Output", framework::make_ddim(output_shape)); ctx->SetOutputDim("Output", framework::make_ddim(output_shape));
} }
......
...@@ -36,12 +36,6 @@ class PriorBoxOp : public framework::OperatorWithKernel { ...@@ -36,12 +36,6 @@ class PriorBoxOp : public framework::OperatorWithKernel {
PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW."); PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW."); PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
PADDLE_ENFORCE_LT(input_dims[2], image_dims[2],
"The height of input must smaller than image.");
PADDLE_ENFORCE_LT(input_dims[3], image_dims[3],
"The width of input must smaller than image.");
auto min_sizes = ctx->Attrs().Get<std::vector<float>>("min_sizes"); auto min_sizes = ctx->Attrs().Get<std::vector<float>>("min_sizes");
auto max_sizes = ctx->Attrs().Get<std::vector<float>>("max_sizes"); auto max_sizes = ctx->Attrs().Get<std::vector<float>>("max_sizes");
auto variances = ctx->Attrs().Get<std::vector<float>>("variances"); auto variances = ctx->Attrs().Get<std::vector<float>>("variances");
......
...@@ -152,6 +152,25 @@ class TestPriorBox(unittest.TestCase): ...@@ -152,6 +152,25 @@ class TestPriorBox(unittest.TestCase):
assert box.shape[3] == 4 assert box.shape[3] == 4
class TestPriorBox2(unittest.TestCase):
def test_prior_box(self):
program = Program()
with program_guard(program):
data_shape = [None, 3, None, None]
images = fluid.data(name='pixel', shape=data_shape, dtype='float32')
conv1 = fluid.layers.conv2d(images, 3, 3, 2)
box, var = layers.prior_box(
input=conv1,
image=images,
min_sizes=[100.0],
aspect_ratios=[1.],
flip=True,
clip=True)
assert len(box.shape) == 4
assert box.shape == var.shape
assert box.shape[3] == 4
class TestDensityPriorBox(unittest.TestCase): class TestDensityPriorBox(unittest.TestCase):
def test_density_prior_box(self): def test_density_prior_box(self):
program = Program() program = Program()
......
...@@ -2830,8 +2830,7 @@ class TestBook(LayerTest): ...@@ -2830,8 +2830,7 @@ class TestBook(LayerTest):
print(str(program)) print(str(program))
def test_deformable_conv(self): def test_deformable_conv(self):
with program_guard(fluid.default_main_program(), with self.static_graph():
fluid.default_startup_program()):
input = layers.data( input = layers.data(
name='input', name='input',
append_batch_size=False, append_batch_size=False,
...@@ -2856,6 +2855,23 @@ class TestBook(LayerTest): ...@@ -2856,6 +2855,23 @@ class TestBook(LayerTest):
padding=1) padding=1)
return (out) return (out)
def test_deformable_conv2(self):
with self.static_graph():
input = fluid.data(
name='input', shape=[None, 3, None, None], dtype="float32")
offset = fluid.data(
name='offset', shape=[None, 18, None, None], dtype="float32")
mask = fluid.data(
name='mask', shape=[None, 9, None, None], dtype="float32")
out = layers.deformable_conv(
input=input,
offset=offset,
mask=mask,
num_filters=2,
filter_size=3,
padding=1)
return (out)
def test_unfold(self): def test_unfold(self):
with self.static_graph(): with self.static_graph():
x = layers.data(name='x', shape=[3, 20, 20], dtype='float32') x = layers.data(name='x', shape=[3, 20, 20], dtype='float32')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册