A method of counting swine

By constructing the PIG-P2PNet model and combining ECA, CSA modules and Focal Dice loss, the Hungarian matching algorithm was optimized, solving the problems of large counting errors and high labor costs in pig counting. This resulted in efficient and accurate pig counting, adapting to various environments, and performing particularly well in high-density occlusion scenarios.

CN119810015BActive Publication Date: 2025-11-11ZHEJIANG FORESTRY UNIVERSITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411668345.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-21
Publication Date
2025-11-11
Estimated Expiration
2044-11-21

AI Technical Summary

Technical Problem

Existing pig counting methods rely on manual statistics, which are easily affected by subjective factors and the environment, resulting in large counting errors. Frequent manual intervention increases labor costs and may cause biosecurity risks. The use of electronic ear tags may be detrimental to pig welfare, and there is a lack of efficient and accurate counting solutions.

Method used

Using a point-labeled pig counting dataset, a PIG-P2PNet model was built. By adding an ECA module to the backbone network VGG16 and a CSA module to the FPN structure, Focal Dice loss was used to improve classification accuracy. A context-aware Hungarian matching algorithm was designed, and uncertainty cost, weighted distance, and adaptive density penalty mechanisms were introduced to optimize matching accuracy.

Benefits of technology

In complex and high-density pig farm environments, the PIG-P2PNet model demonstrates adaptability and generalization ability, excellent counting accuracy and robustness, significantly improving counting accuracy and real-time performance, and reducing errors and resource consumption.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119810015B_ABST
    Figure CN119810015B_ABST
Patent Text Reader

Abstract

This invention discloses a pig counting method, comprising the following steps: constructing a pig counting dataset based on point annotation; adding an efficient channel attention (ECA) module after each body in the VGG16 backbone network of the P2Pnet; adding a coordinate channel mixed attention (CSA) module to the feature pyramid (FPN) structure of the P2Pnet; replacing the cross-entropy loss function used in the classification loss of the P2Pnet with Focal Dice loss to improve the classification accuracy of the model; improving the Hungarian matching algorithm in the point regression task, including introducing an uncertainty cost handling mechanism, a weighted distance handling mechanism, and an adaptive density penalty mechanism to improve matching accuracy; finally obtaining the improved model as PIG-P2PNet, and applying the PIG-P2PNet to pig counting. The beneficial effects of this invention are: it has adaptability and generalization ability in various practical environments, exhibits excellent performance in counting accuracy and robustness, and is particularly outstanding in high-density, complex occlusion-prone feeding environments.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the technical field of pig farm management, and more particularly to a method for counting pigs. Background Technology

[0002] With the development of modern agricultural technology, automated and digitalized farming is gradually becoming a trend. Among these trends, accurate pig counting is a key link in improving the efficiency and profitability of pig farm management. Accurate pig counting not only helps farmers monitor the dynamics of their pig herd in real time and optimize feed distribution, but also assists in health management and economic benefit assessment, which is especially important in high-density farming environments.

[0003] Traditional pig counting relies primarily on manual methods, which are susceptible to subjective factors and environmental influences. Furthermore, counting errors increase significantly with the size of the pig herd. Frequent manual intervention not only increases labor costs but may also pose biosecurity risks. While electronic ear tags offer some solutions, their use may adversely affect pig welfare. Therefore, modern pig farming urgently needs an efficient and accurate counting method to meet the demands of refined management. Summary of the Invention

[0004] The purpose of this section is to outline some aspects of embodiments of the present invention and to briefly describe some preferred embodiments. Simplifications or omissions may be made in this section, as well as in the abstract and title of this application, to avoid obscuring the purpose of these documents; however, such simplifications or omissions should not be construed as limiting the scope of the invention.

[0005] In view of the aforementioned existing problems, the present invention is proposed.

[0006] Therefore, the technical problem solved by the present invention is to provide a method for counting pigs.

[0007] To address the aforementioned technical problems, this invention provides the following technical solution: a pig counting method, comprising the following steps: constructing a pig counting dataset based on point annotation; building a population counting model P2PNet; adding an efficient channel attention ECA module after each body in the backbone network VGG16 of the P2PNet; adding a coordinate channel shuffling attention CSA module to the feature pyramid FPN structure of the P2PNet; replacing the cross-entropy loss function used for classification loss in the P2PNet with Focal Dice loss to improve the classification accuracy of the model; improving the Hungarian matching algorithm in the point regression task by designing a context-aware Hungarian matching algorithm, including introducing an uncertainty cost handling mechanism, a weighted distance handling mechanism, and an adaptive density penalty mechanism to improve matching accuracy; finally obtaining the improved model as PIG-P2PNet, and applying the PIG-P2PNet to pig counting.

[0008] Preferably, adding the ECA module includes the following steps: dividing the VGG16 backbone network into four bodies, and introducing the ECA module at the end of each body. The ECA module first performs global average pooling on the input feature map, compressing the spatial dimension of each channel into a single value to obtain global information for each channel; applying 1D convolution operation to process the pooled features, using convolution kernels to capture local dependencies between adjacent channels, and processing the convolution result through a Sigmoid activation function to generate attention weights for each channel, with values ​​limited to (0,1); weighting the attention weights with each channel of the original input feature map one by one, scaling the features through multiplication to enhance the expression of important channels and suppress information from secondary channels.

