Skip to content

Commit de6fa26

Browse files
committed
Merge remote-tracking branch 'upstream/4.x' into '5.x'
2 parents 5e4537c + dd1fbd0 commit de6fa26

21 files changed

+57
-7
lines changed

testdata/dnn/download_models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ def is_archive(self):
257257
url='http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel',
258258
sha='c449ea74dd7d83751d1357d6a8c323fcf4038962',
259259
filename='fcn8s-heavy-pascal.caffemodel'),
260+
Model(
261+
name='Fcn',
262+
url='https://github.com/onnx/models/raw/491ce05590abb7551d7fae43c067c060eeb575a6/validated/vision/object_detection_segmentation/fcn/model/fcn-resnet50-12.onnx',
263+
sha='1bb0c7e0034038969aecc6251166f1612a139230',
264+
filename='onnx/models/fcn-resnet50-12.onnx'),
260265
Model(
261266
name='Ssd_vgg16',
262267
url='https://www.dropbox.com/s/8apyk3uzk2vl522/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel?dl=1',

testdata/dnn/segmentation_exp.png

-74 Bytes
Loading

testdata/dnn/tflite/generate.py

Lines changed: 52 additions & 7 deletions
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)
@@ -88,17 +93,57 @@ def split(x):
8893
inp = np.random.standard_normal((1, 3)).astype(np.float32)
8994
save_tflite_model(split, inp, 'split')
9095

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
91104

92105
fully_connected = tf.keras.models.Sequential([
93106
tf.keras.layers.Dense(3),
94107
tf.keras.layers.ReLU(),
95108
tf.keras.layers.Softmax(),
96109
])
97110

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))
104112
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')
Binary file not shown.
Binary file not shown.
Binary file not shown.
1.25 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
784 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)