aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/nn/rewrite/library/fc_clustering_layer.py
diff options
context:
space:
mode:
authorNathan Bailey <nathan.bailey@arm.com>2024-02-27 12:46:52 +0000
committerNathan Bailey <nathan.bailey@arm.com>2024-04-16 12:57:10 +0100
commit427e02696f1ede596ef6dce82787a37e122efa78 (patch)
tree1fae7f7c8cb10af4f7c5119b73371b709c2c7caa /src/mlia/nn/rewrite/library/fc_clustering_layer.py
parent2973b6d52914023f9b8797aec8309957457d4189 (diff)
downloadmlia-427e02696f1ede596ef6dce82787a37e122efa78.tar.gz
feat: Implement the clustering rewrite for fp32
Implements a clustering rewrite for fully connected layers for fp32 models Resolves: MLIA-1079 Signed-off-by: Nathan Bailey <nathan.bailey@arm.com> Change-Id: I4c12f0bf911219b4066f0760976e424ebe900a0b
Diffstat (limited to 'src/mlia/nn/rewrite/library/fc_clustering_layer.py')
-rw-r--r--src/mlia/nn/rewrite/library/fc_clustering_layer.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/mlia/nn/rewrite/library/fc_clustering_layer.py b/src/mlia/nn/rewrite/library/fc_clustering_layer.py
index 07c07ac..72931c0 100644
--- a/src/mlia/nn/rewrite/library/fc_clustering_layer.py
+++ b/src/mlia/nn/rewrite/library/fc_clustering_layer.py
@@ -3,11 +3,24 @@
"""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
-from mlia.nn.rewrite.library.fc_layer import get_keras_model
-
def get_keras_model_clus(input_shape: Any, output_shape: Any) -> keras.Model:
"""Generate TensorFlow Lite model for clustering rewrite."""
- return get_keras_model(input_shape, output_shape)
+ clustering_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.Flatten(),
+ keras.layers.Dense(units=output_shape),
+ ]
+ ),
+ **clustering_params
+ )
+ return model