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:
@@ -297,6 +297,7 @@ static const std::vector<std::string> models{std::string("argmax"),
|
|||||||
std::string("split_test_dim_int64"),
|
std::string("split_test_dim_int64"),
|
||||||
std::string("split_test_list"),
|
std::string("split_test_list"),
|
||||||
std::string("split_test_list_tensor"),
|
std::string("split_test_list_tensor"),
|
||||||
|
std::string("sqrt_float32"),
|
||||||
std::string("squeeze"),
|
std::string("squeeze"),
|
||||||
std::string("squeeze_null_axes"),
|
std::string("squeeze_null_axes"),
|
||||||
std::string("stack_test_float32"),
|
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_input2_3"),
|
||||||
std::string("strided_slice_input3_1"),
|
std::string("strided_slice_input3_1"),
|
||||||
std::string("strided_slice_input3_2"),
|
std::string("strided_slice_input3_2"),
|
||||||
|
std::string("swish_default_params"),
|
||||||
|
std::string("swish_beta"),
|
||||||
std::string("tanh"),
|
std::string("tanh"),
|
||||||
std::string("trilinear_downsample_false_0"),
|
std::string("trilinear_downsample_false_0"),
|
||||||
std::string("trilinear_downsample_false_1"),
|
std::string("trilinear_downsample_false_1"),
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from save_model import saveModel
|
from save_model import saveModel
|
||||||
import paddle
|
import paddle
|
||||||
|
import ops
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
@@ -34,7 +35,7 @@ def make_rois(batch_size, width, height, pooled_width, pooled_height, spatial_sc
|
|||||||
return rois, rois_num
|
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()
|
paddle.enable_static()
|
||||||
|
|
||||||
with paddle.static.program_guard(paddle.static.Program(), paddle.static.Program()):
|
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)
|
name='rois', shape=rois_data.shape, dtype=rois_data.dtype)
|
||||||
rois_num = paddle.static.data(
|
rois_num = paddle.static.data(
|
||||||
name='rois_num', shape=rois_num_data.shape, dtype=rois_num_data.dtype)
|
name='rois_num', shape=rois_num_data.shape, dtype=rois_num_data.dtype)
|
||||||
# TODO: 'aligned' attribute is not supported by Paddle 2.1
|
out = ops.roi_align(input=x,
|
||||||
out = paddle.fluid.layers.roi_align(input=x,
|
rois=rois,
|
||||||
rois=rois,
|
output_size=(pooled_height, pooled_width),
|
||||||
pooled_height=pooled_height,
|
spatial_scale=spatial_scale,
|
||||||
pooled_width=pooled_width,
|
sampling_ratio=sampling_ratio,
|
||||||
spatial_scale=spatial_scale,
|
rois_num=rois_num,
|
||||||
sampling_ratio=sampling_ratio,
|
aligned=aligned)
|
||||||
rois_num=rois_num)
|
|
||||||
|
|
||||||
cpu = paddle.static.cpu_places(1)
|
cpu = paddle.static.cpu_places(1)
|
||||||
exe = paddle.static.Executor(cpu[0])
|
exe = paddle.static.Executor(cpu[0])
|
||||||
@@ -81,13 +81,14 @@ def main():
|
|||||||
pooled_height = 2
|
pooled_height = 2
|
||||||
pooled_width = 2
|
pooled_width = 2
|
||||||
sampling_ratio = -1
|
sampling_ratio = -1
|
||||||
|
aligned = False
|
||||||
|
|
||||||
roi_per_batch = 1
|
roi_per_batch = 1
|
||||||
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
|
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
|
||||||
pooled_height, spatial_scale, roi_per_batch)
|
pooled_height, spatial_scale, roi_per_batch)
|
||||||
|
|
||||||
roi_align("roi_align_test", x, rois, rois_num, pooled_height,
|
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
|
batch_size = 1
|
||||||
channels = 3
|
channels = 3
|
||||||
@@ -101,13 +102,14 @@ def main():
|
|||||||
pooled_height = 2
|
pooled_height = 2
|
||||||
pooled_width = 2
|
pooled_width = 2
|
||||||
sampling_ratio = 2
|
sampling_ratio = 2
|
||||||
|
aligned = True
|
||||||
|
|
||||||
roi_per_batch = 2
|
roi_per_batch = 2
|
||||||
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
|
rois, rois_num = make_rois(batch_size, width, height, pooled_width,
|
||||||
pooled_height, spatial_scale, roi_per_batch)
|
pooled_height, spatial_scale, roi_per_batch)
|
||||||
|
|
||||||
roi_align("roi_align_test2", x, rois, rois_num, pooled_height,
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -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()
|
||||||
@@ -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()
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "openvino/opsets/opset8.hpp"
|
#include "openvino/opsets/opset9.hpp"
|
||||||
|
|
||||||
namespace ov {
|
namespace ov {
|
||||||
namespace frontend {
|
namespace frontend {
|
||||||
namespace paddle {
|
namespace paddle {
|
||||||
namespace op {
|
namespace op {
|
||||||
namespace default_opset = ov::opset8;
|
namespace default_opset = ov::opset9;
|
||||||
|
|
||||||
} // namespace op
|
} // namespace op
|
||||||
} // namespace paddle
|
} // namespace paddle
|
||||||
|
|||||||
@@ -9,12 +9,19 @@ namespace ov {
|
|||||||
namespace frontend {
|
namespace frontend {
|
||||||
namespace paddle {
|
namespace paddle {
|
||||||
namespace op {
|
namespace op {
|
||||||
|
using AlignedMode = default_opset::ROIAlign::AlignedMode;
|
||||||
|
using PoolingMode = default_opset::ROIAlign::PoolingMode;
|
||||||
NamedOutputs roi_align(const NodeContext& node) {
|
NamedOutputs roi_align(const NodeContext& node) {
|
||||||
const auto data_node = node.get_input("X");
|
const auto data_node = node.get_input("X");
|
||||||
const auto roi_node = node.get_input("ROIs");
|
const auto roi_node = node.get_input("ROIs");
|
||||||
// TODO: support 'aligned' feature #82319
|
|
||||||
const auto aligned = node.get_attribute("aligned", false);
|
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
|
// TODO: support multiple batches #83232
|
||||||
if (data_node.get_partial_shape().rank().is_static() && data_node.get_partial_shape()[0].is_static())
|
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);
|
auto sampling_ratio = node.get_attribute<int>("sampling_ratio", -1);
|
||||||
sampling_ratio = (sampling_ratio <= 0) ? 0 : sampling_ratio;
|
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,
|
return node.default_single_output_mapping({std::make_shared<default_opset::ROIAlign>(data_node,
|
||||||
roi_node,
|
roi_node,
|
||||||
fake_roisNum_node,
|
fake_roisNum_node,
|
||||||
@@ -43,7 +49,8 @@ NamedOutputs roi_align(const NodeContext& node) {
|
|||||||
pooled_w,
|
pooled_w,
|
||||||
sampling_ratio,
|
sampling_ratio,
|
||||||
spatial_scale,
|
spatial_scale,
|
||||||
"avg")},
|
pooling_mode,
|
||||||
|
aligned_mode)},
|
||||||
{"Out"});
|
{"Out"});
|
||||||
}
|
}
|
||||||
} // namespace op
|
} // namespace op
|
||||||
|
|||||||
21
src/frontends/paddle/src/op/sqrt.cpp
Normal file
21
src/frontends/paddle/src/op/sqrt.cpp
Normal 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
|
||||||
23
src/frontends/paddle/src/op/swish.cpp
Normal file
23
src/frontends/paddle/src/op/swish.cpp
Normal 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
|
||||||
@@ -81,9 +81,11 @@ OP_CONVERTER(softmax);
|
|||||||
OP_CONVERTER(softplus);
|
OP_CONVERTER(softplus);
|
||||||
OP_CONVERTER(sigmoid);
|
OP_CONVERTER(sigmoid);
|
||||||
OP_CONVERTER(split);
|
OP_CONVERTER(split);
|
||||||
|
OP_CONVERTER(sqrt);
|
||||||
OP_CONVERTER(squeeze);
|
OP_CONVERTER(squeeze);
|
||||||
OP_CONVERTER(stack);
|
OP_CONVERTER(stack);
|
||||||
OP_CONVERTER(strided_slice);
|
OP_CONVERTER(strided_slice);
|
||||||
|
OP_CONVERTER(swish);
|
||||||
OP_CONVERTER(tanh);
|
OP_CONVERTER(tanh);
|
||||||
OP_CONVERTER(transpose2);
|
OP_CONVERTER(transpose2);
|
||||||
OP_CONVERTER(trilinear_interp_v2);
|
OP_CONVERTER(trilinear_interp_v2);
|
||||||
@@ -173,9 +175,11 @@ std::map<std::string, CreatorFunction> get_supported_ops() {
|
|||||||
{"softplus", op::softplus},
|
{"softplus", op::softplus},
|
||||||
{"sigmoid", op::sigmoid},
|
{"sigmoid", op::sigmoid},
|
||||||
{"split", op::split},
|
{"split", op::split},
|
||||||
|
{"sqrt", op::sqrt},
|
||||||
{"squeeze2", op::squeeze},
|
{"squeeze2", op::squeeze},
|
||||||
{"stack", op::stack},
|
{"stack", op::stack},
|
||||||
{"strided_slice", op::strided_slice},
|
{"strided_slice", op::strided_slice},
|
||||||
|
{"swish", op::swish},
|
||||||
{"sync_batch_norm", op::batch_norm},
|
{"sync_batch_norm", op::batch_norm},
|
||||||
{"tanh", op::tanh},
|
{"tanh", op::tanh},
|
||||||
{"transpose2", op::transpose2},
|
{"transpose2", op::transpose2},
|
||||||
|
|||||||
Reference in New Issue
Block a user