Two-stage lightweight pig pain sensing method based on YOLOv8-SNMT

Through the two-stage model of YOLOv8-SlimNeck and MobileNetV3-TACH, the problems of lag in pig pain recognition and high computing resource consumption are solved, efficient and accurate pain detection in resource-constrained environments are achieved, and intelligent breeding is supported.

CN120472495APending Publication Date: 2025-08-12FUZHOU NONGFUTENG INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510526184.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-25
Publication Date
2025-08-12

AI Technical Summary

Technical Problem

The existing pig pain recognition methods have lag, rely on human experience and high computing resources, making it difficult to achieve accurate and real-time pain detection.

Method used

A two-stage lightweight model based on YOLOv8-SlimNeck and MobileNetV3-TACH was adopted. First, pig facial detection was performed through YOLOv8-SlimNeck to remove background interference, and then pain status classification was performed using MobileNetV3-TACH.

Benefits of technology

Real-time pain recognition in resource-constrained environments is realized, which reduces computing resource consumption, improves recognition accuracy, reduces pig stress response, and provides a contactless pain detection solution.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120472495A_ABST
    Figure CN120472495A_ABST
Patent Text Reader

Abstract

The invention discloses a two-stage lightweight pig pain sensing method based on YOLOv8-SNMT, and relates to the technical field of pig health monitoring and recognition. The method comprises the following steps: (1) acquiring pig image data (S1); (2) making a pig data set (S2); (3) constructing and training a YOLOv8-SlimNeck face detection model (S3); (4) acquiring a face image of the pig by using a YOLOv8-SlimNeck model (S4); (5) making a pig face data set (S5); (6) constructing and training a MobileNetV3-TACH pig pain classification model (S6); and (7) identifying the pain state of the pig by using the MobileNetV3-TACH model (S7). According to the method, a two-stage pig pain state classification model is provided, a YOLOv8 model structure is improved, good balance is achieved between classification precision and calculation efficiency, and the real-time monitoring requirement in a resource-limited environment is met; an efficient and lightweight solution is provided for non-contact pain detection of live pigs in the limiting fence, and important technical support is provided for intelligent live pig health monitoring.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of pig health monitoring and identification technology, and in particular to a two-stage lightweight pig pain perception method based on YOLOv8-SNMT. Background Art

[0002] Pain is a common factor affecting livestock welfare, often leading to decreased productivity and severely impacting farm profitability. However, effectively alleviating pain requires accurate identification. Pain recognition and detection are effective tools for timely identifying diseased animals and studying drug efficacy.

[0003] Currently, pain recognition methods are primarily categorized into objective and subjective methods. Objective methods rely on physiological parameters such as blood levels and heart rate, while subjective methods rely on the observation and quantification of facial features by livestock operators. However, objective methods exhibit a certain lag and can induce stress responses in livestock, so subjective assessment remains a common method in actual production. However, due to the relatively simple facial muscle structure of pigs, pain recognition is relatively difficult. Furthermore, subjective assessment relies heavily on human experience and is susceptible to interference from individual differences and subjective factors. Therefore, an automated, intelligent assessment method is urgently needed to improve accuracy and consistency.

[0004] To achieve real-time recognition of pig pain, promote the deployment of pain recognition models in embedded devices, and enhance their applicability, this paper proposes a two-stage, lightweight pig pain perception method based on YOLOv8-SNMT. Based on YOLOv8, this model combines efficient object detection and lightweight feature extraction methods to ensure high recognition accuracy while reducing computing resource consumption, thus meeting the real-time and efficiency requirements of modern, intelligent farming. Summary of the Invention

[0005] The purpose of this invention is to overcome the shortcomings of existing pig pain perception methods and provide a two-stage, lightweight pig pain perception method based on YOLOv8-SNMT. This method aims to reduce the workload of pig farmers, achieve contactless pain recognition in pigs, and avoid pig stress reactions. While ensuring high recognition accuracy and reducing computing resource consumption, it also promotes the deployment of pain recognition models in embedded devices and accelerates the development of intelligent farming.

[0006] The object of the present invention is achieved like this:

