# Design of Demonstration-Driven Assembling Manipulator
## 3 Implementation
### Object Recognition with YOLO  
Due to the end-to-end characteristic of deep convolutional neural networks, YOLO directly predicts the locations and the classes of objects in a given input image. The model of YOLO consists of many layers. The first several layers (typically convolutional layers) are used for feature extraction and the last several layers are used for the prediction of the final detection results. Figure 6: Teaching the automatic control process of the manipulator. In detail, YOLO divides an input image into S \(\times\) S grids. Each grid is responsible for outputting B bounding boxes, each of which has four location information variables (x, y, l, w), where (x, y) means the predicted center coordinates and (l, w) means the length and width, as well as one confidence score represented by Pr(Object) which predicts whether this bounding box has object or not. Additionally, C conditional probabilities represented by Pr(Class | Object) are also generated for each grid to indicate the class label for the object. So, the final output layer should have S \(\times\) S \(\times\) (5B + C) units [20]. We can calculate the class label for each bounding box as:  
\[\text{Pr(Class)}\times\text{IOU}=\text{Pr(Class}\,\text{IObject)}\times\text{ Pr(Object)}\times\text{IOU} \tag{4}\]  
If the predicted and bounding box of the object and its corresponding ground truth are denoted as B\({}_{\text{p}}\) and B\({}_{\text{g}}\), respectively, which are both rectangles, the IOU (Intersection Over Union) is defined as the ratio of the area of the intersection of B\({}_{\text{p}}\) and B\({}_{\text{g}}\) to that of their union. Notably, the loss function should be designed carefully in the training process. The objective of the loss function is to achieve a good balance among the three terms, namely, the location of objects, the prediction of object existence, and the classification of object class. This function can be expressed as follows in Equation (5) and Table 3.  
\[J=E+F+G+H+I \tag{5}\]  
Equation (5) is the loss function, which has five parts. The first is the loss of center coordinates of the bounding box. To all real objects, square loss is taken between the predicted values and its ground truth. The second is the prediction loss of length and width of the bounding box. The square root is chosen, since the big deviations are usually much bigger than small ones, and the square root could avoid deviations of small bounding boxes. The third and the fourth parts represent the loss functions of confidence scores of all bounding boxes. And the last one is the class prediction of recognition. \(\lambda\)coord is the weight of the importance of losses related to coordinate predictions. Increasing this weight can help improve the average accuracy. Anoobj can decrease the penalty loss of a bounding box without an object, as many bounding boxes do not contain objects. Here, the default values \(\lambda\)coord = 5 and \(\lambda\)noobj = 0.5 are set according to Reference [20]. YOLO is first pretrained on Pascal VOC 2007 and 2012 datasets and then finetuned on our own datasets to make it adapt to our scenarios. Our training images are collected by the camera of the system and labeled using labelImg ([[https://github.com/tzutalin/labelImg](https://github.com/tzutalin/labelImg)]([https://github.com/tzutalin/labelImg](https://github.com/tzutalin/labelImg))), which is a widely used annotation tool for object detection. YOLO is run with CUDA (Compute Unified Device Architecture) to ensure its real-time performance. The whole application process consists of six steps, as Figure 7 shows: