Skip to content

Commit 723bdf2

Browse files
authored
Merge pull request #1168 from CNOCycle:tflite/transpose
* Refine data transpose when generating testing data for tflite models * Add test cases for tflite models * Add more test cases for permutation op in tflite model * Update testing data for permutation op in tflite model
1 parent 1458fff commit 723bdf2

13 files changed

+66
-1
lines changed

testdata/dnn/tflite/generate.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ def save_tflite_model(model, inp, name):
5858
out = model(inp)
5959
out = np.array(out)
6060

61-
if len(inp.shape) == 4:
61+
# convert NHWC to NCHW format
62+
if inp.ndim == 4:
6263
inp = inp.transpose(0, 3, 1, 2)
64+
inp = np.copy(inp, order='C').astype(inp.dtype)
65+
66+
if out.ndim == 4:
6367
out = out.transpose(0, 3, 1, 2)
68+
out = np.copy(out, order='C').astype(out.dtype)
6469

6570
np.save(f'{name}_inp.npy', inp)
6671
np.save(f'{name}_out_Identity.npy', out)
@@ -102,3 +107,63 @@ def split(x):
102107

103108
inp = np.random.standard_normal((1, 2)).astype(np.float32)
104109
save_tflite_model(fully_connected, inp, 'fully_connected')
110+
111+
permutation_3d = tf.keras.models.Sequential([
112+
tf.keras.layers.Permute((2,1))
113+
])
114+
115+
permutation_3d = tf.function(
116+
permutation_3d.call,
117+
input_signature=[tf.TensorSpec((1,2,3), tf.float32)],
118+
)
119+
inp = np.random.standard_normal((1, 2, 3)).astype(np.float32)
120+
save_tflite_model(permutation_3d, inp, 'permutation_3d')
121+
122+
# Temporarily disabled as TFLiteConverter produces a incorrect graph in this case
123+
#permutation_4d_0123 = tf.keras.models.Sequential([
124+
# tf.keras.layers.Permute((1,2,3)),
125+
# tf.keras.layers.Conv2D(3,1)
126+
#])
127+
#
128+
#permutation_4d_0123 = tf.function(
129+
# permutation_4d_0123.call,
130+
# input_signature=[tf.TensorSpec((1,2,3,4), tf.float32)],
131+
#)
132+
#inp = np.random.standard_normal((1, 2, 3, 4)).astype(np.float32)
133+
#save_tflite_model(permutation_4d_0123, inp, 'permutation_4d_0123')
134+
135+
permutation_4d_0132 = tf.keras.models.Sequential([
136+
tf.keras.layers.Permute((1,3,2)),
137+
tf.keras.layers.Conv2D(3,1)
138+
])
139+
140+
permutation_4d_0132 = tf.function(
141+
permutation_4d_0132.call,
142+
input_signature=[tf.TensorSpec((1,2,3,4), tf.float32)],
143+
)
144+
inp = np.random.standard_normal((1, 2, 3, 4)).astype(np.float32)
145+
save_tflite_model(permutation_4d_0132, inp, 'permutation_4d_0132')
146+
147+
permutation_4d_0213 = tf.keras.models.Sequential([
148+
tf.keras.layers.Permute((2,1,3)),
149+
tf.keras.layers.Conv2D(3,1)
150+
])
151+
152+
permutation_4d_0213 = tf.function(
153+
permutation_4d_0213.call,
154+
input_signature=[tf.TensorSpec((1,2,3,4), tf.float32)],
155+
)
156+
inp = np.random.standard_normal((1, 2, 3, 4)).astype(np.float32)
157+
save_tflite_model(permutation_4d_0213, inp, 'permutation_4d_0213')
158+
159+
permutation_4d_0231 = tf.keras.models.Sequential([
160+
tf.keras.layers.Permute((2,3,1)),
161+
tf.keras.layers.Conv2D(3,1)
162+
])
163+
164+
permutation_4d_0231 = tf.function(
165+
permutation_4d_0231.call,
166+
input_signature=[tf.TensorSpec((1,2,3,4), tf.float32)],
167+
)
168+
inp = np.random.standard_normal((1, 2, 3, 4)).astype(np.float32)
169+
save_tflite_model(permutation_4d_0231, inp, 'permutation_4d_0231')
784 Bytes
Binary file not shown.
152 Bytes
Binary file not shown.
152 Bytes
Binary file not shown.
1.21 KB
Binary file not shown.
224 Bytes
Binary file not shown.
Binary file not shown.
1.22 KB
Binary file not shown.
224 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)