[0009] Preferably, adding the CSA module includes the following steps: the CSA module first uses coordinate attention to enhance the capture of spatial location information; and optimizes channel features through channel shuffling attention.

[0010] Preferably, the CSA module includes the following steps: First, the CSA module receives an input feature map; extracts spatial information in the horizontal and vertical directions through global pooling, and generates spatially relevant attention weights through concatenation and convolution; weights the feature map using the attention weights to further enhance key spatial features; then, the feature map is divided into multiple channel groups, each group extracts features using convolution, and the order is shuffled by channel rearrangement; the channel groups generate channel-level attention weights through global average pooling and normalization to adjust the feature expression intensity of each channel and highlight key channels; finally, the processed feature map is rearranged and weighted.

[0011] Preferably, the uncertainty cost handling mechanism includes the following steps: adjusting the matching cost based on the confidence level to reduce the matching probability of low-confidence prediction points, as shown in the following formula.

[0012] C uncertainty (i,j)=C point (i,j)·u i

[0013] In the formula, C point (i,j) represents the Euclidean distance between the predicted point and the true point, u i This indicates the confidence level of the predicted point.

[0014] Preferably, the weighted distance processing mechanism includes the following steps: applying weights to the distance between the predicted point and the true point during the matching process, as shown in the following formula:

[0015]

[0016] In the formula, σ is the weight of the uncertainty cost and is used to balance the uncertainty cost, and ∈ is used to prevent the denominator from being zero; by applying weights, the matching cost between points is optimized, and points with smaller distances will be given greater weights.

[0017] Preferably, incorporating the adaptive density penalty mechanism includes the following steps: calculating the difference in local density using the KNN algorithm, flexibly adjusting the matching cost, improving matching precision in dense regions, and providing greater tolerance in sparse regions, as shown in the following formula.

[0018] C density (i,j)=exp(-λ·|d i -d j |)

[0019] In the formula, d i and d j Let represent the local density of the predicted point and the true point, respectively, and λ be the parameter controlling the exponential smoothing.

[0020] Preferably, the improved final matching cost is obtained by combining classification cost, weighted distance cost, uncertainty cost, and adaptive density penalty cost, as shown in the following formula:

[0021] C total (i,j)=α·C class (i,j)+β·C distance (i,j)+γ·C density (i,j)

[0022] In the formula, α, β, and γ are the weights of each cost item, respectively, and C class (i,j) is the classification cost.

[0023] Preferably, replacing the cross-entropy loss function with Focal Dice loss includes,

[0024] The formula for calculating Focal Loss is as follows:

[0025]

[0026] In the formula, α is a weighting factor that adjusts the positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, p t This is the model's probability prediction for each category, calculated as follows:

[0027]

[0028] p t This refers to the negative exponent of the cross-entropy loss, where γ is a factor that adjusts the model's focus on the classified samples; Dice Loss measures the overlap between the predicted region and the true target region, and its calculation formula is as follows.

[0029]

[0030] In the formula, p i It is the probability distribution predicted by the model, g i It is the one-hot encoded representation of the true label. ∈ is used to prevent the denominator from being zero. The smaller the Dice Loss value, the closer the model prediction is to the true label.

[0031] Preferably, the final formula for calculating the Focal Dice loss is as follows:

[0032] L focal-dice =L focal +L dice

[0033] L is coming soon focal and L dice Add them together.

[0034] The beneficial effects of this invention are: it has adaptability and generalization ability in various practical environments, and performs well in terms of counting accuracy and robustness, especially in high-density, complex shading breeding environments. Attached Figure Description

[0035] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort. Wherein:

[0036] Figure 1 These are schematic diagrams of pig images under different scenarios described in this invention;

[0037] Figure 2 This is a schematic diagram illustrating an example of pig dot marking as described in this invention;

[0038] Figure 3 This is a schematic diagram illustrating an example of data augmentation using horizontal flipping, rotation, and cropping / filling as described in this invention.

[0039] Figure 4 This is a schematic diagram illustrating the data enhancement examples of Gaussian noise, Gaussian blur, and color saturation adjustment described in this invention.

[0040] Figure 5 This is a schematic diagram of the PIG-P2PNet network structure described in this invention;

[0041] Figure 6 This is a schematic diagram of the ECA module structure described in this invention;

[0042] Figure 7 This is a schematic diagram of the CSA module structure described in this invention;

[0043] Figure 8 This is a schematic diagram comparing the visualization results of different counting models described in this invention;

[0044] Figure 9 This is a schematic diagram of the prediction results of PIG-P2PNet under different densities described in this invention;

[0045] Figure 10 This is a schematic diagram comparing the localization performance of PIG-P2PNet and P2PNet as described in this invention. Detailed Implementation

[0046] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present invention, and not all of them. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the protection scope of the present invention.

[0047] Many specific details are set forth in the following description in order to provide a full understanding of the invention. However, the invention may also be practiced in other ways different from those described herein, and those skilled in the art can make similar extensions without departing from the spirit of the invention. Therefore, the invention is not limited to the specific embodiments disclosed below.

[0048] Secondly, the term "one embodiment" or "embodiment" as used herein refers to a specific feature, structure, or characteristic that may be included in at least one implementation of the present invention. The phrase "in one embodiment" appearing in different places in this specification does not necessarily refer to the same embodiment, nor is it a single or selective embodiment that is mutually exclusive with other embodiments.

