aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/nn/rewrite/library
diff options
context:
space:
mode:
Diffstat (limited to 'src/mlia/nn/rewrite/library')
-rw-r--r--src/mlia/nn/rewrite/library/clustering.py (renamed from src/mlia/nn/rewrite/library/fc_clustering_layer.py)22
-rw-r--r--src/mlia/nn/rewrite/library/fc_layer.py2
-rw-r--r--src/mlia/nn/rewrite/library/sparsity.py (renamed from src/mlia/nn/rewrite/library/fc_sparsity24_layer.py)21
3 files changed, 41 insertions, 4 deletions
diff --git a/src/mlia/nn/rewrite/library/fc_clustering_layer.py b/src/mlia/nn/rewrite/library/clustering.py
index 7cc383e..1d22dc6 100644
--- a/src/mlia/nn/rewrite/library/fc_clustering_layer.py
+++ b/src/mlia/nn/rewrite/library/clustering.py
@@ -7,7 +7,7 @@ import tensorflow_model_optimization as tfmot
from keras.api._v2 import keras # Temporary workaround for now: MLIA-1107
-def get_keras_model_clus(input_shape: Any, output_shape: Any) -> keras.Model:
+def fc_clustering_rewrite(input_shape: Any, output_shape: Any) -> keras.Model:
"""Generate TensorFlow Lite model for clustering rewrite."""
rewrite_params = {
"number_of_clusters": 32,
@@ -24,3 +24,23 @@ def get_keras_model_clus(input_shape: Any, output_shape: Any) -> keras.Model:
**rewrite_params
)
return model
+
+
+def conv2d_clustering_rewrite(
+ input_shape: Any, conv2d_parameters: dict[str, Any]
+) -> keras.Model:
+ """Generate TensorFlow Lite model for clustering rewrite."""
+ rewrite_params = {
+ "number_of_clusters": 32,
+ "cluster_centroids_init": tfmot.clustering.keras.CentroidInitialization.LINEAR,
+ }
+ model = tfmot.clustering.keras.cluster_weights(
+ to_cluster=keras.Sequential(
+ [
+ keras.layers.InputLayer(input_shape=input_shape),
+ keras.layers.Conv2D(**conv2d_parameters),
+ ]
+ ),
+ **rewrite_params
+ )
+ return model
diff --git a/src/mlia/nn/rewrite/library/fc_layer.py b/src/mlia/nn/rewrite/library/fc_layer.py
index 041ce85..cb98cb9 100644
--- a/src/mlia/nn/rewrite/library/fc_layer.py
+++ b/src/mlia/nn/rewrite/library/fc_layer.py
@@ -6,7 +6,7 @@ from typing import Any
from keras.api._v2 import keras # Temporary workaround for now: MLIA-1107
-def get_keras_model(input_shape: Any, output_shape: Any) -> keras.Model:
+def fc_rewrite(input_shape: Any, output_shape: Any) -> keras.Model:
"""Generate TensorFlow Lite model for rewrite."""
model = keras.Sequential(
(
diff --git a/src/mlia/nn/rewrite/library/fc_sparsity24_layer.py b/src/mlia/nn/rewrite/library/sparsity.py
index 531b34a..8b74b72 100644
--- a/src/mlia/nn/rewrite/library/fc_sparsity24_layer.py
+++ b/src/mlia/nn/rewrite/library/sparsity.py
@@ -1,13 +1,13 @@
# SPDX-FileCopyrightText: Copyright 2024, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
-"""Example rewrite with one fully connected 2:4 sparsity layer."""
+"""Example rewrite with one fully connected clustered layer."""
from typing import Any
import tensorflow_model_optimization as tfmot
from keras.api._v2 import keras # Temporary workaround for now: MLIA-1107
-def get_keras_model(input_shape: Any, output_shape: Any) -> keras.Model:
+def fc_sparsity_rewrite(input_shape: Any, output_shape: Any) -> keras.Model:
"""Generate TensorFlow Lite model for rewrite."""
model = tfmot.sparsity.keras.prune_low_magnitude(
to_prune=keras.Sequential(
@@ -21,3 +21,20 @@ def get_keras_model(input_shape: Any, output_shape: Any) -> keras.Model:
)
return model
+
+
+def conv2d_sparsity_rewrite(
+ input_shape: Any, conv2d_parameters: dict[str, Any]
+) -> keras.Model:
+ """Generate TensorFlow Lite model for rewrite."""
+ model = tfmot.sparsity.keras.prune_low_magnitude(
+ to_prune=keras.Sequential(
+ [
+ keras.layers.InputLayer(input_shape=input_shape),
+ keras.layers.Conv2D(**conv2d_parameters),
+ ]
+ ),
+ sparsity_m_by_n=(2, 4),
+ )
+
+ return model