# Augmented Reality and Machine Learning Incorporation Using YOLOv3 and ARKit
## 4 Object Detection
In YOLOv3, the loss function \(L\) can be calculated using the following equation:  
\[L=Error_{boxes}-Error_{confidence}-Error_{classes}, \tag{1}\]  
where the \(Error_{boxes}\) is the bounding box coordinate regression, which can be defined as follows:  
Figure 11: YOLOv3 neural network architecture. Figure 12: YOLOv3 detection procedure at tensor level. \[Error_{boxes}=\lambda_{coordinate}\sum_{i=0}^{S^{2}}\sum_{j=0}^{N}l_{ij}^{ obj}\bigg{[}\big{(}t_{x}-t_{x}^{\prime}\big{)}^{2}+\Big{(}t_{y}-t_{y}^{\prime} \Big{)}^{2}+\big{(}t_{w}-t_{w}^{\prime}\big{)}^{2}+\big{(}t_{h}-t_{h}^{\prime} \big{)}^{2}\bigg{]}, \tag{2}\]  
where \(S^{2}=S\times S\) cells, \(N\) is the number of bounding boxes in each predicted cell with the corresponding coordinates (\(t_{x}\), \(t_{y}\), \(t_{h}\), \(t_{w}\)), and \(\lambda_{coordinate}\) is the coordinate error weight. The \(Error_{confidence}\) is used to calculate the loss of confidence of the existing object in the bounding box, which can be defined as follows:  
\[Error_{confidence}=\sum_{i=0}^{S^{2}}\sum_{j=0}^{N}l_{ij}^{obj} \big{[}c_{i}^{\prime}log(c_{i})+\big{(}1-c_{i}^{\prime}\big{)}log(1-c_{i}) \big{]}+ \tag{3}\] \[\lambda_{confidence}\sum_{i=0}^{S^{2}}\sum_{j=0}^{N}l_{ij}^{ confidence}\big{[}c_{i}^{\prime}log(c_{i})+\big{(}1-c_{i}^{\prime}\big{)}log(1-c_{i}) \big{]},\]  
where \(c\) is the number of classes, \(\lambda_{confidence}\) is the confidence error weight. The final \(Error_{classes}\) can be calculated using the following equation:  
\[Error_{classes}=\sum_{i=0}^{S^{2}}l_{i}^{obj}\sum_{cclasses}\big{[}p_{i}^{ \prime}(c)log(p_{i}(c))+(1-p_{i}^{\prime}(c))log(1-p_{i}(c))\big{]}. \tag{4}\]  
**Mean average precision**. In most computer vision-based deep neural networks, we use the mean average precision metric (mAP) to evaluate the trained model. Firstly, let us find out what precision means. The precision measures how accurate the model prediction can be by counting the number of correct predictions over the total predictions, described as follows:  
Figure 10:0  
where \(TP\) is true positives, meaning that the positive predictions are correct. \(FP\) represents false positives, meaning that the positive predictions are incorrect. The mAP, on the other hand, does not take the average of those precision values; it uses the Intersection Over Union metric (IOU). The IOU metric determines whether the prediction is correct. It measures overlap between the predicted bounding box and the ground truth bounding box over the union of their area, as described in Figure 10:1  
In each case, we usually set up a default threshold value. If the IoU value is higher than the threshold value, the prediction is defined as \(TP\), otherwise \(FP\). The recall value is calculated in Equation (6) by using \(TP\), \(FP\) and the false negatives \(FN\). Figure 10:2  
Figure 10:3 The Intersection Over Union can be calculated by dividing the area of overlap between the bounding boxes by the area of the union. The average precision \(AP\) can then be defined by finding the area under the precision-recall curve, as shown in the following equation:  
Figure 10:4  
The final \(mAP\) can be calculated as:  
Figure 10:5  
where \(AP_{c}\) is the \(AP\) value of the class \(c\) and \(n\) is the number of classes. The YOLO model also uses \(mAP50\) or mean average precision 50. This metric calculates the \(mAP\) value from 50% to 90% of the \(IoU\) value and increases every iteration by 5%, then takes the average of all of them together. **Training parameters**. The original YOLOv3 was evaluated on the Microsoft COCO (Common Objects in Context) dataset [35]. We keep most of the training parameters as described in the original YOLO paper [32]. However, we replaced the training data with our synthetic data during the training process, as described in Section 3. We trained the network for 80 iterations using 80% of the dataset for training and the rest for validation. The batch size of 16 and sub-division of 8 matched the training hardware requirement for Nvidia RTX 2080 Super.