A method for measuring body size of group-raised pigs based on stacked hourglass network
Through the combination of stacked hourglass network and SVM algorithm, the problems of low measurement efficiency and poor accuracy in measuring mass pig body rulers are solved, and accurate non-contact pig body size measurement is achieved in the mass farming environment.
Patent Information
- Application Number
- CN202111350628.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-15
- Publication Date
- 2024-11-22
- Estimated Expiration
- 2041-11-15
AI Technical Summary
The prior art has problems such as low measurement efficiency, transmission of human and animal diseases and image acquisition in the measurement of pig body rulers in the prior art, especially in the case of pig body occlusion and adhesion in the pig body.
Using a stacked hourglass network method, the pig body image separation is performed through Mask R-CNN, and the pig body joint nodes are obtained by combining the stacked hourglass network, and the support vector machine (SVM) algorithm is designed for node screening to realize contactless body ruler measurement. .
Improve the accuracy and efficiency of pig body size measurement, avoid the problem of low accuracy of joint node detection caused by pig house environment complexity and pig body occlusion, and provide a non-contact measurement method suitable for group pig farms. .
Smart Images

Figure CN114120359B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the fields of computer vision, detection and breeding technology, and in particular to a method for measuring the body size of group-raised pigs based on a stacked hourglass network. Background Art
[0002] Modern pig farms tend to be intelligent and automated, which requires modern intelligent methods to manage pigs in group-raising mode. The body size of pigs can reflect the growth and development of pigs, and is also an important indicator for breeding and meat quality evaluation of breeding pigs. At present, the body size measurement methods of group-raising pigs in my country mainly include manual measurement and traditional machine learning methods. Among them, the direct contact of the pig body by manual measurement methods will cause stress reactions in pigs, low measurement efficiency, and disease transmission between humans and animals. The body size measurement of pigs based on traditional machine learning methods is mostly to extract the contour of the static image of the pig, and then use the inflection point extraction algorithm based on the concave structure of the envelope to find the key corner points of the pig body, so as to calculate the body size data of the pig. However, this method requires the collected pig body image to be complete, the back is straight, and there is no tilted head. In the actual image acquisition process, due to the living habits of pigs, it is difficult to collect ideal images, and the pig bodies in the group-raising state are easy to block and adhere to each other. This method is difficult to apply to group-raising pig farms.
[0003] Joint point detection comes from human posture detection. Human joint point detection is the basis of many computer vision tasks, such as action classification, abnormal behavior detection, and autonomous driving. In recent years, with the development of deep learning technology, the effect of human joint point detection has been continuously improved, and it has begun to be widely used in related fields of computer vision. Stacked hourglass network is a kind of joint point detection technology. It stacks lightweight hourglass networks in an end-to-end manner, which can not only extract features at different scales, but also obtain the spatial relationship between joint points.
[0004] The present invention aims at group-raising pigs in complex environments, uses instance segmentation technology to separate the pig body in the image, obtains the pig body joints for body size measurement based on a stacked hourglass network, and designs a joint point screening algorithm to screen the joint point detection results before measuring the pig body size, which partially solves the defects existing in the prior art in pig body size measurement. Summary of the invention
[0005] To solve the above problems, the present invention discloses a method for measuring the body size of group-raised pigs based on a stacked hourglass network. The technical problems to be solved by the present invention are: first, separating pigs in a group-raised state; second, obtaining the joints of the pigs based on the stacked hourglass network; third, screening the detection results of the joints of the pigs; and fourth, providing a non-contact body size measurement method based on the screened joint detection results.
[0006] In order to solve the above technical problems and achieve the purpose of the present invention, the technical solution adopted by the present invention is as follows:
[0007] A method for measuring the body size of group-raised pigs based on a stacked hourglass network comprises the following steps:
[0008] S1. An initial sample library of pig images is obtained in a group pig house environment. The initial sample library is input into a Mask Region-based Convolutional Neural Network (Mask R-CNN). The region of interest (RoI) obtained by the Mask R-CNN region proposal network and the mask (Mask) obtained by the mask network are extracted, and the two are fused to separate the multiple target pigs in the image and establish a new sample library.
[0009] S2. Input the new sample library into the Stacked Hourglass Network (SHN) to obtain the pig body joint points. The Stacked Hourglass Network uses the residual module as the basic module and can extract features of different scales. By stacking 4 hourglass networks, the spatial relationship between feature points can be better obtained.
[0010] S3. Design a joint point screening method based on the support vector machine (SVM) classification algorithm. By giving pig joint point samples with different characteristics, find a hyperplane to classify the samples, and then screen out the joint point samples suitable for body size measurement.
[0011] S4. Construct a body size measurement model, use the joint point screening results to obtain the five joint points of the pig body, and calculate the body size data of the pig.
[0012] Further, the specific process of step S1 is as follows:
[0013] S11. In a group pig house, a depth camera is installed directly above the pig house to obtain a depth image from a bird's-eye view. The obtained depth image is enhanced by using a median filter to remove a large amount of interference noise and contrast-limited adaptive histogram equalization to obtain an original sample library. The image annotation tool LabelMe is used to annotate the external bounding boxes of the pigs in the images in the initial sample library to obtain an initial training sample set.
[0014] S12. Input the initial sample library into Mask R-CNN. Mask R-CNN is a two-stage instance segmentation algorithm improved on the basis of Faster R-CNN, which can simultaneously realize multi-target classification, target detection and mask segmentation.
[0015] S13. Extract the RoI obtained by the region proposal network of Mask R-CNN and the Mask obtained by the mask network, fuse the two, and then separate the multiple target pigs in the image to establish a new sample library.
[0016] Furthermore, the Mask R-CNN in step S12 is a two-stage target detection network: in the first stage, a feature pyramid network is constructed to perform multi-scale feature fusion, and then the RoI of the target object is generated by the region proposal box network to separate the pigs in the group pig house; in the second stage, the region of interest is aligned (RoI Align) and then input into the fully connected layer to obtain the classification and bounding box regression results. At the same time, Mask R-CNN also outputs a binary Mask of a pig for each RoI, and the RoI and Mask of the pig are used as the basis for building a new sample library.
[0017] Furthermore, the backbone feature extraction network of the Mask R-CNN in step S12 is composed of a deep residual network resnet101 and a feature pyramid network. The pig image can obtain five feature layers of different scales through the backbone feature extraction network. The five feature layers are sequentially input into the region proposal box network to predict the candidate box coordinates of the pig, and then the obtained candidate box is used to intercept and fix the size of the feature map input into the RoI Align layer to obtain a RoI of fixed size. Finally, the output of the three branches of Mask R-CNN is obtained through the fully connected layer, two of which perform classification and border regression operations, and the other branch uses a full convolutional network to classify the pixels in the detection box that has been obtained to contain the pig, and obtains the final pig Mask by upsampling.
[0018] Furthermore, the RoI and Mask in step S13 refer to the RGB RoI image of the pig separated from the group pig image by Mask R-CNN and the binary Mask image matched therewith. The binary Mask image of the pig is three-channel copied and expanded, and then the dot multiplication operation is performed with the RGB RoI image, and the obtained Mask RGB image of the pig achieves background removal. The MaskRGB image is fixed to a size of 256×256 pixels (zero padding is performed if the size is insufficient), and it is amplified by 90 degrees, 180 degrees, 270 degrees, horizontal mirroring, and vertical mirroring to obtain a new sample library.
[0019] Furthermore, before inputting the stacked hourglass network in step S2, the new sample library is divided into training set, validation set and test set. The outer bounding box of the pig in the image in the training set, as well as the five joint points of the left ear root, right ear root, left back, right back and tail root are labeled using the labeling software LabelMe. The training sample set includes an image data set and a label file in .txt format.
[0020] Furthermore, the stacked hourglass network in step S2 is composed of four stacked hourglass networks, and the structure of each hourglass network is bilaterally symmetrical. The residual module is used as the basic module to extract features of pig images at different scales. The semantic information of the pig image learned by the previous hourglass network module is used as the input of the next hourglass network module through the stacking of convolutional layers and pooling layers, so that the stacked hourglass network learns rich semantic features. There is a cross-stage connection between each hourglass network structure in the stacked hourglass network, and each hourglass network corresponds to a loss function and participates in the final loss function calculation, thereby reducing the final loss information and improving the prediction accuracy of the model.
[0021] Furthermore, step S2 inputs the new sample library into the stacked hourglass network. Specifically, the pig image first passes through the initial convolution module to reduce the resolution from 256 to 64. After a downsampling operation, the image is input into the first hourglass network. The output result of the hourglass network is processed by the residual module and the convolution layer to obtain the first response heat map. The input feature map, output feature map, and response heat map of the previous hourglass network are combined. Figure 3 The four hourglass networks are fused and used as the input of the next hourglass network. The response heat map generated by each hourglass network is used to calculate the loss value. After four cascaded hourglass networks, a response heat map is finally output. The response heat map is transformed with the original image to obtain the five joints of the pig: left ear root, right ear root, left back, right back, and tail root.
[0022] Further, the specific process of step S3 is as follows:
[0023] S31, marking the pig body joint points obtained in step S2 as body measurement points, such as Figure 3 As shown, point 1 and point 2 represent the measurement points of the left ear root and the right ear root, point 3 and point 4 represent the measurement points of the left back and the right back, point 5 represents the measurement point of the tail root, and the measurement point 6 is set as the midpoint of point 1 and point 2.
[0024] S32. Establish five features to distinguish good and bad joint point samples, such as Figure 4 As shown, specifically, the number of pig joint points num, the connection line L obtained in step S2 are calculated 12 Connect with L 56 Angle ∠1, connecting line L 34 Connect with L56 Angle ∠2, connecting line L 12 Connect with L 34 The angle of the extension line ∠3, point 6 to L 34 The vertical distance d1 from point 5 to L 34 The ratio of the vertical distance d2 Create feature vector Where i represents the i-th sample, JP i Represents the feature vector of the i-th sample. If the joint point sample represented by the feature vector of the i-th sample is a sample suitable for body measurement, JP i Marked as 1, otherwise marked as -1.
[0025] S33. Use the SVM algorithm with Gaussian kernel function to build a joint point screening model. The SVM binary classification model is specifically: g(JP)=w T φ(JP)+b
[0026] Where w is the weight coefficient of the hyperplane, b is the classification threshold, and the kernel function φ(JP) represents the ability to map data to a high-dimensional space to increase linear learning. φ(JP) is selected as the Gaussian kernel function, as shown in the formula:
[0027]
[0028] The penalty coefficient and kernel coefficient of the model are searched by grid search method to find the optimal parameters of the model. The joint point samples obtained by stacking hourglass networks are classified to screen out the joint point samples suitable for body size measurement.
[0029] Furthermore, the body measurement model in step S4 measures the body size of the pig, including body length and body width, where body width refers to the width of the pig's back. Using the pig joint points obtained by the joint point screening algorithm in step S3, the distance from point 5 to point 6 is calculated to obtain the pig's body length data, and the distance from point 3 to point 4 is calculated to obtain the pig's body width data. The body size calculation formula is shown in Table 1:
[0030] Table 1
[0031]
[0032] (III) Beneficial effects
[0033] Compared with the prior art, the present invention provides a method for measuring the body size of group-raised pigs based on a stacked hourglass network, which has the following beneficial effects:
[0034] 1. This group-raised pig body size measurement method based on the stacked hourglass network integrates multiple fields such as computer vision technology, detection technology and breeding technology.
[0035] 2. This group-raised pig body size measurement method based on the stacked hourglass network, before using the stacked hourglass network to obtain joint points, first separates the group-raised pigs to obtain single pig images, and performs size fixation and data enhancement to improve the quality of training samples input into the stacked hourglass network, and avoid the problem of low joint point detection accuracy caused by complex pig house environment, pig body occlusion, adhesion, etc.
[0036] 3. The group-raised pig body size measurement method based on the stacked hourglass network designs a joint point screening method based on SVM. Before calculating the pig body size data, the joint point detection results are screened. Specifically, the pig body in the image is required to be complete, without tilted head or lowered head, and the back is straight, so as to avoid the problem of low accuracy of pig body size calculation due to unsatisfactory pig posture and inaccurate joint point detection results. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 is a flow chart of the overall method of the present invention;
[0038] Figure 2 It is a diagram of the stacked hourglass network structure;
[0039] Figure 3 It is a schematic diagram of the measurement points of pig body size data of the present invention, wherein 1 and 2 represent the measurement points of the left ear root and the right ear root, 3 and 4 represent the measurement points of the left back and the right back, 5 represents the measurement point of the tail root, and 6 represents the midpoint of 1 and 2.
[0040] Figure 4 This is a partial characteristic schematic diagram of the present invention for screening pig body joint points, where ∠1 represents the connecting line L 12 Connect with L 56 The angle of ∠2 represents the line L 34 Connect with L 56 The angle of ∠3 represents the line L 12 Connect with L 34 The angle of the extension line, Indicates point 6 to L 34 The vertical distance d1 from point 5 to L 34 The ratio of the vertical distance d2. DETAILED DESCRIPTION
[0041] The present invention is further explained below in conjunction with the accompanying drawings and specific embodiments. It should be understood that the following specific embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention. It should be noted that the words "front", "rear", "left", "right", "upper" and "lower" used in the following description refer to directions in the accompanying drawings, and the words "inner" and "outer" refer to directions toward or away from the geometric center of a specific component, respectively.
[0042] Figure 1The present invention is further described below in conjunction with the accompanying drawings and embodiments.
[0043] Example 1
[0044] The specific implementation is:
[0045] Step 1. In a group pig house, a depth camera is used to obtain a depth image of the group pig house, and the original image is preprocessed to complete the establishment of an initial sample library; the initial sample library is input into Mask R-CNN, which is a two-stage instance segmentation algorithm improved on the basis of Faster R-CNN, and can simultaneously realize multi-target classification, target detection and mask segmentation; the RoI obtained by the region proposal box network of Mask R-CNN and the Mask obtained by the mask network are extracted, and the two are fused to separate the multi-target pigs in the image and establish a new sample library.
[0046] Step 2: Input the new sample library into the stacked hourglass network to obtain the pig body joint points. The stacked hourglass network uses the residual module as the basic module and can extract features of different scales. By stacking 4 hourglass networks, the spatial relationship between feature points can be better obtained.
[0047] Step 3: Design a joint point screening method based on the SVM classification algorithm. By giving pig joint point samples with different characteristics, find a hyperplane to classify the samples, and then screen out joint point samples suitable for body size detection.
[0048] Step 4: Construct a body size measurement model, use the joint point screening method to obtain the five joint points of the pig, and calculate the body size data of the pig.
[0049] The method for establishing the initial sample library of step one specifically includes: the depth image of the pigs can be obtained in real time by installing a depth camera in a group pig house, for example, by fixing the depth camera at a certain height from the ground and shooting at a top-down angle to obtain a depth video image. The depth image provides distance information and provides data information for subsequent body size measurement. The depth video image is converted into a depth image for annotating training samples and recognition tests. For the collected depth image, a large amount of interference noise is first removed by the median filtering method, and then the depth image is enhanced by contrast-limited adaptive histogram equalization to improve the contrast and obtain an initial sample library of depth images. Use the image annotation tool LabelMe to annotate the external bounding box of the pig body in the image in the initial sample library to obtain an initial training sample set.
[0050] The specific process of the Mask R-CNN in step 1 for instance segmentation of the group pig image is as follows: first, the group pig image is scaled to a fixed size and input into the backbone convolutional neural network, and five feature maps of different scales of the pig image are obtained through Resnet101 and the feature pyramid network. The feature maps are sequentially sent to the region proposal box network to extract the RoI where the target may exist, that is, the candidate box predicted by the region proposal box network is obtained. Then, the candidate box is used to intercept and fix the size of the feature map input to the RoI Align layer. RoI Align retains the image values on the floating point coordinates directly discarded by the quantization operation through the scaling method "bilinear interpolation", so that the pixels of the original image and the pixels of the feature map are completely aligned to obtain a RoI of fixed size. Finally, the output of the three branches of Mask R-CNN is obtained through the fully connected layer: two of the branches are classified and bounding box regression operations through the fully connected layer. Classification is to separate the detected area containing pigs from the background, and bounding box regression is used to correct the bounding box containing pigs to make it more accurate. The newly added mask branch of Mask R-CNN uses a fully convolutional network to classify the pixels in the detection box and obtains a pig mask that adapts to the size of the original image through upsampling.
[0051] During the training process, the loss function of the two-stage Mask R-CNN instance segmentation algorithm model can be divided into three parts: one is the loss function of the bounding box regression, one is the loss function of the classification network, and the other is the loss function of the mask branch. The expression of the total error function is as follows:
[0052] l=l cls +l reg +l mask
[0053] The classification branch loss function l cls is the cross entropy function, and the regression branch loss function l reg for smooth L1 Loss function, segmentation mask branch loss function l mask is the average binary cross entropy loss function.
[0054] The RoI and Mask in step 1 refer to the RoI image of pigs separated by Mask R-CNN from a group pig image of a fixed size and the Mask image matching it. The RoI image is an RGB image, and the Mask image is a binary image. The Mask image channel is first copied and expanded to become a three-channel image, and then a point multiplication operation is performed with the RoI image to obtain a fused Mask RGB image, which achieves the purpose of pig body separation and background removal, and is conducive to subsequent joint point detection. Since the image data size required by the stacked hourglass network for joint point detection is 256×256 pixels, the obtained Mask RGB image is scaled to a size of 256×256 pixels and then amplified by 90 degrees, 180 degrees, 270 degrees, horizontal mirroring and vertical mirroring to finally obtain a new sample library. The new sample library is divided into training set, validation set and test set. The labeling software LabelMe is used to label the outer bounding box of the pig in the training set image, as well as the five joint points of the left ear root, right ear root, left back, right back, and tail root. The training sample set includes an image data set and a label file in .txt format.
[0055] The stacked hourglass network structure in step 2 is as follows Figure 2 As shown in the figure, the stacked hourglass network is composed of 4 stacked hourglass networks, among which the residual module is the basic module in the network. The residual module is mainly composed of a main path consisting of three convolutional layers with different kernel scales and a bypass consisting of a convolutional layer with a kernel scale of 1. It extracts higher-level features while retaining the information of the original level, does not change the data size, and only changes the data depth.
[0056] The training process of the stacked hourglass network is as follows: the pig image of 256×256 pixel size first passes through a 7×7 convolution layer with a step size of 2, and then passes through a residual module and a maximum pooling layer to make the image pixel size become 64×64, and then input into the first hourglass network. After the convolution input of the residual module, it is divided into a main path and a bypass path. The bypass performs feature extraction at the original scale and retains the spatial information of each joint point on the image. The main path is first reduced to half of its original size through a downsampling, and then input into the residual module and the convolution layer to gradually extract features at different resolutions. After each feature extraction, the image is restored to its original size by upsampling, and it is fused with the previously retained image with extracted features at the original scale. The output result of each hourglass network is processed by the residual module and the convolution layer to obtain the first response heat map. This is repeated, and the input feature map, output feature map, and response heat map of the previous hourglass network are combined. Figure 3The four-order hourglass network can extract joint features of 1 / 2, 1 / 4, 1 / 8, and 1 / 16 of the original size, completing multi-scale feature extraction. After four cascaded hourglass networks, a response heat map is finally output. The response heat map and the original image are transformed into coordinates to obtain the joint points of the pig.
[0057] Each heat map output by the hourglass network is compared with the real heat map, and the loss value is calculated in each of the four stages. The loss values of the four stages participate in the calculation of the entire loss function of the hourglass network, which improves the prediction accuracy of the model. The loss function is calculated using the mean square error as follows:
[0058]
[0059] where x i ′ represents the joint point coordinates in the predicted heat map, x i represents the true value coordinates; n represents the number of joint points contained in the image, which is 5 here. The total loss is as follows:
[0060] l=l1+l2+l3+l4
[0061] Among them l i (i=1,2,3,4) represents the heat map loss at each stage.
[0062] The stacked hourglass network training parameters of step 2 are set as follows: bach_size is set to 4, RMSprop is selected as the network optimizer, the initial value of the learning rate is 0.00025, and the learning rate decreases by 4% for every 2000 steps added during the training process.
[0063] The step three uses the SVM algorithm with a Gaussian kernel function to screen the joint points of the pig body. First, the five joint points of the pig body obtained in step two are marked as body measurement points, and characteristic values are established on this basis. Then, the penalty coefficient and kernel coefficient of the model are searched using a grid search method to establish a pig body joint point screening model.
[0064] The body measurement points are as follows: Figure 3 As shown, point 1 and point 2 represent the measurement points of the left ear root and the right ear root, point 3 and point 4 represent the measurement points of the left back and the right back, point 5 represents the measurement point of the tail root, and measurement point 6 is set as the midpoint of point 1 and point 2. Five features are established to distinguish the quality of joint point samples, such as Figure 4 As shown, specifically, the number of pig joints num and the connection line L obtained in step 2 are calculated. 12 Connect with L 56 Angle ∠1, connecting line L 34 Connect with L 56 Angle ∠2, connecting line L 12Connect with L 34 The angle of the extension line ∠3, point 6 to L 34 The vertical distance d1 from point 5 to L 34 The ratio of the vertical distance d2 Create a sample set as follows: Where i represents the i-th sample, JP i Represents the feature vector of the i-th sample. If the joint point sample represented by the feature vector of the i-th sample is suitable for body size detection, JP i Marked as 1, that is, y i =1, otherwise y i =-1. The sample feature is recorded as: JP = [JP1, JP2, ..., JP n ], that is, a 5×n matrix, the label is: Y = [y1, y2, ..., y n ], that is, a 1×n row vector. The above matrices and vectors are combined into a 6×n matrix X=(JP; Y) as a sample set, and divided into a training set and a test set.
[0065] The SVM two-classification model is specifically:
[0066] g(JP)=w T φ(JP)+b
[0067] Where w is the weight coefficient of the hyperplane, b is the classification threshold, and the kernel function φ(JP) represents the ability to map data to a high-dimensional space to increase linear learning. φ(JP) is selected as the Gaussian kernel function, as shown in the formula:
[0068]
[0069] According to the nonlinear SVM binary classification model, the target optimization function is established:
[0070]
[0071] In condition: y i (w T φ(JP i )+b)≥1-ξ i Below, where ξ i ≥0, i=1,2,...,n. In the formula, ξ i is the slack variable for each sample i. Each sample has a corresponding slack variable, which represents the degree to which the sample does not satisfy the constraint. C is called the penalty parameter. The larger the C value, the greater the penalty for classification.
[0072] The learning process of training samples is essentially to find the penalty coefficient C and kernel coefficient through grid search method. The optimal combination of , and establish the optimal joint point screening model.
[0073] The calculation of the pig body size data in step 4 specifically includes calculating the body length and body width data, and the body length is defined as the length from the midpoint of the two ears to the tail root of the pig, and the body width is the width of the pig's back. The joint points of the pigs obtained by the joint point screening method in step 3 are screened, and the distance from point 5 to point 6 is calculated. Get the pig's body length data and calculate the distance from point 3 to point 4 Obtain the body width data of pigs.
[0074] The technical means disclosed in the scheme of the present invention are not limited to the technical means disclosed in the above-mentioned implementation mode, but also include technical schemes composed of any combination of the above technical features.
Claims
1. A method for measuring body size of group-raised pigs based on a stacked hourglass network, characterized in that: The following steps are involved: Step S1, obtaining an initial sample library of pig images in a group pig house environment, inputting the initial sample library into Mask R-CNN, extracting RoI obtained by the region proposal box network of Mask R-CNN and Mask obtained by the mask network, fusing the two, separating multiple target pig bodies in the image, and establishing a new sample library; the specific steps of step S1 are as follows: Step S11, in a group pig house, a depth camera is installed above the pig house to obtain a depth image from a bird's-eye view; a large amount of interference noise is removed from the obtained depth image by a median filtering method, and contrast-limited adaptive histogram equalization is used to enhance the image to obtain an original sample library; annotate the pig body bounding box of the image in the initial sample library with the image annotation tool LabelMe to obtain an initial training sample set; Step S12: input the initial sample library into Mask R-CNN, which is a two-stage instance segmentation algorithm improved on the basis of Faster R-CNN, and can simultaneously realize multi-target classification, target detection and mask segmentation; The backbone feature extraction network of the Mask R-CNN in step S12 is composed of a deep residual network resnet101 and a feature pyramid network. The pig image can obtain five feature layers of different scales through the backbone feature extraction network. The five feature layers are sequentially input into the region proposal box network to predict the candidate box coordinates of the pig, and then the obtained candidate box is used to intercept and fix the size of the feature map input into the RoI Align layer to obtain a RoI of fixed size; finally, the output of the three branches of the Mask R-CNN is obtained through the fully connected layer, two of which perform classification and border regression operations, and the other branch uses a fully convolutional network to classify the pixels in the detection box that has been obtained to contain the pig, and obtains the final pig Mask by upsampling; Step S13, extracting the RoI obtained by the region proposal box network of Mask R-CNN and the Mask obtained by the mask network, fusing the two, and then separating the multiple target pigs in the image to establish a new sample library; Step S2, inputting the new sample library constructed in S1 into the stacked hourglass network to obtain the joint points of the pig body. The stacked hourglass network uses the residual module as the basic module, which can extract features of different scales. By stacking four hourglass networks, the spatial relationship between the joint points can be better obtained; Step S3, designing a joint point screening method based on the SVM classification algorithm, by giving pig body joint point samples with different characteristics, finding a hyperplane to classify the samples, and then screening out joint point samples suitable for body size measurement; before the step S3 uses the SVM algorithm with a Gaussian kernel function to screen the pig body joint points, the obtained pig body joint points are marked as body size measurement points, where point 1 and point 2 represent the left ear root and right ear root measurement points, point 3 and point 4 represent the left back and right back measurement points, point 5 represents the tail root measurement point, and the measurement point 6 is set to the midpoint of point 1 and point 2; on this basis, 5 features for distinguishing the quality of joint point samples are established and input into the SVM for learning, and the number of pig body joint points num, the connection line L obtained from step S2 are calculated. 12 Connect with L 56 Angle ∠1, connecting line L 34 Connect with L 56 Angle ∠2, connecting line L 12 Connect with L 34 The angle of the extension line ∠3, point 6 to L 34 The vertical distance d1 from point 5 to L 34 The ratio of the vertical distance d2 The SVM two-classification model is specifically: g(JP)=w T φ(JP)+b Where w is the weight coefficient of the hyperplane, b is the classification threshold, and the kernel function φ(JP) represents the ability to map data to a high-dimensional space to increase linear learning. φ(JP) is selected as the Gaussian kernel function, as shown in the formula: The penalty coefficient and kernel coefficient of the model are searched by grid search method to find the optimal parameters of the model, and the joint point samples obtained by stacking hourglass networks are classified to screen out the joint point samples suitable for body size measurement. Step S4: construct a body size measurement model, use the joint point screening results to obtain five joint points of the pig body, and calculate the body size data of the pig.
2. The method for measuring body size of group-raised pigs based on a stacked hourglass network according to claim 1, characterized in that: The Mask R-CNN in step S12 is a two-stage target detection network: in the first stage, a feature pyramid network is constructed to perform multi-scale feature fusion, and then the RoI of the target object is generated by the region proposal box network to separate the pigs in the group pig house; in the second stage, the region of interest is aligned (RoI Align) and then input into the fully connected layer to obtain the classification and bounding box regression results. At the same time, Mask R-CNN also outputs a binary Mask of a pig for each RoI, and the RoI and Mask of the pig are used as the basis for building a new sample library.
3. The method for measuring body size of group-raised pigs based on a stacked hourglass network according to claim 1, characterized in that: Before inputting the stacked hourglass network in step S2, the new sample library is divided into a training set, a validation set and a test set; the outer bounding box of the pig in the image in the training set, as well as the five joint points of the left ear root, the right ear root, the left back, the right back and the tail root are labeled using the labeling software LabelMe, and the training sample set includes an image data set and a label file in .txt format.
4. The method for measuring body size of group-raised pigs based on a stacked hourglass network according to claim 1, characterized in that: The stacked hourglass network in step S2 is composed of four stacked hourglass networks. The structure of each hourglass network is bilaterally symmetrical. The residual module is used as the basic module to extract features of pig images at different scales. The semantic information of the pig images learned by the previous hourglass network module is used as the input of the next hourglass network module through the stacking of convolutional layers and pooling layers, so that the stacked hourglass network learns rich semantic features. There is a cross-stage connection between each hourglass network structure in the stacked hourglass network. Each hourglass network corresponds to a loss function and participates in the final loss function calculation, thereby reducing the final loss information and improving the prediction accuracy of the model.
5. The method for measuring body size of group-raised pigs based on a stacked hourglass network according to claim 1, characterized in that: The step S2 inputs the new sample library into the stacked hourglass network, specifically, the pig image first passes through the initial convolution module to reduce the resolution from 256 to 64, and the image is input into the first hourglass network after a downsampling operation, and the output result of the hourglass network is processed by the residual module and the convolution layer to obtain the first response heat map; The input feature map, output feature map and response heat map of the previous hourglass network are fused as the input of the next hourglass network. The response heat map generated by each hourglass network participates in the calculation of the loss value. After four cascaded hourglass networks, a response heat map is finally output. The response heat map is transformed with the original image to obtain the five joint points of the pig: left ear root, right ear root, left back, right back, and tail root.
6. The method for measuring body size of group-raised pigs based on a stacked hourglass network according to claim 1, characterized in that: The body measurement in step S4 includes measuring the body length and body width of the pig, where the body width refers to the width of the pig's back; Calculate the distance from point 5 to point 6 Get the pig's body length data and calculate the distance from point 3 to point 4 Obtain the body width data of pigs.
