Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/4.x' into '5.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
mshabunin committed Jun 11, 2024
2 parents 5e4537c + dd1fbd0 commit de6fa26
Show file tree
Hide file tree
Showing 21 changed files with 57 additions and 7 deletions.
5 changes: 5 additions & 0 deletions testdata/dnn/download_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ def is_archive(self):
url='http://dl.caffe.berkeleyvision.org/fcn8s-heavy-pascal.caffemodel',
sha='c449ea74dd7d83751d1357d6a8c323fcf4038962',
filename='fcn8s-heavy-pascal.caffemodel'),
Model(
name='Fcn',
url='https://github.com/onnx/models/raw/491ce05590abb7551d7fae43c067c060eeb575a6/validated/vision/object_detection_segmentation/fcn/model/fcn-resnet50-12.onnx',
sha='1bb0c7e0034038969aecc6251166f1612a139230',
filename='onnx/models/fcn-resnet50-12.onnx'),
Model(
name='Ssd_vgg16',
url='https://www.dropbox.com/s/8apyk3uzk2vl522/VGG_ILSVRC2016_SSD_300x300_iter_440000.caffemodel?dl=1',
Expand Down
Binary file modified testdata/dnn/segmentation_exp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 52 additions & 7 deletions testdata/dnn/tflite/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,14 @@ def save_tflite_model(model, inp, name):
out = model(inp)
out = np.array(out)

if len(inp.shape) == 4:
# convert NHWC to NCHW format
if inp.ndim == 4:
inp = inp.transpose(0, 3, 1, 2)
inp = np.copy(inp, order='C').astype(inp.dtype)

if out.ndim == 4:
out = out.transpose(0, 3, 1, 2)
out = np.copy(out, order='C').astype(out.dtype)

np.save(f'{name}_inp.npy', inp)
np.save(f'{name}_out_Identity.npy', out)
Expand Down Expand Up @@ -88,17 +93,57 @@ def split(x):
inp = np.random.standard_normal((1, 3)).astype(np.float32)
save_tflite_model(split, inp, 'split')

def keras_to_tf(model, input_shape):
tf_func = tf.function(
model.call,
input_signature=[tf.TensorSpec(input_shape, tf.float32)],
)
inp = np.random.standard_normal((input_shape)).astype(np.float32)

return tf_func, inp

fully_connected = tf.keras.models.Sequential([
tf.keras.layers.Dense(3),
tf.keras.layers.ReLU(),
tf.keras.layers.Softmax(),
])

fully_connected = tf.function(
fully_connected.call,
input_signature=[tf.TensorSpec((1,2), tf.float32)],
)

inp = np.random.standard_normal((1, 2)).astype(np.float32)
fully_connected, inp = keras_to_tf(fully_connected, (1, 2))
save_tflite_model(fully_connected, inp, 'fully_connected')

permutation_3d = tf.keras.models.Sequential([
tf.keras.layers.Permute((2, 1))
])

permutation_3d, inp = keras_to_tf(permutation_3d, (1, 2, 3))
save_tflite_model(permutation_3d, inp, 'permutation_3d')

# (1, 2, 3) is temporarily disabled as TFLiteConverter produces a incorrect graph in this case
permutation_4d_list = [(1, 3, 2), (2, 1, 3), (2, 3, 1)]
for perm_axis in permutation_4d_list:
permutation_4d_model = tf.keras.models.Sequential([
tf.keras.layers.Permute(perm_axis),
tf.keras.layers.Conv2D(3, 1)
])

permutation_4d_model, inp = keras_to_tf(permutation_4d_model, (1, 2, 3, 4))
model_name = f"permutation_4d_0{''.join(map(str, perm_axis))}"
save_tflite_model(permutation_4d_model, inp, model_name)

global_average_pooling_2d = tf.keras.models.Sequential([
tf.keras.layers.GlobalAveragePooling2D(keepdims=True),
tf.keras.layers.ZeroPadding2D(1),
tf.keras.layers.GlobalAveragePooling2D(keepdims=False)
])

global_average_pooling_2d, inp = keras_to_tf(global_average_pooling_2d, (1, 7, 7, 5))
save_tflite_model(global_average_pooling_2d, inp, 'global_average_pooling_2d')

global_max_pool = tf.keras.models.Sequential([
tf.keras.layers.GlobalMaxPool2D(keepdims=True),
tf.keras.layers.ZeroPadding2D(1),
tf.keras.layers.GlobalMaxPool2D(keepdims=True)
])

global_max_pool, inp = keras_to_tf(global_max_pool, (1, 7, 7, 5))
save_tflite_model(global_max_pool, inp, 'global_max_pooling_2d')
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/tflite/global_max_pooling_2d.tflite
Binary file not shown.
Binary file added testdata/dnn/tflite/global_max_pooling_2d_inp.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_3d.tflite
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_3d_inp.npy
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_3d_out_Identity.npy
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0132.tflite
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0132_inp.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0213.tflite
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0213_inp.npy
Binary file not shown.
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0231.tflite
Binary file not shown.
Binary file added testdata/dnn/tflite/permutation_4d_0231_inp.npy
Binary file not shown.
Binary file not shown.

0 comments on commit de6fa26

Please sign in to comment.