aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/nn/rewrite/library/clustering.py
blob: 81bfd906a78b6b55a21ec89b936279435d6ebe51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# SPDX-FileCopyrightText: Copyright 2024, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Rewrite functions used to return layers ready for clustering."""
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.helper_functions import compute_conv2d_parameters


def fc_clustering_rewrite(input_shape: Any, output_shape: Any) -> keras.Model:
    """Fully connected TensorFlow Lite model ready for clustering."""
    rewrite_params = {
        "number_of_clusters": 4,
        "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),
            ]
        ),
        **rewrite_params
    )
    return model


def conv2d_clustering_rewrite(input_shape: Any, output_shape: Any) -> keras.Model:
    """Conv2d TensorFlow Lite model ready for clustering."""
    rewrite_params = {
        "number_of_clusters": 4,
        "cluster_centroids_init": tfmot.clustering.keras.CentroidInitialization.LINEAR,
    }
    conv2d_parameters = compute_conv2d_parameters(
        input_shape=input_shape, output_shape=output_shape
    )
    model = tfmot.clustering.keras.cluster_weights(
        to_cluster=keras.Sequential(
            [
                keras.layers.InputLayer(input_shape=input_shape),
                keras.layers.Conv2D(**conv2d_parameters),
                keras.layers.BatchNormalization(),
                keras.layers.ReLU(),
            ]
        ),
        **rewrite_params
    )
    return model