Agricultural pest detection method based on non-scale depth network
Technical Field
The invention relates to the technical field of accurate agricultural pest target detection, in particular to an agricultural pest detection method based on a non-scale depth network.
Background
China is a big agricultural country, agricultural production accounts for a great proportion in national economy, however, agricultural production is reduced due to pest attack, and the quality of agricultural products is greatly damaged. The monitoring of the species and the number of the agricultural pests is a precondition and a key for predicting and preventing the agricultural pests, and has important application value and significance.
The traditional agricultural pest detection method mainly depends on the plant protection expert to carry out manual identification according to the features of pests, the detection accuracy rate of the traditional agricultural pest detection method is influenced by the knowledge level, experience level and subjective consciousness of the expert, and certain subjectivity and limitation exist. Meanwhile, the agricultural pests are various and numerous in number, and a large amount of manpower, material resources and financial resources are consumed for detecting the agricultural pests.
With the continuous development of computer vision technology, agricultural pest detection by using a deep learning network is more and more widely applied. In the prior art, the detection of pests with different scales is realized by setting recommendation frames with different sizes. However, the scale of the artificially set recommendation box is limited, and cannot well cover targets with different scales, and the non-scale features of the pest targets cannot be obtained and utilized, so that the robustness is poor. Meanwhile, due to the fact that a large number of target frames are recommended, repeated recommendation is serious, detection time is prolonged, and particularly, detection accuracy of small target objects is low.
Disclosure of Invention
The invention aims to provide an agricultural pest detection method based on a non-scale depth network, which overcomes the defects in the prior art and improves the agricultural pest detection precision and efficiency.
In order to achieve the purpose, the invention adopts the following technical scheme:
an agricultural pest detection method based on a non-scale depth network comprises the following steps:
(1) preprocessing given pest image training data.
(2) Constructing a pest non-scale target detector.
(3) And extracting scale-free features of the pest image to be detected, and predicting the confidence coefficient and the position of the pest target.
(4) And post-optimizing the confidence and the position of the acquired pest target.
(5) And judging the position and the number of the pest targets in the pest image to be detected.
Further, the step of "preprocessing the given pest image training data" in step (1) specifically includes the following steps:
(11) compressing and converting all pest image training data into a fixed size.
(12) And marking a minimum rectangular frame containing the pest target in each image, and acquiring coordinate information of the real target position of the pest.
Further, the step (2) of constructing a pest dimensionless target detector specifically comprises the following steps:
(21) randomly initializing weights and biases (W, B) of a deep neural network of a pest non-scale target detector, the weights W of the deep neural network comprising weights and of a depth feature encoderWeights of object detectors, the depth feature encoder comprising N1Layer positive convolution module, N2The target detector is a single-layer fully-connected network, and an output layer of the target detector comprises 6 neurons.
(22) Obtaining a scale-free positive and negative sample by adopting a center point falling method, and sliding on an output characteristic diagram of a deconvolution module by using a sliding window with the size of n multiplied by n; on each sliding position, mapping the central position of the sliding window corresponding to the sliding position to an original image, making a circle with the mapped central point as an original point and R as a radius, and if the central point of a real target frame falls in the circle, regarding the sliding window as a positive sample and marking the sample as 1; otherwise, it is regarded as a negative sample, and is marked as 0.
The original image is an input pest image, and the invention refers to the pest image transformed by resize operation in step (11); the real target frame refers to the smallest rectangular frame of the pest target marked in step (12).
(23) Obtaining a non-scale deep network learning loss function L according to the positive and negative sample sets obtained in the step (22):
wherein k denotes a sample number, NcFor the size of the batch process, NrEqual to the number of samples, α is the balance factor, pkRepresenting a probability value of predicting a pest target;indicating the corresponding sample label, the positive sample takes 1 and the negative sample takes 0.
Representing the true bounding box of the parameterized sample, tk={tx,ty,tw,thDenotes a parameterized sample prediction bounding box; tx=x/Winput,ty=y/Hinput,tw=log(w/Winput),th=log(h/Hinput) (ii) a Correspondingly, x, y, w and h are respectively the abscissa, the ordinate, the width and the height of the upper left corner point of the predicted bounding box; x is the number of*,y*,w*,h*Respectively the abscissa, ordinate, width and height of the upper left corner of the real bounding box; winput、HinputThe width and height of the input pest image are respectively.
(24) Learning the weight and the bias parameters of the deep neural network of the insect scale-free target detector by adopting a BP algorithm, and iterating for N times until the parameters of the target detector reach the optimum:
where l denotes the number of target detector network layers l 1,2, …, N1+N2+2,WlWeight matrix representing the l-th layer, BlThe bias parameter of the l-th layer is indicated, and η indicates the learning rate.
(25) Obtaining optimal non-scale object detector, specifically including obtaining parameters W of optimal feature encoderl,Bl,l=1,2,…,N1+N2+1 and the weight and bias parameters of the optimal target detector: wc1,Wc2,Wb1,Wb2,Wb3,Wb4,bc1,bc1,Bb1,Bb2,Bb3,Bb4Wherein W isc1,Wc2As target confidence regression weight, bc1,bc2As a bias parameter, Wb1,Wb2,Wb3,Wb4As position regression weights, Bb1,Bb2,Bb3,Bb4Is a bias parameter.
Further, the step (3) of extracting the non-scale features of the pest image to be detected and predicting the confidence coefficient and the position of the pest target specifically comprises the following steps:
(31) and (5) obtaining the non-scale characteristic Y of the pest image to be detected by using the non-scale target detector trained in the step (25):
wherein,the weights for the non-scale feature modules are,for the non-scale feature module bias parameters,is the output of the deconvolution module and,it can be recursively derived from the following formula: xl=σ(Wl*Xl-1+Bl),XlFor each layer output characteristic, l denotes the index of the number of target detector network layers, l ═ 1,2, …, N1+N2Denotes the convolution operation, σ () is the ReLu function, X0Inputting a pest image matrix to be detected.
(32) And (5) calculating the confidence coefficient c of each pest candidate frame in the pest image to be detected by using the optimal target detector trained in the step (25) and adopting the following formula:
wherein,e-2.72 is a mathematical constant.
(33) And (5) obtaining the pest target position by using the optimal target detector trained in the step (25) as follows:
x-w on the upper left-hand abscissax×WinputThe ordinate y of the upper left corner is wy×HinputWidth ofAnd height
Wherein, wx=σ(Wb1*Y+bb1),wy=σ(Wb2*Y+bb2),ww=σ(Wb3*Y+bb3),wh=σ(Wb4*Y+bb4)。
Further, the step (4) of obtaining confidence and position of pest target by post optimization specifically includes the following steps:
(41) the Q and P values are corrected using an area compensation strategy, wherein s represents an area of a pest target candidate frame, s1In order to set the area threshold, lambda is an adjusting factor, and the value range of lambda is (0, 1)]And recalculating to obtain the target confidence
(42) Post-optimizing the position of the pest target:
(421) and sorting the target candidate frames according to the confidence coefficient c of the pest target, and marking the candidate frame with the maximum confidence coefficient.
(422) And calculating the intersection ratio of the pest target candidate box with the highest confidence value and each of the rest candidate boxes.
(423) The ratio of the removed intersection is larger than a set threshold value NtThe candidate frame of (1).
(424) And (5) repeating the steps (421), (422) and (423) on the reserved pest target candidate frames until the last candidate frame is reached, ending the iteration, and outputting all marked candidate frames.
Further, the step (5) of determining the position and number of the pest target in the pest image to be detected specifically includes the following steps: giving a threshold value t to all marked candidate frames obtained in the step (4)sSelecting a target confidence value greater than tsAnd taking the candidate frame as a final pest target detection result, and calculating the number of the candidate frame as the total number of pests in the pest image to be detected.
Compared with the prior art, the invention has the beneficial effects that:
(1) the invention adopts a center point-falling method to obtain the scale-free positive and negative samples, utilizes the depth neural network to encode the scale-free characteristics of the pest image, avoids the manual setting of the scale of the target reference frame, is suitable for the recognition of pests with different scales, and improves the precision and the flexibility of the agricultural pest recognition.
(2) The invention adopts an area compensation strategy to balance the confidence weights of the small target and the large target, and is particularly beneficial to the identification and detection of the small pest target.
Drawings
FIG. 1 is a flow chart of a method of the present invention;
FIG. 2 is a structural view of a non-scale target detector for vermin according to the present invention;
FIG. 3 is a schematic illustration of a center-drop method;
FIG. 4 is a flow chart of pest target confidence and location post-optimization in the present invention.
Detailed Description
The invention is further described below with reference to the accompanying drawings:
an agricultural pest detection method based on a non-scale depth network as shown in fig. 1 comprises the following steps:
s1, preprocessing given pest image training data, and specifically comprises the following steps:
and S11, converting all the pest image training data into a fixed size through a resize operation. The resize function is a function in matlab or opencv used to compress an image to a specified size.
And S12, marking a minimum rectangular frame containing the pest target in each image, and acquiring coordinate information of the real pest target position.
And S2, constructing a pest non-scale target detector. The construction of the pest dimensionless target detector specifically comprises the following steps:
s21, randomly initializing weights and biases (W, B) of a deep neural network of the pest non-scale target detector, wherein the weights W of the deep neural network comprise weights of a depth feature encoder and weights of target detectors, and the depth feature encoder comprises N1Layer positive convolution module, N2A layer deconvolution module and a 1-layer scale-free feature module. The non-scale feature module outputs k P-dimensional non-scale feature vectors, the target detector is a single-layer fully-connected network, the output layer comprises 6 neurons, and 2 neurons are used forTarget confidence is predicted, and the other 4 neurons are used for predicting pest target positions.
S22, as shown in figure 3, obtaining a scale-free positive and negative sample by adopting a center point falling method, and sliding on an output characteristic diagram of a deconvolution module by using a sliding window with the size of n multiplied by n; on each sliding position, mapping the central position of the sliding window corresponding to the sliding position to an original image, making a circle with the mapped central point as an original point and R as a radius, and if the central point of a real target frame falls in the circle, regarding the sliding window as a positive sample and marking the sample as 1; otherwise, it is regarded as a negative sample, and is marked as 0.
The original image is an input pest image, and the invention refers to the pest image transformed by resize operation in step (11); the real target frame refers to the smallest rectangular frame of the pest target marked in step (12).
S23, obtaining a non-scale deep network learning loss function L according to the positive and negative sample sets obtained in the step S22:
wherein k denotes a sample number, NcFor the size of the batch process, NrEqual to the number of samples, α is the balance factor, pkRepresenting a probability value of predicting a pest target;indicating the corresponding sample label, the positive sample takes 1 and the negative sample takes 0.
Representing the true bounding box of the parameterized sample, tk={tx,ty,tw,thDenotes a parameterized sample prediction bounding box; tx=x/Winput,ty=y/Hinput,tw=log(w/Winput),th=log(h/Hinput) (ii) a Correspondingly, x, y, w and h are respectively the abscissa, the ordinate, the width and the height of the upper left corner point of the predicted bounding box; x is the number of*,y*,w*,h*Respectively the abscissa, ordinate, width and height of the upper left corner of the real bounding box; winput、WinputThe width and height of the input pest image are respectively.
S24, learning the weight and the bias parameter of the deep neural network of the insect scale-free target detector by adopting a BP algorithm, and iterating for N times until the target detector parameter is optimal:
where l denotes the number of target detector network layers l 1,2, …, N1+N2+2,WlWeight matrix representing the l-th layer, BlThe bias parameter of the l-th layer is indicated, and η indicates the learning rate.
S25, obtaining the optimal non-scale target detector, specifically, obtaining the parameter W of the optimal feature encoderl,Bl,l=1,2,…,N1+N2+1 and the weight and bias parameters of the optimal target detector: wc1,Wc2,Wb1,Wb2,Wb3,Wb4,bc1,bc1,Bb1,Bb2,Bb3,Bb4Wherein W isc1,Wc2As target confidence regression weight, bc1,bc2As a bias parameter, Wb1,Wb2,Wb3,Wb4As position regression weights, Bb1,Bb2,Bb3,Bb4Is a bias parameter.
S3, extracting the scale-free characteristics of the pest image to be detected, and predicting the confidence coefficient and the position of the pest target, wherein the method specifically comprises the following steps:
s31, obtaining the scale-free features of the pest image to be detected by using the scale-free feature encoder trained in the step S25:
wherein,the weights for the non-scale feature modules are,for the non-scale feature module bias parameters,is the output of the deconvolution module and,it can be recursively derived from the following formula: xl=σ(Wl*Xl-1+Bl),XlFor each layer output characteristic, l denotes the index of the number of target detector network layers, l ═ 1,2, …, N1+N2Denotes the convolution operation, σ () is the ReLu function, X0Inputting a pest image matrix to be detected.
S32, calculating the confidence coefficient c of each pest candidate frame in the pest image to be detected by using the optimal target detector trained in the step (25) by adopting the following formula:
wherein,e-2.72 is a mathematical constant.
S33, using the optimal target detector trained in the step S25 to obtain the target position of the pests as follows:
x-w on the upper left-hand abscissax×WinputThe ordinate y of the upper left corner is wy×HinputWidth ofAnd height
Wherein, wx=σ(Wb1*Y+bb1),wy=σ(Wb2*Y+bb2),ww=σ(Wb3*Y+bb3),wh=σ(Wb4*Y+bb4)。
And S4, post-optimizing the confidence and the position of the acquired pest target.
As shown in fig. 4, the "confidence and location of pest target obtained by post optimization" specifically includes the following steps:
s41, correcting the Q and P values by using an area compensation strategy, wherein s represents an area of a pest target candidate frame, s1In order to set the area threshold, lambda is an adjusting factor, and the value range of lambda is (0, 1)]And recalculating to obtain the target confidence
S42, post-optimizing the position of the pest target:
and S421, sequencing the target candidate frames according to the confidence coefficient c of the pest target, and marking the candidate frame with the maximum confidence coefficient.
S422, calculating the intersection ratio of the pest target candidate frame with the highest confidence value and each of the rest candidate frames.
S423, removing the intersection ratio larger than the set threshold value NtThe candidate frame of (1).
And S424, repeating the steps (421), (422) and (423) on the reserved pest target candidate frames until the last candidate frame is reached, ending the iteration, and outputting all marked candidate frames.
S5, judging the position and the number of the pest target in the pest image to be detected, which comprises the following steps: giving a threshold value t to all marked candidate frames obtained in the step (4)sSelecting a target confidence value greater than tsAnd taking the candidate frame as a final pest target detection result, and calculating the number of the candidate frame as the total number of pests in the pest image to be detected.
The above-mentioned embodiments are merely illustrative of the preferred embodiments of the present invention, and do not limit the scope of the present invention, and various modifications and improvements of the technical solution of the present invention by those skilled in the art should fall within the protection scope defined by the claims of the present invention without departing from the spirit of the present invention.