Piglet nest average weight estimation method and system based on instance segmentation
By combining YOLOv5s and OD_SeGAN models with pixel regression technology, the problem of high-precision segmentation and weight estimation of multiple piglets in a lactating piglet litter was solved, achieving efficient and stable weight estimation under complex occlusion conditions.
Patent Information
- Application Number
- CN202511085806.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-04
- Publication Date
- 2025-11-21
AI Technical Summary
Existing technologies struggle to achieve high-precision segmentation and weight estimation of multiple piglets in suckling pig litters, especially lacking effective solutions under complex occlusion conditions.
By employing the YOLOv5s object detection model and an improved generative adversarial network (OD_SeGAN) combined with pixel regression technology, non-contact real-time estimation of the average weight of piglets in a litter is achieved through instance segmentation and linear fitting.
Achieving high-precision segmentation and weight estimation of piglet pens in complex occlusion environments improves segmentation accuracy and weight estimation stability, making it suitable for real-time weight estimation tasks in dense scenarios.
Smart Images

Figure CN120997500A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the fields of intelligent breeding and computer vision technology, specifically relating to a contactless animal weight estimation system and method based on deep learning, and particularly to a technical solution for high-precision segmentation and weight estimation of suckling piglets using instance segmentation technology. Background Technology
[0002] In modern animal husbandry, accurate measurement of piglet weight is crucial for scientific feeding management and economic efficiency. Traditional weight measurement methods typically rely on weighbridges or scales, which are time-consuming, labor-intensive, and can cause stress to piglets. With the development of machine vision technology, more and more research is exploring image-based non-contact weight estimation methods. Non-contact visual monitoring technology can provide continuous, real-time weight data without interfering with the animals' normal activities, thus helping to improve breeding efficiency.
[0003] However, existing research mainly focuses on estimating the weight of individual pigs, while a mature solution is lacking for estimating the average weight of multiple piglets in a lactation litter. In the farrowing house environment, severe occlusion often occurs in acquired images due to piglets overlapping and the cramped space. This poses a significant challenge to the accurate extraction of segmented piglet instances. Therefore, it is necessary to propose a system for high-precision segmentation and weight estimation of piglets in lactation litters under complex occlusion conditions. Summary of the Invention
[0004] This application addresses the problems of low efficiency, stress, and difficulty in continuous monitoring of litter weight of suckling piglets in large-scale pig farms, which still rely on manual methods. It provides a method and system for estimating litter weight based on instance segmentation. The system uses the YOLOv5s object detection model and an improved generative adversarial network (OD_SeGAN) as its core, enabling accurate segmentation of piglets under complex occlusion conditions. It then combines pixel regression and linear fitting techniques to estimate litter weight, while ensuring the stability of the estimation results through multiple samplings and sampling frequency control. The system includes components such as a video acquisition module, a deep learning inference module, a weight estimation regression module, and a data visualization platform, supporting contactless real-time weight estimation and result display.
[0005] Technical solution:
[0006] This invention first presents a method for estimating the average weight of piglets in a litter based on instance segmentation, which includes the following steps:
[0007] S1. Data Acquisition: The entire lactation area is filmed from above the farrowing pen using a camera, and the video data is obtained and saved.
[0008] S2. Data preprocessing: The acquired video data is sampled at fixed intervals, and then the Perceptual Hash Image Deduplication (PHA) algorithm is applied to remove redundant images from the sampled frames.
[0009] S3. Piglet Target Detection: A lightweight YOLOv5s model is used for piglet target detection. Each identified bounding box is expanded around its original size and used as input for subsequent instance segmentation models.
[0010] S4. Construct the instance segmentation model OD_SeGAN to achieve pixel-level accurate segmentation and output the instance segmentation mask for each piglet.
[0011] S5. Construct a regression model for average litter weight of piglets: Based on the segmentation mask of each piglet instance obtained in S4, in the training phase, severely occluded frames are first preprocessed to remove them. The pixel area of each piglet in the remaining frames is extracted and paired with the real average litter weight obtained by synchronous weighing. A regression model for average litter weight of piglets is constructed with the average piglet pixel area as the independent variable and the average litter weight as the dependent variable.
[0012] S6. Average Dig Weight Calculation and Multi-Frame Fusion Estimation: In the estimation stage, occlusion determination is performed on the real-time frame and the average pixel area is calculated. The average pixel area is then input into the regression model to output the estimated average dig weight of the frame. Subsequently, a sufficient number of frames are collected within a set time window, and the arithmetic mean of the estimation results of each frame is taken to obtain a robust final average dig weight.
[0013] Preferably, in S1, the collection time covers piglets from 9 to 21 days of age.
[0014] Preferably, in S2, the perceptual hash image deduplication algorithm is as follows:
[0015] S2-1. Reduce the image to 8×8 pixels and convert it to grayscale;
[0016] S2-2. Low-frequency features are extracted by Discrete Cosine Transform (DCT) to generate a 64-bit binary hash value representing image structure information.
[0017] S2-3. Calculate the Hamming distance and similarity between images, and set a similarity filtering threshold. Images with similarity exceeding the threshold are considered redundant and removed. The similarity calculation formula is as follows:
[0018]
[0019] Where hash1-hash2 is the Hamming distance between the two image hash values, and hash_size is 8.
[0020] Preferably, in S3, the loss function of the YOLOv5s model is:
[0021]
[0022] In the formula, L IOU ,L dis These are the crossover ratio loss and the distance loss, respectively. asp Represents the height and width loss, ρ 2 (b,b gt w represents the Euclidean distance between the center points of the ground truth bounding box and the center point of the predicted bounding box. c ,h c These are the width and height of the minimum bounding boxes for the predicted bounding box and the ground truth bounding box, respectively.
[0023] Preferably, in S4, the instance segmentation model OD_SeGAN introduces two optimizations:
[0024] 1) Integrating dilated convolution modules into the generator effectively expands the receptive field to enhance the ability to perceive contextual information;
[0025] 2) An attention module is embedded in the key convolutional layer to adaptively adjust the channel response and enhance the ability to distinguish salient regions of piglets.
[0026] Preferably, in S4, the loss function formula is as follows:
[0027]
[0028] Where .y is the real label, y' is the generator's mapping to the piglet image, n is the number of samples, D(y) is the discriminator's judgment result on the real label, and λ1, λ2, and λ3 represent the proportion of each part of the loss in the generator loss function.
[0029] Preferably, in S5, the expression for establishing the linear regression model is:
[0030] y = ax + b + ε
[0031] Where y is the average weight of piglets in the litter, x is the average number of pixels per piglet, a and b are fitting parameters, and ε is the error term;
[0032] The objective function equation to be minimized is:
[0033]
[0034] Where y i and These represent the actual average weight of piglets per litter and the model prediction result, respectively, where n is the number of samples.
[0035] Preferably, in S2, sample images are randomly selected from the collected images at a sampling frequency of 1000 times / 8 hours, which means that 1000 images are randomly sampled from the image library (which contains many images) collected during the daytime for subsequent processing.
[0036] This invention also provides a system for estimating the average weight of piglets in a litter based on instance segmentation, which includes a video acquisition module, a data processing module, a database module, and a visualization and alarm module, wherein:
[0037] Video capture module: The camera is deployed above the farrowing pen to capture video streams of the entire lactation area, used to obtain video of the activity area of the piglets during the lactation period;
[0038] The data processing module includes an image preprocessing and sample selection unit, an object detection unit, an instance segmentation unit, a weight estimation unit, and a multi-frame fusion and filtering unit. Specifically:
[0039] Multi-frame fusion and filtering unit: performs frame extraction at fixed intervals on the acquired video data, and then applies the Perceptual Hash Image Deduplication (PHA) algorithm to remove redundant images from the extracted frame samples;
[0040] Target detection unit: A lightweight YOLOv5s model is used for piglet target detection. Each identified bounding box is expanded around its original size and used as input for the subsequent instance segmentation model.
[0041] Instance segmentation unit: used to achieve pixel-level accurate segmentation and output the instance segmentation mask for each piglet;
[0042] Weight estimation unit: Based on the segmentation mask of each piglet instance, the severely occluded frames are preprocessed during the training phase. The pixel area of each piglet in the remaining frames is extracted and paired with the real litter weight obtained by synchronous weighing. A piglet litter weight regression model is constructed with the average piglet pixel area as the independent variable and the litter weight as the dependent variable.
[0043] Multi-frame fusion and filtering unit: It performs occlusion determination and calculates the average pixel area for real-time frames, and inputs it into the regression model to output the estimated value of the average cell weight for that frame; then, it gathers a sufficient number of frames within a set time window, takes the arithmetic mean of the estimation results of each frame, and obtains the robust final average cell weight.
[0044] Beneficial effects of the present invention
[0045] (1) An improved OD_SeGAN instance segmentation model is proposed, which combines dilated convolution and SE attention mechanism to achieve pixel-level accurate segmentation of multiple piglets in the complex occlusion environment of sow lactation. It effectively restores the contour structure of the occluded part, improves the model's ability to model the structure of local areas, and solves the problem of poor separation accuracy of traditional segmentation methods in overlapping occlusion scenarios.
[0046] (2) The YOLOv5s target detection model was introduced and its loss function was optimized from CIoU to EIoU to improve the accuracy and convergence speed of bounding box localization. At the same time, an automatic boundary expansion mechanism was designed in the candidate box preprocessing stage, which significantly improved the integrity of the segmented region and laid the foundation for subsequent segmentation and weight estimation.
[0047] (3) The PHA perceptual hashing algorithm is innovatively introduced to remove redundancy from image samples, thereby achieving efficient deduplication, enhancing data diversity, avoiding interference from low-information samples in model training, improving the representativeness of the overall training set and the generalization ability of the model, and providing data support for high-precision segmentation models.
[0048] (4) Construct a nest average weight estimation strategy based on pixel area regression. By utilizing the linear relationship between the number of pixels in the segmentation mask and the actual weight, a fast weight estimation method without contact or multi-dimensional feature modeling is realized. It is simple, efficient, and highly adaptable, and is especially suitable for real-time weight estimation tasks in dense scenes. Attached Figure Description
[0049] Figure 1 This is a system structure diagram of the present invention.
[0050] Figure 2 This is a flowchart of the method of the present invention.
[0051] Figure 3 This is a network structure diagram of the present invention. Detailed Implementation
[0052] The present invention will be further described below with reference to embodiments, but the scope of protection of the present invention is not limited thereto:
[0053] This invention proposes a method and system for estimating the average weight of piglets in a litter based on instance segmentation, combined with... Figure 1 The system includes:
[0054] Video capture module: Deployed at a fixed position above the sow's farrowing crate, it is used to acquire video streams of the activity area of suckling piglets, supports 1080P high-definition real-time capture, and meets the needs of collecting behavioral characteristics of multiple piglets.
[0055] Data processing module:
[0056] 1. Image preprocessing and sample selection unit: Based on the perceptual hashing algorithm (PHA), similarity analysis is performed on the acquired frames to automatically remove redundant images and retain representative frames with rich information and obvious pose changes for training and model accuracy optimization.
[0057] 2. Target Detection Unit: Loads the improved YOLOv5s target detection model, performs fast inference on the input video frames, identifies the bounding box position of the piglet target, and uses it as a candidate region for instance segmentation.
[0058] 3. Instance Segmentation Unit: Receives the detection bounding box image and feeds it into the OD_SeGAN instance segmentation model for pixel-level mask generation. This model combines dilated convolution and SE attention mechanisms to accurately segment multiple overlapping piglets in occluded environments and outputs a binary mask for each individual.
[0059] 4. Weight estimation unit: Based on the number of pixels in the segmented mask region and combined with a pre-fitted pixel-weight linear regression model, the weight of each piglet is estimated, and the average weight of the entire litter of piglets is automatically calculated.
[0060] 5. Multi-frame fusion and filtering unit: Estimates the average weight of consecutive frames within a specified sampling window, combines the changes in confidence between frames with the judgment of motion amplitude, adjusts the sampling frequency, removes abnormal frames, and outputs a stable average estimate.
[0061] Database module: Used to store information such as original image frames, detection box coordinates, segmentation mask, estimated weight results, and sampling timestamps, and supports historical data query, model tracing, and trend statistical analysis.
[0062] Visualization and Alarm Module: Used to display the detection box, segmentation outline and estimated weight value in real time, draw the average weight change curve of the litter, and trigger management early warning prompts when abnormal fluctuations or continuous weight loss are detected to assist farmers in decision-making.
[0063] Combination Figure 2 The main process of the method is as follows:
[0064] (1) Data Acquisition: Fixed high-definition network cameras (1920×1080 resolution, 25 frames / second) were deployed above the farrowing pens (approximately 2.2 meters high) to capture overhead views covering the entire lactation area. The monitoring equipment continuously recorded video data via an NVR. The standard pen size was 2.45m×1.8m, and the data collection covered the critical rearing stage of piglets from 9 to 21 days of age. Videos from each pen were saved in .mp4 format for subsequent model training and online estimation.
[0065] (2) Data Preprocessing: For the acquired video data, frame extraction is first performed at fixed intervals (1 frame is extracted every 20 seconds). Then, the PHA (Perceptual Hashing Algorithm) image deduplication algorithm is applied to remove redundant images from the extracted frame samples. This method first reduces the image to 8×8 pixels and converts it to grayscale. Then, low-frequency features are extracted through Discrete Cosine Transform (DCT) to generate a 64-bit binary hash value representing the image structure information. Subsequently, the Hamming distance and similarity between images are calculated, and a screening threshold of 0.78 is set. When the similarity exceeds this value, it is considered a redundant image and is removed.
[0066] The similarity calculation formula is as follows:
[0067]
[0068] Where hash1-hash2 is the Hamming distance between the hash values of the two images, and hash_size is 8, which means that the similarity is compressed to the range [0,1]. The closer to 1, the higher the similarity between the two images.
[0069] (3) Piglet Target Detection: Addressing the challenges of limited space and severe individual occlusion in the sow lactation environment, this invention employs a lightweight YOLOv5s model for piglet target detection and improves its loss function by introducing EIoU instead of the original CIoU to enhance the accuracy and convergence efficiency of bounding box regression. The detection module primarily generates candidate regions. Each identified bounding box is scaled to 256×256 pixels by expanding its four sides by 10 pixels from its original size, serving as input to the subsequent instance segmentation model and significantly reducing the computational burden. Experimental results show that the improved model achieves an average accuracy (AP@0.5) of 89.4% in the lactation scenario. While maintaining detection speed, it demonstrates superior stability and performance compared to traditional methods such as Faster R-CNN in detecting unoccluded individuals. The EIoU loss function formula is as follows:
[0070]
[0071] Where Lasp represents the height and width loss, and w and h represent the width and height of the detection box, respectively.
[0072] (4) Instance segmentation: combined with Figure 3 An improved OD_SeGAN (Occlusion-Decoupled SeGAN) instance segmentation model is introduced based on the YOLOv5s detection bounding boxes to achieve pixel-level accurate segmentation. This model innovatively introduces two structural optimizations: 1. Fusing a dilated convolution module into the generator to effectively expand the receptive field and enhance contextual awareness; 2. Embedding a Squeeze-and-Excitation (SE) attention module in key convolutional layers to adaptively adjust channel responses and strengthen the ability to discriminate salient regions of piglets. The generator adopts a U-Net-style encoder-decoder structure and fuses multi-level features through skip connections, accurately reconstructing the complete morphology of occluded piglets; the discriminator performs local discrimination on the segmentation mask. Through end-to-end adversarial training, the model can learn the prior structure of occluded regions, improving occlusion inference accuracy and ultimately generating high-quality instance masks.
[0073] The formula for the GAN loss function is as follows:
[0074]
[0075] Where y is the real label, y' is the generator's mapping to the piglet image, n is the number of samples, D(y) is the discriminator's discrimination result of the real label, and λ1, λ2, and λ3 represent the proportion of each part of the loss in the generator loss function. In this system, λ1 = λ2 = λ3 = 1 is set.
[0076] (5) After obtaining the instance segmentation mask for each piglet, the system performs connected component analysis on it and counts the number of pixels within the mask region, which serves as the input feature for the weight estimation model. Based on the experimental results of the calibration samples, a linear regression model is constructed using the least squares method. The optimal regression coefficient aa and bias term bb are solved by fitting the training dataset to achieve the optimal mapping between pixel area and weight. Since the average weight of piglets in a litter fluctuates little during the lactation period, and the weight change corresponding to a single pixel is relatively stable under the overhead view, the linear model shows good estimation accuracy and applicability in this study.
[0077] The expression for the linear model is:
[0078] y = ax + b + ε
[0079] Where y is the average weight of piglets in a litter, x is the average number of pixels per piglet, a and b are fitting parameters, and ε is the error term.
[0080] The objective function equation to be minimized is:
[0081]
[0082] Where y i and These represent the actual average weight of piglets per litter and the model prediction result, respectively, where n is the number of samples.
[0083] (6) Litter weight calculation and multi-frame fusion estimation: In practical applications, the number of visible pixels of piglets in images at different time points fluctuates due to the piglets' autonomous activities and the sow's posture changes within the same pen. To ensure the representativeness and stability of the weight estimation results, based on the OD_SeGAN segmentation results, each column of video is sampled frame by frame. After removing images with severe occlusion, approximately 10,000 valid images are retained from each column to extract the number of piglet pixels and calculate the average pixel value. During the estimation stage, the system counts the number of all segmented piglet instances in each frame and averages their estimated weights to obtain the estimated litter weight for the current frame.
[0084] (7) Sampling Frequency Control Mechanism: In the breeding field, the activity of piglets and the posture changes of sows will cause fluctuations in the number of pixels in the image, thus affecting the stability of the weight estimation results. Too low a sampling frequency may lead to inaccurate estimation, while too high a frequency will increase the system burden. Therefore, this study tested the estimation stability at different sampling frequencies, and calculated the standard deviation of the average number of pixels per kilogram in the weight estimation process, as well as the Pearson correlation coefficient and its standard deviation between the litter weight and the average pixel value. It was found that when the sampling frequency reached 1000 times / 8 hours, the weight estimation data tended to be stable, the correlation between pixels and weight was high, and the error was small.
[0085] The Pearson correlation coefficients are as follows:
[0086]
[0087] In the formula, r is the Pearson correlation coefficient, and X i and Y i These represent the average weight of piglets per litter and the average number of pixels per piglet, respectively. and X i and Y i The average value is denoted by n, where n is the average weight of piglets per litter.
[0088] (8) System Integration and Real-time Visualization: Integrate the above models and logic into the edge or server system to build a real-time monitoring platform. The system includes: a video acquisition module, a deep learning inference module, a weight estimation and regression module, and a data storage and visualization platform. The front end displays the piglet segmentation boundary and estimated weight in real time, while the back end supports historical trend curves, abnormal weight alarms, and correlation with individual sow management.
[0089] The specific embodiments described herein are merely illustrative of the spirit of the invention. Those skilled in the art to which this invention pertains may make various modifications or additions to the described specific embodiments or use similar methods to substitute them, without departing from the spirit of the invention or exceeding the scope defined by the appended claims.
Claims
1. A method for estimating the average weight per litter of piglets based on instance segmentation, characterized in that... It includes the following steps: S1. Data Acquisition: The entire lactation area is filmed from above the farrowing pen using a camera, and the video data is obtained and saved. S2. Data preprocessing: The acquired video data is sampled at fixed intervals, and then the Perceptual Hash Image Deduplication (PHA) algorithm is applied to remove redundant images from the sampled frames. S3. Piglet Target Detection: A lightweight YOLOv5s model is used for piglet target detection. Each identified bounding box is expanded around its original size and used as input for subsequent instance segmentation models. S4. Construct the instance segmentation model OD_SeGAN to achieve pixel-level accurate segmentation and output the instance segmentation mask for each piglet. S5. Construct a regression model for average litter weight of piglets: Based on the segmentation mask of each piglet instance obtained in S4, in the training phase, severely occluded frames are first preprocessed to remove them. The pixel area of each piglet in the remaining frames is extracted and paired with the real average litter weight obtained by synchronous weighing. A regression model for average litter weight of piglets is constructed with the average piglet pixel area as the independent variable and the average litter weight as the dependent variable. S6. Average Dig Weight Calculation and Multi-Frame Fusion Estimation: In the estimation stage, occlusion determination is performed on the real-time frame and the average pixel area is calculated. The average pixel area is then input into the regression model to output the estimated average dig weight of the frame. Subsequently, a sufficient number of frames are collected within a set time window, and the arithmetic mean of the estimation results of each frame is taken to obtain a robust final average dig weight.
2. The method according to claim 1, characterized in that... In S1, the collection period covered piglets from 9 to 21 days of age.
3. The method according to claim 1, characterized in that... In S2, the perceptual hash image deduplication algorithm is as follows: S2-1. Reduce the image to 8×8 pixels and convert it to grayscale; S2-2. Low-frequency features are extracted by Discrete Cosine Transform (DCT) to generate a 64-bit binary hash value representing image structure information. S2-3. Calculate the Hamming distance and similarity between images, and set a similarity filtering threshold. Images with similarity exceeding the threshold are considered redundant and removed. The similarity calculation formula is as follows: Where hash1-hash2 is the Hamming distance between the two image hash values, and hash_size is 8.
4. The method according to claim 1, characterized in that... In S3, the loss function of the YOLOv5s model is: In the formula, L IOU ,L dis These are the crossover ratio loss and the distance loss, respectively. asp Represents the height and width loss, ρ 2 (b,b gt w represents the Euclidean distance between the center points of the ground truth bounding box and the center point of the predicted bounding box. c ,h c These are the width and height of the minimum bounding boxes for the predicted bounding box and the ground truth bounding box, respectively.
5. The method according to claim 1, characterized in that... In S4, the instance segmentation model OD_SeGAN introduces two optimizations: 1) Integrating dilated convolution modules into the generator effectively expands the receptive field to enhance the ability to perceive contextual information; 2) An attention module is embedded in the key convolutional layer to adaptively adjust the channel response and enhance the ability to distinguish salient regions of piglets.
6. The method according to claim 1, characterized in that... In S4, the loss function formula is as follows: Where .y represents the real label. y ' represents the generator's mapping to the piglet image, n is the number of samples, D(y) is the discriminator's judgment result on the true label, and λ1, λ2, and λ3 represent the proportion of each part of the loss in the generator loss function.
7. The method according to claim 1, characterized in that... In S5, the expression for building a linear regression model is: y = ax + b + ε Where y is the average weight of piglets in the litter, x is the average number of pixels per piglet, a and b are fitting parameters, and ε is the error term; The objective function equation to be minimized is: Where y i and These represent the actual average weight of piglets per litter and the model prediction result, respectively, where n is the number of samples.
8. The method according to claim 1, characterized in that... In S2, sample images are randomly selected from the collected images at a sampling frequency of 1000 times / 8 hours.
9. A system for estimating the average weight of piglets in a litter based on instance segmentation, characterized in that... It includes a video acquisition module, a data processing module, a database module, and a visualization and alarm module, among which: Video capture module: The camera is deployed above the farrowing pen to capture video streams of the entire lactation area, used to obtain video of the activity area of the piglets during the lactation period; The data processing module includes an image preprocessing and sample selection unit, an object detection unit, an instance segmentation unit, a weight estimation unit, and a multi-frame fusion and filtering unit. Specifically: Multi-frame fusion and filtering unit: performs frame extraction at fixed intervals on the acquired video data, and then applies the Perceptual Hash Image Deduplication (PHA) algorithm to remove redundant images from the extracted frame samples; Target detection unit: A lightweight YOLOv5s model is used for piglet target detection. Each identified bounding box is expanded around its original size and used as input for the subsequent instance segmentation model. Instance segmentation unit: used to achieve pixel-level accurate segmentation and output the instance segmentation mask for each piglet; Weight estimation unit: Based on the segmentation mask of each piglet instance, the severely occluded frames are preprocessed during the training phase. The pixel area of each piglet in the remaining frames is extracted and paired with the real litter weight obtained by synchronous weighing. A piglet litter weight regression model is constructed with the average piglet pixel area as the independent variable and the litter weight as the dependent variable. Multi-frame fusion and filtering unit: It performs occlusion determination and calculates the average pixel area for real-time frames, and inputs it into the regression model to output the estimated value of the average cell weight for that frame; then, it gathers a sufficient number of frames within a set time window, takes the arithmetic mean of the estimation results of each frame, and obtains the robust final average cell weight.
Citation Information
Cited By
Piglet weight evaluation method and system based on time sequence characteristics and attention mechanism
CN122336863A