[0007] Pig facial features are key indicators for assessing pig pain, especially changes in the eyes, ears, and nose, which can effectively determine the severity of the pain. This paper uses the YOLOv8-SlimNeck model to quickly detect and accurately locate pig faces, effectively removing background interference in complex breeding environments. The MobileNetV3-TACH model is then used to extract and classify pig pain features to improve recognition accuracy.

[0008] Specifically, this method is:

[0009] It consists of two stages: the first stage is pig face detection based on the YOLOv8-SlimNeck model, and the second stage is facial image pain state classification based on the MobileNetV3-TACH model.

[0010] ① Obtain pig image data: Use Hikvision high-definition cameras to capture images of pigs in the confinement pens. The images include the pigs’ facial features and background information about the breeding environment.

[0011] ② Pig dataset preparation: Use the annotation software Labelme to annotate the pig facial areas in the collected images;

[0012] ③Build and train the YOLOv8-SlimNeck face detection model: We constructed the SlimNeck module to lightweight the Neck layer of the YOLOv8 model and trained the improved model using a dataset of labeled pig images;

[0013] ④ Use the YOLOv8-SlimNeck model to obtain pig facial images: Using the trained YOLOv8-SlimNeck model weights, we can quickly detect and accurately locate the pig faces in the dataset, effectively removing background interference information in complex breeding environments;

[0014] ⑤ Pig face dataset preparation: Use Python program to crop the anchor boxes in the YOLOv8-SlimNeck model detection results to generate a facial image dataset;

[0015] ⑥ Build and train the MobileNetV3-TACH pig pain classification model: Introduce the Triplet Attention module based on the MobileNetV3 network, build the TABneck feature extraction module and the Classify-Head classification module, and use the extracted pig facial image dataset for model training;

[0016] ⑦ Use the MobileNetV3-TACH model to identify the pain state of pigs: Use the trained MobileNetV3-TACH model weights to identify the pain state of pig facial images and determine whether they are in pain.

[0017] Compared with the prior art, the present invention has the following innovations, advantages and positive effects:

[0018] ① The present invention proposes a two-stage pig pain state classification model;

[0019] ② This paper improves the YOLOv8 model structure, achieving a good balance between classification accuracy and computational efficiency, meeting the real-time monitoring needs in resource-constrained environments;

[0020] ③The present invention provides an efficient and lightweight solution for non-contact pain detection of pigs in confined pens, and provides important technical support for intelligent pig health monitoring. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 It is a flow chart of the overall steps of this method;

[0022] Figure 2 This is the model structure diagram of YOLOv8-SNMT;

[0023] Figure 3 This is the loss change curve of YOLOv8-SlimNeck;

[0024] Figure 4 This is the validation set accuracy change curve of MobileNetV3 and MobileNetV3-TACH;

[0025] Figure 5 This is the loss accuracy change curve of MobileNetV3 and MobileNetV3-TACH.

[0026] Notes:

[0027] 1. YOLOv8-SNMT model: The two-stage lightweight pig pain perception model proposed in this paper includes the YOLOv8-SlimNeck model and the MobileNetV3-TACH model.

[0028] 2. YOLOv8-SlimNeck model: YOLOv8 is a target detection model. Based on this, the present invention replaces the original Neck module of the YOLOv8 model with the SlimNeck module.

[0029] 3. SlimNeck: SlimNeck is a method to reduce model complexity and maintain accuracy. It consists of GSConv, GSBottle neck and VoV-GSCSP structure.

[0030] 4. MobileNetV3-TACH model: MobileNetV3 is a classification model. Based on this model, the present invention constructs a TA Bneck structure to replace the original Bneck structure, and introduces the Classify-Head classification module of YOLOv8 to classify pigs as painful or non-painful.

[0031] 5. TABnck: TABnck is a method to reduce model complexity and maintain accuracy. It is constructed by replacing the traditional SE attention mechanism with the Triplet Attention module.

[0032] 6. Classify-Head: The classification structure of the YOLOv8 model, consisting of convolutional layers, adaptive average pooling, and binary cross entropy loss.

[0033] 7. Labelme: Labelme is a graphic image annotation tool. DETAILED DESCRIPTION

[0034] The following is a detailed description with reference to the accompanying drawings and embodiments.

