Paddle Frontend Op conversion: ROIAlign9,Sqrt,Swish (#11661)

* Paddle Frontend Op conversion: ROIAlign9,Sqrt,Swish

* modify import ppdet way based on the latest master branch
This commit is contained in:
Bo Liu
2022-05-26 08:38:31 +08:00
committed by GitHub
parent dd930fdb6e
commit 1cce278fcb
9 changed files with 163 additions and 17 deletions

View File

@@ -297,6 +297,7 @@ static const std::vector<std::string> models{std::string("argmax"),
std::string("split_test_dim_int64"),
std::string("split_test_list"),
std::string("split_test_list_tensor"),
std::string("sqrt_float32"),
std::string("squeeze"),
std::string("squeeze_null_axes"),
std::string("stack_test_float32"),
@@ -312,6 +313,8 @@ static const std::vector<std::string> models{std::string("argmax"),
std::string("strided_slice_input2_3"),
std::string("strided_slice_input3_1"),
std::string("strided_slice_input3_2"),
std::string("swish_default_params"),
std::string("swish_beta"),
std::string("tanh"),
std::string("trilinear_downsample_false_0"),
std::string("trilinear_downsample_false_1"),

View File

@@ -7,6 +7,7 @@
import numpy as np
from save_model import saveModel
import paddle
import ops
import sys
@@ -34,7 +35,7 @@ def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_sc
return rois, rois_num
def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio):
def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled_width, spatial_scale, sampling_ratio, aligned):
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
@@ -44,14 +45,13 @@ def roi_align(name: str, x_data, rois_data, rois_num_data, pooled_height, pooled
name='rois', shape=rois_data.shape, dtype=rois_data.dtype)
rois_num = paddle.static.data(
name='rois_num', shape=rois_num_data.shape, dtype=rois_num_data.dtype)
# TODO: 'aligned' attribute is not supported by Paddle 2.1
out = paddle.fluid.layers.roi_align(input=x,
rois=rois,
pooled_height=pooled_height,
pooled_width=pooled_width,
spatial_scale=spatial_scale,
sampling_ratio=sampling_ratio,
rois_num=rois_num)
out = ops.roi_align(input=x,
rois=rois,
output_size=(pooled_height, pooled_width),
spatial_scale=spatial_scale,
sampling_ratio=sampling_ratio,
rois_num=rois_num,
aligned=aligned)
cpu = paddle.static.cpu_places(1)
exe = paddle.static.Executor(cpu[0])
@@ -81,13 +81,14 @@ def main():
pooled_height = 2
pooled_width = 2
sampling_ratio = -1
aligned = False
roi_per_batch = 1
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
pooled_height, spatial_scale, roi_per_batch)
roi_align("roi_align_test", x, rois, rois_num, pooled_height,
pooled_width, spatial_scale, sampling_ratio)
pooled_width, spatial_scale, sampling_ratio, aligned)
batch_size = 1
channels = 3
@@ -101,13 +102,14 @@ def main():
pooled_height = 2
pooled_width = 2
sampling_ratio = 2
aligned = True
roi_per_batch = 2
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
pooled_height, spatial_scale, roi_per_batch)
roi_align("roi_align_test2", x, rois, rois_num, pooled_height,
pooled_width, spatial_scale, sampling_ratio)
pooled_width, spatial_scale, sampling_ratio, aligned)
if __name__ == "__main__":

View File

@@ -0,0 +1,41 @@
#
# sqrt paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd
import sys
def sqrt(name: str, x, data_type):
pdpd.enable_static()
with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
node_x = pdpd.static.data(
name='input_x', shape=x.shape, dtype=data_type)
out = pdpd.sqrt(x=node_x, name='sqrt')
cpu = pdpd.static.cpu_places(1)
exe = pdpd.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(pdpd.static.default_startup_program())
outs = exe.run(
feed={'input_x': x},
fetch_list=[out])
saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out],
inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])
return outs[0]
def main():
data_type = 'float32'
x = np.array([0.1, 0.2, 0.3, 0.4]).astype(data_type)
sqrt("sqrt_float32", x, data_type)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,45 @@
#
# swish paddle model generator
#
import numpy as np
from save_model import saveModel
import paddle as pdpd
import sys
def swish(name: str, x, data_type, input_beta):
pdpd.enable_static()
with pdpd.static.program_guard(pdpd.static.Program(), pdpd.static.Program()):
node_x = pdpd.static.data(
name='input_x', shape=x.shape, dtype=data_type)
out = pdpd.fluid.layers.swish(x=node_x, beta=input_beta, name='swish')
cpu = pdpd.static.cpu_places(1)
exe = pdpd.static.Executor(cpu[0])
# startup program will call initializer to initialize the parameters.
exe.run(pdpd.static.default_startup_program())
outs = exe.run(
feed={'input_x': x},
fetch_list=[out])
saveModel(name, exe, feedkeys=['input_x'], fetchlist=[out],
inputs=[x], outputs=[outs[0]], target_dir=sys.argv[1])
return outs[0]
def main():
data_type = 'float32'
input_beta = 1.0
x = np.random.randn(2, 3).astype(data_type)
swish("swish_default_params", x, data_type, input_beta)
input_beta = 2.0
x = np.random.randn(2, 3).astype(data_type)
swish("swish_beta", x, data_type, input_beta)
if __name__ == "__main__":
main()

