tensorflow / model-optimization

A toolkit to optimize ML models for deployment for Keras and TensorFlow, including quantization and pruning.
https://www.tensorflow.org/model_optimization
Apache License 2.0
1.49k stars 319 forks source link

Not able to cluster Conv1DTranspose layer #940

Open rameshkunasi opened 2 years ago

rameshkunasi commented 2 years ago

Hi, Describe the bug A clear and concise description of what the bug is.

Getting below error while trying to cluster the convolution layers. Is Conv1DTranspose clustering is possible or not? Can anyone suggest how to solve this.

Please initialize Clusterwith a supported layer. Layers should either be aClusterableLayer instance, or should be supported by the ClusteringRegistry. You passed: <class 'keras.layers.convolutional.Conv1DTranspose'>

System information

TensorFlow version (installed from source or binary): Binary

TensorFlow Model Optimization version (installed from source or binary): 0.7.1

Python version: 3.8

Describe the expected behavior

Describe the current behavior

Code to reproduce the issue Provide a reproducible code that is the bare minimum necessary to generate the problem.

import tensorflow as tf
import tensorflow_model_optimization as tfmot

cluster_weights = tfmot.clustering.keras.cluster_weights
CentroidInitialization = tfmot.clustering.keras.CentroidInitialization

time_dat = tf.keras.layers.Input(batch_shape=(1, 16000))
x1 = tf.keras.layers.Conv1D(10, 256, 64, padding = "valid",use_bias=False)(tf.expand_dims(time_dat, axis=-1))
x2 = tf.keras.layers.Conv1DTranspose(1, 256, 64, padding = "valid",use_bias=False)(x1)
model = tf.keras.models.Model(inputs=time_dat, outputs=[x2])

clustering_params = {
  'number_of_clusters': 50,
  'cluster_centroids_init': CentroidInitialization.LINEAR
}

def apply_clustering_to_dense(layer):
  if isinstance(layer, tf.keras.layers.Conv1D):
    return cluster_weights(layer, **clustering_params)
  return layer

clustered_model = tf.keras.models.clone_model(
    model,
    clone_function=apply_clustering_to_dense,
)

Screenshots If applicable, add screenshots to help explain your problem.

Additional context Add any other context about the problem here.

thaink commented 2 years ago

@Xhark Could you take a look.

wwwind commented 2 years ago

Hi @rameshkunasi Thank you for the reporting the problem. This PR https://github.com/tensorflow/model-optimization/pull/952 adds the requested support of Conv1DTranspose. I added unit test based on your example - please take a look whether it works for you. Thanks

Ben-Yee commented 2 years ago

When I was trying to prune,I got the similar issue:

Please initialize Prune with a supported layer. Layers should either be supported by the PruneRegistry (built-in keras layers) or should be a PrunableLayer instance, or should has a customer defined get_prunable_weights method. You passed: <class 'tensorflow.python.keras.layers.convolutional.Conv1DTranspose'>