Skip to content

Commit

Permalink
[WIP] Parameter replacement. Div,Gemm,Mul,Reshape,Resize,Sub,Transpose
Browse files Browse the repository at this point in the history
  • Loading branch information
PINTO0309 committed Oct 13, 2022
1 parent 7c705f8 commit 23fb286
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 25 deletions.
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Self-Created Tools to convert ONNX files (NCHW) to TensorFlow format (NHWC). The
$ docker run --rm -it \
-v `pwd`:/workdir \
-w /workdir \
ghcr.io/pinto0309/onnx2tf:0.0.25
ghcr.io/pinto0309/onnx2tf:0.0.26
or
Expand Down Expand Up @@ -72,6 +72,7 @@ usage: onnx2tf
[-rlr]
[-rpw]
[-me]
[-prf PARAM_REPLACEMENT_FILE]
[-n]
optional arguments:
Expand Down Expand Up @@ -130,6 +131,9 @@ optional arguments:
(input_tensor - mean) / tf.sqrt(variance + mvn_epsilon)
Default: 0.0000000001
-prf PARAM_REPLACEMENT_FILE, --param_replacement_file PARAM_REPLACEMENT_FILE
Parameter replacement file path. (.json)
-n, --non_verbose
Do not show all information logs. Only error logs are displayed.
```
Expand All @@ -155,6 +159,7 @@ convert(
replace_leakyrelu_to_pseudo_leakyrelu: Union[bool, NoneType] = False,
replace_power_to_pseudo_power: Optional[bool] = False,
mvn_epsilon: Union[float, NoneType] = 0.0000000001,
param_replacement_file: Optional[str] = '',
non_verbose: Union[bool, NoneType] = False
) -> keras.engine.training.Model

Expand Down Expand Up @@ -222,6 +227,9 @@ convert(
(input_tensor - mean) / tf.sqrt(variance + mvn_epsilon)
Default: 0.0000000001

param_replacement_file: Optional[str]
Parameter replacement file path. (.json)

non_verbose: Optional[bool]
Do not show all information logs. Only error logs are displayed.
Only one of replace_argmax_to_reducemax_and_indicies_is_int64 and
Expand All @@ -235,7 +243,7 @@ convert(
```

## [WIP] Parameter replacement
This tool is used to convert `NCW` to `NWC`, `NCHW` to `NHWC`, `NCDHW` to `NDHWC`, `NCDDHW` to `NDDHWC`, `NCDDDDDDHW` to `NDDDDDDHWC`. Therefore, as stated in the Key Concepts, the conversion will inevitably break down at some point in the model. You need to look at the entire conversion log to see which OP transpositions are failing and correct them yourself. I dare to explain very little because I know that no matter how much detail I put in the README, you guys will not read it at all.
This tool is used to convert `NCW` to `NWC`, `NCHW` to `NHWC`, `NCDHW` to `NDHWC`, `NCDDHW` to `NDDHWC`, `NCDDDDDDHW` to `NDDDDDDHWC`. Therefore, as stated in the Key Concepts, the conversion will inevitably break down at some point in the model. You need to look at the entire conversion log to see which OP transpositions are failing and correct them yourself. I dare to explain very little because I know that no matter how much detail I put in the README, you guys will not read it at all. `attribute` or `INPUT constant` or `INPUT Initializer` can be replaced with the specified value.

"A conversion error occurs." Please don't post such low level questions as issues.