[0049] This invention is described in detail with reference to the schematic diagrams. When detailing the embodiments of this invention, for ease of explanation, the cross-sectional views illustrating the device structure may be partially enlarged, not adhering to the usual scale. Furthermore, the schematic diagrams are merely examples and should not be construed as limiting the scope of protection of this invention. In actual fabrication, the three-dimensional spatial dimensions of length, width, and depth should be included.

[0050] Furthermore, in the description of this invention, it should be noted that the terms "upper," "lower," "inner," and "outer," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. These terms are used solely for the convenience of describing the invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on the invention. In addition, the terms "first," "second," or "third" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.

[0051] Unless otherwise explicitly specified and limited, the terms "installation," "connection," and "joining" in this invention should be interpreted broadly. For example, they can refer to fixed connections, detachable connections, or integral connections; similarly, they can refer to mechanical connections, electrical connections, or direct connections, or indirect connections through an intermediate medium, or internal connections between two components. Those skilled in the art can understand the specific meaning of the above terms in this invention based on the specific circumstances.

[0052] Example 1

[0053] Pig counting is a crucial task in modern pig farming, playing a key role in assessing farm scale, optimizing feeding strategies, and improving economic efficiency. However, the complex environment of pig farms, with its high pig density and numerous obstructions, poses challenges to counting accuracy. Therefore, this embodiment proposes a pig counting model, PIG-P2PNet, based on the human population counting model P2PNet, including the following steps:

[0054] S1: Construct a pig counting dataset based on point annotations;

[0055] S2: Building the P2PNet crowd counting model;

[0056] S3: Add an efficient channel attention ECA module after each body in the P2Pnet backbone network VGG16;

[0057] S4: Add a CSA module for coordinate channel shuffling attention to the feature pyramid FPN structure of P2Pnet;

[0058] S5: Replace the cross-entropy loss function used in P2Pnet for classification loss with Focal Dice loss to improve the classification accuracy of the model;

[0059] S6: Improve the Hungarian matching algorithm in the point regression task by designing a context-aware Hungarian matching algorithm, including introducing an uncertainty cost handling mechanism, a weighted distance handling mechanism, and an adaptive density penalty mechanism to improve matching accuracy;

[0060] S7: The final improved model is PIG-P2PNet, which is then applied to pig counting.

[0061] First, by introducing an efficient channel attention mechanism into the backbone network, the dependencies between channels are effectively captured, enhancing the ability to identify overlapping pigs. Second, a coordinate channel shuffling attention module is integrated into the feature pyramid, strengthening the extraction and interaction capabilities of spatial location information and channel features, and improving the model's adaptability in different density scenarios. Furthermore, a context-aware Hungarian matching algorithm is designed, which improves matching accuracy by introducing weighted distance penalties, uncertainty costs, and adaptive density penalties.

[0062] Example 2

[0063] This embodiment applies the pig counting method to a complex pig farm environment. In practice, it includes the construction of a dataset and the construction of a herd pig counting model, as detailed below:

[0064] (1) Dataset construction.

[0065] ①Data source:

[0066] The experimental data in this embodiment comes from different pig farms, covering public datasets, online images, and data collected from actual pig farms. Public datasets include the iFlytek Pig Inventory Dataset and the University of Nebraska-Lincoln Pig Dataset. Online images mainly come from Roboflow and Kaggle. The remaining data were collected from Zhejiang Qingzhu Agricultural and Animal Husbandry Co., Ltd. in Pingyao Town, Yuhang District, Hangzhou City, Zhejiang Province, and the experimental pig farm in Gaohong Town, Lin'an District, Hangzhou City, Zhejiang Province. Most of the public datasets and online images are top-view images, with relatively minor occlusion issues. By collecting side-view data from actual pig farms, the diversity and complexity of the data were increased. Images with significant differences in time and lighting conditions were selected from the acquired data, while images with high similarity and low clarity were removed. Finally, 2200 original images were selected, including top-view and side-view images, day and night images, and high-density and low-density images, representing various scenes. Data from different pig farms is shown below. Figure 1 As shown.

[0067] ②Dataset preprocessing:

[0068] All images of different resolutions were uniformly cropped and padded to 640×640 pixels, and the dataset was labeled using the open-source annotation tool X-labeling. When labeling, the center of the pig as a whole was prioritized; if severe occlusion or overlap occurred, the center of the visible portion was labeled. An example of pig labeling is shown below. Figure 2 As shown.

[0069] Based on the annotations, data augmentation is then performed using methods such as flipping, rotating, cropping and filling, adding noise, Gaussian blur, and adjusting color saturation. Examples of data augmentation are shown below. Figure 3-4 As shown. Figure 3 The images from left to right illustrate horizontal flipping, rotation, and cropping / filling processes. Figure 4 The images from left to right illustrate Gaussian noise, Gaussian blur, and color saturation enhancement processing, respectively.

[0070] ③ Dataset construction:

[0071] The 2200 original labeled pig images were randomly divided into training, validation, and test sets in a 7:1.5:1.5 ratio. The training set underwent data augmentation, increasing the number of images from 1540 to 2640, bringing the total number of images in the dataset to 3300. Dataset information is shown in Table 1.

[0072] Table 1: Dataset.

[0073]

[0074] (2) Pig herd counting model

[0075] ①PIG-P2PNet network structure:

