## 3 Methods
### Improvement to the YOLOv5
#### 3.2.1 Improvement on the Network Structure  
First, we embedded the coordinated attention mechanism into the backbone network, enabling it to make targeted choices when extracting features from input samples [32]. The attention mechanism improved the model performance by assigning higher weights to features that are beneficial to network model training, and assigning lower weights to features that have no or even adverse effects on the training conducive of the network model. The object detection algorithm is expected to be able to identify dense small fruits  
Figure 4: Core components of YOLOv5: (**a**) Focus; (**b**) CBL; (**c**) Bottleneck; (**d**) C3; (**e**) SPP. accurately in various situations in complex orchard environments. To improve the detection accuracy on fruits in a natural environment, the attention mechanism is commonly used in the design of the target detection network to better extract the features of citrus. Coordinate attention is the latest attention mechanism, and the principle is shown in Figure 5a. The channel attention works by converting the feature tensor into a single feature vector through the 2D global pool while coordinate attention factorizes channel attention into two parallel 1D feature encoding processes, for the purpose of integrating spatial coordinate information with the generated attention maps. In this way, the network can capture the long-distance dependencies along one spatial direction, while retaining accurate position information along another spatial direction. Based on the above process, a pair of direction-aware and position-sensitive attention maps were then generated from the obtained feature maps. The feature map output by the coordinated attention mechanism is shown in Figure 5b. These attention maps can be used as complementarily information to the input feature maps to strengthen the representation of the object of interest. Coordinate attention captures the cross-channel information, direction perception, and position-sensitive information concurrently, which undoubtedly helps the detector find and identify objects of interest with more accuracy. The coordinated attention mechanism is flexible and lightweight. As such, in this study, we embedded it into the backbone network of the YOLOv5 architecture to improve the detection performance for dense and small objects. Second, we used an efficient two-way cross-scale connection and weighted feature fusion BiFPN in the neck network to replace the PANet multiscale feature fusion network, as shown in Figure 6. The low-level feature map commonly has a higher resolution and contains more information about the location and detailed information of the target object. However, because there are fewer features extracted by the convolutional layer, the semantics of the lower-level feature maps are lower; thus, the feature maps will also contain more noise. While higher-level feature maps are rich in semantic information, these feature maps have low resolution and relatively insufficient perception of the image details. Therefore, we can use BiFPN to make full use of the feature context information of the low-level and high-level features and effectively merge the high-level and low-level features to improve the model detection performance. In BiFPN, the optimization method of cross-scale connection was used: (1) the nodes that only have one input edge are deleted;  
Figure 5: Coordinated attention mechanism. (**a**) Network structure. (**b**) Feature map output from the coordinated attention mechanism. (2) additional edges are inserted between the original input node and output node to fuse more features without using too much cost, if the two nodes locate at the same level; and (3) each bidirectional (top-down and bottom-up) path is treated as one feature network layer, and the procedure is repeated in the same layer multiple times to enable more high-level feature fusion [33]. When input features of different scales are used for feature fusion, their contributions to output features are usually unequal.