@@ -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 )
@@ -88,17 +93,57 @@ def split(x):
88
93
inp = np .random .standard_normal ((1 , 3 )).astype (np .float32 )
89
94
save_tflite_model (split , inp , 'split' )
90
95
96
+ def keras_to_tf (model , input_shape ):
97
+ tf_func = tf .function (
98
+ model .call ,
99
+ input_signature = [tf .TensorSpec (input_shape , tf .float32 )],
100
+ )
101
+ inp = np .random .standard_normal ((input_shape )).astype (np .float32 )
102
+
103
+ return tf_func , inp
91
104
92
105
fully_connected = tf .keras .models .Sequential ([
93
106
tf .keras .layers .Dense (3 ),
94
107
tf .keras .layers .ReLU (),
95
108
tf .keras .layers .Softmax (),
96
109
])
97
110
98
- fully_connected = tf .function (
99
- fully_connected .call ,
100
- input_signature = [tf .TensorSpec ((1 ,2 ), tf .float32 )],
101
- )
102
-
103
- inp = np .random .standard_normal ((1 , 2 )).astype (np .float32 )
111
+ fully_connected , inp = keras_to_tf (fully_connected , (1 , 2 ))
104
112
save_tflite_model (fully_connected , inp , 'fully_connected' )
113
+
114
+ permutation_3d = tf .keras .models .Sequential ([
115
+ tf .keras .layers .Permute ((2 , 1 ))
116
+ ])
117
+
118
+ permutation_3d , inp = keras_to_tf (permutation_3d , (1 , 2 , 3 ))
119
+ save_tflite_model (permutation_3d , inp , 'permutation_3d' )
120
+
121
+ # (1, 2, 3) is temporarily disabled as TFLiteConverter produces a incorrect graph in this case
122
+ permutation_4d_list = [(1 , 3 , 2 ), (2 , 1 , 3 ), (2 , 3 , 1 )]
123
+ for perm_axis in permutation_4d_list :
124
+ permutation_4d_model = tf .keras .models .Sequential ([
125
+ tf .keras .layers .Permute (perm_axis ),
126
+ tf .keras .layers .Conv2D (3 , 1 )
127
+ ])
128
+
129
+ permutation_4d_model , inp = keras_to_tf (permutation_4d_model , (1 , 2 , 3 , 4 ))
130
+ model_name = f"permutation_4d_0{ '' .join (map (str , perm_axis ))} "
131
+ save_tflite_model (permutation_4d_model , inp , model_name )
132
+
133
+ global_average_pooling_2d = tf .keras .models .Sequential ([
134
+ tf .keras .layers .GlobalAveragePooling2D (keepdims = True ),
135
+ tf .keras .layers .ZeroPadding2D (1 ),
136
+ tf .keras .layers .GlobalAveragePooling2D (keepdims = False )
137
+ ])
138
+
139
+ global_average_pooling_2d , inp = keras_to_tf (global_average_pooling_2d , (1 , 7 , 7 , 5 ))
140
+ save_tflite_model (global_average_pooling_2d , inp , 'global_average_pooling_2d' )
141
+
142
+ global_max_pool = tf .keras .models .Sequential ([
143
+ tf .keras .layers .GlobalMaxPool2D (keepdims = True ),
144
+ tf .keras .layers .ZeroPadding2D (1 ),
145
+ tf .keras .layers .GlobalMaxPool2D (keepdims = True )
146
+ ])
147
+
148
+ global_max_pool , inp = keras_to_tf (global_max_pool , (1 , 7 , 7 , 5 ))
149
+ save_tflite_model (global_max_pool , inp , 'global_max_pooling_2d' )
0 commit comments