[0076] The P2PNet crowd counting model suffers from accuracy drops when used for counting pigs in complex and dense scenes. Therefore, this embodiment improves upon it. First, an Efficient Channel Attention (ECA) module is introduced after each body in the VGG16 backbone network of P2PNet. ECA adaptively adjusts channel weights, highlighting key features and suppressing irrelevant information, thus improving the model's detection and classification performance in complex scenes. Next, a Coordinate and Shuffle Attention (CSA) module is introduced into the Feature Pyramid Networks (FPN). The CSA module combines spatial and channel information to form richer feature interactions, enabling the model to pay attention to the synergistic relationships between different scales, positions, and channel features of the target, thereby significantly improving the model's accuracy and robustness. Finally, the cross-entropy loss function in P2PNet is replaced with Focal Dice loss to handle imbalanced positive and negative samples and dense occlusion by pigs, effectively improving the model's prediction accuracy. Finally, the classification and regression heads are used to predict the image location, generating predicted points for the target location. During model training, a context-aware Hungarian matching algorithm is applied to optimize the matching process between ground truth points and predicted points, addressing the issues of ground truth points being ignored and incorrect matching. The improved model is named PIG-P2PNet, and its network structure is as follows: Figure 5 As shown, the improvements are indicated by dashed boxes.

[0077] ②ECA module:

[0078] For point regression pig counting tasks, dense scenes often involve numerous and unevenly distributed targets. Traditional VGG networks have limitations in capturing local features and distinguishing adjacent individuals in these areas, leading to decreased prediction accuracy, especially underfitting in high-density scenes. Furthermore, VGG is prone to confusing targets with background information in complex backgrounds, lacking effective inter-channel interaction, which affects localization accuracy and model robustness. To address the challenges of distinguishing dense targets, inaccurate local feature extraction, and confusion in complex backgrounds, the VGG16 backbone network is divided into four bodies, with an ECA module introduced at the end of each body. This hierarchical design effectively enhances the model's extraction capabilities at different feature levels. ECA utilizes 1D convolutions to strengthen local dependencies between channels, enabling the model to better focus on individual pigs within dense regions, thereby improving target discrimination. The ECA module structure is as follows: Figure 6 As shown.

[0079] The ECA module first performs global average pooling on the input feature map, compressing the spatial dimension of each channel into a single value to obtain global information for each channel. Then, it applies 1D convolution operations to process the pooled features, using convolution kernels (…). Figure 6 The function sets k=3 to capture local dependencies between adjacent channels. The convolution result is processed by a sigmoid activation function to generate attention weights for each channel, with values ​​limited to (0,1). Finally, the attention weights are weighted one by one with each channel of the original input feature map. By multiplying and scaling the features, the expression of important channels is enhanced while the information of secondary channels is suppressed.

[0080] ③CSA module:

[0081] The introduction of the CSA module addresses the challenges faced by the P2PNet model when processing complex or densely populated pig scenes, including difficulty in distinguishing overlapping targets, challenges in extracting features from complex backgrounds, and insufficient interaction between spatial and channel features. The CSA module first utilizes coordinate attention to enhance the capture of spatial location information, and then optimizes channel features through channel shuffling attention. Its structure is as follows: Figure 7 As shown.

[0082] The CSA module first receives the input feature map, extracts horizontal and vertical spatial information through global pooling, and generates spatially relevant attention weights through concatenation and convolution to enhance important regions and suppress background noise. These weights are then used to weight the feature map, further strengthening key spatial features. Next, the feature map is divided into multiple channel groups, each group extracting features through convolution, and the order is shuffled by channel rearrangement to ensure effective interaction of cross-channel information. Channel groups are then used to generate channel-level attention weights through global average pooling and normalization, adjusting the feature expression intensity of each channel and highlighting key channels. Finally, the processed feature map is rearranged and weighted to ensure full utilization of spatial and channel information, improving the model's feature representation ability and recognition performance in complex scenes.

[0083] ④ Context-aware Hungarian matching algorithm:

[0084] In point regression tasks, the traditional Hungarian matching algorithm can improve counting accuracy by accurately matching predicted and real points through global optimization. However, this algorithm also has shortcomings in pig counting tasks. First, in dense regions, the algorithm cannot accurately match real and predicted points, resulting in multiple pigs' predicted points being incorrectly matched, leading to a significantly lower count than the actual number. Second, in sparse regions, the algorithm's matching is too loose, causing predicted points far from the real target to be incorrectly matched with the nearest real point, resulting in inaccurate counting. Furthermore, the algorithm does not consider the confidence level of predicted points, leading to low-confidence predicted points being incorrectly matched with high-confidence real points, thus causing miscounting or undercounting.

[0085] To address the issues of inaccurate matching of dense regions, overly lenient matching of sparse regions, and failure to consider the confidence level of prediction points in the traditional Hungarian algorithm for pig counting tasks, this embodiment improves upon it by designing a context-aware Hungarian algorithm.

[0086] First, an uncertainty cost handling mechanism is introduced to adjust the matching cost based on the confidence level, thereby reducing the matching probability of low-confidence prediction points and thus reducing the risk of mismatches. The formula is as follows:

[0087] C uncert ainty (i,j)=C point (i,j)·u i (1)

[0088] In equation (1), C point (i,j) represents the Euclidean distance between the predicted point and the true point, u i This indicates the confidence level of the predicted point.

