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

[Experimental / WIP] Generate a miniscule model for each OP only, retain the results of test inference, and propagate to all OPs. STEP.1,2 #184

Merged
merged 37 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ec7b40e
[Experimental] Generate a miniscule model for each OP only, retain th…
PINTO0309 Feb 11, 2023
d98fba2
cleaning
PINTO0309 Feb 11, 2023
f61bd5d
Consider the case where `tf_partial_model_tensors` is `None`
PINTO0309 Feb 11, 2023
3dfaaae
np.dtype->tf.dtype conversion supported, scalar value supported, np.n…
PINTO0309 Feb 11, 2023
fac625b
error message improve
PINTO0309 Feb 11, 2023
fa06e6b
del
PINTO0309 Feb 11, 2023
0e2bf64
error message improve
PINTO0309 Feb 11, 2023
14f3e5c
Skip inference test for single OP model if input geometry contains None
PINTO0309 Feb 11, 2023
682953b
Merge branch 'main' into imp_pre_pred
PINTO0309 Feb 11, 2023
d235a8f
Merge branch 'main' into imp_pre_pred
PINTO0309 Feb 11, 2023
66e4f15
Merge branch 'main' into imp_pre_pred
PINTO0309 Feb 11, 2023
ec91ca5
Streamline Softmax validation
PINTO0309 Feb 12, 2023
a7aaf8b
Correction of `Reshape` transposition defects and broadcast support f…
PINTO0309 Feb 12, 2023
d27db64
Improvement of reference variables
PINTO0309 Feb 12, 2023
bf4d4d4
Improvement of reference variables
PINTO0309 Feb 12, 2023
5afe782
Supports broadcast of scalar values
PINTO0309 Feb 12, 2023
e824da6
Support for explicit_broadcast
PINTO0309 Feb 12, 2023
5f44c1f
Support for 1D tensors
PINTO0309 Feb 12, 2023
3226469
`Mul`, `Div`, `Mod`
PINTO0309 Feb 12, 2023
9479d65
`tf.dtypes` -> `tf.dtypes.DType`
PINTO0309 Feb 12, 2023
a7a7bca
`Sin`, `Cos`, `Sigmoid`
PINTO0309 Feb 12, 2023
82310ff
Fix input tensors
PINTO0309 Feb 12, 2023
1850978
`Resize`
PINTO0309 Feb 12, 2023
dc91d0e
Bug fix `Softmax`
PINTO0309 Feb 12, 2023
e9d2748
Merge branch 'main' into imp_pre_pred
PINTO0309 Feb 13, 2023
52c1ce6
Merge branch 'main' into imp_pre_pred
PINTO0309 Feb 13, 2023
d515058
`MatMul`, `Transpose`, `Unsqueeze`
PINTO0309 Feb 13, 2023
81b5fa3
Support for undefined dimensions of a scalar
PINTO0309 Feb 13, 2023
531b944
Remove unnecessary codes
PINTO0309 Feb 14, 2023
f0ef4fc
`Conv`
PINTO0309 Feb 14, 2023
c17c947
Address a situation where the test data is None if the first OP of th…
PINTO0309 Feb 14, 2023
f9cf87b
Handling of cases where None is included in the input geometry
PINTO0309 Feb 14, 2023
c1d4a65
`Concat`, `Gemm`, `InstanceNormalization`
PINTO0309 Feb 14, 2023
d2514c0
Addresses the issue of Keras input OP not allowing 1D geometry
PINTO0309 Feb 14, 2023
53ecc93
bug fix
PINTO0309 Feb 14, 2023
ffc1b31
bug fix
PINTO0309 Feb 14, 2023
2748479
bug fix
PINTO0309 Feb 14, 2023
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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.6.7
ghcr.io/pinto0309/onnx2tf:1.7.0

or

