# Deep Learning-Based Algorithm for Recognizing Tennis Balls
## 3 Experimental Method of Recognition Algorithm
### The Recognition Algorithm of Tennis Ball Based on Deep Learning
The loss here is the classical binary cross-entropy loss, \(p_{i}\) is the anchor predicted as the target. The probability of the GT label is:  
\[p_{i}^{*}=\left\{ \begin{array}{ll}0\;\textit{negative label}\\ 1\;\textit{positive label}\end{array} \right. \tag{5}\]  
Figure 14: Adding mask branches based on Faster R-CNN framework. \(p_{i}^{*}\) is 1 in the presence of objects (positive) and 0 in the absence of objects (negative), meaning that only the foreground is computed as loss and the background is not computed as loss. Moreover, \(L_{cls}(p_{i},p_{i}^{*})\) is the logarithmic loss for both categories (target and non-target). \[L_{cls}(p_{i},p_{i}^{*})=-log[p_{i}^{*}p_{i}+(1-p_{i}^{*})(1-p_{i})] \tag{6}\]  
The classification loss of RPN is the cross-entropy loss of binary classification, while Fast R-CNN is the cross-entropy loss of multi-classification. A total of 128 RoIs are selected in Fast R-CNN during training, i.e., \(N_{cls}\) = 128, and the values of labels are 0 to 4. \[L_{box}=\lambda\frac{1}{N_{reg}}\sum_{i}p_{i}^{*}L_{reg}(t_{i},t_{i}^{*}) \tag{7}\]  
Regarding the regression loss function \(L_{box}\), as shown in Formula (7), where \(t_{i}=\{t_{x},t_{y},t_{w},t_{h}\}\) is a vector representing the offset predicted by the anchor, RPN training phase (RoIs, Fast R-CNN training phase), and \(x\), \(y\), \(w\), and \(h\) represent the center coordinates, width, and height of the anchor box anchor, respectively. \(t_{i}^{*}\) is the same dimension as \(t_{i}\) vector that represents the actual offset of anchor, RPN training phase (RoIs, Fast R-CNN training phase) with respect to GT:  
\[L_{reg}(t_{i},t_{i}^{*})=R(t_{i}-t_{i}^{*}) \tag{8}\]  
\(R\) in Formula (8) is the function, with the difference that here \(\sigma\) = 3, RPN training phase (\(\sigma\) = 1, Fast R-CNN training phase):  
\[smooth_{L_{1}}(x)=\left\{ \begin{matrix}0.5x^{2}*1/\sigma^{2}\text{ }if\ |x|<1/\sigma^{2}\\ |x|-0.5\text{ }otherwise\end{matrix} \right. \tag{9}\]  
Among them,  
\[x=t_{i}-t_{i}^{*} \tag{10}\]  
For each anchor, \(p_{i}^{*}\) is multiplied after calculating the part. As mentioned before, \(p_{i}^{*}\) is 1 in the presence of objects (positive) and 0 in the absence of objects (negative), implying that only the foreground is calculated as loss and the background is not. The parameter can be interpreted as a weight parameter introduced to balance the classification loss and the regression loss. Finally, the mask loss function is discussed. For each RoI, the mask branch has an output of K \(\times\) m \(\times\) m dimensions, which encodes K masks of size m \(\times\) m, and each RoI has K categories. We use the per-pixel sigmoid and define it as the average binary cross-entropy loss. An RoI corresponding to the kth category in GT is defined only over the kth mask (the other k-1 masks output no contribution to the overall loss). Our definition allows the network to generate a mask for each class without competing with other classes; we rely on the class labels predicted by the classification branch to select the output mask. This differs from semantic segmentation using FCN, which typically uses a per-pixel sigmoid and a multinomial cross-entropy loss, in which case there is competition between masks; since we use a per-pixel sigmoid and a binary loss, there is no competition between the different masks. Experience shows that this can improve the effectiveness of instance segmentation. The specific formula for the loss function of the mask is shown in Formula (11):  
\[L_{mask}=\frac{1}{m^{2}}\sum_{i}^{k}\left(1^{k}\right)\sum_{1}^{m^{2}}[-y* log(sigmoid(x))-(1-y)*log(1-sigmoid(x))] \tag{11}\]  
where \(1^{k}\) means 1 when the kth channel corresponding to the target is the real category, otherwise 0; \(y\) means the label value of the mask at the current position, 0 or 1; \(x\) means the output value at the current position, and _sigmoid(x)_ means the result of the output \(x\) transformed by the sigmoid function.