[0089] Secondly, the weighted distance processing mechanism was improved by applying weights to the distance between predicted and actual points during the matching process. When the prediction contains uncertainty, the uncertainty cost is added to the point distance cost to increase the matching cost of predicted points with high uncertainty, thereby weakening these points in the matching process. The weighted distance cost calculation formula is shown in Equation (2). By optimizing the matching cost between points through this formula, points with smaller distances will be given greater weights, thereby ensuring that the matching in dense areas is more stringent, while sparse areas maintain a certain degree of flexibility.

[0090]

[0091] In equation (2), σ is the weight of uncertainty cost and is used to balance uncertainty cost, and ∈ is used to prevent the denominator from being zero.

[0092] Finally, an adaptive density penalty mechanism is incorporated. This mechanism calculates local density differences using the KNN algorithm, flexibly adjusting the matching cost to improve matching precision in dense regions and provide greater tolerance in sparse regions, thereby optimizing overall matching accuracy. The calculation formula for this mechanism is as follows:

[0093] C density (i,j)=exp(-λ·|d i -d j |)(3)

[0094] In equation (3), d i and d j Let represent the local density of the predicted point and the true point, respectively, and λ be the parameter controlling the exponential smoothing.

[0095] In summary, by combining classification cost, weighted distance cost, uncertainty cost, and adaptive density penalty cost, the algorithm is better able to adapt to the point distribution characteristics of different regions. The final matching cost expression of the improved algorithm is:

[0096] C total (i,j)=α·C class (i,j)+β·C distance (i,j)+γ·C density (i,j) (4)

[0097] In equation (4), α, β, and γ are the weights of each cost item, C class (i,j) is the classification cost.

[0098] ⑤ Loss function optimization:

[0099] P2PNet's loss function includes classification loss and regression loss. The cross-entropy loss used for classification has limitations in point regression for pig counting tasks. First, when target pigs are scarce in a group image, the background area becomes larger, leading to an imbalance in the positive and negative sample ratio. This makes the cross-entropy loss overly sensitive to class imbalance, causing the model to favor predicting the background and neglecting the target pigs. Second, this loss function lacks targeted optimization for difficult-to-distinguish samples (such as the point annotations of neighboring pigs), misclassifying overlapping pigs as a single class rather than as independent individuals.

[0100] To address the issues of cross-entropy loss in handling class imbalance and identifying neighboring targets, it is replaced with Focal Dice Loss. Focal Dice Loss combines the advantages of Focal Loss and Dice Loss, and can better handle imbalanced positive and negative samples and dense occlusion by pigs, thereby significantly improving the model's prediction accuracy.

[0101] Focal Loss adds an adjustment factor to the cross-entropy loss function to reduce the weight of easily classified samples while giving greater weight to difficult-to-classify samples, thus addressing the class imbalance problem. The formula for Focal Loss is:

[0102]

[0103] In equation (5), α is a weighting factor for adjusting the positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, so as to avoid the impact of an imbalance in the ratio of positive and negative samples on model training; p t It is the model's probability prediction for each category, calculated as follows: That is, the negative exponent of cross-entropy loss; γ is a factor that adjusts the model's focus on classified samples. A larger γ will make the model focus more on samples that are difficult to classify.

[0104] Dice Loss is primarily used to measure the overlap between the predicted region and the true target region. It is particularly suitable for handling scenarios where the target boundary is blurred or the shape is irregular. Its calculation formula is as follows:

[0105]

[0106] In equation (6), p i It is the probability distribution predicted by the model, g i It is the one-hot encoded representation of the true label, where ∈ is a very small constant used to prevent the denominator from being zero. The smaller the Dice Loss value, the closer the model prediction is to the true label.

[0107] In summary, the formula for calculating Focal Dice Loss is as follows:

[0108] L focal-dice =L focal +L dice (7).

[0109] Example 3

[0110] This embodiment does not verify the actual effect of this method in the above embodiments; a comparative experiment is conducted to verify it. Details are as follows:

[0111] (1) Experimental environment.

[0112] The operating system was Ubuntu 11.3.0, CUDA version 11.7, Python version 3.10.14, and the deep learning framework used was PyTorch 2.0.1. Hyperparameter settings during training were as follows: learning rate 1e-5, 500 training epochs, batch size adjusted according to the memory usage of different models (PIG-P2PNet model batch size was 12), and input image size was 640×640 pixels. Hardware requirements included a 12th Gen Intel(R) Core(TM) i7-12700KF processor, an NVIDIA GeForce RTX 3090Ti graphics card with 24GB of VRAM.

[0113] (2) Evaluation indicators.

[0114] To comprehensively evaluate the performance of the pig counting model, five evaluation metrics were selected: Mean Absolute Error (MAE), Root Mean Squared Error (RMSE), Normalized Absolute Error (NAE), Frames Per Second (FPS), and Number of Parameters (Parameter).

[0115] (3) Ablation experiment.

[0116] The contribution of each improvement measure was evaluated by comparing and analyzing six different schemes. The experiment started with the baseline model (without any improvements), and then the ECA module, CSA module, Context-AwareMatcher algorithm, and Focal Dice Loss were introduced separately for testing. Finally, the four modules were integrated into the baseline model to evaluate the overall performance of the model. The final experimental results are shown in Table 2.

[0117] Table 2: Ablation test results.

[0118]

