Skip to content
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

[PaddleV3] 修复 onnx 中不兼容的自定义层与写法并修复相关模型 #1082

Merged
merged 17 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions test_benchmark/ONNX/SwinTransformer/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import paddle.fluid as fluid
import paddle
import numpy as np
import sys
Expand All @@ -11,11 +10,9 @@
exe = paddle.static.Executor(paddle.CPUPlace())

# test dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)

LN_nums = 0
for i, op in enumerate(prog.block(0).ops):
if op.type in ['feed', 'fetch']:
Expand Down
9 changes: 3 additions & 6 deletions test_benchmark/ONNX/bert6/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -16,11 +15,9 @@
exe = paddle.static.Executor(paddle.CPUPlace())

# test dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)

result = exe.run(prog,
feed={
inputs[0]: input_data["input_ids"],
Expand Down
12 changes: 0 additions & 12 deletions test_benchmark/ONNX/black.list
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
SwinTransformer
bert6
mmdetection_fasterrcnn
mmdetection_fcos
mmdetection_fsaf
mmdetection_retina
mmdetection_ssd
mmdetection_yolov3
mnasnet_a1
mobilefacenet
mobilenet_v2
yolov5s
dataset
tools
output
9 changes: 3 additions & 6 deletions test_benchmark/ONNX/mmdetection_fasterrcnn/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,9 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)

data = np.load(
"../dataset/mmdetection_fasterrcnn/real_img_data_fasterrcnn_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)
Expand Down
9 changes: 3 additions & 6 deletions test_benchmark/ONNX/mmdetection_fcos/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,9 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)

data = np.load("../dataset/mmdetection_fcos/real_img_data_fcos_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/mmdetection_fsaf/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,8 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load("../dataset/mmdetection_fsaf/real_img_data_fsaf_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/mmdetection_retina/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,8 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load("../dataset/mmdetection_retina/real_img_data_retina_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/mmdetection_ssd/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,8 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load("../dataset/mmdetection_ssd/real_img_data_ssd_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/mmdetection_yolov3/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -10,11 +9,8 @@
paddle.enable_static()
exe = paddle.static.Executor(paddle.CPUPlace())
# test Dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load("../dataset/mmdetection_yolov3/real_img_data_yolo_50.npy")
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/mobilefacenet/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import paddle
import paddle.fluid as fluid
import numpy as np
import pickle
import sys
Expand All @@ -11,11 +10,8 @@
exe = paddle.static.Executor(paddle.CPUPlace())

# test dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load('../dataset/mobilefacenet/input.npy')
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
8 changes: 2 additions & 6 deletions test_benchmark/ONNX/yolov5s/pd_infer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import paddle.fluid as fluid
import paddle
import numpy as np
import sys
Expand All @@ -10,11 +9,8 @@
exe = paddle.static.Executor(paddle.CPUPlace())

# test dygraph
[prog, inputs, outputs] = fluid.io.load_inference_model(
dirname="pd_model_dygraph/inference_model/",
executor=exe,
model_filename="model.pdmodel",
params_filename="model.pdiparams")
[prog, inputs, outputs] = paddle.static.load_inference_model(
path_prefix="pd_model_dygraph/inference_model/model", executor=exe)
data = np.load('../dataset/yolov5s/input_0.npy')
result = exe.run(prog, feed={inputs[0]: data}, fetch_list=outputs)

Expand Down
22 changes: 19 additions & 3 deletions x2paddle/decoder/onnx_shape_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,18 @@ def get_attribute(node, attr_name, default_value=None):
return default_value


def filter_dim(dim):
"""
onnx latest version make `unk_` dim instead of `None`
"""
if isinstance(dim, str) and dim.startswith('unk_'):
return None

return dim


def get_dim_from_type_proto(dim):
return getattr(dim, dim.WhichOneof('value')) if type(
return filter_dim(getattr(dim, dim.WhichOneof('value'))) if type(
dim.WhichOneof('value')) == str else None


Expand Down Expand Up @@ -73,7 +83,13 @@ def as_scalar(x):
assert len(x) == 1
return x[0]
elif type(x) == np.ndarray:
return np.asscalar(x)
_x = None
try:
_x = np.asscalar(x)
except:
_x = as_scalar(x.tolist())

return _x
else:
return x

Expand Down Expand Up @@ -508,7 +524,7 @@ def _get_int_values(self, node, broadcast=False):
if len(v.shape) > 1:
new_v = None # ignore value for rank > 1
elif len(v.shape) == 0:
new_v = int(np.asscalar(v))
new_v = int(as_scalar(v))
else:
assert len(v.shape) == 1
new_v = [int(vv) for vv in v]
Expand Down
Loading