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

Fixed a lack of processing of OP name sanitizing when the -onimc and -osd or -oiqt options are specified in combination. #445

Merged
merged 2 commits into from
Aug 8, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ Video speed is adjusted approximately 50 times slower than actual speed.
$ docker run --rm -it \
-v `pwd`:/workdir \
-w /workdir \
ghcr.io/pinto0309/onnx2tf:1.15.8
ghcr.io/pinto0309/onnx2tf:1.15.9

or

# Authentication is not required for pulls from Docker Hub.
$ docker run --rm -it \
-v `pwd`:/workdir \
-w /workdir \
docker.io/pinto0309/onnx2tf:1.15.8
docker.io/pinto0309/onnx2tf:1.15.9

or

Expand Down
17 changes: 17 additions & 0 deletions json_samples/replace_mobilenetmss.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"format_version": 1,
"operations": [
{
"op_name": "/Transpose_1",
"param_target": "attributes",
"param_name": "perm",
"values": [0,1,2,3]
},
{
"op_name": "/Transpose_2",
"param_target": "attributes",
"param_name": "perm",
"values": [0,2,3,1]
}
]
}
2 changes: 1 addition & 1 deletion onnx2tf/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from onnx2tf.onnx2tf import convert, main

__version__ = '1.15.8'
__version__ = '1.15.9'
39 changes: 35 additions & 4 deletions onnx2tf/onnx2tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,18 +667,49 @@ def convert(

def sanitizing(node):
if hasattr(node, 'name'):
node.name = node.name.replace(':','_')
node.name = node.name.replace(':','__')
if hasattr(node, 'outputs'):
for o in node.outputs:
if hasattr(o, 'name'):
o.name = o.name.replace(':','__')
elif hasattr(o, '_name'):
o._name = o._name.replace(':','__')
if output_signaturedefs or output_integer_quantized_tflite:
node.name = re.sub('^/', 'wa/', node.name)
if hasattr(node, 'outputs'):
for o in node.outputs:
if hasattr(o, 'name'):
o.name = re.sub('^/', 'wa/', o.name)
elif hasattr(o, '_name'):
o._name = re.sub('^/', 'wa/', o._name)
elif hasattr(node, '_name'):
node._name = node._name.replace(':','_')
node._name = node._name.replace(':','__')
if hasattr(node, 'outputs'):
for o in node.outputs:
if hasattr(o, 'name'):
o.name = o.name.replace(':','__')
elif hasattr(o, '_name'):
o._name = o._name.replace(':','__')
if output_signaturedefs or output_integer_quantized_tflite:
node._name = re.sub('^/', 'wa/', node._name)
if hasattr(node, 'outputs'):
for o in node.outputs:
if hasattr(o, 'name'):
o.name = re.sub('^/', 'wa/', o.name)
elif hasattr(o, '_name'):
o._name = re.sub('^/', 'wa/', o._name)

# sanitizing ':', '/'
_ = [sanitizing(graph_input) for graph_input in graph.inputs]
_ = [sanitizing(graph_node) for graph_node in graph.nodes]
_ = [sanitizing(graph_output) for graph_output in graph.outputs]
if output_signaturedefs or output_integer_quantized_tflite:
new_output_names = []
for output_name in output_names:
output_name = output_name.replace(':','__')
output_name = re.sub('^/', 'wa/', output_name)
new_output_names.append(output_name)
output_names = new_output_names
try:
onnx_graph = gs.export_onnx(graph)
except Exception as ex:
Expand Down Expand Up @@ -903,7 +934,7 @@ def sanitizing(node):
output_names=output_names,
)
if not outputs:
output_names = [output_name.replace(':','_') for output_name in output_names]
output_names = [output_name.replace(':','__') for output_name in output_names]
if output_signaturedefs or output_integer_quantized_tflite:
output_names = [re.sub('^/', '', output_name) for output_name in output_names]
outputs = get_tf_model_outputs(
Expand All @@ -914,7 +945,7 @@ def sanitizing(node):
# Bring back output names from ONNX model
for output, name in zip(outputs, output_names):
if hasattr(output, 'node'):
output.node.layer._name = name.replace(':','_')
output.node.layer._name = name.replace(':','__')
if output_signaturedefs or output_integer_quantized_tflite:
output.node.layer._name = re.sub('^/', '', output.node.layer._name)

Expand Down
4 changes: 2 additions & 2 deletions onnx2tf/ops/If.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def make_node(
)
sys.exit(1)
# substitution because saved_model does not allow colons
then_branch_graph_node.name = then_branch_graph_node.name.replace(':','_')
then_branch_graph_node.name = then_branch_graph_node.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
then_branch_graph_node.name = re.sub('^/', 'wa/', then_branch_graph_node.name)
Expand Down Expand Up @@ -108,7 +108,7 @@ def make_node(
)
sys.exit(1)
# substitution because saved_model does not allow colons
else_branch_graph_node.name = else_branch_graph_node.name.replace(':','_')
else_branch_graph_node.name = else_branch_graph_node.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
else_branch_graph_node.name = re.sub('^/', 'wa/', else_branch_graph_node.name)
Expand Down
4 changes: 2 additions & 2 deletions onnx2tf/ops/_Loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def run_subgraph(iter_cnt, cond, v, scan_outputs):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_input.name = body_input.name.replace(':','_')
body_input.name = body_input.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_input.name = re.sub('^/', 'wa/', body_input.name)
Expand All @@ -152,7 +152,7 @@ def run_subgraph(iter_cnt, cond, v, scan_outputs):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_node.name = body_node.name.replace(':','_')
body_node.name = body_node.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_node.name = re.sub('^/', 'wa/', body_node.name)
Expand Down
20 changes: 10 additions & 10 deletions onnx2tf/ops/__Loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def make_node(
) if M is not None else tf.constant(tf.int32.max, tf.int32)
M_name = None
if not isinstance(graph_node_input_1, np.ndarray):
graph_node_input_1.name = graph_node_input_1.name.replace(':','_')
graph_node_input_1.name = graph_node_input_1.name.replace(':','__')
M_name = graph_node_input_1.name
else:
M_name = graph_node.inputs[0].name.replace(':','_')
M_name = graph_node.inputs[0].name.replace(':','__')
M_name = f'{M_name}_M'
if kwargs['output_signaturedefs']:
M_name = re.sub('^/', 'wa/', M_name)
Expand All @@ -113,10 +113,10 @@ def make_node(

cond_init_name = None
if not isinstance(graph_node_input_2, np.ndarray):
graph_node_input_2.name = graph_node_input_2.name.replace(':','_')
graph_node_input_2.name = graph_node_input_2.name.replace(':','__')
cond_init_name = graph_node_input_2.name
else:
cond_init_name = graph_node.inputs[0].name.replace(':','_')
cond_init_name = graph_node.inputs[0].name.replace(':','__')
cond_init_name = f'{cond_init_name}_cond_init'
if kwargs['output_signaturedefs']:
cond_init_name = re.sub('^/', 'wa/', cond_init_name)
Expand Down Expand Up @@ -182,7 +182,7 @@ def make_node(
# )
# sys.exit(1)
# # substitution because saved_model does not allow colons
# body_input.name = body_input.name.replace(':','_')
# body_input.name = body_input.name.replace(':','__')
# # Substitution because saved_model does not allow leading slashes in op names
# if kwargs['output_signaturedefs']:
# body_input.name = re.sub('^/', 'wa/', body_input.name)
Expand All @@ -204,7 +204,7 @@ def make_node(
# )
# sys.exit(1)
# # substitution because saved_model does not allow colons
# body_node.name = body_node.name.replace(':','_')
# body_node.name = body_node.name.replace(':','__')
# # Substitution because saved_model does not allow leading slashes in op names
# if kwargs['output_signaturedefs']:
# body_node.name = re.sub('^/', 'wa/', body_node.name)
Expand Down Expand Up @@ -243,7 +243,7 @@ def run_subgraph(iter_cnt, ):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_input.name = body_input.name.replace(':','_')
body_input.name = body_input.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_input.name = re.sub('^/', 'wa/', body_input.name)
Expand All @@ -265,7 +265,7 @@ def run_subgraph(iter_cnt, ):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_node.name = body_node.name.replace(':','_')
body_node.name = body_node.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_node.name = re.sub('^/', 'wa/', body_node.name)
Expand Down Expand Up @@ -309,7 +309,7 @@ def run_subgraph(iter_cnt, ):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_input.name = body_input.name.replace(':','_')
body_input.name = body_input.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_input.name = re.sub('^/', 'wa/', body_input.name)
Expand All @@ -332,7 +332,7 @@ def run_subgraph(iter_cnt, ):
)
sys.exit(1)
# substitution because saved_model does not allow colons
body_node.name = body_node.name.replace(':','_')
body_node.name = body_node.name.replace(':','__')
# Substitution because saved_model does not allow leading slashes in op names
if kwargs['output_signaturedefs']:
body_node.name = re.sub('^/', 'wa/', body_node.name)
Expand Down