[0119] Table 2 shows that each improvement introduced individually significantly enhanced model performance. The introduction of the ECA module reduced the MAE by 18.5% and RMSE by 3% compared to the baseline model, indicating that this module effectively improved the model's feature representation ability by enhancing inter-channel feature interactions. Adding the CSA module to FPN reduced MAE and RMSE by 19.5%, demonstrating the effectiveness of the CSA module in improving feature fusion and suppressing irrelevant features. The introduction of the context-aware matching algorithm reduced the MAE and RMSE by 23.8% and 16.6% respectively compared to the baseline model, indicating that this algorithm is effective in optimizing target matching and improving target localization accuracy. Replacing the cross-entropy loss function in P2PNet with Focal Dice Loss reduced the MAE and RMSE to 1.145 and 1.804 respectively, demonstrating that Focal Dice Loss is effective in handling class imbalance and improving the model's small target detection capability. When all the improved methods were integrated into the baseline model, the model's MAE, RMSE, and NAE reached 0.942, 1.460, and 0.043, respectively, which were 28.7%, 24.3%, and 34.8% lower than the baseline model. Meanwhile, the number of parameters and FPS were 21.660M and 45 frames per second, respectively. This shows that the synergistic effect of each module significantly improved the overall performance of the PIG-P2PNet model without increasing the number of additional parameters.

[0120] (4) Performance comparison of different counting models.

[0121] To further verify the performance of the PIG-P2PNet model, it was compared with the original model P2PNet and three typical counting models CSRNet, CANNet and CLTR. The results are shown in Table 3.

[0122] Table 3: Comparison results of different models.

[0123]

[0124] As shown in Table 3, the MAE of the PIG-P2PNet model reached 0.942, which was reduced by 60.4%, 50.9%, 28.7% and 20.9% respectively compared with the CSRNet, CANNet, P2PNet and CLTR models, showing a significant advantage.

[0125] In detail, both CSRNet and CANNet employ density map estimation-based counting methods. Although their model parameters are relatively small, their performance on key metrics such as MAE, RMSE, and NAE is unsatisfactory, showing a significant gap compared to other point regression-based models. This indicates that density map-based models have limitations when handling complex scenes, especially pig counting tasks with severe overlap and occlusion. In contrast, the point regression-based counting models P2PNet and CLTR demonstrate better counting accuracy and robustness across the three key metrics of MAE, RMSE, and NAE. The CLTR model, in particular, shows significant advantages in target matching and feature extraction. However, its large parameter count and low FPS limit its performance in resource-sensitive scenarios such as edge computing devices, lacking application flexibility and real-time performance. In contrast, the P2PNet model maintains high accuracy while offering lower computational overhead and a higher frame rate, making it more suitable for real-world counting tasks with high real-time requirements.

[0126] Compared to the four models mentioned above, the PIG-P2PNet model not only achieved the best performance in the three key metrics of MAE, RMSE, and NAE, but also achieved a level comparable to P2PNet in terms of parameter count and computational efficiency. In summary, PIG-P2PNet demonstrates significant advantages in accuracy, stability, parameter control, and real-time processing capabilities. To more intuitively illustrate the performance of these models in the pig counting task, visualizations of the prediction graphs for each model are provided, such as... Figure 8 As shown in the figure (GT represents the number of actual numbers and Pred represents the number of predicted numbers).

[0127] Depend on Figure 8 It can be seen that although the P2PNet and CLTR models can predict results consistent with the actual number, this is actually because they mistakenly identify one pig as two, while missing the other one. Figure 8 In the second row of samples, the red boxes in the P2PNet and CLTR predictions indicate missed detections, while the boxes indicate incorrect predictions. The CANNet and CSRNet models, based on density map estimation, cannot accurately locate pigs; they can only estimate the overall number. Therefore, the PIG-P2PNet model provides the most accurate counting results.

[0128] (5) Performance analysis of the PIG-P2PNet model under different pig population densities.

[0129] In pig counting method research, the accuracy and robustness of the model under different pig density conditions is a key challenge. Pig densities range from sparse to extremely crowded, so the model must be able to maintain accuracy under various density conditions. To this end, validation experiments were conducted on the PIG-P2PNet model at different density levels, and the results are shown in Table 4.

[0130] Table 4: Results of verification experiments at different densities.

[0131]

[0132] Table 4 shows that in low-density scenes (Level 1), where each image contains 5 to 15 pigs, the model's MAE, RMSE, and NAE are 0.470, 0.816, and 0.041, respectively. This indicates that the model has excellent counting accuracy in this scene, accurately capturing the number of sparsely distributed targets. When the scene density increases to a medium level (Level 2), where each image contains 16 to 30 pigs, the model's MAE and RMSE reach 0.924 and 1.354, respectively, indicating that the model can maintain stable error control at higher densities. Simultaneously, the NAE decreases to 0.037 at this density, indicating good stability of the model at medium densities. In high-density scenes (Level 3), where each image contains more than 30 pigs and there is significant occlusion and overlap, PIG-P2PNet's MAE, RMSE, and NAE reach 1.924, 2.377, and 0.058, respectively. Although the model's error increases in high-density scenarios, it still maintains a relatively low error level, indicating that the model possesses strong feature representation capabilities and the ability to handle complex scenarios. To more intuitively demonstrate the counting performance of PIG-P2PNet at different densities, a visualization of the prediction plot is provided, such as... Figure 9 As shown.

[0133] The PIG-P2PNet model not only achieves accurate pig counting at different densities, but also significantly improves its localization accuracy compared to previous models. Its localization performance is comparable to that of the P2PNet model. Figure 10 As shown. From Figure 10 As can be seen, the different × marks represent the actual locations and the locations predicted by the model. Although the counting results of both are consistent with the actual values, there are differences in positioning accuracy.

