## 3 Materials and Methods
### The Model
#### 3.2.2 Sparse Autoencoders SAE  
The developed SAE in this study consists of an encoder followed by a decoder. The SAE includes a total of six layers: one input, four hidden, and one output. The input and output layers each contain 150 neurons, which match the shape of the data. The encoder is composed of two dense layers, each followed by a LeakyReLU activation layer. LeakyReLU has been shown to work well in autoencoders, as it helps to prevent the issue of \"dead neurons\", which can occur when ReLU activation outputs zero for a given input, leading to zero gradients during backpropagation and effectively stopping learning. The use of LeakyReLU can improve the stability and performance of the network during training. The  
Figure 3: FEAPAS structure.  
decoder is developed in a similar but reverse way to the encoder and includes a Sigmoid activation function in the output layer. The goal is to train our SAE to reconstruct the upper part of the face from input images without the help of the face detection step used in other methods.  
In general, autoencoders \(A\) with input \(I=\{x_{1},x_{2},\ \ldots,\ x_{n}\}\), hidden representation \(R_{g}=\{h_{1},h_{2},\ \ldots,\ h_{i}\}\), and output \(R_{f}=\{x_{1}^{\sim},\ x_{2}^{\sim},\ \ldots,\ x_{n}^{\sim}\}\) can be represented as in Equation (1).  
\[x_{i}^{\sim}=f(g(x_{i})) \tag{1}\]  
The encoder function \(g\) transfers \(x_{i}\in I\) to \(h_{i}\in R_{g}\) as shown in Equation (2).  
\[h_{i}=g(x_{i}) \tag{2}\]  
Equation (3) explains how the decoder function \(f\) transfers \(h_{i}\in R_{g}\) to \(x_{i}\in I\).  
\[x_{i}^{\sim}=f(h_{i}) \tag{3}\]  
Training the autoencoders aims to minimize \(\Delta\), where \(\Delta\) represents the difference between the input and the output of the autoencoder as shown in Equation (4).  
\[\min(f\cdot g)<\Delta(x_{i},\ x_{i}^{\sim})> \tag{4}\]  
The mean squared error \((MSE)\) is commonly used as a loss function in the autoencoder. Assuming \(M\) equals the number of the observations in the training dataset, then \(MSE\) focuses on the distance between compressed data and reconstructed data as shown in Equation (5) [29; 30].  
\[L_{MSE}=\frac{1}{M}\sum\limits_{i=1}^{M}\left|x_{i}-x_{i}^{\sim}\right|^{2} \tag{5}\]  
To prepare the data for the training and testing phase of SAE, we applied an MTCNN detector on all frontal images in KDEF dataset, then we used the OpenCV library to extract the upper part of the face. Finally, we matched each image of the same person with the same expression but taken from different angles (full left, full right, half left, half right, and straight-on) to the same extracted upper part of the frontal view image. Figure 4 shows an example of our data preparation method.  
SAE was trained to 100 epochs on the prepared KDEF dataset using an Adaptive Moment estimation (ADAM) optimizer [31] and a 46 batch size. The validation loss was 0.036 as shown in Figure 5.  
Figure 4: Data preparation for training and testing SAE. Images (AF01 AFFL, AF01 AFFR, AF01 AFHL, AF01 AFHR, and AF01 AFS) from KDEF dataset. Reprinted/adapted with permission from Ref. [15].  
An illustration of SAE input and output is presented in Figure 6. SAE is designed to process an input image in various poses and generate the upper portion of the face in that image. The output image produced by SAE demonstrated excellent performance in the SAFEPA model, exhibiting promising levels of accuracy. Note that the blurriness of the output does not impact the network's ability to detect pain assessment.