# A Lightweight Crop Pest Detection Method Based on Convolutional Neural Networks
## 2 Materials and Methods
### Generate Prior Boxes with K-Means++  
The prediction box generation method used in this study is derived from YOLOv3, and the elements required in the process of generating the prediction boxes for a pest are shown in Figure 2. Here, \(\left(g_{x},\ g_{y}\right)\) are the coordinates of the grid that contains the pest, \(b_{w}\) and \(b_{h}\) are the size of the prior box, \(\left(p_{x},p_{y}\right)\) are the coordinates of the center point of the prediction box, and \(p_{w}\) and \(p_{h}\) are the size of the prediction box. \begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Index** & **Number** & **From** & **Operator** & **Output** \\ \hline
0 & 1 & \(-1\) & Conv, \(16\), \(3\times 3\) & \(16\times 416\times 416\) \\
1 & 1 & \(-1\) & Conv, \(32\), \(3\times 3\), /2 & \(32\times 208\times 208\) \\
2 & 2 & \(-1\) & Bottleneck, \(32\) & \(32\times 208\times 208\) \\
3 & 1 & \(-1\) & Conv, \(64\), \(3\times 3\), /2 & \(64\times 104\times 104\) \\
4 & 3 & \(-1\) & Bottleneck, \(64\) & \(64\times 104\times 104\) \\
5 & 1 & \(-1\) & Conv, \(128\), \(3\times 3\), /2 & \(128\times 52\times 52\) \\
6 & 6 & \(-1\) & Bottleneck, \(128\) & \(128\times 52\times 52\) \\
7 & 1 & \(-1\) & Conv, \(256\), \(3\times 3\), /2 & \(256\times 26\times 26\) \\
8 & 3 & \(-1\) & Bottleneck, \(256\) & \(256\times 26\times 26\) \\
9 & 1 & \(-1\) & Conv, \(512\), \(3\times 3\), /2 & \(512\times 13\times 13\) \\
10 & 2 & \(-1\) & Bottleneck, \(512\) & \(512\times 13\times 13\) \\
11 & 1 & \(-1\) & Bottleneck, \(1024\) & \(1024\times 13\times 13\) \\
12 & 1 & \(-1\) & Conv, \(256\), \(1\times 1\) & \(256\times 13\times 13\) \\
13 & 1 & \(-1\) & Conv, \(512\), \(3\times 3\) & \(512\times 13\times 13\) \\
14 & 1 & \(-1\) & Conv, \(60\), \(1\times 1\) & \(60\times 13\times 13\) \\
15 & 1 The detailed process of adjusting the yellow prior box and generating the red prediction box is shown in Equations (1)-(4), where \(\big{(}m_{x},m_{y},m_{w},m_{h}\big{)}\) is the offset metric output from the bounding box regression part of the detection method, and \(\sigma\) is the logistic activation function. The center-point coordinates \(\big{(}p_{x},p_{y}\big{)}\), and the size represented by \(p_{w}\) and \(p_{h}\) in the prediction box can be calculated by adjusting the coordinates \(\big{(}g_{x},\ g_{y}\big{)}\) of the upper left corner point in the grid that contains the pest, and the size represented by \(b_{w}\) and \(b_{h}\) of the prior box with the offset metric. Then, the detection result is the output. \[p_{x}=\sigma(m_{x})+g_{x} \tag{1}\]  
\[p_{y}=\sigma\big{(}m_{y}\big{)}+g_{y} \tag{2}\]  
\[p_{w}=b_{w}e^{m_{w}} \tag{3}\]  
\[p_{h}=b_{h}e^{m_{h}} \tag{4}\]  
It is clear that the prior boxes play a decisive role in the detection effect. Many traditional object detection methods adopt artificially designed prior boxes, which cannot guarantee their applicability to different datasets. Setting unsuitable prior boxes will cause the bounding box regression to drastically oscillate in the early stage, slowing down the convergence of the model and leading to a decrease in the detection precision. Therefore, YOLOv3 adopts the k-means to cluster the sizes of the crop pests contained in the dataset and takes the final clustering centers as the prior boxes. The process of generating prior boxes for YOLOv3 is as follows:  
1. Select k initial clustering centers randomly from the size set C composed of the crop pest sizes contained in the dataset labels. 2. Assign the class of each sample in size set C to the class that the nearest clustering center belongs to. 3. The sample mean is calculated in each class cluster as the new clustering center. 4. Repeat step 2 and step 3 until the clustering process converges. The k-means method clusters with randomly selected initial clustering centers, and finally tends towards becoming trapped in the local optimum, making it difficult to achieve the global optimum.