[0134] By comparing the bounding box regions, it can be seen that the predicted location of PIG-P2PNet is closer to the actual annotation, and it performs more accurately in target localization.

[0135] Therefore, this embodiment addresses the common problems of missed and false detections of pigs in complex rearing environments such as high-density pig herds and multiple obstructions. It proposes an improved pig counting algorithm, PIG-P2PNet, based on the population counting model P2PNet. The main effects are as follows:

[0136] First, we constructed a pig counting dataset based on point annotation, which includes diverse scenarios ranging from low density to high density and different occlusion conditions, to ensure the model's adaptability and generalization ability in various real-world environments.

[0137] Secondly, PIG-P2PNet first introduces the ECA attention mechanism into the P2PNet backbone network, enabling the model to better focus on key features and suppress background noise. Secondly, it adds a CSA module to the FPN structure, effectively improving feature fusion quality and making the model more adaptable to images with high density and occluded regions. Furthermore, it replaces the traditional cross-entropy loss with FocalDice Loss, which helps address class imbalance and improves the detection accuracy of small targets. Finally, it designs a context-aware Hungarian matching algorithm, making the model more accurate in matching predicted points with ground truth points.

[0138] Third, experimental results show that compared to the P2PNet model, PIG-P2PNet reduces MAE, RMSE, and NAE by 28.7%, 24.3%, and 34.8%, respectively. Compared to other classic counting models CSRNet, CANNet, and CLTR, PIG-P2PNet reduces MAE by 60.4%, 50.9%, and 20.9%, respectively; RMSE by 51.2%, 48.5%, and 16.1%; and NAE by 71.5%, 53.3%, and 30.6%, respectively.

[0139] PIG-P2PNet demonstrates excellent counting accuracy and robustness, particularly in high-density, complex shading environments. In the future, point regression-based pig counting methods are expected to further improve the precision management of pig farms and contribute to the development of intelligent farming.

[0140] It should be recognized that embodiments of the present invention can be implemented or carried out by computer hardware, a combination of hardware and software, or by computer instructions stored in a non-transitory computer-readable storage medium. The method can be implemented using standard programming techniques—including a non-transitory computer-readable storage medium configured with a computer program, wherein such a storage medium causes the computer to operate in a specific and predefined manner—according to the methods and drawings described in the specific embodiments. Each program can be implemented in a high-level procedural or object-oriented programming language to communicate with the computer system. However, if desired, the program can be implemented in assembly or machine language. In any case, the language can be a compiled or interpreted language. Furthermore, for this purpose, the program can run on a programmed application-specific integrated circuit (ASIC).

[0141] Furthermore, the procedures described in this embodiment can be performed in any suitable order unless otherwise indicated by this embodiment or clearly contradicted by the context. The procedures (or variations and / or combinations thereof) described in this embodiment can be executed under the control of one or more computer systems configured with executable instructions, and can be implemented by hardware or a combination thereof as code (e.g., executable instructions, one or more computer programs, or one or more applications) that commonly executes on one or more processors. The computer program includes a plurality of instructions executable by one or more processors.

[0142] Furthermore, the method can be implemented in any suitable type of computing platform, including but not limited to personal computers, minicomputers, mainframes, workstations, networked or distributed computing environments, standalone or integrated computer platforms, or in communication with charged particle tools or other imaging devices, etc. Aspects of the invention can be implemented as machine-readable code stored on a non-transitory storage medium or device, whether removable or integrated into a computing platform, such as a hard disk, optical read and / or write storage medium, RAM, ROM, etc., such that it can be read by a programmable computer, and when the storage medium or device is read by the computer, it can be used to configure and operate the computer to perform the processes described herein. Furthermore, the machine-readable code, or portions thereof, can be transmitted via wired or wireless networks. The invention described in this embodiment includes these and other different types of non-transitory computer-readable storage media when such media comprises instructions or programs that implement the steps described above in conjunction with a microprocessor or other data processor. When programmed according to the methods and techniques described in this invention, the invention also includes the computer itself. A computer program can be applied to input data to perform the functions described in this embodiment, thereby transforming the input data to generate output data stored in non-volatile memory. The output information can also be applied to one or more output devices such as a display. In a preferred embodiment of the invention, the converted data represents physical and tangible objects, including specific visual depictions of physical and tangible objects generated on a display.

[0143] As used herein, the terms “component,” “module,” “system,” etc., are intended to refer to a computer-related entity, which may be hardware, firmware, a combination of hardware and software, software, or running software. For example, a component may be, but is not limited to, a process running on a processor, a processor, an object, an executable file, a running thread, a program, and / or a computer. As an example, an application running on a computing device and the computing device itself can both be components. One or more components may reside in a running process and / or thread, and components may be located in a single computer and / or distributed among two or more computers. Furthermore, these components are capable of execution from various computer-readable media having various data structures thereon. These components may communicate locally and / or remotely via signals, such as based on one or more data packets (e.g., data from a component that interacts with a local system, another component in a distributed system, and / or signals that interact with other systems via a network such as the Internet).

