docstr: In this test we test that 'kernel' weights are clustered.
docstr_tok: ["In","this","test","we","test","that","'kernel","'","weights","are","clustered","."]
code: def testMnist(self, preserve_sparsity, cluster_per_channel):
      """In this test we test that 'kernel' weights are clustered."""
      model = self.model
      _, (x_test, y_test) = self.dataset
  
      # Indices of Conv2D and Dense layers, respectively.
      layer_indices = [2, 5]
  
      # Dict to store the layer bias weight counts to
      # ensure they aren't clustered
      nr_of_bias_weights = {}
  
      # Checks that number of original weights ('kernel') and biases
      # are greater than the number of clusters for all clusterable layers
      for i in layer_indices:
          nr_of_unique_weights = _get_number_of_unique_weights(
              model, i, "kernel"
          )
          self.assertGreater(nr_of_unique_weights, NUMBER_OF_CLUSTERS)
  
          nr_of_bias_weights[i] = _get_number_of_unique_weights(
              model, i, "bias"
          )
          self.assertGreater(nr_of_bias_weights[i], NUMBER_OF_CLUSTERS)
  
      results_original = model.evaluate(x_test, y_test)
      self.assertGreater(results_original[1], 0.8)
  
      model_copy = _deepcopy_model(model)
      clustered_model = _cluster_model(
          model_copy,
          NUMBER_OF_CLUSTERS,
          preserve_sparsity=preserve_sparsity,
          cluster_per_channel=cluster_per_channel,
      )
  
      results = clustered_model.evaluate(x_test, y_test)
  
      self.assertGreater(results[1], 0.8)
  
      for i in layer_indices:
          nr_of_unique_weights = _get_number_of_unique_weights(
              clustered_model, i, "kernel"
          )
          if cluster_per_channel and isinstance(
              clustered_model.layers[i], tf.keras.layers.Conv2D
          ):
              self.assertLessEqual(
                  nr_of_unique_weights,
                  NUMBER_OF_CLUSTERS * NUMBER_OF_CHANNELS,
              )
          else:
              self.assertLessEqual(
                  nr_of_unique_weights, NUMBER_OF_CLUSTERS
              )
  
          # checks that we don't cluster 'bias' weights
          clustered_nr_of_bias_weights = _get_number_of_unique_weights(
              clustered_model, i, "bias"
          )
          self.assertEqual(
              nr_of_bias_weights[i], clustered_nr_of_bias_weights
          )
  
file_path: model-optimization/tensorflow_model_optimization/python/core/clustering/keras/mnist_clustering_test.py
imports: from absl.testing import parameterized
  import tensorflow
  from tensorflow_model_optimization.python.core.clustering.keras import cluster
  from tensorflow_model_optimization.python.core.clustering.keras import cluster_config
label_desc: Use image node_id 2 for calling the FunctionalTest obj's underlying member method code with example usage: obj.testMnist(preserve_sparsity, cluster_per_channel) without return types