Pig counting method
By introducing ECA and CSA modules in the pig counting model P2PNet, and using Focal Dice loss and the Hungarian matching algorithm based on situational awareness, the problem of large counting error in traditional pig counting methods in high-density environments is solved, and the pig counting effect with high accuracy and robustness is achieved.
Patent Information
- Application Number
- CN202411668345.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-21
- Publication Date
- 2025-04-11
- Estimated Expiration
- 2044-11-21
AI Technical Summary
The traditional pig counting method relies on manual statistics and is susceptible to subjective factors and the environment. It has significant counting errors in high-density breeding environments, which have high labor costs and biosafety risks.
A pig counting method based on point annotation is adopted to build a population counting model P2PNet, and an ECA module with efficient channel attention and a CSA module with coordinate channel shuffling attention are added to its backbone network VGG16. Focal Dice loss is used to improve the classification accuracy of the model, and a Hungarian matching algorithm based on scenario perception is designed to improve the matching accuracy.
It shows adaptability and generalization ability in various practical environments, excellent counting accuracy and robustness, and is particularly effective in high-density and complex occlusion feeding environments.
Smart Images

Figure CN119810015A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of pig farm management, and in particular to a pig counting method. Background Art
[0002] With the development of modern agricultural technology, automated and digital farming has gradually become a trend, among which accurate pig counting is the key to improving pig farm management efficiency and benefits. Accurate pig counting can not only help farmers grasp the dynamics of the pig herd in real time and optimize feed distribution, but also assist in health management and economic benefit evaluation, especially in high-density farming environments.
[0003] Traditional pig counting mainly relies on manual statistics, which is easily affected by subjective factors and the environment. Moreover, as the number of pigs increases, the counting error will increase significantly. In addition, frequent manual intervention not only increases labor costs, but also may cause biosecurity risks. Although electronic ear tags provide a solution to a certain extent, their use may have an adverse effect on pig welfare. Therefore, the modern pig farming industry urgently needs an efficient and accurate counting method to meet the needs of refined management. Summary of the invention
[0004] The purpose of this section is to summarize some aspects of embodiments of the present invention and briefly introduce some preferred embodiments. Some simplifications or omissions may be made in this section and the specification abstract and the invention title of this application to avoid blurring the purpose of this section, the specification abstract and the invention title, and such simplifications or omissions cannot be used to limit the scope of the present invention.
[0005] In view of the above 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 solve the above technical problems, the present invention provides the following technical solutions: a pig counting method, comprising the following steps: constructing a pig counting data set based on point annotation; building a crowd counting model P2PNet; adding an ECA module of efficient channel attention behind each body in the backbone network VGG16 of the P2Pnet; adding a CSA module of coordinate channel shuffle attention to the feature pyramid FPN structure of the P2Pnet; replacing the cross entropy loss function used in the 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, and designing a Hungarian matching algorithm based on context perception, including introducing an uncertainty cost processing mechanism, a weighted distance processing mechanism and an adaptive density penalty mechanism to improve the matching accuracy; finally, the improved model is PIG-P2PNet, and the PIG-P2PNet is applied 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 the global information of each channel; applying a 1D convolution operation to process the pooled features, using a convolution kernel to capture the local dependencies between adjacent channels, and processing the convolution results with a Sigmoid activation function to generate an attention weight for each channel, with the value range limited to (0,1); weighting the attention weights one by one with each channel of the original input feature map, scaling the features by multiplication, enhancing the expression of important channels and suppressing the information of secondary channels.
[0009] Preferably, adding the CSA module includes the following steps: the CSA module first uses coordinate attention to strengthen the capture of spatial position information; and optimizes channel features through channel shuffle attention.
[0010] Preferably, the CSA module includes: the CSA module first receives an input feature map; extracts spatial information in the horizontal and vertical directions through global pooling, and generates space-related attention weights through splicing and convolution; uses the attention weights to weight the feature map to further strengthen key spatial features; then divides the feature map into multiple channel groups, each group extracts features through convolution, and disrupts the order through channel rearrangement; the channel group generates channel-level attention weights through global average pooling and normalization, adjusts the feature expression strength of each channel, and highlights the key channel; finally, the processed feature map is rearranged and weighted.
[0011] Preferably, introducing the uncertainty cost processing mechanism includes the following steps: adjusting the matching cost according to 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 Indicates the confidence level of the predicted point.
[0014] Preferably, the introduction of the weighted distance processing mechanism includes the following steps: applying weights to the distances between the predicted points and the real points 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 to optimize the matching cost between points, points with smaller distances will be given larger weights.
[0017] Preferably, adding the adaptive density penalty mechanism includes the following steps: by using the KNN algorithm to calculate the difference in local density, flexibly adjusting the matching cost, improving the matching precision in dense areas, and providing a larger tolerance in sparse areas, as shown in the following formula:
[0018] C density (i, j) = exp(-λ·|p i -p j |)
[0019] In the formula, p i and p j They represent the local density of predicted points and true points respectively, and λ is the parameter controlling exponential smoothing.
[0020] Preferably, the improved final matching cost is obtained by combining the classification cost, the weighted distance cost, the uncertainty cost and the 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, C class (i, j) is the classification cost.
[0023] Preferably, the cross entropy loss function is replaced by Focal Dice loss including:
[0024] The calculation formula of Focal Loss is as follows:
[0025]
[0026] In the formula, α is the weight factor for adjusting positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, p t is the model's probability prediction for each category, which is calculated as follows,
[0027]
[0028] p t That is, the negative exponent of the cross entropy loss, Y is the factor that adjusts the model's attention to the classified samples; Dice Loss is used to measure the overlap between the predicted area and the true target area, and its calculation formula is as follows:
[0029]
[0030] In the formula, p i is the probability distribution predicted by the model, g i It is the unique hot encoding 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 calculation formula of the Focal Dice loss is as follows:
[0032] L focal-dice =L focaf +L dice
[0033] Coming soon focal and L dice Add.
[0034] The beneficial effects of the present invention are as follows: the invention has adaptability and generalization ability in various practical environments, and performs excellently in counting accuracy and robustness, and the effect is particularly prominent in a high-density, complexly shielded breeding environment. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for describing the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative labor. Among them:
[0036] Figure 1 Schematic diagram of pig images in different scenarios according to the present invention;
[0037] Figure 2 A schematic diagram of an example of pig point marking according to the present invention;
[0038] Figure 3 A schematic diagram of a data enhancement example of horizontal flipping, rotation, cropping and filling according to the present invention;
[0039] Figure 4 A schematic diagram of an example of data enhancement using Gaussian noise, Gaussian blur, and color saturation adjustment according to the present invention;
[0040] Figure 5 A schematic diagram of the PIG-P2PNet network structure of the present invention;
[0041] Figure 6 It is a schematic diagram of the ECA module structure of the present invention;
[0042] Figure 7 It is a schematic diagram of the CSA module structure of the present invention;
[0043] Figure 8 A schematic diagram for comparing the visualization results of different counting models according to the present invention;
[0044] Fig. 9 It is a schematic diagram of the prediction results of PIG-P2PNet at different densities according to the present invention;
[0045] Fig.10 This is a schematic diagram comparing the positioning performance of PIG-P2PNet and P2PNet described in the present invention. DETAILED DESCRIPTION
[0046] In order to make the above-mentioned purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are described in detail below in conjunction with the drawings of the specification. Obviously, the described embodiments are part of the embodiments of the present invention, but not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary persons in the art without creative work should fall within the scope of protection of the present invention.
[0047] In the following description, many specific details are set forth to facilitate a full understanding of the present invention, but the present invention may also be implemented in other ways different from those described herein, and those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present 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 term "in one embodiment" that appears in different places in this specification does not necessarily refer to the same embodiment, nor does it refer to a separate or selective embodiment that is mutually exclusive with other embodiments.
[0049] The present invention is described in detail with reference to schematic diagrams. When describing the embodiments of the present invention, for the sake of convenience, the cross-sectional diagrams showing the device structure will not be partially enlarged according to the general scale, and the schematic diagrams are only examples, which should not limit the scope of protection of the present invention. In addition, in actual production, the three-dimensional dimensions of length, width and depth should be included.
[0050] At the same time, in the description of the present invention, it should be noted that the directions or positional relationships indicated by the terms "upper, lower, inner and outer" are based on the directions or positional relationships shown in the drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific direction, be constructed and operated in a specific direction, and therefore cannot be understood as limiting the present invention. In addition, the terms "first, second or third" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance.
[0051] In the present invention, unless otherwise clearly specified and limited, the terms "install, connect, connect" should be understood in a broad sense, for example: it can be a fixed connection, a detachable connection or an integral connection; it can also be a mechanical connection, an electrical connection or a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood according to specific circumstances.
[0052] Example 1
[0053] Pig counting is an important task in modern pig farming, and plays a key role in evaluating the scale of breeding, optimizing feeding strategies, and improving economic benefits. However, in a complex pig farm environment, factors such as high pig density and multiple occlusions make counting accuracy a challenge. Therefore, this embodiment proposes a pig counting model PIG-P2PNet based on the crowd counting model P2PNet, which includes the following steps:
[0054] S1: Construct a pig counting dataset based on point annotation;
[0055] S2: Build the crowd counting model P2PNet;
[0056] S3: Add an ECA module with efficient channel attention after each body in the backbone network VGG16 of P2Pnet;
[0057] S4: Add the CSA module of coordinate channel shuffle attention to the feature pyramid FPN structure of P2Pnet;
[0058] S5: Replace the cross entropy loss function used in the classification loss of P2Pnet with Focal Dice loss to improve the classification accuracy of the model;
[0059] S6: Improve the Hungarian matching algorithm in the point regression task and design a context-aware Hungarian matching algorithm, including introducing uncertainty cost processing mechanism, weighted distance processing mechanism and adaptive density penalty mechanism to improve matching accuracy;
[0060] S7: The final improved model is PIG-P2PNet, which is applied to pig counting.
[0061] Firstly, by introducing an efficient channel attention mechanism into the backbone network, the dependencies between channels are effectively captured, thus enhancing the recognition ability of overlapping pigs. Secondly, the coordinate channel shuffle attention module is integrated into the feature pyramid to enhance the extraction and interaction capabilities of spatial position information and channel features, and improve the adaptability of the model in different density scenarios. In addition, a scenario-aware Hungarian matching algorithm is designed, which improves the matching accuracy by introducing weighted distance penalty, uncertainty cost and adaptive density penalty.
[0062] Example 2
[0063] This embodiment applies the pig counting method to a complex pig farm environment, which actually includes the construction of a data set and a group pig counting model, as follows:
[0064] (1) Dataset construction.
[0065] ①Data source:
[0066] The experimental data of this embodiment are derived from different pig farms, covering public data sets, online pictures, and data collected from actual pig farms. Public data sets include iFlytek pig inventory data set and the University of Nebraska-Lincoln pig data set. Online pictures are mainly from Roboflow and Kaggle websites. The remaining data were collected from Zhejiang Qingzhu Agriculture 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. Most of the public data sets and online pictures are top views, and the occlusion problem is relatively light. By collecting side view data of actual pig farms, the diversity and complexity of the data are increased. Images with large differences in time and lighting conditions were selected from the acquired data, and pictures with high similarity and low clarity were eliminated. Finally, a total of 2,200 original images including top views and side views, day and night, high density and low density, etc. were screened out. The data of different pig farms are shown as follows: Figure 1 shown.
[0067] ②Dataset preprocessing:
[0068] All images of different resolutions were uniformly cropped and padded to 640×640 pixels, and the open source annotation tool X-labeling was used to annotate the data set. The center position of the entire pig was given priority during annotation. If there was severe occlusion or overlap, the center position of the visible part was annotated. Figure 2 shown.
[0069] Based on the annotation, data enhancement is performed by flipping, rotating, cropping and filling, adding noise, Gaussian blurring, adjusting color saturation, etc. Examples of data enhancement include: Figure 3-4 shown. Figure 3 From left to right, they are the illustrations of horizontal flipping, rotation, cropping and filling processing. Figure 4 From left to right in the figure are Gaussian noise, Gaussian blur and color saturation enhancement processing.
[0070] ③Dataset construction:
[0071] The 2200 original pig images after annotation were randomly divided into training set, validation set and test set in the ratio of 7:1.5:1.5. The training set was data enhanced, and the number of images increased from the original 1540 to 2640, and the number of images in the entire data set reached 3300. The data set information is shown in Table 1.
[0072] Table 1: Dataset.
[0073]
[0074] (2) Pig group counting model
[0075] ①PIG-P2PNet network structure:
[0076] The accuracy of the crowd counting model P2PNet decreases when used for pig counting in complex and dense scenes, so this embodiment improves it. First, the Efficient Channel Attention (ECA) module is introduced after each body in the backbone network VGG16 of P2PNet. ECA adaptively adjusts the channel weights, highlights key features and suppresses irrelevant information, and improves the detection and classification performance of the model in complex scenes. Then the Coordinate and Shuffle Attention (CSA) module is introduced in the Feature Pyramid Networks (FPN). The CSA module combines spatial information and channel information to form a richer feature interaction, so that the model can pay attention to the synergistic relationship between the different scales, positions and channel features of the target, thereby significantly improving the accuracy and robustness of the model. Then the cross entropy loss function in P2PNet is replaced with Focal Dice loss to deal with the imbalance of positive and negative samples and the dense occlusion of pigs, effectively improving the prediction accuracy of the model. Finally, the classification head and regression head are used to predict the image and generate the predicted points of the target location. The context-aware Hungarian matching algorithm is applied during the model training process to optimize the matching process between the real points and the predicted points and solve the problem of real points being ignored and mismatched. The improved model is named PIG-P2PNet, and its network structure is as follows: Figure 5 As shown, the improved part is marked by a dotted box.
[0077] ②ECA module:
[0078] For the pig counting task of point regression, the number of targets in dense scenes is large and the distribution is uneven. The traditional VGG network has limitations in capturing the local features of these areas and distinguishing adjacent individuals, resulting in reduced prediction accuracy, especially in high-density scenes. It is prone to underfitting problems. In addition, VGG easily confuses target and background information in complex backgrounds, and lacks effective interaction between channels, which affects the positioning accuracy and robustness of the model. To address the problems of difficulty in distinguishing between dense targets, inaccurate local feature extraction, and confusion in complex backgrounds, the VGG16 backbone network is divided into four bodies, and the ECA module is introduced at the end of each body. This hierarchical design effectively enhances the model's extraction capabilities at different feature levels. ECA uses 1D convolution to strengthen the local dependency between channels, allowing the model to better focus on individual pigs in dense areas, thereby improving target distinction capabilities. The ECA module structure is shown below. Figure 6 shown.
[0079] The ECA module first performs global average pooling on the input feature map to compress the spatial dimension of each channel into a single value to obtain the global information of each channel. Then a 1D convolution operation is applied to process the pooled features, using the convolution kernel ( Figure 6 The k=3 set in captures the local dependencies between adjacent channels. The convolution result is processed by the Sigmoid activation function to generate the attention weight of each channel, and the value range is limited to (0,1). Finally, the attention weight is weighted one by one with each channel of the original input feature map, and the features are scaled by multiplication to enhance the expression of important channels and suppress the information of minor channels.
[0080] ③CSA module:
[0081] The introduction of the CSA module can solve the problems of the P2PNet model in dealing with complex or pig-dense scenes, such as difficulty in distinguishing overlapping targets, difficulty in extracting complex background interference features, and insufficient interaction between spatial and channel features. The CSA module first uses coordinate attention to enhance the capture of spatial position information, and then optimizes channel features through channel shuffle attention. Its structure is as follows: Figure 7 shown.
[0082] The CSA module first receives the input feature map, extracts spatial information in the horizontal and vertical directions through global pooling, generates spatially related attention weights through splicing and convolution, strengthens important areas and suppresses background noise, and uses these weights to weight the feature map to further strengthen key spatial features. The feature map is then divided into multiple channel groups, each of which uses convolution to extract features, and the order is disrupted by channel rearrangement to ensure effective interaction of cross-channel information. The channel group generates channel-level attention weights through global average pooling and normalization, adjusts the feature expression strength of each channel, and highlights the key channels. Finally, the processed feature maps are rearranged and weighted to ensure full utilization of spatial and channel information, improving the model's feature expression ability and recognition performance in complex scenes.
[0083] ④ Hungarian matching algorithm based on context awareness:
[0084] In the point regression task, the traditional Hungarian matching algorithm can accurately match the predicted points and the real points through global optimization, thereby improving the accuracy of counting. However, in the pig counting task, the algorithm also has shortcomings. First, in dense areas, the algorithm cannot accurately match the real points with the predicted points, resulting in the predicted points of multiple pigs being incorrectly matched, and the counting results are significantly lower than the actual number. Secondly, in sparse areas, the matching of the algorithm is too loose, and the predicted points far away from the real target will be mistakenly matched to the nearest real point, resulting in inaccurate counting. In addition, the algorithm does not take into account the confidence of the predicted points, which will cause low-confidence predicted points to be incorrectly matched with high-confidence real points, which will lead to the problem of miscounting or missing counts.
[0085] In order to solve the problems of inaccurate matching of dense areas, too loose matching of sparse areas and failure to consider the confidence of prediction points in the traditional Hungarian algorithm in the pig counting task, this embodiment improves it and designs a Hungarian algorithm based on context awareness.
[0086] First, an uncertainty cost processing mechanism is introduced to adjust the matching cost according to the confidence level, reduce the matching probability of low-confidence prediction points, and thus reduce the risk of mismatching. The formula is as follows:
[0087] C uncertainty (i, j) = C point (i, j)·u i (1)
[0088] In formula (1), 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.
[0089] Secondly, the weighted distance processing mechanism is improved to apply weights to the distance between the predicted point and the real point during the matching process. When the prediction contains uncertainty values, the uncertainty cost is added to the point distance cost to increase the matching cost of the predicted points with high uncertainty, so that these points are weakened in the matching. The weighted distance cost calculation formula is shown in formula (2). This formula optimizes the matching cost between points. Points with smaller distances will be given larger weights, thereby ensuring stricter matching in dense areas, while maintaining a certain degree of flexibility in sparse areas.
[0090]
[0091] In formula (2), σ 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.
[0092] Finally, an adaptive density penalty mechanism is added. By using the KNN algorithm to calculate the difference in local density, the matching cost is flexibly adjusted to improve the matching precision in dense areas and provide a larger tolerance in sparse areas, thereby optimizing the overall matching accuracy. The calculation formula of this mechanism is as follows:
[0093] C density (i, j) = exp(-λ·|p i -p j |) (3)
[0094] In formula (3), p i and p j They represent the local density of predicted points and true points respectively, and λ is the parameter controlling exponential smoothing.
[0095] In summary, by combining classification cost, weighted distance cost, uncertainty cost and adaptive density penalty cost, the algorithm can better 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 formula (4), α, β and γ are the weights of each cost item, C class (i, j) is the classification cost.
[0098] ⑤Loss function optimization:
[0099] The loss function of P2PNet includes classification loss and regression loss. The cross-entropy loss used in the classification loss has limitations in the pig counting task of point regression. First, when the target pigs are scarce in the group pig image, the background area will account for a large proportion, resulting in an imbalance in the ratio of positive and negative samples, making the cross-entropy loss too sensitive to category imbalance, so that the model tends to predict the background and ignore the target pigs. Second, the loss function lacks targeted optimization for samples that are difficult to distinguish (such as point annotations of adjacent pigs), and will misjudge overlapping pigs as a single category rather than independent individuals.
[0100] In order to solve the problem of cross entropy loss in dealing with class imbalance and identifying adjacent targets, it is replaced by Focal Dice Loss. Focal Dice Loss combines the advantages of Focal Loss and Dice Loss, and can better deal with the imbalance of positive and negative samples and dense occlusion of pigs, thereby significantly improving the prediction accuracy of the model.
[0101] Focal Loss adds an adjustment factor based on the cross entropy loss function to reduce the weight of samples that are easy to classify and give more weight to samples that are difficult to classify, thereby solving the problem of class imbalance. The calculation formula of Focal Loss is:
[0102]
[0103] In formula (5), α is the weight factor for adjusting positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, avoiding the impact of imbalance in the ratio of positive and negative samples on model training; pt is the model's probability prediction for each category, which is calculated as That is, the negative exponent of the cross entropy loss; γ is a factor that adjusts the model's attention to classified samples. A larger γ will make the model focus more on samples that are difficult to classify.
[0104] Dice Loss is mainly used to measure the overlap between the predicted area and the real target area. It is especially suitable for processing scenes with blurred target boundaries or irregular shapes. Its calculation formula is:
[0105]
[0106] In formula (6), p i is the probability distribution predicted by the model, g i is the unique hot encoding representation of the true label, and ∈ 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 calculation formula of Focal Dice Loss is:
[0108] L focal-dice =L focal +L dice (7).
[0109] Example 3
[0110] The actual effect of this method in the above-mentioned embodiment is not verified in this embodiment, and a comparative experiment is performed to verify it. The details are as follows:
[0111] (1) Experimental environment.
[0112] The operating system is Ubuntu 11.3.0, the CUDA version is 11.7, the Python version is 3.10.14, and the deep learning framework is Pytorch 2.0.1. The hyperparameter settings during the training process are: the learning rate is 1e-5, the training rounds are 500 rounds, the batch size is adjusted according to the video memory usage of different models, the PIG-P2PNet model batch size is 12, and the input image size is 640×640 pixels. The hardware conditions include the 12th Gen Intel(R) Core(TM) i7-12700KF processor, NVIDIAGeForce RTX 3090Ti graphics card, and 24GB video memory.
[0113] (2)Evaluation indicators
[0114] In order to comprehensively evaluate the performance of the pig counting model, five evaluation indicators were selected, including mean absolute error (MAE), root mean square 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 is evaluated by comparing and analyzing six different solutions. The experiment starts with the baseline model (without introducing any improvements), and then separately introduces the ECA module, CSA module, context-aware matching algorithm (Context-AwareMatcher) and Focal Dice Loss for testing. Finally, the four modules are integrated into the baseline model at the same time to evaluate the comprehensive performance of the model. The final experimental results are shown in Table 2.
[0117] Table 2: Ablation experiment results.
[0118]
[0119] As shown in Table 2, each improvement introduced separately has significantly improved the performance of the model. The introduction of the ECA module reduces the MAE of the model by 18.5% and the RMSE by 3% compared with the baseline model, indicating that the module effectively improves the feature expression ability of the model by enhancing the feature interaction between channels. After the CSA module is added to FPN, the MAE and RMSE are reduced by 19.5%, respectively, indicating the effectiveness of the CSA module in improving feature fusion and suppressing irrelevant features. The introduction of the context-aware matching algorithm reduces the MAE and RMSE of the model by 23.8% and 16.6% respectively compared with the baseline model, indicating that the algorithm is effective in optimizing target matching and improving target positioning accuracy. After replacing the cross entropy loss function in P2PNet with Focal Dice Loss, the MAE and RMSE of the model are reduced to 1.145 and 1.804 respectively, indicating that Focal Dice Loss is effective in dealing with category imbalance and improving the model's small target detection ability. When all the improved methods were integrated into the baseline model, the MAE, RMSE and NAE of the model reached 0.942, 1.460 and 0.043, respectively, which were reduced by 28.7%, 24.3% and 34.8% compared with the baseline model. At the same time, the number of parameters and FPS were 21.660M and 45 frames / second, respectively, which shows that the synergy 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 is compared with the improved 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 reaches 0.942, which is 60.4%, 50.9%, 28.7% and 20.9% lower than that of the CSRNet, CANNet, P2PNet and CLTR models, respectively, showing a significant advantage.
[0125] From a specific analysis, both CSRNet and CANNet use counting methods based on density map estimation. Although the number of model parameters is relatively small, their performance in key indicators such as MAE, RMSE and NAE is not ideal, and there is a large gap compared with other point regression-based models. This shows that density map-based models have certain limitations in dealing with complex scenes, especially pig counting tasks with severe overlap and occlusion. In comparison, the point regression-based counting models P2PNet and CLTR have shown better counting accuracy and robustness in the three key indicators of MAE, RMSE and NAE. In particular, the CLTR model has significant advantages in target matching and feature extraction, but due to its large number of parameters and low FPS, its performance is limited in scenarios that are sensitive to resource consumption, such as edge computing devices, and lacks application flexibility and real-time performance. In contrast, the P2PNet model can maintain high accuracy while having low computational overhead and higher frame rate, so it is more suitable for counting tasks with high real-time requirements in actual scenarios.
[0126] Compared with the above four models, the PIG-P2PNet model not only achieved the best performance in the three key indicators of MAE, RMSE and NAE, but also achieved a level comparable to P2PNet in terms of parameter quantity and computational efficiency. In short, PIG-P2PNet has shown obvious advantages in accuracy, stability, parameter quantity control and real-time processing capabilities. In order to more intuitively demonstrate the effects of the above models in the pig counting task, the visual prediction graphs of each model are given, as shown in the figure below. Figure 8 As shown (in the figure, GT is the true number and Pred is the predicted number).
[0127] Depend on Figure 8 It can be seen that although the P2PNet and CLTR models can predict results consistent with the actual number, it is actually because they mistakenly identify one pig as two and miss the other one. Figure 8 In the second row of samples, the red boxes of the prediction results of P2PNet and CLTR indicate missed detections and the boxes indicate incorrect predictions. The CANNet and CSRNet models based on density map estimation cannot accurately locate the pigs and can only estimate the overall number. Therefore, the counting results of the PIG-P2PNet model are the most accurate.
[0128] (5) Performance analysis of the PIG-P2PNet model under different pig population densities.
[0129] In the study of pig counting methods, the accuracy and robustness of the model under different pig density conditions is a key challenge. The density of pigs varies from sparse to extremely crowded, so the model must be able to maintain accuracy under various density conditions. To this end, the PIG-P2PNet model was validated with different density levels, and the results are shown in Table 4.
[0130] Table 4: Validation experiment results at different densities.
[0131]
[0132] As shown in Table 4, in the low-density scene (Level 1), that is, when each image contains 5 to 15 pigs, the MAE, RMSE, and NAE of the model are 0.470, 0.816, and 0.041, respectively. This shows that the model has excellent counting accuracy in this scene and can accurately capture the number of sparsely distributed targets. When the scene density increases to a medium level (Level 2), that is, each image contains 16 to 30 pigs, the MAE and RMSE of the model reach 0.924 and 1.354, respectively, indicating that the model can maintain stable error control at higher density. At the same time, the NAE at this density is reduced to 0.037, indicating that the model has good stability at medium density. In the high-density scene (Level 3), that is, when each image contains more than 30 pigs and there are a lot of occlusions and overlaps, the MAE, RMSE, and NAE of PIG-P2PNet reach 1.924, 2.377, and 0.058, respectively. Although the model error increases in high-density scenarios, it can still maintain a relatively low error level, indicating that the model has strong feature expression capabilities and the ability to cope with complex scenarios. In order to more intuitively demonstrate the counting effect of PIG-P2PNet at different densities, a visual prediction graph is given, such as Fig. 9 shown.
[0133] The PIG-P2PNet model can not only achieve accurate pig counting at different densities, but also significantly improve its positioning accuracy compared with the original model. Fig.10 As shown. Fig.10 It can be seen that the different × represent the actual marked position and the model predicted position. Although the counting results of the two are consistent with the true value, there is a difference in positioning accuracy.
[0134] By comparing the box areas, we can see that the predicted position of PIG-P2PNet is closer to the actual annotation and is more accurate in target positioning.
[0135] Therefore, this embodiment aims at the common pig missed detection and false detection problems in complex breeding environments such as high-density pigs and multiple occlusions, and proposes an improved pig counting algorithm PIG-P2PNet based on the crowd counting model P2PNet. The main effects are as follows:
[0136] First, a point-annotated pig counting dataset was constructed, which includes diverse scenarios ranging from low density to high density and different occlusion conditions, to ensure the adaptability and generalization ability of the model in various practical environments.
[0137] Second, 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, the CSA module is added to the FPN structure, which effectively improves the quality of feature fusion and makes the model more adaptable to images with high density and occluded areas. In addition, replacing the traditional cross entropy loss with FocalDice Loss helps to deal with the problem of class imbalance and improve the detection accuracy of small targets; finally, a Hungarian matching algorithm based on context perception is designed to make the model more accurate in matching predicted points with real points.
[0138] Third, the experimental results show that compared with the P2PNet model, the MAE, RMSE and NAE of PIG-P2PNet are reduced by 28.7%, 24.3% and 34.8% respectively. Compared with other classic counting models CSRNet, CANNet and CLTR, PIG-P2PNet reduces MAE by 60.4%, 50.9% and 20.9%, RMSE by 51.2%, 48.5% and 16.1%, and NAE by 71.5%, 53.3% and 30.6%.
[0139] PIG-P2PNet performs well in counting accuracy and robustness, especially in high-density and complex occlusion breeding environments. In the future, the pig counting method based on point regression is expected to further improve the refined management level of pig farms and promote the development of intelligent breeding.
[0140] It should be appreciated that embodiments of the present invention may be implemented or enforced by computer hardware, a combination of hardware and software, or by computer instructions stored in a non-transitory computer-readable memory. The method may be implemented in a computer program using standard programming techniques - including a non-transitory computer-readable storage medium configured with a computer program, wherein the storage medium so configured causes the computer to operate in a specific and predefined manner - according to the methods and drawings described in the specific embodiments. Each program may be implemented in a high-level procedural or object-oriented programming language to communicate with a computer system. However, if desired, the program may be implemented in an assembly or machine language. In any case, the language may be a compiled or interpreted language. In addition, the program may be run on a programmed ASIC for this purpose.
[0141] In addition, the operations of the process described in this embodiment may be performed in any suitable order, unless otherwise indicated in this embodiment or otherwise clearly contradicted by the context. The process described in this embodiment (or variations and / or combinations thereof) may be performed under the control of one or more computer systems configured with executable instructions, and may be implemented as a code (e.g., executable instructions, one or more computer programs, or one or more applications) executed jointly on one or more processors, by hardware or a combination thereof. The computer program includes a plurality of instructions that may be executed by one or more processors.
[0142] Further, the method can be implemented in any type of computing platform that is operably connected to a suitable computer, including but not limited to a personal computer, a minicomputer, a mainframe, a workstation, a network or distributed computing environment, a separate or integrated computer platform, or in communication with a charged particle tool or other imaging device, etc. Aspects of the present invention can be implemented in 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, an optical read and / or write storage medium, a RAM, a ROM, etc., so 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 process described herein. In addition, the machine-readable code, or part thereof, can be transmitted via a wired or wireless network. When such media includes instructions or programs that implement the steps described above in conjunction with a microprocessor or other data processor, the invention described in this embodiment includes these and other different types of non-transitory computer-readable storage media. When programmed according to the methods and techniques described in the present invention, the present invention also includes the computer itself. The computer program can be applied to input data to perform the functions described in this embodiment, thereby converting the input data to generate output data stored in a 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 present invention, the converted data represents a physical and tangible object, including a specific visual depiction of the physical and tangible object produced on a display.
[0143] As used in this application, the terms "component", "module", "system", etc. are intended to refer to a computer-related entity, which can be hardware, firmware, a combination of hardware and software, software, or software in operation. For example, a component can be, but is not limited to: a process running on a processor, a processor, an object, an executable file, a thread in execution, a program, and / or a computer. As an example, an application running on a computing device and the computing device can both be components. One or more components can exist in a process and / or thread in execution, and a component can be located in a computer and / or distributed between two or more computers. In addition, these components can be executed from various computer-readable media having various data structures thereon. These components can communicate in a local and / or remote process manner, such as according to a signal having one or more data packets (e.g., data from a component that interacts with another component in a local system, a distributed system, and / or interacts with other systems in a signal manner through a network such as the Internet).
[0144] It should also be understood that the present invention is described through implementation methods, 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, full consideration should be given to the core essence of the solutions defined by the claims. In the specification, there must be other technical problems that are not related to the core technical problems proposed in this embodiment. The corresponding technical features and technical solutions do not belong to the essence of this embodiment and are non-essential technical features. Therefore, they can be referred to as implicit disclosures. Those skilled in the art can fully implement them in combination with existing technologies 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 rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention may be modified or replaced by equivalents without departing from the spirit and scope of the technical solutions of the present invention, which should all be included in the scope of the claims of the present invention.
Claims
1. A method for counting pigs, characterized in that: The following steps are included: Construct a pig counting dataset based on point annotation; Build the crowd counting model P2PNet; Add an ECA module with efficient channel attention after each body in the backbone network VGG16 of the P2Pnet; A CSA module of coordinate channel shuffle attention is added to the feature pyramid FPN structure of the P2Pnet; The cross entropy loss function used in the classification loss of the P2Pnet is replaced with Focal Dice loss to improve the classification accuracy of the model; Improve the Hungarian matching algorithm in the point regression task and design a context-aware Hungarian matching algorithm, including introducing uncertainty cost processing mechanism, weighted distance processing mechanism and adaptive density penalty mechanism to improve matching accuracy; The final improved model is PIG-P2PNet, which is applied to pig counting.
2. The method for counting pigs according to claim 1, characterized in that: Joining the ECA module includes the following steps, Divide the VGG16 backbone network into four bodies and introduce the ECA module at the end of each body The ECA module first performs global average pooling on the input feature map to compress the spatial dimension of each channel into a single value to obtain the global information of each channel; Apply 1D convolution operation to process the pooled features, use convolution kernel to capture the local dependency between adjacent channels, and the convolution result is processed by Sigmoid activation function to generate attention weight for each channel, with the value range limited to (0,1); The attention weights are weighted one by one with each channel of the original input feature map, and the features are scaled by multiplication to enhance the expression of important channels and suppress the information of minor channels.
3. The pig counting method according to claim 1, characterized in that: Joining the CSA module includes the following steps, The CSA module first uses coordinate attention to enhance the capture of spatial position information; Optimizing channel features via channel shuffled attention.
4. The method for counting pigs according to claim 1, characterized in that: The CSA module includes, The CSA module first receives an input feature map; The spatial information in horizontal and vertical directions is extracted through global pooling, and the spatially related attention weights are generated through splicing and convolution; The feature map is weighted using the attention weights to further strengthen key spatial features; The feature map is then divided into multiple channel groups, each group uses convolution to extract features, and the order is disrupted by channel rearrangement; The channel group generates channel-level attention weights through global average pooling and normalization, adjusts the feature expression strength of each channel, and highlights the key channels; Finally, the processed feature maps are rearranged and weighted.
5. The pig counting method according to claim 1, characterized in that: The introduction of the uncertainty cost processing mechanism includes the following steps: Adjust the matching cost according to the confidence level to reduce 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.
6. The method for counting pigs according to claim 5, characterized in that: Introducing the weighted distance processing mechanism includes the following steps: In the matching process, the distance between the predicted point and the real point is weighted as follows: Where σ 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 to optimize the matching cost between points, points with smaller distances will be given larger weights.
7. The method for counting pigs according to claim 6, characterized in that: Adding the adaptive density penalty mechanism includes the following steps: By using the KNN algorithm to calculate the difference in local density, the matching cost is flexibly adjusted to improve the matching precision in dense areas and provide a larger tolerance in sparse areas, as shown in the following formula: C density (i,j)=exp(-λ·|p i -p j |) In the formula, p i and p j They represent the local density of predicted points and true points respectively, and λ is the parameter controlling exponential smoothing.
8. The method for counting pigs according to claim 7, characterized in that: include, By combining the classification cost, weighted distance cost, uncertainty cost and adaptive density penalty cost, the improved final matching cost is obtained as follows: 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, C class (i,j) is the classification cost.
9. The method for counting pigs according to claim 1, characterized in that: The cross entropy loss function is replaced by Focal Dice loss including, The calculation formula of Focal Loss is as follows: In the formula, α is the weight factor for adjusting positive and negative samples and is used to control the contribution of positive and negative samples to the total loss, p t is the model's probability prediction for each category, which is calculated as follows, p t That is, the negative exponent of the cross entropy loss, and γ is a factor that adjusts the model's attention to the classification samples; Dice Loss is used to measure the overlap between the predicted area and the true target area. The calculation formula is as follows: In the formula, p i is the probability distribution predicted by the model, g i It is the unique hot encoding 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.
10. The method for counting pigs according to claim 9, characterized in that: The final calculation formula of Focal Dice loss is as follows: L focal-dice =L focal +L dice Coming soon focal and L dice Add.
Citation Information
Patent Citations
Pig monitoring and counting method based on improved YOLOv7 model
CN116469034A
Personnel departure detection method based on Hungary algorithm and P2PNet
CN117058623A