[0035] 1. Methods

[0036] 1. Overall process

[0037] like Figure 1 The overall steps are as follows:

[0038] ① Obtain pig image data: Use Hikvision high-definition cameras to capture images of pigs in the confinement pens. The image content includes the pigs’ facial features and background information of the breeding environment (S1);

[0039] ② Pig dataset preparation: Use the annotation software Labelme to annotate the pig facial areas in the collected images-S2;

[0040] ③Build and train the YOLOv8-SlimNeck face detection model: Lightweight the Neck layer of the YOLOv8 model by building the SlimNeck module, and train the improved model using the labeled pig image dataset - S3;

[0041] ④ Use the YOLOv8-SlimNeck model to obtain pig facial images: Using the trained YOLOv8-SlimNeck model weights, we can quickly detect and accurately locate the pig faces in the dataset, effectively removing background interference information in the complex breeding environment (S4);

[0042] ⑤ Pig face dataset production: Use Python program to crop the anchor boxes in the YOLOv8-SlimNeck model detection results to generate the facial image dataset - S5;

[0043] ⑥ Build and train the MobileNetV3-TACH pig pain classification model: Introduce the Triplet Attention module based on the MobileNetV3 network, build the TABneck feature extraction module and the Classify-Head classification module, and use the extracted pig facial image dataset for model training (S6);

[0044] ⑦ Use the MobileNetV3-TACH model to identify the pain state of pigs: Use the trained MobileNetV3-TACH model weights to identify the pain state of pigs' facial images and determine whether they are in pain - S7.

[0045] 2. Split steps

[0046] 1. Step 2:

[0047] First, the captured image pixels are converted to 640×640 to speed up the training process of the YOLOv8-SlimNeck model;

[0048] Secondly, because the YOLO model requires a .txt label file format, and the file format annotated using the annotation software Labelme is .json, it needs to be converted to .txt format through a python program.

[0049] 2. Step 3:

[0050] The SlimNeck feature processing network consists of GSConv, GS Bottleneck and VoV-GSCSP structures; GSConv is a hybrid convolution that integrates depthwise separable convolution, standard convolution and channel shuffling; channel shuffling, as a general hybrid strategy, exchanges local feature information across channels, so that the information generated by standard convolution can more effectively penetrate into the depthwise separable convolution output, thereby achieving more sufficient information fusion; GSConv minimizes the impact of the inherent defects of depthwise separable convolution on model performance, while retaining and strengthening the computational advantages of depthwise separable convolution; while ensuring low time complexity, this method retains the implicit connection between channels as much as possible, thereby maintaining high detection accuracy while being lightweight; on the basis of GSConv, GS Bottleneck is further introduced, and the cross-stage local network module VoV-GSCSP is constructed through the aggregation strategy.

[0051] 3. Step 4:

[0052] We used the trained YOLOv8-SlimNeck face detection model to infer images from the test set and obtain pig facial anchor frames. To evaluate model performance, we calculated MAP@0.5, parameter count, and computational complexity. A higher MAP@0.5 indicates a higher model accuracy, while a smaller parameter count and computational complexity indicate a more lightweight model. The specific calculation formula is as follows:

[0053]

[0054] MAP@0.5:0.5≤CIOU#(4)

[0055] Where IOU: intersection-over-union ratio, O: the area of the intersection of two bounding boxes, U: the area of the union of two bounding boxes, α: weight function, v: correction factor, d: the distance between the center point of the predicted box and the true box, c: the diagonal distance of the minimum enclosing rectangle, CIOU: complete intersection-over-union ratio, MAP@0.5: the value whose average value of the complete intersection-over-union ratio is greater than 0.5.

[0056] 4. Step 6:

[0057] The TABneck feature extraction module is constructed by replacing the traditional SE attention mechanism with the Triplet Attention module. The Triplet Attention module consists of three complementary branches. Two of these branches are used to model the cross-channel interaction between the channel dimension C and the spatial dimension W / H, respectively, while the third branch is used to calculate the traditional spatial attention weights. The Z-Pool module reduces the 0th dimension of the tensor to 2 dimensions by performing global average pooling and maximum pooling operations along specific dimensions and concatenating the resulting features. This strategy not only retains rich feature representations but also effectively reduces computational complexity. Its mathematical formula is as follows:

