# Robust Image Inpainting Forensics by Using an Attention-Based Feature Pyramid Network
## 3 Methods
### Loss Function  
The task of inpainting forensics can be characterized as a binary classification problem, wherein the primary objective of the forensics network is to categorize every pixel in the input image as either tamper-free or tampered. In most binary classification tasks, the binary cross entropy (BCE) loss function is widely employed as the preferred loss function. The BCE can be mathematically defined as follows:  
\[L_{B}(G,O)=-\frac{1}{HW}\sum_{i=1}^{H}\sum_{j=1}^{W}(G(i,j)\log O(i,j)(1-G(i,j) )\log(1-O(i,j))), \tag{3}\]  
in this equation, \(G(i,j)\) and \(O(i,j)\) represent the \((i,j)\)th pixel point in the ground truth and the output map, respectively. The resolution of the input image, with dimensions \(H\times W\), is set to 256 in this paper.  
However, in the case of the majority of inpainted images, the ratio of tampered regions is minimal, resulting in a significant disparity between negative samples (un-inpainted areas) and positive samples (inpainted areas). Consequently, if the sole supervision method for training is binary cross-entropy (BCE), the trained model may possess a robust capability to classify negative samples, but it may encounter challenges in accurately classifying positive samples. This, in turn, poses a difficulty for the model to precisely detect the inpainted area. We propose the utilization of the focus loss function [43] as a solution to address the issue of class imbalance. The focus loss function incorporates a modulation factor into the BCE loss function, thereby decreasing the significance of over-classified negative samples in the overall loss. This approach effectively improves the classification performance of positive samples. The focus loss function is mathematically defined as follows:  
\[L_{F}(G,O)=-\frac{1}{HW}\sum_{i=1}^{H}\sum_{j=1}^{W}(\alpha(1-O(i,j))^{\gamma} G(i,j)\log O(i,j)+(1-\alpha)(O(i,j))^{\gamma}(1-G(i,j))\log(1-O(i,j))), \tag{4}\]where the focus loss employs a weighting factor, denoted by \(\alpha\in\{0{\sim}1\}\) to achieve a balanced representation of the positive and negative sample categories. Specifically, \(\alpha\) represents the weight assigned to positive samples, while \((1-\alpha)\) represents the weight assigned to negative samples. Given that the inpainted areas utilized in our paper constitute a range of 5-15%, we set \(\alpha=0.9\). Additionally, the focus loss employs a focusing parameter, denoted by \(\gamma\in\{0{\sim}5\}\), to effectively address the imbalance of difficult and easy samples. Empirical evidence suggests that the optimal experimental outcome is achieved when \(\gamma=2\).  
As the attention mechanism constitutes the core algorithm of this study and there are essential differences between the processing of high-level feature maps and low-level feature maps, the direct impact of the quality of high-level feature maps on network performance necessitates the adoption of focus loss for high-level feature map \(H\), called HF loss. The resulting loss function utilized in this paper is obtained by combining the two focus losses:  
\[L=\lambda_{1}L_{F}(G,H)+\lambda_{2}L_{F}(G,O), \tag{5}\]  
where the hyperparameters \(\lambda_{i},i\in\{1,2\}\) indicate the relative significance of the high-level and output feature maps. For the purposes of this study, both hyperparameters are assigned a value of 1 to underscore the paramount importance of the high-level feature map.