Integrate WASI-NN into WAMR (#1521)
Initial integration of WASI-NN based on #1225: - Implement the library core/iwasm/libraries/wasi-nn - Support TensorFlow, CPU, F32 at the first stage - Add cmake variable `-DWAMR_BUILD_WASI_NN` - Add test case based on Docker image and update document Refer to #1573
This commit is contained in:
16
core/iwasm/libraries/wasi-nn/test/models/average.py
Executable file
16
core/iwasm/libraries/wasi-nn/test/models/average.py
Executable file
@ -0,0 +1,16 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
from utils import save_model
|
||||
|
||||
model = tf.keras.Sequential([
|
||||
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
|
||||
tf.keras.layers.AveragePooling2D(
|
||||
pool_size=(5, 5), strides=None, padding="valid", data_format=None)
|
||||
|
||||
])
|
||||
|
||||
# Export model to tflite
|
||||
|
||||
save_model(model, "average.tflite")
|
||||
17
core/iwasm/libraries/wasi-nn/test/models/max.py
Executable file
17
core/iwasm/libraries/wasi-nn/test/models/max.py
Executable file
@ -0,0 +1,17 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from utils import save_model
|
||||
|
||||
model = tf.keras.Sequential([
|
||||
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
|
||||
tf.keras.layers.MaxPooling2D(
|
||||
pool_size=(5, 5), strides=None, padding="valid", data_format=None)
|
||||
|
||||
])
|
||||
|
||||
# Export model to tflite
|
||||
|
||||
save_model(model, "max.tflite")
|
||||
15
core/iwasm/libraries/wasi-nn/test/models/mult_dimension.py
Normal file
15
core/iwasm/libraries/wasi-nn/test/models/mult_dimension.py
Normal file
@ -0,0 +1,15 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
from utils import save_model
|
||||
|
||||
model = tf.keras.Sequential([
|
||||
tf.keras.layers.InputLayer(input_shape=[3, 3, 1]),
|
||||
tf.keras.layers.Conv2D(1, (1, 1), kernel_initializer=tf.keras.initializers.Constant(
|
||||
value=1), bias_initializer='zeros'
|
||||
)
|
||||
])
|
||||
# Export model to tflite
|
||||
|
||||
save_model(model, "mult_dim.tflite")
|
||||
33
core/iwasm/libraries/wasi-nn/test/models/mult_outputs.py
Executable file
33
core/iwasm/libraries/wasi-nn/test/models/mult_outputs.py
Executable file
@ -0,0 +1,33 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from keras.layers import AveragePooling2D, Conv2D
|
||||
|
||||
from tensorflow.keras import Input, Model
|
||||
|
||||
from utils import save_model
|
||||
|
||||
|
||||
inputs = Input(shape=(4, 4, 1))
|
||||
|
||||
output1 = Conv2D(1, (4, 1), kernel_initializer=tf.keras.initializers.Constant(
|
||||
value=1), bias_initializer='zeros'
|
||||
)(inputs)
|
||||
output2 = AveragePooling2D(pool_size=(
|
||||
4, 1), strides=None, padding="valid", data_format=None)(inputs)
|
||||
|
||||
model = Model(inputs=inputs, outputs=[output1, output2])
|
||||
|
||||
inp = np.arange(16).reshape((1, 4, 4, 1))
|
||||
|
||||
print(inp)
|
||||
|
||||
res = model.predict(inp)
|
||||
|
||||
print(res)
|
||||
print(res[0].shape)
|
||||
print(res[1].shape)
|
||||
|
||||
save_model(model, "mult_out.tflite")
|
||||
17
core/iwasm/libraries/wasi-nn/test/models/sum.py
Executable file
17
core/iwasm/libraries/wasi-nn/test/models/sum.py
Executable file
@ -0,0 +1,17 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from utils import save_model
|
||||
|
||||
model = tf.keras.Sequential([
|
||||
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
|
||||
tf.keras.layers.Conv2D(1, (5, 5), kernel_initializer=tf.keras.initializers.Constant(
|
||||
value=1), bias_initializer='zeros'
|
||||
)
|
||||
])
|
||||
|
||||
# Export model to tflite
|
||||
|
||||
save_model(model, "sum.tflite")
|
||||
13
core/iwasm/libraries/wasi-nn/test/models/utils.py
Normal file
13
core/iwasm/libraries/wasi-nn/test/models/utils.py
Normal file
@ -0,0 +1,13 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
import tensorflow as tf
|
||||
import pathlib
|
||||
|
||||
|
||||
def save_model(model, filename):
|
||||
converter = tf.lite.TFLiteConverter.from_keras_model(model)
|
||||
tflite_model = converter.convert()
|
||||
tflite_models_dir = pathlib.Path("./")
|
||||
tflite_model_file = tflite_models_dir/filename
|
||||
tflite_model_file.write_bytes(tflite_model)
|
||||
Reference in New Issue
Block a user