Expand All @@ -255,6 +263,12 @@ This tool is used to convert `NCW` to `NWC`, `NCHW` to `NHWC`, `NCDHW` to `NDHWC
"param_target": "attributes", # attributes or inputs
"param_name": "axes",
"values": [2] # Disable parameter transposition or overwrite parameters
},
{
"op_name": "Resize__697",
"param_target": "inputs",
"param_name": "Concat__696:0",
"values": [26,26] # Replacement of unk__x (Resize OP, sizes height/width parameter)
}
]
}
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__ = '0.0.25'
__version__ = '0.0.26'
4 changes: 2 additions & 2 deletions onnx2tf/onnx2tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def convert(
Default: 0.0000000001
param_replacement_file: Optional[str]
Parameter replacement path. (.json)
Parameter replacement file path. (.json)
non_verbose: Optional[bool]
Do not show all information logs. Only error logs are displayed.\n
Expand Down Expand Up @@ -453,7 +453,7 @@ def main():
'--param_replacement_file',
type=str,
default='',
help='Parameter replacement path. (.json)'
help='Parameter replacement file path. (.json)'
)
parser.add_argument(
'-n',
Expand Down
33 changes: 24 additions & 9 deletions onnx2tf/ops/Div.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import tensorflow as tf
import onnx_graphsurgeon as gs
from onnx2tf.utils.common_functions import (
get_replacement_parameter,
replace_parameter,
get_constant_or_variable,
print_node_info,
inverted_operation_enable_disable,
Expand All @@ -14,6 +16,7 @@

@print_node_info
@inverted_operation_enable_disable
@get_replacement_parameter
def make_node(
*,
graph_node: gs.Node,
Expand Down Expand Up @@ -57,6 +60,25 @@ def make_node(
'dtype': dtype,
}

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

# Param replacement
input_tensor_1 = replace_parameter(
value_before_replacement=input_tensor_1,
param_target='inputs',
param_name=graph_node.inputs[0].name,
**kwargs,
)
input_tensor_2 = replace_parameter(
value_before_replacement=input_tensor_2,
param_target='inputs',
param_name=graph_node.inputs[1].name,
**kwargs,
)

# Generation of TF OP
target_cast_dtype = [
np.int8,
Expand All @@ -65,16 +87,9 @@ def make_node(
np.int64,
]

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

divided_tensor = tf.math.divide(
x=tf_layers_dict[graph_node_input_1.name]['tf_node'] \
if isinstance(graph_node_input_1, gs.Variable) else graph_node_input_1,
y=tf_layers_dict[graph_node_input_2.name]['tf_node'] \
if isinstance(graph_node_input_2, gs.Variable) else graph_node_input_2,
x=input_tensor_1,
y=input_tensor_2,
name=graph_node.name,
)

Expand Down
57 changes: 53 additions & 4 deletions onnx2tf/ops/Gemm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import tensorflow as tf
import onnx_graphsurgeon as gs
from onnx2tf.utils.common_functions import (
get_replacement_parameter,
replace_parameter,
get_constant_or_variable,
print_node_info,
inverted_operation_enable_disable,
Expand All @@ -14,6 +16,7 @@

@print_node_info
@inverted_operation_enable_disable
@get_replacement_parameter
def make_node(
*,
graph_node: gs.Node,
Expand Down Expand Up @@ -71,10 +74,8 @@ def make_node(
shape = graph_node_output.shape
dtype = graph_node_output.dtype

if bool(graph_node.attrs.get('transA', 0)):
x = tf.transpose(x)
if bool(graph_node.attrs.get('transB', 0)):
y = tf.transpose(y)
transA = bool(graph_node.attrs.get('transA', 0))
transB = bool(graph_node.attrs.get('transB', 0))
alpha = graph_node.attrs.get('alpha', 1.0)
beta = graph_node.attrs.get('beta', 1.0)

Expand All @@ -85,7 +86,55 @@ def make_node(
'dtype': dtype,
}

# Param replacement
x = replace_parameter(
value_before_replacement=x,
param_target='inputs',
param_name=graph_node.inputs[0].name,
**kwargs,
)
y = replace_parameter(
value_before_replacement=y,
param_target='inputs',
param_name=graph_node.inputs[1].name,
**kwargs,
)
z = replace_parameter(
value_before_replacement=z,
param_target='inputs',
param_name=graph_node.inputs[2].name,
**kwargs,
)
alpha = replace_parameter(
value_before_replacement=alpha,
param_target='attributes',
param_name='alpha',
**kwargs,
)
beta = replace_parameter(
value_before_replacement=beta,
param_target='attributes',
param_name='beta',
**kwargs,
)
transA = replace_parameter(
value_before_replacement=transA,
param_target='attributes',
param_name='transA',
**kwargs,
)
transB = replace_parameter(
value_before_replacement=transB,
param_target='attributes',
param_name='transB',
**kwargs,
)

# Generation of TF OP
if transA == True:
x = tf.transpose(x)
if transB == True:
y = tf.transpose(y)
# We cast to either input or attribute data type to preserve precision
if input_tensor_x_dtype in [tf.float64]:
# cast to input data type
Expand Down
19 changes: 18 additions & 1 deletion onnx2tf/ops/Mul.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import tensorflow as tf
import onnx_graphsurgeon as gs
from onnx2tf.utils.common_functions import (
get_replacement_parameter,
replace_parameter,
get_constant_or_variable,
print_node_info,
inverted_operation_enable_disable,
Expand All @@ -14,6 +16,7 @@

@print_node_info
@inverted_operation_enable_disable
@get_replacement_parameter
def make_node(
*,
graph_node: gs.Node,
Expand Down Expand Up @@ -57,12 +60,26 @@ 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

# Param replacement
input_tensor_1 = replace_parameter(
value_before_replacement=input_tensor_1,
param_target='inputs',
param_name=graph_node.inputs[0].name,
**kwargs,
)
input_tensor_2 = replace_parameter(
value_before_replacement=input_tensor_2,
param_target='inputs',
param_name=graph_node.inputs[1].name,
**kwargs,
)

# Generation of TF OP
tf_layers_dict[graph_node_output.name]['tf_node'] = \
tf.math.multiply(
x=input_tensor_1,
Expand Down
17 changes: 17 additions & 0 deletions onnx2tf/ops/Reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import tensorflow as tf
import onnx_graphsurgeon as gs
from onnx2tf.utils.common_functions import (
get_replacement_parameter,
replace_parameter,
get_constant_or_variable,
convert_axis,
print_node_info,
Expand All @@ -16,6 +18,7 @@

@print_node_info
@inverted_operation_enable_disable
@get_replacement_parameter
def make_node(
*,
graph_node: gs.Node,
Expand Down Expand Up @@ -106,6 +109,20 @@ def make_node(
else:
transposed_reshape_shape = reshape_shape

# Param replacement
transposed_tensor = replace_parameter(
value_before_replacement=transposed_tensor,
param_target='inputs',
param_name=graph_node.inputs[0].name,
**kwargs,
)
transposed_reshape_shape = replace_parameter(
value_before_replacement=transposed_reshape_shape,
param_target='inputs',
param_name=graph_node.inputs[1].name,
**kwargs,
)

# Reshape
tf_layers_dict[graph_node_output.name]['tf_node'] = \
tf.reshape(
Expand Down
49 changes: 48 additions & 1 deletion onnx2tf/ops/Resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from tensorflow.keras.layers import Lambda # type: ignore
import onnx_graphsurgeon as gs
from onnx2tf.utils.common_functions import (
get_replacement_parameter,
replace_parameter,
get_constant_or_variable,
print_node_info,
inverted_operation_enable_disable,
Expand All @@ -15,6 +17,7 @@

@print_node_info
@inverted_operation_enable_disable
@get_replacement_parameter
def make_node(
*,
graph_node: gs.Node,
Expand Down Expand Up @@ -84,7 +87,6 @@ def make_node(
}



def upsampling2d_bilinear(input_tensor, new_size, align_corners, half_pixel_centers, name):
return tf.compat.v1.image.resize_bilinear(
images=input_tensor,
Expand Down Expand Up @@ -151,6 +153,51 @@ def upsampling2d_nearest(input_tensor, new_size, align_corners, half_pixel_cente
new_values[new_idx] = graph_node_output.shape[idx]
new_size = new_values[-3:-1]

# Param replacement
input_tensor = replace_parameter(
value_before_replacement=input_tensor,
param_target='inputs',
param_name=graph_node.inputs[0].name,
**kwargs,
)
roi = replace_parameter(
value_before_replacement=roi,
param_target='inputs',
param_name=graph_node.inputs[1].name,
**kwargs,
)
scales = replace_parameter(
value_before_replacement=scales,
param_target='inputs',
param_name=graph_node.inputs[2].name,
**kwargs,
)
new_size = replace_parameter(
value_before_replacement=new_size,
param_target='inputs',
param_name=graph_node.inputs[3].name,
**kwargs,
)

coordinate_transformation_mode = replace_parameter(
value_before_replacement=coordinate_transformation_mode,
param_target='attributes',
param_name='coordinate_transformation_mode',
**kwargs,
)
extrapolation_value = replace_parameter(
value_before_replacement=extrapolation_value,
param_target='attributes',
param_name='extrapolation_value',
**kwargs,
)
mode = replace_parameter(
value_before_replacement=mode,
param_target='attributes',
param_name='mode',
**kwargs,
)

# TODO: upsampling2d_bilinear_5d
# TODO: upsampling2d_nearest_5d
resized_tensor = None
Expand Down
Loading

0 comments on commit 23fb286

Please sign in to comment.