forked from ARM-software/CMSIS-NN
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use legacy Keras 2.0 for test data generation (ARM-software#120)
Keras 3.0 is used by default since Tensorflow 2.16.1, and since this contains some breaking changes the scripts need to stay at 2.0
- Loading branch information
1 parent
f0957f8
commit 2e5dcdc
Showing
9 changed files
with
42 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# SPDX-FileCopyrightText: Copyright 2010-2023 Arm Limited and/or its affiliates <[email protected]> | ||
# SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
@@ -18,6 +18,7 @@ | |
|
||
import tensorflow as tf | ||
import numpy as np | ||
import tf_keras as keras | ||
|
||
|
||
class AddMulSettings(TestSettings): | ||
|
@@ -90,16 +91,16 @@ def generate_data(self, input_data1=None, input_data2=None) -> None: | |
inttype_tf = tf.int8 | ||
|
||
# Create a one-layer functional Keras model as add/mul cannot use a sequntial Keras model. | ||
input1 = tf.keras.layers.Input(shape=input_shape[1:]) | ||
input2 = tf.keras.layers.Input(shape=input_shape[1:]) | ||
input1 = keras.layers.Input(shape=input_shape[1:]) | ||
input2 = keras.layers.Input(shape=input_shape[1:]) | ||
if self.test_type == 'add': | ||
layer = tf.keras.layers.Add()([input1, input2]) | ||
layer = keras.layers.Add()([input1, input2]) | ||
elif self.test_type == 'mul': | ||
layer = tf.keras.layers.Multiply()([input1, input2]) | ||
layer = keras.layers.Multiply()([input1, input2]) | ||
else: | ||
raise RuntimeError("Wrong test type") | ||
out = tf.keras.layers.Lambda(function=lambda x: x)(layer) | ||
model = tf.keras.models.Model(inputs=[input1, input2], outputs=out) | ||
out = keras.layers.Lambda(function=lambda x: x)(layer) | ||
model = keras.models.Model(inputs=[input1, input2], outputs=out) | ||
|
||
interpreter = self.convert_and_interpret(model, inttype_tf) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# SPDX-FileCopyrightText: Copyright 2010-2023 Arm Limited and/or its affiliates <[email protected]> | ||
# SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
@@ -18,7 +18,7 @@ | |
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
import tf_keras as keras | ||
|
||
class PoolingSettings(TestSettings): | ||
|
||
|
@@ -85,18 +85,18 @@ def generate_data(self, input_data=None) -> None: | |
input_data = tf.cast(input_data, tf.float32) | ||
|
||
# Create a one-layer Keras model | ||
model = tf.keras.models.Sequential() | ||
model = keras.models.Sequential() | ||
input_shape = (self.batches, self.y_input, self.x_input, self.input_ch) | ||
model.add(tf.keras.layers.InputLayer(input_shape=input_shape[1:], batch_size=self.batches)) | ||
model.add(keras.layers.InputLayer(input_shape=input_shape[1:], batch_size=self.batches)) | ||
if self.test_type == 'avgpool': | ||
model.add( | ||
tf.keras.layers.AveragePooling2D(pool_size=(self.filter_y, self.filter_x), | ||
keras.layers.AveragePooling2D(pool_size=(self.filter_y, self.filter_x), | ||
strides=(self.stride_y, self.stride_x), | ||
padding=self.padding, | ||
input_shape=input_shape[1:])) | ||
elif self.test_type == 'maxpool': | ||
model.add( | ||
tf.keras.layers.MaxPooling2D(pool_size=(self.filter_y, self.filter_x), | ||
keras.layers.MaxPooling2D(pool_size=(self.filter_y, self.filter_x), | ||
strides=(self.stride_y, self.stride_x), | ||
padding=self.padding, | ||
input_shape=input_shape[1:])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# SPDX-FileCopyrightText: Copyright 2010-2023 Arm Limited and/or its affiliates <[email protected]> | ||
# SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
@@ -17,7 +17,7 @@ | |
import math | ||
from test_settings import TestSettings | ||
import tensorflow as tf | ||
|
||
import tf_keras as keras | ||
|
||
class SoftmaxSettings(TestSettings): | ||
softmax_input_integer_bits = 5 | ||
|
@@ -147,9 +147,9 @@ def generate_data(self, input_data=None, weights=None, biases=None) -> None: | |
output_data = interpreter.get_tensor(output_layer["index"]) | ||
else: | ||
# Create a one-layer Keras model. | ||
model = tf.keras.models.Sequential() | ||
model = keras.models.Sequential() | ||
input_shape = (self.y_input, self.x_input) | ||
model.add(tf.keras.layers.Softmax(input_shape=input_shape)) | ||
model.add(keras.layers.Softmax(input_shape=input_shape)) | ||
|
||
interpreter = self.convert_and_interpret(model, inttype, tf.expand_dims(input_data, axis=0)) | ||
output_details = interpreter.get_output_details() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# SPDX-FileCopyrightText: Copyright 2010-2023 Arm Limited and/or its affiliates <[email protected]> | ||
# SPDX-FileCopyrightText: Copyright 2010-2024 Arm Limited and/or its affiliates <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
@@ -26,7 +26,7 @@ | |
|
||
import numpy as np | ||
import tensorflow as tf | ||
|
||
import tf_keras as keras | ||
|
||
class TestSettings(ABC): | ||
|
||
|
@@ -453,8 +453,8 @@ def convert_and_interpret(self, model, inttype, input_data=None, dataset_shape=N | |
return self.interpret_model(input_data, inttype) | ||
|
||
def convert_model(self, model, inttype, dataset_shape=None): | ||
model.compile(loss=tf.keras.losses.categorical_crossentropy, | ||
optimizer=tf.keras.optimizers.Adam(), | ||
model.compile(loss=keras.losses.categorical_crossentropy, | ||
optimizer=keras.optimizers.Adam(), | ||
metrics=['accuracy']) | ||
n_inputs = len(model.inputs) | ||
|
||
|