-
Notifications
You must be signed in to change notification settings - Fork 177
add im2sequence op #1239
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
Closed
Closed
add im2sequence op #1239
Changes from all commits
Commits
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,81 @@ | ||
// Copyright (c) 2022 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/im2sequence.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace paddle2onnx | ||
{ | ||
REGISTER_MAPPER(im2sequence, Im2SequenceMapper) | ||
|
||
int32_t Im2SequenceMapper::GetMinOpset(bool verbose) | ||
{ | ||
return 7; | ||
} | ||
|
||
std::vector<int64_t> CalculateNewShape(const std::vector<int64_t> &input_shape, | ||
const std::vector<int64_t> &kernels, | ||
const std::vector<int64_t> &strides, | ||
const std::vector<int64_t> &paddings) | ||
{ | ||
int64_t N = input_shape[0]; | ||
int64_t C = input_shape[1]; | ||
int64_t H = input_shape[2]; | ||
int64_t W = input_shape[3]; | ||
|
||
int64_t kernel_h = kernels[0]; | ||
int64_t kernel_w = kernels[1]; | ||
int64_t stride_h = strides[0]; | ||
int64_t stride_w = strides[1]; | ||
int64_t pad_top = paddings[0]; | ||
int64_t pad_bottom = paddings[1]; | ||
int64_t pad_left = paddings[2]; | ||
int64_t pad_right = paddings[3]; | ||
|
||
int64_t out_h = (H + pad_top + pad_bottom - kernel_h) / stride_h + 1; | ||
int64_t out_w = (W + pad_left + pad_right - kernel_w) / stride_w + 1; | ||
|
||
std::vector<int64_t> new_shape = {N, C, out_h * kernel_h, out_w * kernel_w}; | ||
|
||
return new_shape; | ||
} | ||
|
||
void Im2SequenceMapper::Opset7() | ||
{ | ||
auto input_info = GetInput("X"); | ||
auto output_info = GetOutput("Out"); | ||
|
||
std::vector<int64_t> kernels = kernels_; | ||
std::vector<int64_t> strides = strides_; | ||
std::vector<int64_t> paddings = paddings_; | ||
|
||
std::vector<int64_t> new_shape = CalculateNewShape(input_info[0].shape, kernels_, strides_, paddings_); | ||
std::vector<int64_t> starts = {0, 0}; | ||
std::vector<int64_t> ends = {-1, -1}; | ||
std::vector<int64_t> axes = {2, 3}; | ||
std::vector<int64_t> steps = {1, 1}; | ||
|
||
std::string reshaped_input = helper_->Reshape(input_info[0].name, new_shape); | ||
|
||
std::vector<int64_t> starts_i64(starts.begin(), starts.end()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里重新copy构造的原因是什么? |
||
std::vector<int64_t> ends_i64(ends.begin(), ends.end()); | ||
std::vector<int64_t> axes_i64(axes.begin(), axes.end()); | ||
std::vector<int64_t> steps_i64(steps.begin(), steps.end()); | ||
std::string sliced_input = helper_->Slice(reshaped_input, starts_i64, ends_i64, axes_i64); | ||
helper_->MakeNode("Identity", {sliced_input}, {output_info[0].name}); | ||
} | ||
|
||
} |
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 @@ | ||
// Copyright (c) 2022 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 Im2SequenceMapper : public Mapper | ||
{ | ||
public: | ||
Im2SequenceMapper(const PaddleParser &p, OnnxHelper *helper, int64_t block_id, | ||
int64_t op_id) : Mapper(p, helper, block_id, op_id) | ||
{ | ||
GetAttr("kernels", &kernels_); | ||
GetAttr("out_stride", &out_stride_); | ||
GetAttr("paddings", &paddings_); | ||
GetAttr("strides", &strides_); | ||
} | ||
int32_t GetMinOpset(bool verbose = false); | ||
void Opset7(); | ||
|
||
private: | ||
std::vector<int64_t> kernels_; | ||
std::vector<int64_t> out_stride_; | ||
std::vector<int64_t> paddings_; | ||
std::vector<int64_t> strides_; | ||
}; | ||
} |
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
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.
这些变量似乎都没有用到,可以移除?