Skip to content

[ONNX] prelu support slope len(shape) ==1, but shape !=[1] #418

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

Merged
merged 2 commits into from
Oct 14, 2020
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
1 change: 1 addition & 0 deletions x2paddle/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ def onnx2paddle(model_path, save_dir, params_merge=False):
mapper = ONNXOpMapper(model)
print("Model optimizing ...")
optimizer = ONNXOptimizer(mapper)
optimizer.delete_redundance_code()
print("Model optimized.")

print("Paddle model and code generating ...")
Expand Down
13 changes: 10 additions & 3 deletions x2paddle/op_mapper/onnx2paddle/opset9/opset.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,6 @@ def InstanceNormalization(self, node):
def Expand(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True)
val_shape = self.graph.get_input_node(node, idx=1, copy=True)
if len(val_shape.outputs) == 1:
self.omit_nodes.append(val_shape.layer_name)
val_x_dtype = val_x.dtype
name_ones = node.layer_name + '_ones'
attr_ones = {
Expand Down Expand Up @@ -1256,10 +1254,19 @@ def PRelu(self, node):

mode = 'channel'
shape_slope = val_slope.out_shapes[0]
if len(shape_slope) == 1:

if shape_slope == [1]:
mode = 'all'
elif len(shape_slope) > 2:
mode = 'element'

if mode == 'channel' and len(shape_slope) == 1:
# paddle params shape need be [1, channel]
slope_data = _const_weight_or_none(val_slope)
slope_data = np.reshape(slope_data, [1] + shape_slope)
self.weights[val_slope.layer_name] = slope_data

self.omit_nodes.append(val_slope.layer_name)
attr = {
"param_attr": string(val_slope.layer_name),
'mode': string(mode)
Expand Down