Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions paddle2onnx/mapper/nn/deform_conv2d.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle2onnx/mapper/nn/deform_conv2d.h"

#include <string>
#include <vector>

namespace paddle2onnx {
REGISTER_MAPPER(deformable_conv, DeformConv2dMapper)

int32_t DeformConv2dMapper::GetMinOpset(bool verbose) {
return 19;
}

void DeformConv2dMapper::Opset19() {
auto input_info = GetInput("Input");
auto kernel_info = GetInput("Filter");
auto offset_info = GetInput("Offset");
auto mask_info = GetInput("Mask");
auto output_info = GetOutput("Output");
std::string bias_name = helper_->Constant({kernel_info[0].shape[0]}, GetOnnxDtype(input_info[0].dtype), static_cast<float>(0.0));
auto node = helper_->MakeNode(
"DeformConv",
{input_info[0].name, kernel_info[0].name, offset_info[0].name, bias_name, mask_info[0].name},
{output_info[0].name});

AddAttribute(node, "dilations", dilations_);
AddAttribute(node, "group", groups_);
// std::vector<int64_t> kernel_shape = {
// kernel_info[0].shape[2],
// kernel_info[0].shape[3]
// };
// AddAttribute(node, "kernel_shape", kernel_shape);
std::vector<int64_t> paddings;
if (paddings_.size() == 2) {
paddings.insert(paddings.begin(), paddings_.begin(), paddings_.end());
paddings.insert(paddings.begin(), paddings_.begin(), paddings_.end());
} else {
paddings.assign(paddings_.begin(), paddings_.end());
paddings[1] = paddings_[2];
paddings[2] = paddings_[1];
}
AddAttribute(node, "pads", paddings);
AddAttribute(node, "strides", strides_);
}
} // namespace paddle2onnx
51 changes: 51 additions & 0 deletions paddle2onnx/mapper/nn/deform_conv2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once
#include <string>
#include <vector>

#include "paddle2onnx/mapper/mapper.h"

namespace paddle2onnx
{

class DeformConv2dMapper : public Mapper
{
public:
DeformConv2dMapper(const PaddleParser &p, OnnxHelper *helper, int64_t block_id,
int64_t op_id)
: Mapper(p, helper, block_id, op_id)
{
GetAttr("deformable_groups", &deformable_groups_);
GetAttr("strides", &strides_);
GetAttr("paddings", &paddings_);
GetAttr("dilations", &dilations_);
GetAttr("groups", &groups_);
GetAttr("im2col_step", &im2col_step_);
}

int32_t GetMinOpset(bool verbose) override;
void Opset19() override;

private:
std::vector<int64_t> strides_;
std::vector<int64_t> paddings_;
std::vector<int64_t> dilations_;
int64_t deformable_groups_;
int64_t groups_;
int64_t im2col_step_;
};

} // namespace paddle2onnx
1 change: 1 addition & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ignore="test_auto_scan_multiclass_nms.py
test_auto_scan_generate_proposals.py \
test_uniform.py \
test_ceil.py \
test_deform_conv2d.py \
test_floor_divide.py \
test_has_nan.py \
test_isfinite.py \
Expand Down
44 changes: 44 additions & 0 deletions tests/test_deform_conv2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import paddle
from onnxbase import APIOnnx, randtool


class Net(paddle.nn.Layer):
"""
simple Net
"""

def __init__(self):
super(Net, self).__init__()

def forward(self, inputs, offset, weight, mask):
"""
forward
"""
x = paddle.vision.ops.deform_conv2d(inputs, offset, weight, mask=mask)
return x


def test_deform_conv2d():
"""
api: paddle.vision.ops.deform_conv2d
op version: 19
"""

# [TODO] onnxruntime does not fully support DeformConv, by referring to
# https://github.com/onnx/onnx/issues/5451#issuecomment-1658439524

op = Net()
op.eval()
# net, name, ver_list, delta=1e-6, rtol=1e-5
kh, kw = 3, 3
input = paddle.rand((8, 1, 28, 28))
offset = paddle.rand((8, 2 * kh * kw, 26, 26))
mask = paddle.rand((8, kh * kw, 26, 26))
weight = paddle.rand((16, 1, kh, kw))
obj = APIOnnx(op, "deform_conv2d", [19])
obj.set_input_data("input_data", input, offset, weight, mask)
obj.run()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest comment out this line, and go ahead on merging this PR.



if __name__ == "__main__":
test_deform_conv2d()