View File

@@ -2,13 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "openvino/opsets/opset8.hpp"
#include "openvino/opsets/opset9.hpp"
namespace ov {
namespace frontend {
namespace paddle {
namespace op {
namespace default_opset = ov::opset8;
namespace default_opset = ov::opset9;
} // namespace op
} // namespace paddle

View File

@@ -9,12 +9,19 @@ namespace ov {
namespace frontend {
namespace paddle {
namespace op {
using AlignedMode = default_opset::ROIAlign::AlignedMode;
using PoolingMode = default_opset::ROIAlign::PoolingMode;
NamedOutputs roi_align(const NodeContext& node) {
const auto data_node = node.get_input("X");
const auto roi_node = node.get_input("ROIs");
// TODO: support 'aligned' feature #82319
const auto aligned = node.get_attribute("aligned", false);
PADDLE_OP_CHECK(node, !aligned, "OpenVINO not support 'aligned' feature!");
// Paddle only use 'avg' interpolation mode
const auto pooling_mode = PoolingMode::AVG;
AlignedMode aligned_mode;
if (aligned)
aligned_mode = AlignedMode::HALF_PIXEL_FOR_NN;
else
aligned_mode = AlignedMode::ASYMMETRIC;
// TODO: support multiple batches #83232
if (data_node.get_partial_shape().rank().is_static() && data_node.get_partial_shape()[0].is_static())
@@ -35,7 +42,6 @@ NamedOutputs roi_align(const NodeContext& node) {
auto sampling_ratio = node.get_attribute<int>("sampling_ratio", -1);
sampling_ratio = (sampling_ratio <= 0) ? 0 : sampling_ratio;
// Paddle only use 'avg' interpolation mode
return node.default_single_output_mapping({std::make_shared<default_opset::ROIAlign>(data_node,
roi_node,
fake_roisNum_node,
@@ -43,7 +49,8 @@ NamedOutputs roi_align(const NodeContext& node) {
pooled_w,
sampling_ratio,
spatial_scale,
"avg")},
pooling_mode,
aligned_mode)},
{"Out"});
}
} // namespace op

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2018-2022 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 sqrt(const NodeContext& node) {
const auto x = node.get_input("X");
return node.default_single_output_mapping({std::make_shared<default_opset::Sqrt>(x)}, {"Out"});
}
} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2018-2022 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 swish(const NodeContext& node) {
const auto x = node.get_input("X");
const float beta = node.get_attribute<float>("beta", 1.0f);
const auto beta_node = default_opset::Constant::create(element::f32, Shape{}, {beta});
return node.default_single_output_mapping({std::make_shared<default_opset::Swish>(x, beta_node)}, {"Out"});
}
} // namespace op
} // namespace paddle
} // namespace frontend
} // namespace ov

View File

@@ -81,9 +81,11 @@ OP_CONVERTER(softmax);
OP_CONVERTER(softplus);
OP_CONVERTER(sigmoid);
OP_CONVERTER(split);
OP_CONVERTER(sqrt);
OP_CONVERTER(squeeze);
OP_CONVERTER(stack);
OP_CONVERTER(strided_slice);
OP_CONVERTER(swish);
OP_CONVERTER(tanh);
OP_CONVERTER(transpose2);
OP_CONVERTER(trilinear_interp_v2);
@@ -173,9 +175,11 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
{"softplus", op::softplus},
{"sigmoid", op::sigmoid},
{"split", op::split},
{"sqrt", op::sqrt},
{"squeeze2", op::squeeze},
{"stack", op::stack},
{"strided_slice", op::strided_slice},
{"swish", op::swish},
{"sync_batch_norm", op::batch_norm},
{"tanh", op::tanh},
{"transpose2", op::transpose2},