# Buckwheat Disease Recognition Based on Convolution Neural Network
## 4 Buckwheat Disease Recognition Based on Convolution Neural Network
### Convolutional Neural Network Structure of Buckwheat Disease Recognition
#### 4.2.2 Convolution Based on Cosine Similarity  
Buckwheat disease data are collected from field, which is limited by the sampling environment and disturbed by noise. Therefore, in order to achieve a higher activation value after convolution operation, only the positions with similar characteristics to the convolution kernel can be obtained in the feature diagram. At the same time, it is necessary to reduce the difference between features and avoid the interference of sample noise on feature extraction. In this paper, the idea of cosine similarity was introduced into the operation of the convolution layer [34], and the input feature map and the convolution kernel were regarded as two vectors, and the correlation between them was calculated.  
Figure 8: Inception 2 structure.  
Figure 7: Inception 1 structure.  
In the traditional convolutional neural network, the output of \(J\)-th feature map belonging to \(l\)-th convolution layer can be expressed as follows:  
\[x_{I}^{l}\ =\ g\Bigg{(}\sum\limits_{I\in M}x_{I}^{l-1}\ *\ W_{IJ}^{l}\ +\ B_{J}^{l}\Bigg{)} \tag{2}\]  
In the equation, \(g(.)\) represents the activation function, \(M\) represents the set of input feature maps, \(W_{IJ}^{l}\) represents the convolution kernel vector used between the \(I\)-th feature map and the \(J\)-th feature map. \(B_{I}^{l}\) is the bias.  
The cosine similarity is an index to measure the similarity between two vectors. It calculates the cosine of the angle between two vectors. The smaller the angle, the higher the correlation between the two vectors, the larger the cosine. Its value range is (\(-1\),\(1\)). Here is how the cosine similarity between \(X\) and \(Y\) is calculated, where \(n\) is the dimension of vector.  
\[\cos(X,Y)\ =\ \frac{\sum\limits_{i=1}^{n}x_{i}y_{i}}{\sqrt{\sum\limits_{i=1}^{n}x_ {i}^{2}}.\sqrt{\sum\limits_{i=1}^{n}y_{i}^{2}}}\ =\ \frac{\langle X,Y\rangle}{\|X\|\|Y\|} \tag{3}\]  
\(F_{I}\left(X_{I}^{l-1},W_{IJ}^{l}\right)\) represents the similarity function between the input feature map of the \(l\)-th convolutional layer and the convolution kernel. \(X\) represents the input feature map vector. The convolution operation based on cosine similarity can be expressed as the following equation:  
\[W_{r\times z}\cdot X_{r\times z}\ =\ \sum\limits_{i=1}^{r}\sum\limits_{j=1}^{ z}w_{ij}x_{ij}/\left(\sqrt{\sum\limits_{i=1}^{r}\sum\limits_{j=1}^{z}w_{ij}^{2}}.\sqrt{\sum\limits_{i=1}^{r}\sum\limits_{j=1}^{z}x_{ij}^{2}}\right) \tag{4}\]  
In Equation (4), \(r\ \times\ z\) represents the size of the convolution kernel. \(w_{ij}\) and \(x_{ij}\) respectively represent the coefficients in the convolution kernel and the feature map. The similarity function can be expressed as:  
\[F_{I}\left(X_{I}^{l-1},W_{IJ}^{l}\right)\ =\ \sum\limits_{I\in M}X_{I}^{l-1} \cdot W_{IJ}^{l}\ +\ B_{J}^{l} \tag{5}\]  
Therefore, in the \(l\)-th convolution layer, the output based on cosine similarity operation is expressed as Equation (6)  
\[x_{I}^{l}\ =\ g(F_{I}) \tag{6}\] \[W_{IJ}^{l}\ =\ \left[W_{1I}^{l},W_{12}^{l},\ldots,W_{In}^{l} \right]^{T}\] \[X\ =\ \left[X_{1}^{l-1},X_{2}^{l-1},\ldots,X_{n}^{l-1}\right]^{T}\]  
\(g(.)\) represents the activation function. The higher the similarity between the input feature map and the convolution kernel \(W_{IJ}^{l}\), the greater the output value of the convolution layer.