# EHANet: An Effective Hierarchical Aggregation Network for Face Parsing
## 3 Methodology
### EHANet for Segmentation  
The loss functions can be described as:  
\[L_{\rm s}=-\frac{1}{N}\sum_{n\in N}\sum_{i,j}p_{n}^{\rm s}(i,j)\cdot\log(\hat{p_{n }}(i,j)) \tag{2}\]  
\[L_{b}=\left\{ \begin{array}{ccc}-\sum_{i,j}\beta\cdot\log(\hat{p}^{b}(i,j))&p^ {b}(i,j)=1,\\ -\sum\limits_{i,j}(1-\beta)\cdot\log(1-\hat{p}^{b}(i,j))&p^{b}(i,j)=0\end{array} \right. \tag{3}\]  
\[L_{c}=-\frac{1}{N}\sum_{n\in N}\sum_{i,j}\cdot p_{n}^{c}(i,j)\cdot\log(\hat{p_{ n}^{c}}(i,j)) \tag{4}\]  
where \(L_{s}\), \(L_{b}\), and \(L_{c}\) refer to the losses of the coarse-segmentation, boundary-aware, and combined-segmentation branches, respectively. \(\hat{p}\) and \(p\) represent the N-channel confidence map and the N-channel ground truth, respectively. (\(i\), \(j\)) denotes the 2D coordinates of the pixel. Based on the binary cross-entropy, boundary-aware loss allocates an appropriate weight ratio \(\beta\) to alleviate the imbalance of foreground and background categories. According to strategy 1, contrary to CE2P [35], we introduce a coefficient \(\theta\) to consolidate the influence of the boundary. As shown in Equations (5) and (6), a positive \(\alpha\) is set to enforce the weight of boundary pixels, which is called weighted boundary-aware loss. \[L_{w}=-\frac{1}{N}\sum_{n\in N}\sum_{i,j}\theta\cdot p_{n}^{c}(i,j)\cdot\log( \hat{p_{n}^{c}}(i,j)) \tag{5}\]  
\[\theta=\left\{ \begin{array}{ccc}1+\alpha&p^{b}(i,j)=1,\\ 1&p^{b}(i,j)=0\end{array} \right. \tag{6}\]  
\[L_{total}=\lambda_{s}\cdot L_{s}+\lambda_{b}\cdot L_{b}+\lambda_{w}\cdot L_{w} \tag{7}\]  
\(L_{total}\) denotes the total loss function. \(\lambda_{s}\), \(\lambda_{b}\), and \(\lambda_{w}\) are utilized to balance the losses during training. The parameters \(\Theta\) of the EHANet are optimized by minimizing Equation (7), which are formulated as \(\min_{\Theta}L=L_{total}\). During the training process, \(\lambda_{s}\), \(\lambda_{b}\), and \(\lambda_{c}\) are empirically set to 1, 1, and 2 respectively. Moreover, \(\alpha\) is experimentally set to 50. For a more detailed discussion of \(\alpha\), refer to Section 5.3.1. To strike a balance between speed and performance, we built a lightweight network based on a truncated ResNet18 [9] backbone, which was pretrained on ImageNet. The ResNet [9] backbone is composed of a stem block and four bottlenecks. Among them, the stem block contains a \(7\times 7\) convolutional layer and a max pooling layer, each of which reduces the dimensions to 1/2. Except for the first of the four bottlenecks, the steps of the remaining bottlenecks are unified at 2. In addition, each bottleneck consists of two 3\(\times\)3 convolutions and one skip connection. It is worth noting that the default paradigm is \(Conv+BN+Relu\), except when feature fusion is required. Therefore, the total down-sampling rate of the network is 32. Considering that ResNet Stem lacks context, the stage contextual attention mechanism (SCAM) is only laterally connected with stage-1, stage-2, and stage-3, just like the structure of a feature pyramid network (FPN) [8]. Subsequently, an semantic gap compensation block (SGCB) module is added after each SCAM. Moreover, both the reduction factor and the expansion factor in SCAM module are 8. The dilation rates \(r_{i}\) in the three SGCBs are \((1,\ 2,\ 4)\), \((3,\ 6,\ 9)\), and \((7,\ 9,\ 13)\). Regardless of SCAM or SGCB, the number of output channels is set to 256. For the auxiliary boundary-aware (BA) module, we introduce two parallel paths from stage-1 and stage-2 to extract detailed features, and then concatenate them after the up-sampling operation. More specifically, the parallel branches all go through a 3\(\times\)3 deep separable convolution with stride 1 and a 1\(\times\)1 conventional convolutionto refine the features. On the one hand, this module is used as auxiliary supervision. On the other hand, after \(3\times 3\) convolution, it performs fine-segmentation along with the coarse-segmentation sub-network.