# Gender Identification of Chinese Mitten Crab Juveniles Based on Improved Faster R-CNN
## 2 Materials and Methods
### Methods
#### 2.3.4 FocalLoss  
Choosing an appropriate loss function is crucial. Currently, the binary cross-entropy loss function is widely used in segmentation, classification, and object detection tasks [29]. For the binary cross-entropy loss function, its definition is as follows:  
\[L=-y\log y^{\prime}-(1-y)\log(1-y^{\prime})=\begin{cases}-\log y^{\prime}&y=1 \\ -\log(1-y^{\prime})&y=0\end{cases} \tag{2}\]  
In the formula: \(L\) is the cross entropy loss. When the sample is positive, \(y=1\), when it is negative, \(y=0\); and \(y^{\prime}\) represents the probability value of a certain category output by the network, and the value range is between 0 and 1. The output of the cross-entropy loss function depends on the model's prediction of the probability value. For positive samples, a larger probability value indicates a prediction closer to the true value, decreasing the cross-entropy loss. On the contrary, for negative samples, the smaller the target probability value, the value of the loss function also decreases. This probability value is affected by each sample point. In the process of predicting the sex of crab juveniles, the existence of outliers or noise may confuse the model and reduce its prediction effect. At present, in the gender determination of Chinese mitten crab juveniles, many uncertainties and complex background factors may affect the final gender result, which increases the difficulty of using the cross-entropy loss function. Therefore, this study introduces the focal loss function to replace cross-entropy loss to further suppress the interference caused by complex environments. After adding parameters \(\alpha\) and \(\beta\) in Equation (3), the calculation of focal loss is shown in Equation (3).  
\[L_{\text{F}}=\begin{cases}-\alpha\big{(}1-y^{\prime}\big{)}^{\beta}\log y^{ \prime}&y=1\\ -(1-\alpha)y^{\prime\beta}\log(1-y^{\prime})&y=0\end{cases} \tag{3}\]  
Returning to our task, which is gender classification, \(y=\)1 means \"male\" and \(y=\)0 means \"female\". The \"\(\alpha\)\" factor is used to balance the weight of positive and negative samples (\(0<\alpha<1\)) and reduce the weight ratio of a large number of negative samples; while the \"\(\beta\)\" factor reduces the loss of easily classified samples. When (\(1-y^{\prime}\)) tends to 1 after being raised to the power of \(\beta\), resulting in a smaller value of the loss function. However, the loss for positive samples with smaller probability values becomes relatively larger. The same goes for negative samples. By comparing the cross-entropy loss function and focal loss, it can be seen that focal loss can adjust the weight of easy-to-classify samples  
Figure 7: Comparison of the effects of improved NMS.  
and difficult-to-classify samples in the loss function during model training. For our task, we can adjust the \"\(\alpha\)\" value in focal loss to solve the problem of imbalanced samples of crab larvae of different genders. Increasing the value of \"\(\beta\)\" allows the model to pay more attention to samples that are difficult to distinguish. In the gender identification of male and female crab juveniles, these difficult-to-distinguish samples may be crucial for gender prediction. After multiple experiments and comparisons, it was found that when \(\alpha=0.25\) and \(\beta=2\), the loss is at its minimum.