Expand Down
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.6.7'
__version__ = '1.7.0'
1 change: 1 addition & 0 deletions onnx2tf/onnx2tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ def convert(

# Define additional parameters
additional_parameters = {
'input_onnx_file_path': input_onnx_file_path if input_onnx_file_path is not None else None,
'onnx_graph': onnx_graph,
'opset': graph.opset,
'batch_size': batch_size,
Expand Down
67 changes: 66 additions & 1 deletion onnx2tf/ops/Add.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
post_process_transpose,
disable_unnecessary_transpose,
shape_unmatched_special_avoidance_workaround,
make_tf_partial_model_inputs,
dummy_tf_inference,
)
from typing import Any, Dict, List


@print_node_info
Expand Down Expand Up @@ -65,12 +68,27 @@ def make_node(
'dtype': dtype,
}

# Generation of TF OP
input_tensor_1 = tf_layers_dict[graph_node_input_1.name]['tf_node'] \
if isinstance(graph_node_input_1, gs.Variable) else graph_node_input_1
input_tensor_2 = tf_layers_dict[graph_node_input_2.name]['tf_node'] \
if isinstance(graph_node_input_2, gs.Variable) else graph_node_input_2

# Acquisition of test data for validation
if not isinstance(graph_node_input_1, np.ndarray) \
and 'verification_data' in tf_layers_dict[graph_node_input_1.name].keys():
test_data1: np.ndarray = tf_layers_dict[graph_node_input_1.name]['verification_data']
elif isinstance(graph_node_input_1, np.ndarray):
test_data1: np.ndarray = graph_node_input_1
else:
test_data1 = None
if not isinstance(graph_node_input_2, np.ndarray) \
and 'verification_data' in tf_layers_dict[graph_node_input_2.name].keys():
test_data2: np.ndarray = tf_layers_dict[graph_node_input_2.name]['verification_data']
elif isinstance(graph_node_input_2, np.ndarray):
test_data2: np.ndarray = graph_node_input_2
else:
test_data2 = None

# Disable unnecessary Transpose
# 1. If both x and y are gs.Variable
# 2. If only one of the two is the output of Transpose
Expand Down Expand Up @@ -122,12 +140,59 @@ def make_node(
**kwargs,
)

# Generate input OPs for TensorFlow subgraphs
# For inference testing on OP stand-alone
tf_partial_model_inputs: List[tf.keras.Input] = \
make_tf_partial_model_inputs(
input_tensors=[
input_tensor_1,
input_tensor_2,
]
)
tf_partial_model_outputs = None

# Generation of TF OP
### Overall model
tf_layers_dict[graph_node_output.name]['tf_node'] = \
tf.math.add(
x=input_tensor_1,
y=input_tensor_2,
name=graph_node.name,
)
### Partial model
if tf_partial_model_inputs is not None:
tf_partial_model_outputs = \
[
tf.math.add(
x=tf_partial_model_inputs[0],
y=tf_partial_model_inputs[1],
)
]
tf_partial_model = tf.keras.Model(
inputs=tf_partial_model_inputs,
outputs=tf_partial_model_outputs,
)
test_data1 = None
if isinstance(input_tensor_1, np.ndarray):
test_data1 = input_tensor_1
test_data2 = None
if isinstance(input_tensor_2, np.ndarray):
test_data2 = input_tensor_2
tf_partial_model_result_infos: Dict[Any] = dummy_tf_inference(
model=tf_partial_model,
inputs=tf_partial_model_inputs,
verification_datas=[
test_data1,
test_data2,
]
)
tf_layers_dict[graph_node_output.name]['verification_data'] = \
list(tf_partial_model_result_infos.values())[0]
del tf_partial_model
del tf_partial_model_inputs
del tf_partial_model_outputs
del test_data1
del test_data2

# Post-process transpose
tf_layers_dict[graph_node_output.name]['tf_node'] = post_process_transpose(
Expand Down
68 changes: 68 additions & 0 deletions onnx2tf/ops/Concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
make_tf_node_info,
pre_process_transpose,
post_process_transpose,
make_tf_partial_model_inputs,
dummy_tf_inference,
)
from typing import Any, Dict, List


@print_node_info
Expand Down Expand Up @@ -136,12 +139,77 @@ def make_node(
value if len(value.shape) > 0 else tf.reshape(value, [1]) for value in values
]

# Generate input OPs for TensorFlow subgraphs
# For inference testing on OP stand-alone
tf_partial_model_inputs: List[tf.keras.Input] = \
make_tf_partial_model_inputs(
input_tensors=values
)
tf_partial_model_outputs = None

# Generation of TF OP
### Overall model
tf_layers_dict[graph_node_output.name]['tf_node'] = \
tf.concat(
values=values,
axis=axis,
name=graph_node.name,
)
### Partial model
if tf_partial_model_inputs is not None:
test_datas = []
for graph_node_input, value in zip(graph_node.inputs, values):
test_data = None
if not isinstance(value, np.ndarray):
if not isinstance(graph_node_input, np.ndarray) \
and 'verification_data' in tf_layers_dict[graph_node_input.name].keys():
test_data: np.ndarray = tf_layers_dict[graph_node_input.name]['verification_data']
elif isinstance(graph_node_input, np.ndarray):
test_data: np.ndarray = graph_node_input
else:
test_data = None
else:
test_data = value
test_datas.append(test_data)
new_test_datas = []
is_expanded = False
for tf_partial_model_input, test_data in zip(tf_partial_model_inputs, test_datas):
if isinstance(test_data, np.ndarray) \
and len(test_data.shape) == 1 \
and len(tf_partial_model_input.shape) == 2:
test_data = np.expand_dims(test_data, 0)
is_expanded = True
new_test_datas.append(test_data)
test_datas = new_test_datas
tf_partial_model_outputs = \
[
tf.concat(
values=tf_partial_model_inputs,
axis=axis,
) if not is_expanded else \
tf.squeeze(
input=tf.concat(
values=tf_partial_model_inputs,
axis=axis if not is_expanded else axis+1,
),
axis=0,
)
]
tf_partial_model = tf.keras.Model(
inputs=tf_partial_model_inputs,
outputs=tf_partial_model_outputs,
)
tf_partial_model_result_infos: Dict[Any] = dummy_tf_inference(
model=tf_partial_model,
inputs=tf_partial_model_inputs,
verification_datas=test_datas
)
tf_layers_dict[graph_node_output.name]['verification_data'] = \
list(tf_partial_model_result_infos.values())[0]
del tf_partial_model
del tf_partial_model_inputs
del tf_partial_model_outputs
del test_data

# Post-process transpose
tf_layers_dict[graph_node_output.name]['tf_node'] = post_process_transpose(
Expand Down
Loading