[0058] Z-Pool(x)=[MaxPool 0d (x),AvgPool 0d (x)]#(5)

[0059] Among them, 0d is the 0th dimension of the maximum pooling operation and the average pooling operation.

[0060] The first branch is the channel attention calculation branch. The input features pass through Z-Pool, then through convolution, and finally the Sigmoid activation function generates the spatial attention weight; the second branch is the channel C and space W dimension interactive capture branch. The input features first pass through permute and adjust to H×C×W dimensional features, and then perform Z-Pool on the H dimension; the third branch is the channel C and space H dimension interactive capture branch. The input features first pass through permute and adjust to W×H×C dimensional features, and then perform Z-Pool on the W dimension; the outputs of these three branches will be permuted and adjusted to C×H×W dimensional features, and finally added and averaged; the input tensor x∈R C×H×W After Triplet Attention, the output tensor y is obtained, and its calculation formula is as follows:

[0061]

[0062] Among them, σ represents the Sigmoid activation function, ψ1, ψ2, and ψ3 represent the three branches of the standard two-dimensional convolutional layer defined by the convolution kernel k in Triplet Attention.

[0063] The Classify-Head module completes the classification task through convolutional layers, adaptive average pooling, and binary cross-entropy loss. This structure first extracts high-level local features through convolutional layers and uses adaptive average pooling to adjust the pooled output size to accommodate input images of varying sizes. This pooling method not only reduces the number of parameters but also mitigates the risk of overfitting caused by traditional fully connected layers. Simultaneously, this design combines local features with global information to enhance the model's generalization ability and improve the accuracy of classification decisions. Binary cross-entropy loss is suitable for binary classification problems, where the predicted probability of each class is calculated independently without considering the influence of other classes. This loss function can flexibly adjust the predicted probability in the presence of class imbalance, reducing the impact of data imbalance on the overall model performance. Therefore, it is particularly important in tasks where there is an imbalance between pain and non-pain classes.

[0064] 5. Step 7

[0065] Using the trained MobileNetV3-TACH pig pain classification model, we inferred images from the test set and classified pigs as either painful or non-painful. To evaluate model performance, we used accuracy, precision, recall, F1 score, and specificity, while also considering parameters and computational complexity to measure the model's lightweightness. The specific calculation formula is as follows:

[0066]

[0067] Among them, TP is the number of samples correctly predicted by the model as pain, TN is the number of samples correctly predicted by the model as non-pain, FP is the number of samples whose actual category is non-pain but the model incorrectly predicts it as pain; FN is the number of samples whose actual category is pain but the model incorrectly predicts it as non-pain.

[0068] 2. Example

[0069] ① Obtain pig image data

[0070] The present invention collected pig image data at a pig farm in Fuzhou, and collected a total of 955 images of non-painful sows and 195 images of painful sows.

[0071] ②Pig dataset preparation

[0072] A. Resize the collected pig images to 640×640 pixels using a Python program;

[0073] B. Data augmentation using horizontal flipping, hue and saturation adjustment, and noise addition. After augmentation, the number of images of non-painful sows increased from 955 to 2,483, and the number of images of painful sows increased from 195 to 780.

[0074] C. Use the Labelme tool to annotate the facial regions and convert the annotation files into .txt format using Python. Finally, the 3263 images are divided into a training set of 1757, a validation set of 753, and a test set of 753.

[0075] ③Build and train the YOLOv8-SlimNeck face detection model

[0076] like Figure 2 The SlimNeck feature processing network replaces the original Neck feature processing network. The input of the YOLOv8-SlimNeck face detection model becomes the input of the Backbone feature extraction network, the output of the Backbone feature extraction network becomes the input of the SlimNeck feature processing network, and the output of the SlimNeck feature processing network becomes the input of the head detection head, which in turn becomes the final output. The SlimNeck feature processing network replaces the Conv with GSConv, effectively improving the inference speed of the convolutional neural network. The VoV-GSCSP structure replaces the C2f structure to optimize the model's feature utilization efficiency and feature fusion capabilities, further improving network performance.

