# Change and Detection of Emotions Expressed on People's Faces in Photos
## 3 Methodology
### Learning Process  
The classifier used in the generation process was trained using the PyTorch library. Its goal is to correctly classify emotions in images, which is essential for validating and assessing the quality of generated images. The classifier's objective function is based on cross-entropy loss, which is commonly used in classification problems. Class weights were calculated based on the frequency of occurrence of individual emotions in the dataset. These weights are inversely proportional to the number of occurrences of each emotion, which helps balance the impact of less frequent emotions on the learning process. The cross-entropy loss for the classifier is given by  
\[\mathcal{L}_{\text{cls}}=-\sum_{i=1}^{N}w_{i}\cdot y_{i}\cdot\log(g_{i}) \tag{1}\]  
where \(\mathcal{L}_{\text{cls}}\) is the classifier loss; \(N\) is the number of classes; \(w_{i}\) is the weight for class \(i\); \(y_{i}\) is the actual label for class \(i\) (value 1 for the true class, 0 for others); \(\hat{y}_{i}\) is the predicted probability for class \(i\). In the training process of the EmoDNN model, a discriminator was used to distinguish original images from modified ones. The discriminator was trained using a cost function based on hinge loss [37]. The hinge loss for the discriminator consists of two main components: the loss for real images and the loss for generated images. Additionally, a gradient penalty [38] is included to improve training stability. The discriminator analyzes real images \(x_{\text{real}}\) and its output \(\text{out}_{\text{real}}\) should be as high as possible to classify them as real. The hinge loss for real images is calculated as  
\[\mathcal{L}_{\text{real}}=\frac{1}{m}\sum_{i=1}^{m}\text{max}\Big{(}0.1-\text{ out}_{\text{real}}^{(i)}\Big{)} \tag{2}\]  
where \(m\) is the number of samples. The generated images \(x_{\text{fake}}\) are created by the generator based on real images \(x_{\text{real}}\) and desired emotion labels \(x_{\text{fake\_cls}}\). The discriminator analyzes the generated images \(x_{\text{fake}}\) and its output \(\text{out}_{\text{fake}}\) should be as low as possible to classify them as fake. The hinge loss for generated images is calculated as  
\[\mathcal{L}_{\text{fake}}=\frac{1}{m}\sum_{i=1}^{m}\text{max}\Big{(}0.1+\text{ out}_{\text{fake}}^{(i)}\Big{)} \tag{3}\]  
To improve training stability and ensure that gradients are well-conditioned, an additional component called gradient penalty (GP) is used. GP is calculated as the norm of the discriminator's gradient with respect to interpolated samples between real and generated images. The gradient penalty is given by  
\[\text{GP}=(\parallel\
abla_{\hat{x}}D(\hat{x})\parallel_{2}-1)^{2} \tag{4}\]The final cost function for the discriminator is the sum of the losses for real and generated images, as well as the gradient penalty:  
\[\mathcal{L}_{D}=\mathcal{L}_{\text{real}}+\mathcal{L}_{\text{fake}}+\text{GP} \tag{5}\]  
The generator cost function is crucial for training the generator network, which aims to produce realistic images with desired emotions that are indistinguishable from real images. The cost function for the generator combines several components to achieve this goal. The adversarial loss encourages the generator to produce images that the discriminator classifies as real. This is achieved by minimizing the negative output of the discriminator for the generated images \(x_{\text{fake}}\). \[\mathcal{L}_{\text{adv}}=-\frac{1}{m}\sum_{i=1}^{m}\text{out}^{(i)}_{\text{ fake}} \tag{6}\]  
where \(m\) is the batch size and \(\text{out}_{\text{fake}}\) is the discriminator's output for the generated images. The classification loss ensures that the generated images are classified with the desired emotion labels \(x_{\text{fake},\text{cls}}\).