onnx2tf 1.29.5__py3-none-any.whl → 1.29.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- onnx2tf/__init__.py +1 -1
- onnx2tf/ops/AffineGrid.py +187 -0
- onnx2tf/ops/Attention.py +612 -0
- onnx2tf/ops/BlackmanWindow.py +115 -0
- onnx2tf/ops/GridSample.py +466 -369
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/METADATA +6 -6
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/RECORD +11 -8
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/WHEEL +1 -1
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/licenses/LICENSE +0 -0
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/licenses/LICENSE_onnx-tensorflow +0 -0
- {onnx2tf-1.29.5.dist-info → onnx2tf-1.29.7.dist-info}/top_level.txt +0 -0
onnx2tf/__init__.py
CHANGED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import random
|
|
2
|
+
random.seed(0)
|
|
3
|
+
import numpy as np
|
|
4
|
+
np.random.seed(0)
|
|
5
|
+
import tensorflow as tf
|
|
6
|
+
import onnx_graphsurgeon as gs
|
|
7
|
+
from onnx2tf.utils.common_functions import (
|
|
8
|
+
replace_parameter,
|
|
9
|
+
get_constant_or_variable,
|
|
10
|
+
print_node_info,
|
|
11
|
+
inverted_operation_enable_disable,
|
|
12
|
+
make_tf_node_info,
|
|
13
|
+
get_replacement_parameter,
|
|
14
|
+
pre_process_transpose,
|
|
15
|
+
post_process_transpose,
|
|
16
|
+
)
|
|
17
|
+
from typing import Any
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _make_coords(
|
|
21
|
+
size_dim: Any,
|
|
22
|
+
align_corners: bool,
|
|
23
|
+
dtype: Any,
|
|
24
|
+
) -> Any:
|
|
25
|
+
size_dim = tf.cast(size_dim, tf.int32)
|
|
26
|
+
size_f = tf.cast(size_dim, dtype)
|
|
27
|
+
|
|
28
|
+
if align_corners:
|
|
29
|
+
denom = size_f - tf.constant(1.0, dtype=dtype)
|
|
30
|
+
step = tf.where(
|
|
31
|
+
condition=size_dim > 1,
|
|
32
|
+
x=tf.constant(2.0, dtype=dtype) / denom,
|
|
33
|
+
y=tf.constant(0.0, dtype=dtype),
|
|
34
|
+
)
|
|
35
|
+
start = tf.constant(-1.0, dtype=dtype)
|
|
36
|
+
else:
|
|
37
|
+
step = tf.constant(2.0, dtype=dtype) / size_f
|
|
38
|
+
start = tf.constant(-1.0, dtype=dtype) + step / tf.constant(2.0, dtype=dtype)
|
|
39
|
+
|
|
40
|
+
return start + tf.range(size_dim, dtype=dtype) * step
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@print_node_info
|
|
44
|
+
@inverted_operation_enable_disable
|
|
45
|
+
@get_replacement_parameter
|
|
46
|
+
def make_node(
|
|
47
|
+
*,
|
|
48
|
+
graph_node: gs.Node,
|
|
49
|
+
tf_layers_dict: dict,
|
|
50
|
+
**kwargs: dict,
|
|
51
|
+
):
|
|
52
|
+
"""AffineGrid
|
|
53
|
+
|
|
54
|
+
Parameters
|
|
55
|
+
----------
|
|
56
|
+
graph_node: gs.Node
|
|
57
|
+
graph_surgeon Node
|
|
58
|
+
|
|
59
|
+
tf_layers_dict: dict
|
|
60
|
+
optype, shape, dtype, tensorflow graph
|
|
61
|
+
"""
|
|
62
|
+
before_op_output_shape_trans_1 = \
|
|
63
|
+
tf_layers_dict.get(graph_node.inputs[0].name, {}).get('before_op_output_shape_trans', True)
|
|
64
|
+
before_op_output_shape_trans_2 = \
|
|
65
|
+
tf_layers_dict.get(graph_node.inputs[1].name, {}).get('before_op_output_shape_trans', True)
|
|
66
|
+
before_op_output_shape_trans = \
|
|
67
|
+
before_op_output_shape_trans_1 \
|
|
68
|
+
and before_op_output_shape_trans_2
|
|
69
|
+
|
|
70
|
+
graph_node_input_theta = get_constant_or_variable(
|
|
71
|
+
graph_node.inputs[0],
|
|
72
|
+
before_op_output_shape_trans,
|
|
73
|
+
)
|
|
74
|
+
graph_node_input_size = get_constant_or_variable(
|
|
75
|
+
graph_node.inputs[1],
|
|
76
|
+
False \
|
|
77
|
+
if hasattr(graph_node.inputs[1], 'values') \
|
|
78
|
+
and isinstance(graph_node.inputs[1].values, np.ndarray) \
|
|
79
|
+
else before_op_output_shape_trans,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
graph_node_output: gs.Variable = graph_node.outputs[0]
|
|
83
|
+
shape = graph_node_output.shape
|
|
84
|
+
dtype = graph_node_output.dtype
|
|
85
|
+
|
|
86
|
+
# Preserving Graph Structure (Dict)
|
|
87
|
+
tf_layers_dict[graph_node_output.name] = {
|
|
88
|
+
'optype': graph_node.op,
|
|
89
|
+
'shape': shape,
|
|
90
|
+
'dtype': dtype,
|
|
91
|
+
'nhwc': True,
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
theta = tf_layers_dict[graph_node_input_theta.name]['tf_node'] \
|
|
95
|
+
if isinstance(graph_node_input_theta, gs.Variable) else graph_node_input_theta
|
|
96
|
+
size = tf_layers_dict[graph_node_input_size.name]['tf_node'] \
|
|
97
|
+
if isinstance(graph_node_input_size, gs.Variable) else graph_node_input_size
|
|
98
|
+
|
|
99
|
+
# Pre-process transpose
|
|
100
|
+
theta = pre_process_transpose(
|
|
101
|
+
value_before_transpose=theta,
|
|
102
|
+
param_target='inputs',
|
|
103
|
+
param_name=graph_node.inputs[0].name,
|
|
104
|
+
**kwargs,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
align_corners = bool(graph_node.attrs.get('align_corners', 0))
|
|
108
|
+
align_corners = replace_parameter(
|
|
109
|
+
value_before_replacement=align_corners,
|
|
110
|
+
param_target='attributes',
|
|
111
|
+
param_name='align_corners',
|
|
112
|
+
**kwargs,
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
theta_dtype = theta.dtype
|
|
116
|
+
size_tensor = tf.cast(size, tf.int32)
|
|
117
|
+
|
|
118
|
+
size_rank = size_tensor.shape[0] if size_tensor.shape.rank == 1 else None
|
|
119
|
+
|
|
120
|
+
def _build_grid_2d(size_vec):
|
|
121
|
+
N, _, H, W = tf.unstack(size_vec)
|
|
122
|
+
h_coords = _make_coords(H, align_corners, theta_dtype)
|
|
123
|
+
w_coords = _make_coords(W, align_corners, theta_dtype)
|
|
124
|
+
grid_h, grid_w = tf.meshgrid(h_coords, w_coords, indexing='ij')
|
|
125
|
+
ones = tf.ones_like(grid_w, dtype=theta_dtype)
|
|
126
|
+
grid = tf.stack([grid_w, grid_h, ones], axis=-1)
|
|
127
|
+
grid_flat = tf.reshape(grid, shape=[-1, 3])
|
|
128
|
+
grid_flat_t = tf.transpose(grid_flat)
|
|
129
|
+
grid_flat_t = tf.cast(grid_flat_t, theta_dtype)
|
|
130
|
+
out = tf.matmul(theta, grid_flat_t)
|
|
131
|
+
out = tf.transpose(out, perm=[0, 2, 1])
|
|
132
|
+
out = tf.reshape(out, shape=tf.stack([N, H, W, 2]))
|
|
133
|
+
return out
|
|
134
|
+
|
|
135
|
+
def _build_grid_3d(size_vec):
|
|
136
|
+
N, _, D, H, W = tf.unstack(size_vec)
|
|
137
|
+
d_coords = _make_coords(D, align_corners, theta_dtype)
|
|
138
|
+
h_coords = _make_coords(H, align_corners, theta_dtype)
|
|
139
|
+
w_coords = _make_coords(W, align_corners, theta_dtype)
|
|
140
|
+
grid_d, grid_h, grid_w = tf.meshgrid(d_coords, h_coords, w_coords, indexing='ij')
|
|
141
|
+
ones = tf.ones_like(grid_w, dtype=theta_dtype)
|
|
142
|
+
grid = tf.stack([grid_w, grid_h, grid_d, ones], axis=-1)
|
|
143
|
+
grid_flat = tf.reshape(grid, shape=[-1, 4])
|
|
144
|
+
grid_flat_t = tf.transpose(grid_flat)
|
|
145
|
+
grid_flat_t = tf.cast(grid_flat_t, theta_dtype)
|
|
146
|
+
out = tf.matmul(theta, grid_flat_t)
|
|
147
|
+
out = tf.transpose(out, perm=[0, 2, 1])
|
|
148
|
+
out = tf.reshape(out, shape=tf.stack([N, D, H, W, 3]))
|
|
149
|
+
return out
|
|
150
|
+
|
|
151
|
+
if size_rank == 4:
|
|
152
|
+
grid = _build_grid_2d(size_tensor)
|
|
153
|
+
elif size_rank == 5:
|
|
154
|
+
grid = _build_grid_3d(size_tensor)
|
|
155
|
+
else:
|
|
156
|
+
size_dim = tf.shape(size_tensor)[0]
|
|
157
|
+
grid = tf.cond(
|
|
158
|
+
pred=tf.equal(size_dim, 4),
|
|
159
|
+
true_fn=lambda: _build_grid_2d(size_tensor),
|
|
160
|
+
false_fn=lambda: _build_grid_3d(size_tensor),
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
tf_layers_dict[graph_node_output.name]['tf_node'] = grid
|
|
164
|
+
|
|
165
|
+
# Post-process transpose
|
|
166
|
+
tf_layers_dict[graph_node_output.name]['tf_node'] = post_process_transpose(
|
|
167
|
+
value_before_transpose=tf_layers_dict[graph_node_output.name]['tf_node'],
|
|
168
|
+
param_target='outputs',
|
|
169
|
+
param_name=graph_node.outputs[0].name,
|
|
170
|
+
**kwargs,
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# Generation of Debug Info
|
|
174
|
+
tf_layers_dict[graph_node_output.name]['tf_node_info'] = \
|
|
175
|
+
make_tf_node_info(
|
|
176
|
+
node_info={
|
|
177
|
+
'tf_op_type': 'AffineGrid',
|
|
178
|
+
'tf_inputs': {
|
|
179
|
+
'theta': theta,
|
|
180
|
+
'size': size,
|
|
181
|
+
'align_corners': align_corners,
|
|
182
|
+
},
|
|
183
|
+
'tf_outputs': {
|
|
184
|
+
'output': tf_layers_dict[graph_node_output.name]['tf_node'],
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
)
|