# YOLO-OHFD: A YOLO-Based Oriented Hair Follicle Detection Method for Robotic Hair Transplantation
## 2 Materials and Methods
### Structure of YOLO-OHFD
#### 2.2.4 Loss Calculation Based on Oriented Object Detection  
The loss function of YOLO-OHFD includes localization loss L\({}_{\text{bbox}}\), angle classification loss L\({}_{\text{angle\_class}}\), hair follicle classification loss L\({}_{\text{hair\_class}}\), and confidence loss L\({}_{\text{obj}}\), as shown in Equation (4).  
\[L_{hair}=L_{bbox}+L_{angle\_class}+L_{hair\_class}+L_{obj} \tag{4}\]  
The loss function of YOLO-OHFD includes localization loss L\({}_{\text{bbox}}\), angle classification loss L\({}_{\text{angle\_class}}\), hair follicle classification loss L\({}_{\text{hair\_class}}\), and confidence loss L\({}_{\text{obj}}\), as shown in (4).  
First, among the loss calculations, L\({}_{\text{angle\_class}}\), L\({}_{\text{hair\_class}}\), and L\({}_{\text{obj}}\) can be calculated by using binary cross entropy (BCE), as shown in Equation (5).  
\[\begin{array}{l}L_{angle\_class}+L_{hair\_class}+L_{obj}=\\ -\sum\limits_{i}^{N}{[P_{angle\_class}\log(T_{angle\_class})+(1-P_{angle\_ class})\log(1-T_{angle\_class})]}\\ -\sum\limits_{i}^{N}{[P_{hair\_class}\log(T_{hair\_class})+(1-P_{hair\_ class})\log(1-T_{hair\_class})]}\\ -\sum\limits_{i}^{N}{[P_{obj}\log(T_{obj})+(1-P_{obj})\log(1-T_{obj})]}\\ \end{array} \tag{5}\]  
where P\({}_{\text{hair\_class}}\) and \(T_{hair\_class}\) denote the probability distributions of the prediction box and ground truth box. P\({}_{\text{angle\_class}}\) and T\({}_{\text{angle\_class}}\) denote the probability distributions of the predicted and actual angle. P\({}_{\text{obj}}\) and T\({}_{\text{obj}}\) denote the predicted and actual confidence levels. N\({}_{\text{a}}\) indicates the number of rotation angle categories. N\({}_{\text{h}}\) indicates the number of follicle categories. N\({}_{\text{o}}\) indicates the number of anchor box.  
Then, the localization loss is calculated. In the oriented hair follicle detection, the hair follicle has large aspect ratios. Traditional Intersection over Union (IoU) only focuses on the degree of overlap between bounding boxes, which cannot reflect the actual loss of objects with large aspect ratios well, and increase the difficulty of network learning. However, Enhanced IoU (EIoU) is better adapted to the change of detection box size by introducing effective areas, which is more suitable for the calculation of hair follicle localization loss. The calculation of EIoU is shown in Equations (6)-(9).  
\[L_{EIoU}=L_{lou}+L_{dis}+L_{asp} \tag{6}\]  
\[L_{lou}=1-IoU \tag{7}\]  
\[L_{dis}=\frac{\rho^{2}(b,b^{st})}{c^{2}} \tag{8}\]  
\[L_{asp}=\frac{\rho^{2}(w,w^{st})}{c_{w}^{2}}+\frac{\rho^{2}(h,h^{st})}{c_{h}^ {2}} \tag{9}\]  
Figure 8: Structure of decoupled head network. 1, 2, 3, 3+ denote four classification of hair follicles. \((\text{x},\text{y})\) are the center point of the follicle bounding box, and \((\text{h},\text{w})\) are the width and height of the box. \(\theta\) is the angle of the rotation box, expressed in Gray code G\({}_{7}\)G\({}_{6}\)G\({}_{5}\)G\({}_{4}\)G\({}_{3}\)G\({}_{2}\)G\({}_{1}\).  
where L\({}_{\rm{IoU}}\), L\({}_{\rm{dis}}\), and L\({}_{\rm{asp}}\) are the overlap loss, the center distance loss, and the width and height loss of the predicted box and ground truth box. \(\rho\) is the Euclidean distance. b and b\({}^{\rm{gt}}\) are the center of the predicted box and ground truth box. w and h are the width and height of the predicted box. w\({}^{\rm{gt}}\) and h\({}^{\rm{gt}}\) are the width and height of the ground truth box. c\({}_{\rm{w}}\) and c\({}_{\rm{h}}\) are the width and height of the minimum bounding rectangle that covers the predicted box and the ground truth box.