【PaddlePaddle Hackathon 4】add paddle grid_sampler op (#15869)
* update op linspace * rewrite function name * add paddle grid_sample op mapping * add op_fuzzy * adjust op logic * adjust op name * format code * add tests * adjust test * adjust test * adjust test * adjust test * format code --------- Co-authored-by: Xiuchuan Zhai <xiuchuan.zhai@intel.com>
This commit is contained in:
1 parent
ae1245ae92
commit
306df51001
4 files changed
+93
No files matched your search
@@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2018-2023 Intel Corporation
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
|
||||
#include "default_opset.hpp"
|
||||
#include "openvino/frontend/paddle/node_context.hpp"
|
||||
|
||||
namespace ov {
|
||||
namespace frontend {
|
||||
namespace paddle {
|
||||
namespace op {
|
||||
NamedOutputs grid_sampler(const NodeContext& node) {
|
||||
auto data = node.get_input("X");
|
||||
auto grid = node.get_input("Grid");
|
||||
default_opset::GridSample::Attributes attributes{};
|
||||
|
||||
attributes.align_corners = node.get_attribute<bool>("align_corners", 1);
|
||||
attributes.mode = ov::EnumNames<default_opset::GridSample::InterpolationMode>::as_enum(
|
||||
node.get_attribute<std::string>("mode", "bilinear"));
|
||||
attributes.padding_mode = ov::EnumNames<default_opset::GridSample::PaddingMode>::as_enum(
|
||||
node.get_attribute<std::string>("padding_mode", "zeros"));
|
||||
|
||||
return node.default_single_output_mapping({std::make_shared<default_opset::GridSample>(data, grid, attributes)},
|
||||
{"Output"});
|
||||
}
|
||||
} // namespace op
|
||||
} // namespace paddle
|
||||
} // namespace frontend
|
||||
} // namespace ov
|
||||
@@ -50,6 +50,7 @@ OP_CONVERTER(gather);
|
||||
OP_CONVERTER(gather_nd);
|
||||
OP_CONVERTER(gelu);
|
||||
OP_CONVERTER(greater_than);
|
||||
OP_CONVERTER(grid_sampler);
|
||||
OP_CONVERTER(group_norm);
|
||||
OP_CONVERTER(hard_sigmoid);
|
||||
OP_CONVERTER(hard_swish);
|
||||
@@ -163,6 +164,7 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
|
||||
{"generate_proposals_v2", op::generate_proposals_v2},
|
||||
{"greater_equal", op::elementwise_greater_equal},
|
||||
{"greater_than", op::greater_than},
|
||||
{"grid_sampler", op::grid_sampler},
|
||||
{"group_norm", op::group_norm},
|
||||
{"hard_sigmoid", op::hard_sigmoid},
|
||||
{"hard_swish", op::hard_swish},
|
||||
|
||||
@@ -235,6 +235,9 @@ static const std::vector<std::string> models{
|
||||
std::string("greater_than_float32"),
|
||||
std::string("greater_than_int32"),
|
||||
std::string("greater_than_int64"),
|
||||
std::string("grid_sampler_1"),
|
||||
std::string("grid_sampler_2"),
|
||||
std::string("grid_sampler_dyn"),
|
||||
std::string("group_norm_1/group_norm_1.pdmodel"),
|
||||
std::string("group_norm_2/group_norm_2.pdmodel"),
|
||||
std::string("group_norm_3/group_norm_3.pdmodel"),
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
# Copyright (C) 2018-2023 Intel Corporation
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#
|
||||
# grid_sampler paddle model generator
|
||||
#
|
||||
import paddle
|
||||
import numpy as np
|
||||
from save_model import saveModel
|
||||
import sys
|
||||
|
||||
|
||||
def grid_sampler(name: str, x, grid, mode="bilinear", padding_mode="zeros", align_corners=True, not_empty=True,
|
||||
is_dynamic=False):
|
||||
paddle.enable_static()
|
||||
|
||||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
|
||||
if is_dynamic:
|
||||
x_node = paddle.static.data(name="x", shape=(-1, -1, -1, -1), dtype=x.dtype)
|
||||
grid_node = paddle.static.data(name="grid", shape=(-1, -1, -1, 2), dtype=grid.dtype)
|
||||
else:
|
||||
x_node = paddle.static.data(name="x", shape=x.shape, dtype=x.dtype)
|
||||
grid_node = paddle.static.data(name="grid", shape=grid.shape, dtype=grid.dtype)
|
||||
out = paddle.nn.functional.grid_sample(x_node, grid_node, mode=mode, padding_mode=padding_mode,
|
||||
align_corners=align_corners) if not_empty else paddle.nn.functional.grid_sample(
|
||||
x_node, grid_node)
|
||||
place = paddle.CPUPlace()
|
||||
exe = paddle.static.Executor(place)
|
||||
exe.run(paddle.static.default_startup_program())
|
||||
outs = exe.run(feed={"x": x, "grid": grid}, fetch_list=[out])
|
||||
saveModel(name, exe, feedkeys=['x', 'grid'], fetchlist=[out], inputs=[x, grid], outputs=[outs[0]],
|
||||
target_dir=sys.argv[1])
|
||||
|
||||
return outs[0]
|
||||
|
||||
|
||||
def main():
|
||||
dtype = np.float32
|
||||
x = np.random.randn(2, 2, 3, 3).astype(dtype)
|
||||
grid = np.random.uniform(-1, 1, [2, 3, 3, 2]).astype(dtype)
|
||||
grid_sampler(name='grid_sampler_1', x=x, grid=grid, not_empty=False)
|
||||
|
||||
x = np.random.randn(2, 3, 5, 6).astype(dtype)
|
||||
grid = np.random.uniform(-1, 1, [2, 8, 9, 2]).astype(dtype)
|
||||
mode = "nearest"
|
||||
padding_mode = "reflection"
|
||||
align_corners = False
|
||||
grid_sampler(name='grid_sampler_2', x=x, grid=grid, mode=mode, padding_mode=padding_mode,
|
||||
align_corners=align_corners)
|
||||
|
||||
x = np.random.randn(2, 3, 128, 128).astype(dtype)
|
||||
grid = np.random.uniform(-1, 1, [2, 130, 130, 2]).astype(dtype)
|
||||
padding_mode = "border"
|
||||
grid_sampler(name='grid_sampler_dyn', x=x, grid=grid, mode=mode, padding_mode=padding_mode,
|
||||
align_corners=align_corners, is_dynamic=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user