# A Filter Pruning Method of CNN Models Based on Feature Maps Clustering
## 4 Filter Pruning Based on Feature Maps Clustering
### K-Means Method
#### 4.1.3 Determination of the Number of Classes  
At present, there are many methods to determine the clustering number, two of which are selected in this thesis.  
The first method is the Elbow method. This rule examines the relationship between the number of classes K and the cost function \(J\). Cost function \(J\) is the sum of the distances from all data in classes to the center points. It can be expressed as:  
\[J=\sum_{i=1}^{k}\sum_{j}^{m}dist\big{(}x_{i,j},c_{i}\big{)}^{2} \tag{1}\]  
where \(m\) represents the number of data points in the \(i_{th}\) class \(x_{i,j}\) represents the \(j_{th}\) data point in the \(i_{th}\) class. For a class, the smaller the cost function value is, the closer the members in this class are. Conversely, the larger the function value is, the looser the structure within the class is. The cost function decreases with the increase in the number of classes.  
Take Figure 6 as an example. When the number of clusters equals 3, the cost function is greatly improved, so 3 can be considered the best number of clusters. However, it is also possible that the cost function curve does not have a significant curvature change input. At this time, the second method is required to determine the optimal number of clusters.  
The second method is the silhouette coefficient method. For a clustering task, the best clustering should be that the data within the class is as compact as possible, and the data  
Figure 5: The K-Means Algorithm Flowchart.  
Figure 6: Cost Function vs. Cluster Number.  
between classes is as far away as possible. The silhouette coefficient is an index to measure the degree of dispersion and intensity of a class. The formula is expressed as follows:  
\[s(i)=\frac{b(i)-a(i)}{max\{a(i),b(i)\}} \tag{2}\]  
where \(i\) represents the \(i_{th}\) sample, \(b(i)\) denotes the mean distance from the \(i_{th}\) sample to the samples in the nearest class, \(a(i)\) represents the mean of the distance between samples in the same class, \(s(i)\) represents the silhouette coefficient of the sample. The value of \(s(i)\) is in the range of \((-1,1)\). If it is close to \(1\), the classification of this sample is reasonable; if it is close to \(-1\), it means that the sample should be classified into other classes.  
The average value of all samples' \(s(i)\) is called the silhouette coefficient of the whole clustering results. The formula is as follows:  
\[S=\frac{1}{N}\sum_{i=1}^{N}s(i) \tag{3}\]  
In the above formula, \(N\) represents the total number of samples, and \(S\) represents the final silhouette coefficient. The silhouette coefficient is the reasonable and effective measure of the clustering result. In general, the larger the silhouette coefficient is, the better the clustering effect is.  
Take Figure 7 as an example, the relationship between cost function and cluster number is shown in Figure 7a; there is no obvious elbow point in this curve. However, we can use the silhouette coefficient method as is shown in Figure 7b, when the clustering number is 3, the silhouette coefficient is the biggest, meaning that the classification of all samples is the most reasonable. Then, we can determine that the optimal clustering number is 3.