-
Notifications
You must be signed in to change notification settings - Fork 189
【Hackathon 6th No.56】Add deform conv2d op #1256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Zheng-Bicheng
merged 11 commits into
PaddlePaddle:develop
from
xiaoyewww:hackathon/deform_conv
May 29, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9a5383d
Add deform conv2d op
xiaoyewww ea7ef0f
Add deform conv2d op
xiaoyewww 48f0a96
Add deform conv2d op
xiaoyewww 377ead8
Add deform conv2d op
xiaoyewww 2a12f5d
Add deform conv2d op
xiaoyewww b86bf5d
Merge remote-tracking branch 'refs/remotes/origin/develop' into hacka…
Zheng-Bicheng 73be338
update deform_conv2d
Zheng-Bicheng 8aada36
Add deform conv2d op
xiaoyewww 0c0208e
Add deform conv2d op
xiaoyewww 9dc0fb1
Add deform conv2d op
xiaoyewww 33685d3
Add deform conv2d op
xiaoyewww File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test_deform_conv2d() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.