[0077] ④Use the YOLOv8-SlimNeck model to obtain pig facial images

[0078] The hardware configuration of this model is as follows: CPU: Intel i5-12400F, GPU: NVIDIA RTX306012GB, memory capacity: 32GB, deep learning framework: PyTorch version 2.0.0, Torchvision version 0.15.0, Python interpreter version 3.8.20 for training; stochastic gradient descent algorithm is used as the optimizer to optimize the neural network during training, with batch size set to 32 and iteration cycle set to 500.

[0079] like Figure 3 After 500 iterations, the model loss gradually decreased and eventually stabilized, with a loss value of 0.618 on the training set and a loss value of 0.699 on the validation set.

[0080] Compared with the YOLOv8 model, the number of parameters and computational complexity of YOLOv8-SlimNeck are reduced to 0.93 times and 0.90 times that of the YOLOv8 model, respectively, and MAP@0.5 on the test set is improved by 0.4 percentage points.

[0081] Table 1 Comparison of face detection models

[0082]

[0083] ⑤Pig facial dataset production

[0084] The YOLOv8-SlimNeck model weights were used to perform pig face detection on the pig image dataset, and the detected pig face anchor frames were cropped using a Python program; the cropped pig face images were used as the training set, validation set, and test set for the MobileNetV3-TACH pig pain classification model.

[0085] ⑥Build and train the MobileNetV3-TACH pig pain classification model

[0086] like Figure 2 Based on the MobileNetV3 network, the TABneck feature extraction module replaces the Bneck feature extraction module, and the Classify-Head classification module is used for classification. The MobileNetV3-TACH pig pain classification model uses the input of the multi-layer TABneck and Bneck feature extraction modules as the input, and the output of the multi-layer TABneck and Bneck feature extraction modules as the input of the Classify-Head classification module, which outputs the pig pain classification results. The TABneck feature extraction module uses a three-branch structure to capture cross-dimensional feature interactions to calculate weights, extracting salient features of painful faces without increasing the number of parameters or computational complexity. The Classify-Head classification module improves the classification performance of pigs in pain and non-pain.

[0087] ⑦Use MobileNetV3-TACH model to identify the pain state of pigs

[0088] This model was trained in the aforementioned hardware and software environments. The stochastic gradient descent algorithm was used as the optimizer to optimize the neural network. The batch size was set to 32 and the iteration period was set to 200.

[0089] like Figure 4 ,The accuracy of the MobileNetV3 validation set fluctuates greatly in the early stages of training, and the convergence speed is slow. The MobileNetV3-TACH fluctuates less during training, showing better stability;

[0090] like Figure 5 After convergence, the loss value of the MobileNetV3-TACH validation set is significantly smaller than that of MobileNetV3. The loss of the MobileNetV3-TA CH validation set is 0.02, and the loss of the MobileNetV3 validation set is 0.08.

[0091] Compared with the MobileNetV3 model, although the computational complexity of MobileNetV3-TACH has increased by 1.4 times, all performance indicators of the test set have been significantly improved. The accuracy is 3.8 percentage points higher than MobileNetV3, the precision is 2.3 percentage points higher, the recall rate is 13.9 percentage points higher, the F1-Score is 8.5 percentage points higher, and the specificity is 1.0 percentage point higher.

[0092] Table 2 Comparison of pig pain classification models

[0093]

Claims

1. A two-stage lightweight pig pain perception method based on YOLOv8-SNMT, characterized by: ① Obtain pig image data: Use Hikvision high-definition cameras to capture images of pigs in the confinement pens, where the image content includes the pigs' facial features and background information of the breeding environment (S1); ② Pig dataset preparation: Labelme was used to label the facial regions of the pigs in the collected images (S2); ③ Build and train the YOLOv8-SlimNeck face detection model: Lightweight the Neck layer of the YOLOv8 model by building the SlimNeck module, and train the improved model using the labeled pig image dataset (S3); ④ Use the YOLOv8-SlimNeck model to obtain pig facial images: Use the trained YOLOv8-SlimNeck model weights to achieve rapid detection and precise positioning of pig faces in the dataset, effectively removing background interference information in complex breeding environments (S4); ⑤ Pig face dataset preparation: Use Python program to crop the anchor boxes in the YOLOv8-SlimNeck model detection results to generate a facial image dataset (S5); ⑥ Build and train the MobileNetV3-TACH pig pain classification model: Introduce the Triplet Attention module based on the MobileNetV3 network, build the TABneck feature extraction module and the Classify-Head classification module, and use the extracted pig facial image dataset for model training (S6); ⑦ Use the MobileNetV3-TACH model to identify the pain state of pigs: Use the trained MobileNetV3-TACH model weights to identify the pain state of pigs' facial images and determine whether they are in a painful state (S7).

