# Adversarial Validation in Image Classification Datasets by Means of Cumulative Spectral Gradient
## 2 Cumulative Spectral Gradient Metric  
CSG is a metric designed to characterize the difficulty of a classification problem without the need to train a model, particularly the difficulty of its dataset. The calculation of this metric is based on the probabilistic divergence between classes within a spectral clustering framework [23; 24]. First, the CSG method projects the input images into a lower-dimensional latent space, allowing the data features to better align with what the models learn. By performing this projection, images with similar content stay close to each other, and the others move away from each other [23]. The projection function used in this method corresponds to t-SNE (t-distributed Stochastic Neighbor Embedding) applied to the embedding of a Convolutional Neural Network (CNN) autoencoder. From this projection, the overlap between classes is estimated as the expected value (\(E\)) of one distribution (\(P\)) over the other (Equation (1)):  
\[E_{P(\phi(x)|C_{j})}\big{[}P\big{(}\phi(x)|C_{j}\big{)}\big{]} \text{ or }\] \[E_{P\big{(}\phi(x)|C_{j}\big{)}}\big{[}P(\phi(x)|C_{i})\big{]}, \tag{1}\]  
where \(x\) represents the input samples, \(\phi(x)\) corresponds to the projection of the samples with _i.i.d._ characteristics, \(C_{i}\) and \(C_{j}\) denote the two classes. The expected value could be approximated by applying the Monte Carlo method as shown in Equation (2):  
\[E_{P(\phi(x)|C_{j})}\big{[}P\big{(}\phi(x)|C_{j}\big{)}\big{]} \approx\frac{1}{M}\sum_{m=1}^{M}P\big{(}\phi(x)|C_{j}\big{)}, \tag{2}\]  
where \(\{\phi(x_{1}),\phi(x_{2}),\ldots,\phi(x_{M-1}),\phi(x_{M})\}\) are the \(M\) projections from the distribution \(P(\phi(x)|C_{i})\). Since \(P\big{(}\phi(x)|C_{j}\big{)}\) is unknown, it could be approximated by a K-nearest estimator, and the expected value in Equation (2) is estimated by averaging the probability of belonging to one class over the other across \(M\) samples. Thus, the approximate divergence between classes is given by Equation (3):  
\[E_{P(\phi(x)|C_{i})}\big{[}P\big{(}\phi(x)|C_{j}\big{)}\big{]} \approx\frac{1}{M}\sum_{m=1}^{M}\frac{K_{C_{j}}}{M\cdot V}, \tag{3}\]  
where \(K_{C_{j}}\) is the number of neighbors around \(\phi(x)\) of class \(C_{j}\), \(M\) is the number of samples selected in class \(C_{j}\), and \(V\) is the volume of the hypercube surrounding the k closest samples to \(\phi(x)\) in class \(C_{j}\)[23]. The result of this process for a set of \(K\) classes corresponds to a similarity matrix \(S\), of dimensions \(K\times K\), with \(S_{ij}\) being the divergence approximation, through the Monte Carlo method, between \(C_{i}\) and \(C_{j}\). Subsequently, a Laplacian matrix derived from the similarity matrix is calculated as \(L=D-W\). For the calculation of \(W\), each column of the matrix \(S\) is considered a vector of signatures of each class, so that to evaluate the similarity between them, it is sufficient to apply a distance metric between the signature vectors of two different classes. In this case, the Bray-Curtis distance (Equation (4)) is used:  
\[w_{ij}=1-\frac{\sum_{k}^{K}|S_{ik}-S_{jk}|}{\sum_{k}^{K}|S_{ik}+S_{jk}|}, \tag{4}\]  
where \(i\) and \(j\) represent the two evaluated classes and \(K\) the total number of classes. Thus, \(w_{ij}=0\) when there is no overlap between classes \(i\) and \(j\) and \(w_{ij}=1\) when the distributions are the same. On the other hand, \(D\) is a degree matrix and it is defined as given in Equation (5):  
\[D_{i}=\sum_{j}w_{ij}. \tag{5}\]  
The eigenvalues (\(\lambda_{i}\)) of \(L\) make up its spectrum, and the gradient discontinuities in this spectrum are called eigengaps (\(\lambda_{i+1}-\lambda_{i}\)).