@@ -58,9 +58,14 @@ def save_tflite_model(model, inp, name):
58
58
out = model (inp )
59
59
out = np .array (out )
60
60
61
- if len (inp .shape ) == 4 :
61
+ # convert NHWC to NCHW format
62
+ if inp .ndim == 4 :
62
63
inp = inp .transpose (0 , 3 , 1 , 2 )
64
+ inp = np .copy (inp , order = 'C' ).astype (inp .dtype )
65
+
66
+ if out .ndim == 4 :
63
67
out = out .transpose (0 , 3 , 1 , 2 )
68
+ out = np .copy (out , order = 'C' ).astype (out .dtype )
64
69
65
70
np .save (f'{ name } _inp.npy' , inp )
66
71
np .save (f'{ name } _out_Identity.npy' , out )
@@ -102,3 +107,63 @@ def split(x):
102
107
103
108
inp = np .random .standard_normal ((1 , 2 )).astype (np .float32 )
104
109
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' )
0 commit comments