# A Study on Tomato Disease and Pest Detection Method
## 3 YOLOv5 Object Detection Algorithm  
The YOLOv5 object detection algorithm was released by Ultralytics in June 2020 and has since maintained a fast iteration speed, currently developing to version 7.0. Compared with the updated version of YOLOv8, YOLOv5 is simpler in terms of model architecture and faster in terms of speed because its models are smaller and simpler. In terms of accuracy, YOLOv8 has higher performance because its model is more complex. After considering the factors of detection accuracy, model amount, and detection speed, we chose to carry out related research and improvement work based on YOLOv5n because it is more suitable for real-time target detection tasks. There are five network models in the YOLOv5 algorithm, namely YOLOv5n, YOLOv5s, YOLOv5m, YOLOv5l, and YOLOv5x. Each model has the same structure but differs in depth and width, with YOLOv5n having the smallest depth and width and therefore the smallest number of parameters and computational complexity, resulting in faster inference speed. Considering practical application scenarios, this paper selects YOLOv5n as the improved model, and the whole network structure is shown in Figure 1.  
The network structure of YOLOv5n mainly consists of four parts: the input part, the backbone part, the neck part, and the prediction part. The Backbone consists of three modules: CBS, C3_1, and SPPF. The CBS module consists of convolution, batch normalization (BN), and the SiLU activation function. The C3_1 module is a stack of CBS modules with a residual structure that divides the input feature map into two parts. One part is processed by a small convolutional network, and the other part is processed directly by the next layer, and then the two parts are stitched together as the input of the next layer, which can reduce the parameters and computation of the network and at the same time improve the efficiency of feature extraction, thus speeding up model training and inference speed, as shown in Figure 2.  
The SPPF module is a spatial pyramid pooling structure that achieves adaptive output sizes. Unlike traditional pooling structures, the output size of SPPF is independent of the input size, and it can achieve a fixed-dimensional output. The SPPF structure first performs pooling operations of different sizes on the input feature map, then uses convolutional operations to fuse the results of different scales, and finally outputs the fused feature map. The neck uses the PAN (Path Aggregation Network) + FPN (Feature Pyramid Networks) [31] network structure, which mainly consists of CBS, C3_2, Concat, and upsampling modules. C3_1 and C3_2 are used in the backbone and neck of the network, respectively, and the only difference between them is the BottleNeck structure inside, as shown in Figure 3.  
Figure 1: Structure of the YOLOv5n.  
Figure 2: C3_1 structure diagram.  
The purpose of FPN and PAN is to achieve feature fusion. The FPN network generates a feature pyramid with different resolutions by connecting feature maps from different levels of the network. These feature pyramids can capture object information at different scales. The PAN network generates a feature pyramid with different resolutions and semantic information by successively merging higher-level feature maps with lower-level ones. In complex scenarios, using FPN and PAN networks, YOLOv5 can detect objects of different shapes and sizes, improving the robustness and reliability of object detection. The prediction part uses the three different scales of feature maps obtained in the neck to predict large, medium, and small objects. First, these feature maps are divided into lattices, and then predictions are made for each lattice using three anchor boxes. Finally, each detection box outputs a feature vector, including classification probability and object confidence. When an object has multiple prediction boxes, NMS (non-maximum suppression) [32] is used to filter the target boxes.