[0144] It should also be understood that the present invention is described through embodiments, and the embodiments are only clear and complete descriptions of the technical solutions proposed in the claims of the present invention, that is, explanations of the claims. Therefore, when judging whether the technical solutions recorded in the specification of the present invention are sufficiently disclosed, the purpose and core essence of the solutions defined by the claims should be fully considered. There are necessarily other technical problems in the specification that are unrelated to the core technical problem solved by this embodiment. The corresponding technical features and technical solutions are not included in the essence of this embodiment and are non-essential technical features. Therefore, the implicit disclosure can be referred to. Those skilled in the art can fully implement them by combining existing technology and common knowledge. Therefore, there is no need to describe them in detail.

[0145] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the present invention has been described in detail with reference to preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions can be made to the technical solutions of the present invention without departing from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of the claims of the present invention.

Claims

1. A method for counting pigs, characterized in that: Includes the following steps, Construct a pig counting dataset based on point annotation; Build a P2PNet crowd counting model; An efficient channel attention (ECA) module is added after each body in the VGG16 backbone network of the P2Pnet. A CSA module for coordinate channel shuffling attention is added to the characteristic pyramid FPN structure of the P2Pnet. The classification accuracy of the model is improved by replacing the cross-entropy loss function used in the P2Pnet classification loss with Focal Dice loss. An improved Hungarian matching algorithm for point regression tasks is proposed, employing a context-aware algorithm. This algorithm incorporates an uncertainty cost handling mechanism, a weighted distance handling mechanism, and an adaptive density penalty mechanism to enhance matching accuracy. Introducing the aforementioned uncertainty cost handling mechanism includes the following steps: Adjusting the matching cost based on confidence level reduces the matching probability of low-confidence prediction points, as shown in the following formula. C uncertainty (i,j)=C point (i,j)·u i In the formula, C point (i,j) represents the Euclidean distance between the predicted point and the true point, u i Indicates the confidence level of the predicted point; Introducing the weighted distance processing mechanism includes the following steps: During the matching process, weights are applied to the distance between the predicted points and the true points, as shown in the following formula. In the formula, C distance It is the weighted distance cost, C uncertainty It is an uncertain cost, C point σ is the Euclidean distance, σ is the weight of uncertainty cost and is used to balance uncertainty cost, and ∈ is used to prevent the denominator from being zero; The matching cost between points is optimized by applying weights; points that are closer together will be given greater weights. Incorporating the adaptive density penalty mechanism includes the following steps: By using the KNN algorithm to calculate the difference in local density, the matching cost can be flexibly adjusted, improving matching precision in dense regions and providing greater tolerance in sparse regions, as shown in the following equation. C density (i,j)=exp(-λ·|d i -d j |) In the formula, C density It is the adaptive density penalty cost, d i and d j Let represent the local density of the predicted point and the true point, respectively, and λ be the parameter controlling the exponential smoothing. By combining classification cost, weighted distance cost, uncertainty cost, and adaptive density penalty cost, the improved final matching cost is obtained, as shown in the following equation: C total (i,j)=α·C class (i,j)+β·C distance (i,j)+γ·C density (i,j) In the formula, α, β, and γ are the weights of each cost item, respectively, and C class (i,j) is the classification cost; The final improved model is PIG-P2PNet, which is then applied to pig counting.

2. The pig counting method according to claim 1, characterized in that: Adding the ECA module includes the following steps: The VGG16 backbone network is divided into four bodies, and the ECA module is introduced at the end of each body. The ECA module first performs global average pooling on the input feature map, compressing the spatial dimension of each channel into a single value to obtain global information for each channel; The pooled features are processed by applying 1D convolution operations. Convolution kernels are used to capture the local dependencies between adjacent channels. The convolution results are processed by the Sigmoid activation function to generate attention weights for each channel, with values ​​limited to (0,1). The attention weights are weighted one by one with each channel of the original input feature map. By scaling the features through multiplication, the expression of important channels is enhanced and the information of secondary channels is suppressed.

3. The pig counting method according to claim 1, characterized in that: Adding the CSA module includes the following steps: The CSA module first utilizes coordinate attention to enhance the capture of spatial location information; Channel features are optimized through channel shuffling and attention optimization.

4. The pig counting method according to claim 1, characterized in that: The CSA module includes, The CSA module first receives the input feature map; Spatial information in the horizontal and vertical directions is extracted by global pooling, and spatially relevant attention weights are generated by splicing and convolution. The attention weights are used to weight the feature map, further enhancing key spatial features; The feature map is then divided into multiple channel groups. Features are extracted from each group using convolution, and the order is shuffled by channel rearrangement. The channel group generates channel-level attention weights through global average pooling and normalization, adjusting the feature expression intensity of each channel and highlighting key channels. Finally, the processed feature maps are rearranged and weighted.

5. The pig counting method according to claim 1, characterized in that: The replacement of the cross-entropy loss function with Focal Dice loss includes, The formula for calculating Focal Loss is as follows: In the formula, α is a weighting factor that adjusts the positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, p t This is the model's probability prediction for each category, calculated as follows: p t That is, the negative exponent of cross-entropy loss, where γ is a factor that adjusts the degree of attention the model pays to the classified samples; Dice Loss is used to measure the overlap between the predicted region and the true target region. Its calculation formula is as follows: In the formula, p i It is the probability distribution predicted by the model, g i It is the one-hot encoded representation of the true label. ∈ is used to prevent the denominator from being zero. The smaller the Dice Loss value, the closer the model prediction is to the true label.

6. The pig counting method according to claim 5, characterized in that: The final formula for calculating the Focal Dice loss is as follows. L focal-dice =L focal +L dice L is coming soon focal and L dice Add them together.