2. The method according to claim 1, wherein: The step ② is: First, the captured image pixels are converted to 640×640 to accelerate the training process of the YOLOv8-SlimNeck model. Second, because the YOLO model requires a .txt label file format, and the file format annotated by the annotation software Labelme is .json, it needs to be converted to .txt format using a Python program.

3. The method according to claim 1, wherein: The step ③ is: The SlimNeck feature processing network consists of GSConv, GS Bottleneck and VoV-GSCSP structures; GSConv is a hybrid convolution that integrates depthwise separable convolution, standard convolution and channel shuffling; channel shuffling, as a general hybrid strategy, exchanges local feature information across channels, so that the information generated by standard convolution can more effectively penetrate into the depthwise separable convolution output, thereby achieving more sufficient information fusion; GSConv minimizes the impact of the inherent defects of depthwise separable convolution on model performance, while retaining and enhancing the computational advantages of depthwise separable convolution; while ensuring low time complexity, this method retains the implicit connection between channels as much as possible, thereby maintaining high detection accuracy while being lightweight; based on GSConv, the present invention further introduces GS Bottleneck and constructs a cross-stage local network module VoV-GSCSP through an aggregation strategy.

4. The method according to claim 1, wherein: The step ④ is: We used the trained YOLOv8-SlimNeck face detection model to infer images from the test set and obtain pig facial anchor frames. To evaluate model performance, we calculated MAP@0.5, parameter count, and computational complexity. A higher MAP@0.5 indicates a higher model accuracy, while a smaller parameter count and computational complexity indicate a more lightweight model. The specific calculation formula is as follows: MAP@0.5:0.5≤CIOU#(4) Where IOU: intersection-over-union ratio, O: the area of the intersection of two bounding boxes, U: the area of the union of two bounding boxes, α: weight function, v: correction factor, d: the distance between the center point of the predicted box and the true box, c: the diagonal distance of the minimum enclosing rectangle, CIOU: complete intersection-over-union ratio, MAP@0.5: the value whose average value of the complete intersection-over-union ratio is greater than 0.

5.

5. The method according to claim 1, wherein: Described step ⑥ is: The TABneck feature extraction module is constructed by replacing the traditional SE attention mechanism with the Triplet Attention module. The Triplet Attention module consists of three complementary branches. Two of these branches are used to model the cross-channel interactions between the channel dimension C and the spatial dimension W / H, respectively, while the third branch is used to calculate the traditional spatial attention weights. The Z-Pool module reduces the 0th dimension of the tensor to 2 dimensions by performing global average pooling and max pooling operations along specific dimensions and concatenating the resulting features. This strategy not only preserves rich feature representations but also effectively reduces computational complexity. Its mathematical formula is as follows: Z-Pool(x)=[MaxPool 0d (x),AvgPool 0d (x)]#(5) Among them, 0d is the 0th dimension of the maximum pooling operation and the average pooling operation.

6. The method according to claim 1, wherein: Described step ⑦ is: The trained MobileNetV3-TACH pig pain classification model was used to infer images from the test set and classify pigs as painful or non-painful. To evaluate model performance, accuracy, precision, recall, F1 value, and specificity were used. The model's lightweightness was also measured by considering parameters and computational complexity. The specific calculation formula is as follows: Among them, TP is the number of samples correctly predicted by the model as pain, TN is the number of samples correctly predicted by the model as non-pain, FP is the number of samples whose actual category is non-pain but the model incorrectly predicts it as pain; FN is the number of samples whose actual category is pain but the model incorrectly predicts it as non-pain.